comparison Simoleon/Helpers/FavouriteButton.swift @ 47:75c1a05176f6

Refactor code
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 26 Jul 2021 20:08:20 +0100
parents d25b02d439d4
children 7a6a7c677851
comparison
equal deleted inserted replaced
46:ce4eb7416b41 47:75c1a05176f6
27 ) 27 )
28 } 28 }
29 } 29 }
30 30
31 /* 31 /*
32 If currency pair is favourite -> button action is to remove from favourites 32 If currency pair is favourite:
33 else -> button action is to add to favourites 33 * Button action is to remove from favourites
34 else:
35 * Button action is to add to favourites
34 */ 36 */
35 private func favouriteAction(_ favouriteCurrencyPairs: [String]) { 37 private func favouriteAction(_ favouriteCurrencyPairs: [String]) {
36 if favouriteCurrencyPairs.contains(currencyPair) { 38 if favouriteCurrencyPairs.contains(currencyPair) {
37 removeFromFavourites() 39 removeFromFavourites()
38 } else { 40 } else {
41 43
42 simpleSuccess() 44 simpleSuccess()
43 } 45 }
44 46
45 /* 47 /*
46 if currency pair is favourite -> return "star.fill" symbol 48 if currency pair is favourite:
47 else -> return "star" 49 * Return "star.fill" symbol
50 else:
51 * Return "star"
48 */ 52 */
49 private func generateStar(_ favouriteCurrencyPairs: [String]) -> String { 53 private func generateStar(_ favouriteCurrencyPairs: [String]) -> String {
50 if favouriteCurrencyPairs.contains(currencyPair) { 54 if favouriteCurrencyPairs.contains(currencyPair) {
51 return "star.fill" 55 return "star.fill"
52 } else { 56 } else {
53 return "star" 57 return "star"
54 } 58 }
55 } 59 }
56 60
57 /* 61 /*
58 1) Get first favourite core data object that matches the specified currency pair 62 * Get first favourite core data object that matches the specified currency pair
59 2) Delete it 63 * Delete it
60 */ 64 */
61 private func removeFromFavourites() { 65 private func removeFromFavourites() {
62 withAnimation { 66 withAnimation {
63 let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair }) 67 let favouriteObject = favourite.first(where: { $0.currencyPair == currencyPair })
64 viewContext.delete(favouriteObject ?? Favourite()) 68 viewContext.delete(favouriteObject ?? Favourite())
71 } 75 }
72 } 76 }
73 } 77 }
74 78
75 /* 79 /*
76 1) Create a favourite core data object 80 * Create a favourite core data object
77 2) Save it 81 * Save it
78 */ 82 */
79 private func addToFavourites() { 83 private func addToFavourites() {
80 withAnimation { 84 withAnimation {
81 let favourite = Favourite(context: viewContext) 85 let favourite = Favourite(context: viewContext)
82 favourite.currencyPair = currencyPair 86 favourite.currencyPair = currencyPair