comparison Simoleon/Helpers/FavouriteButton.swift @ 42:d25b02d439d4

Minor updates subscription and legal requirements
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 26 Jul 2021 15:35:06 +0100
parents d95582268b44
children 75c1a05176f6
comparison
equal deleted inserted replaced
41:7703c122ce96 42:d25b02d439d4
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct FavouriteButton: View { 10 struct FavouriteButton: View {
11 var currencyPair: String 11 var currencyPair: String
12
13 @State private var starSymbol = "star"
12 @Environment(\.managedObjectContext) private var viewContext 14 @Environment(\.managedObjectContext) private var viewContext
13 @FetchRequest(sortDescriptors: []) private var favourite: FetchedResults<Favourite> 15 @FetchRequest(sortDescriptors: []) private var favourite: FetchedResults<Favourite>
14 16
15 var body: some View { 17 var body: some View {
16 Button(action: { 18 let favouriteCurrencyPairs = favourite.map { $0.currencyPair }
17 if isFavourite() { 19 Button(action: { favouriteAction(favouriteCurrencyPairs) }) {
18 removeFromFavourites()
19 simpleSuccess()
20 } else {
21 addToFavourites()
22 simpleSuccess()
23 }
24 }) {
25 RoundedRectangle(cornerRadius: 15) 20 RoundedRectangle(cornerRadius: 15)
26 .foregroundColor(Color(.secondarySystemBackground)) 21 .foregroundColor(Color(.secondarySystemBackground))
27 .frame(width: 60, height: 60) 22 .frame(width: 60, height: 60)
28 .overlay( 23 .overlay(
29 Image(systemName: generateStar()) 24 Image(systemName: generateStar(favouriteCurrencyPairs))
30 .font(.system(size: 28)) 25 .font(.system(size: 28))
31 .foregroundColor(Color(.systemYellow)) 26 .foregroundColor(Color(.systemYellow))
32
33 ) 27 )
34 } 28 }
35 } 29 }
36 30
37 private func isFavourite() -> Bool { 31 /*
38 let favouriteCurrencyPairs = favourite.map { $0.currencyPair } 32 If currency pair is favourite -> button action is to remove from favourites
33 else -> button action is to add to favourites
34 */
35 private func favouriteAction(_ favouriteCurrencyPairs: [String]) {
36 if favouriteCurrencyPairs.contains(currencyPair) {
37 removeFromFavourites()
38 } else {
39 addToFavourites()
40 }
39 41
40 if favouriteCurrencyPairs.contains(currencyPair) { 42 simpleSuccess()
41 return true
42 } else {
43 return false
44 }
45 } 43 }
46 44
47 private func generateStar() -> String { 45 /*
48 if isFavourite() { 46 if currency pair is favourite -> return "star.fill" symbol
47 else -> return "star"
48 */
49 private func generateStar(_ favouriteCurrencyPairs: [String]) -> String {
50 if favouriteCurrencyPairs.contains(currencyPair) {
49 return "star.fill" 51 return "star.fill"
50 } else { 52 } else {
51 return "star" 53 return "star"
52 } 54 }
53 } 55 }
54 56
57 /*
58 1) Get first favourite core data object that matches the specified currency pair
59 2) Delete it
60 */
55 private func removeFromFavourites() { 61 private func removeFromFavourites() {
56 withAnimation { 62 withAnimation {
57 let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair }) 63 let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair })
58 viewContext.delete(favouriteObject ?? Favourite()) 64 viewContext.delete(favouriteObject ?? Favourite())
59 65
64 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 70 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
65 } 71 }
66 } 72 }
67 } 73 }
68 74
75 /*
76 1) Create a favourite core data object
77 2) Save it
78 */
69 private func addToFavourites() { 79 private func addToFavourites() {
70 withAnimation { 80 withAnimation {
71 let favourite = Favourite(context: viewContext) 81 let favourite = Favourite(context: viewContext)
72 favourite.currencyPair = currencyPair 82 favourite.currencyPair = currencyPair
73 83