Mercurial > public > simoleon
comparison Simoleon/Helpers/FavoriteButton.swift @ 131:501ea028ea5e
Refactor code
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 16 Aug 2021 18:22:38 +0100 |
parents | b0bce2c8e4a9 |
children | 081f0857af51 |
comparison
equal
deleted
inserted
replaced
130:69ce1e7d77b5 | 131:501ea028ea5e |
---|---|
9 | 9 |
10 struct FavoriteButton: View { | 10 struct FavoriteButton: View { |
11 var currencyPair: String | 11 var currencyPair: String |
12 | 12 |
13 @Environment(\.managedObjectContext) private var viewContext | 13 @Environment(\.managedObjectContext) private var viewContext |
14 @FetchRequest(sortDescriptors: []) private var favorite: FetchedResults<Favorite> | 14 @FetchRequest(sortDescriptors: []) private var favorites: FetchedResults<Favorite> |
15 | 15 |
16 @State private var starSymbol = "star" | 16 @State private var starSymbol = "star" |
17 | 17 |
18 var body: some View { | 18 var body: some View { |
19 let favoriteCurrencyPairs = favorite.map { $0.currencyPair } | 19 let favoriteCurrencyPairs = favorites.map { $0.currencyPair } |
20 Button(action: { favoriteAction(favoriteCurrencyPairs) }) { | 20 Button(action: { favoriteAction(favoriteCurrencyPairs) }) { |
21 RoundedRectangle(cornerRadius: 15) | 21 RoundedRectangle(cornerRadius: 15) |
22 .foregroundColor(Color(.secondarySystemBackground)) | 22 .foregroundColor(Color(.secondarySystemBackground)) |
23 .frame(width: 60, height: 60) | 23 .frame(width: 60, height: 60) |
24 .overlay( | 24 .overlay( |
63 * Get first favorite core data object that matches the specified currency pair | 63 * Get first favorite core data object that matches the specified currency pair |
64 * Delete it | 64 * Delete it |
65 */ | 65 */ |
66 private func removeFromFavorites() { | 66 private func removeFromFavorites() { |
67 withAnimation { | 67 withAnimation { |
68 let favoriteObject = favorite.first(where: { $0.currencyPair == currencyPair }) | 68 let favoriteObject = favorites.first(where: { $0.currencyPair == currencyPair }) |
69 viewContext.delete(favoriteObject ?? Favorite()) | 69 viewContext.delete(favoriteObject ?? Favorite()) |
70 | 70 |
71 do { | 71 do { |
72 try viewContext.save() | 72 try viewContext.save() |
73 } catch { | 73 } catch { |