comparison Simoleon/UI/FavoriteButton.swift @ 158:82bd84c5973c

Implemented Favorite Button
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 28 Aug 2021 19:17:55 +0100
parents 84137052813d
children 0c589138a6f3
comparison
equal deleted inserted replaced
157:8c3bbd640103 158:82bd84c5973c
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct FavoriteButton: View { 10 struct FavoriteButton: View {
11 @State var currencyPair: CurrencyPairModel 11 @State var currencyPair: CurrencyPairModel
12 @State private var scale: CGFloat = 1
12 @Environment(\.managedObjectContext) private var viewContext 13 @Environment(\.managedObjectContext) private var viewContext
13 @FetchRequest(sortDescriptors: []) private var favorites: FetchedResults<Favorite> 14 @FetchRequest(sortDescriptors: []) private var favoritePairs: FetchedResults<FavoritePair>
14 @State private var starSymbol = "star"
15 15
16 var body: some View { 16 var body: some View {
17 Button(action: favoriteAction) { 17 Button(action: {
18 animate()
19 if isFavorite() {
20 remove()
21 } else {
22 add()
23 }
24 }) {
18 RoundedRectangle(cornerRadius: 15) 25 RoundedRectangle(cornerRadius: 15)
19 .foregroundColor(Color(.secondarySystemBackground)) 26 .foregroundColor(Color(.secondarySystemBackground))
20 .frame(width: 60, height: 60) 27 .frame(width: 60, height: 60)
21 .overlay( 28 .overlay(
22 Image(systemName: generateStar()) 29 VStack {
23 .font(.system(size: 28)) 30 if isFavorite() {
24 .foregroundColor(Color(.systemYellow)) 31 Image(systemName: "star.fill")
32 } else {
33 Image(systemName: "star")
34 }
35 }
36 .font(.system(size: 28))
37 .foregroundColor(Color(.systemYellow))
25 ) 38 )
26 } 39 }
27 .accessibilityIdentifier("AddToFavorites") 40 .scaleEffect(scale)
41 .animation(.linear(duration: 0.2), value: scale)
28 } 42 }
29 43
30 /* 44 func add() {
31 If currency pair is favorite: 45 let favoritePair = FavoritePair(context: viewContext)
32 * Button action is to remove from favorites 46 favoritePair.baseSymbol = currencyPair.baseSymbol
33 else: 47 favoritePair.quoteSymbol = currencyPair.quoteSymbol
34 * Button action is to add to favorites
35 */
36 private func favoriteAction() {
37 let favoriteCurrencyPairs = favorites.map { $0.currencyPair }
38 let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
39 if favoriteCurrencyPairs.contains(currencyPair) {
40 removeFromFavorites()
41 } else {
42 addToFavorites()
43 }
44 48
45 let haptics = Haptics() 49 do {
46 haptics.simpleSuccess() 50 try viewContext.save()
47 } 51 } catch {
48 52 let nsError = error as NSError
49 /* 53 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
50 if currency pair is favorite:
51 * Return "star.fill" symbol
52 else:
53 * Return "star"
54 */
55 private func generateStar() -> String {
56 let favoriteCurrencyPairs = favorites.map { $0.currencyPair }
57 let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
58 if favoriteCurrencyPairs.contains(currencyPair) {
59 return "star.fill"
60 } else {
61 return "star"
62 } 54 }
63 } 55 }
64 56
65 /* 57 func remove() {
66 * Get first favorite core data object that matches the specified currency pair 58 let favoritePair = favoritePairs.first(
67 * Delete it 59 where: {
68 */ 60 $0.baseSymbol == currencyPair.baseSymbol && $0.quoteSymbol == currencyPair.quoteSymbol
69 private func removeFromFavorites() { 61 })
70 let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)" 62
71 withAnimation { 63 viewContext.delete(favoritePair!)
72 let favoriteObject = favorites.first(where: { $0.currencyPair == currencyPair }) 64
73 viewContext.delete(favoriteObject ?? Favorite()) 65 do {
74 66 try viewContext.save()
75 do { 67 } catch {
76 try viewContext.save() 68 let nsError = error as NSError
77 } catch { 69 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
78 let nsError = error as NSError
79 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
80 }
81 } 70 }
82 } 71 }
83 72
84 /* 73 func isFavorite() -> Bool {
85 * Create a favorite core data object 74 let favoritePair = favoritePairs.first(
86 * Save it 75 where: {
87 */ 76 $0.baseSymbol == currencyPair.baseSymbol && $0.quoteSymbol == currencyPair.quoteSymbol
88 private func addToFavorites() { 77 })
89 let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)" 78
90 withAnimation { 79 guard let _ = favoritePair else { return false }
91 let favorite = Favorite(context: viewContext) 80
92 favorite.currencyPair = currencyPair 81 return true
93 82 }
94 do { 83
95 try viewContext.save() 84 private func animate() {
96 } catch { 85 scale += 0.2
97 let nsError = error as NSError 86 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
98 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 87 scale -= 0.2
99 }
100 } 88 }
101 } 89 }
102 } 90 }
103 91
104 struct FavoriteButton_Previews: PreviewProvider { 92 struct FavoriteButton_Previews: PreviewProvider {