Mercurial > public > simoleon
comparison Simoleon/UI/FavoriteButton.swift @ 161:3913aff613e8
Fix bug that didn't request API on symbol change
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Tue, 31 Aug 2021 10:57:34 +0100 |
parents | 0c589138a6f3 |
children | f5de15e06c77 |
comparison
equal
deleted
inserted
replaced
160:0c589138a6f3 | 161:3913aff613e8 |
---|---|
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct FavoriteButton: View { | 10 struct FavoriteButton: View { |
11 @ObservedObject var currencyPair: CurrencyPair | 11 @ObservedObject var currencyConversion: CurrencyConversion |
12 @State private var scale: CGFloat = 1 | 12 @State private var scale: CGFloat = 1 |
13 @Environment(\.managedObjectContext) private var viewContext | 13 @Environment(\.managedObjectContext) private var viewContext |
14 @FetchRequest(sortDescriptors: []) private var favoritePairs: FetchedResults<FavoritePair> | 14 @FetchRequest(sortDescriptors: []) private var favoritePairs: FetchedResults<FavoritePair> |
15 | 15 |
16 var body: some View { | 16 var body: some View { |
41 .animation(.linear(duration: 0.2), value: scale) | 41 .animation(.linear(duration: 0.2), value: scale) |
42 } | 42 } |
43 | 43 |
44 func add() { | 44 func add() { |
45 let favoritePair = FavoritePair(context: viewContext) | 45 let favoritePair = FavoritePair(context: viewContext) |
46 favoritePair.baseSymbol = currencyPair.baseSymbol | 46 favoritePair.baseSymbol = currencyConversion.baseSymbol |
47 favoritePair.quoteSymbol = currencyPair.quoteSymbol | 47 favoritePair.quoteSymbol = currencyConversion.quoteSymbol |
48 | 48 |
49 do { | 49 do { |
50 try viewContext.save() | 50 try viewContext.save() |
51 } catch { | 51 } catch { |
52 let nsError = error as NSError | 52 let nsError = error as NSError |
55 } | 55 } |
56 | 56 |
57 func remove() { | 57 func remove() { |
58 let favoritePair = favoritePairs.first( | 58 let favoritePair = favoritePairs.first( |
59 where: { | 59 where: { |
60 $0.baseSymbol == currencyPair.baseSymbol && $0.quoteSymbol == currencyPair.quoteSymbol | 60 $0.baseSymbol == currencyConversion.baseSymbol && $0.quoteSymbol == currencyConversion.quoteSymbol |
61 }) | 61 }) |
62 | 62 |
63 viewContext.delete(favoritePair!) | 63 viewContext.delete(favoritePair!) |
64 | 64 |
65 do { | 65 do { |
71 } | 71 } |
72 | 72 |
73 func isFavorite() -> Bool { | 73 func isFavorite() -> Bool { |
74 let favoritePair = favoritePairs.first( | 74 let favoritePair = favoritePairs.first( |
75 where: { | 75 where: { |
76 $0.baseSymbol == currencyPair.baseSymbol && $0.quoteSymbol == currencyPair.quoteSymbol | 76 $0.baseSymbol == currencyConversion.baseSymbol && $0.quoteSymbol == currencyConversion.quoteSymbol |
77 }) | 77 }) |
78 | 78 |
79 guard let _ = favoritePair else { return false } | 79 guard let _ = favoritePair else { return false } |
80 | 80 |
81 return true | 81 return true |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 struct FavoriteButton_Previews: PreviewProvider { | 92 struct FavoriteButton_Previews: PreviewProvider { |
93 static var previews: some View { | 93 static var previews: some View { |
94 FavoriteButton(currencyPair: CurrencyPair()) | 94 FavoriteButton(currencyConversion: CurrencyConversion()) |
95 } | 95 } |
96 } | 96 } |