Mercurial > public > simoleon
comparison Simoleon/Helpers/FavoriteButton.swift @ 53:b0bce2c8e4a9
Refactor UK spelling to US
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Tue, 27 Jul 2021 09:44:51 +0100 |
parents | Simoleon/Helpers/FavouriteButton.swift@7a6a7c677851 |
children | 529feb1fc8d5 501ea028ea5e |
comparison
equal
deleted
inserted
replaced
52:3fa127885e60 | 53:b0bce2c8e4a9 |
---|---|
1 // | |
2 // FavoriteButton.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 19/07/2021. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 struct FavoriteButton: View { | |
11 var currencyPair: String | |
12 | |
13 @Environment(\.managedObjectContext) private var viewContext | |
14 @FetchRequest(sortDescriptors: []) private var favorite: FetchedResults<Favorite> | |
15 | |
16 @State private var starSymbol = "star" | |
17 | |
18 var body: some View { | |
19 let favoriteCurrencyPairs = favorite.map { $0.currencyPair } | |
20 Button(action: { favoriteAction(favoriteCurrencyPairs) }) { | |
21 RoundedRectangle(cornerRadius: 15) | |
22 .foregroundColor(Color(.secondarySystemBackground)) | |
23 .frame(width: 60, height: 60) | |
24 .overlay( | |
25 Image(systemName: generateStar(favoriteCurrencyPairs)) | |
26 .font(.system(size: 28)) | |
27 .foregroundColor(Color(.systemYellow)) | |
28 ) | |
29 } | |
30 } | |
31 | |
32 /* | |
33 If currency pair is favorite: | |
34 * Button action is to remove from favorites | |
35 else: | |
36 * Button action is to add to favorites | |
37 */ | |
38 private func favoriteAction(_ favoriteCurrencyPairs: [String]) { | |
39 if favoriteCurrencyPairs.contains(currencyPair) { | |
40 removeFromFavorites() | |
41 } else { | |
42 addToFavorites() | |
43 } | |
44 | |
45 simpleSuccess() | |
46 } | |
47 | |
48 /* | |
49 if currency pair is favorite: | |
50 * Return "star.fill" symbol | |
51 else: | |
52 * Return "star" | |
53 */ | |
54 private func generateStar(_ favoriteCurrencyPairs: [String]) -> String { | |
55 if favoriteCurrencyPairs.contains(currencyPair) { | |
56 return "star.fill" | |
57 } else { | |
58 return "star" | |
59 } | |
60 } | |
61 | |
62 /* | |
63 * Get first favorite core data object that matches the specified currency pair | |
64 * Delete it | |
65 */ | |
66 private func removeFromFavorites() { | |
67 withAnimation { | |
68 let favoriteObject = favorite.first(where: { $0.currencyPair == currencyPair }) | |
69 viewContext.delete(favoriteObject ?? Favorite()) | |
70 | |
71 do { | |
72 try viewContext.save() | |
73 } catch { | |
74 let nsError = error as NSError | |
75 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
76 } | |
77 } | |
78 } | |
79 | |
80 /* | |
81 * Create a favorite core data object | |
82 * Save it | |
83 */ | |
84 private func addToFavorites() { | |
85 withAnimation { | |
86 let favorite = Favorite(context: viewContext) | |
87 favorite.currencyPair = currencyPair | |
88 | |
89 do { | |
90 try viewContext.save() | |
91 } catch { | |
92 let nsError = error as NSError | |
93 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
94 } | |
95 } | |
96 } | |
97 } | |
98 | |
99 struct FavoriteButton_Previews: PreviewProvider { | |
100 static var previews: some View { | |
101 FavoriteButton(currencyPair: "USD/GBP") | |
102 } | |
103 } |