Mercurial > public > simoleon
comparison Simoleon/Helpers/FavoriteButton.swift @ 187:13d5a8deb6c2
add AboutView and FavoritesView
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Thu, 23 Dec 2021 16:12:22 +0100 |
parents | 2fc95efcb1ee |
children |
comparison
equal
deleted
inserted
replaced
186:1ebd1c5dd302 | 187:13d5a8deb6c2 |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 21/12/21. | 5 // Created by Dennis Concepción Martín on 21/12/21. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import CoreData | |
9 | 10 |
10 struct FavoriteButton: View { | 11 struct FavoriteButton: View { |
12 @Binding var baseCurrency: SupportedCurrencyResult | |
13 @Binding var quoteCurrency: SupportedCurrencyResult | |
11 @State private var scale: CGFloat = 1 | 14 @State private var scale: CGFloat = 1 |
15 @Environment(\.managedObjectContext) private var viewContext | |
16 @FetchRequest(sortDescriptors: []) private var favoritePairs: FetchedResults<FavoritePair> | |
12 | 17 |
13 var body: some View { | 18 var body: some View { |
14 Button(action: {}) { | 19 Button(action: { |
20 animate() | |
21 if isFavorite() { remove() } | |
22 else { add() } | |
23 }) { | |
15 RoundedRectangle(cornerRadius: 15) | 24 RoundedRectangle(cornerRadius: 15) |
16 .foregroundColor(Color(.secondarySystemBackground)) | 25 .foregroundColor(Color(.secondarySystemBackground)) |
17 .frame(width: 60, height: 60) | 26 .frame(width: 60, height: 60) |
18 .overlay( | 27 .overlay( |
19 VStack { | 28 VStack { |
31 .animation(.linear(duration: 0.2), value: scale) | 40 .animation(.linear(duration: 0.2), value: scale) |
32 } | 41 } |
33 | 42 |
34 // Add currency conversion to favorites | 43 // Add currency conversion to favorites |
35 private func add() { | 44 private func add() { |
45 let favoritePair = FavoritePair(context: viewContext) | |
46 favoritePair.baseCurrency = baseCurrency.code | |
47 favoritePair.quoteCurrency = quoteCurrency.code | |
36 | 48 |
49 do { | |
50 try viewContext.save() | |
51 } catch { | |
52 // Replace this implementation with code to handle the error appropriately. | |
53 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
54 let nsError = error as NSError | |
55 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
56 } | |
37 } | 57 } |
38 | 58 |
39 // Remove currency conversion from favorites | 59 // Remove currency conversion from favorites |
40 private func remove() { | 60 private func remove() { |
61 let favoritePair = favoritePairs.first( | |
62 where: { | |
63 $0.baseCurrency == baseCurrency.code && $0.quoteCurrency == quoteCurrency.code | |
64 }) | |
41 | 65 |
66 viewContext.delete(favoritePair!) | |
67 | |
68 do { | |
69 try viewContext.save() | |
70 } catch { | |
71 // Replace this implementation with code to handle the error appropriately. | |
72 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
73 let nsError = error as NSError | |
74 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
75 } | |
42 } | 76 } |
43 | 77 |
44 // Check if currency conversion is in favorites | 78 // Check if currency conversion is in favorites |
45 private func isFavorite() -> Bool { | 79 private func isFavorite() -> Bool { |
46 return false | 80 let favoritePair = favoritePairs.first( |
81 where: { | |
82 $0.baseCurrency == baseCurrency.code && $0.quoteCurrency == quoteCurrency.code | |
83 }) | |
84 | |
85 guard let _ = favoritePair else { return false } | |
86 | |
87 return true | |
47 } | 88 } |
48 | 89 |
49 // Animate favorite button | 90 // Animate favorite button |
50 private func animate() { | 91 private func animate() { |
51 scale += 0.2 | 92 scale += 0.2 |
55 } | 96 } |
56 } | 97 } |
57 | 98 |
58 struct FavoriteButton_Previews: PreviewProvider { | 99 struct FavoriteButton_Previews: PreviewProvider { |
59 static var previews: some View { | 100 static var previews: some View { |
60 FavoriteButton() | 101 FavoriteButton( |
102 baseCurrency: .constant(SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)), | |
103 quoteCurrency: .constant(SupportedCurrencyResult(code: "USD", name: "U.S. Dollar", isCrypto: 0)) | |
104 ) | |
105 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | |
61 } | 106 } |
62 } | 107 } |