diff Simoleon/UI/FavoriteButton.swift @ 156:84137052813d

Refactor code
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 28 Aug 2021 11:15:25 +0100
parents Simoleon/Helpers/FavoriteButton.swift@081f0857af51
children 82bd84c5973c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/UI/FavoriteButton.swift	Sat Aug 28 11:15:25 2021 +0100
@@ -0,0 +1,108 @@
+//
+//  FavoriteButton.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 19/07/2021.
+//
+
+import SwiftUI
+
+struct FavoriteButton: View {
+    @State var currencyPair: CurrencyPairModel
+    @Environment(\.managedObjectContext) private var viewContext
+    @FetchRequest(sortDescriptors: []) private var favorites: FetchedResults<Favorite>
+    @State private var starSymbol = "star"
+    
+    var body: some View {
+        Button(action: favoriteAction) {
+            RoundedRectangle(cornerRadius: 15)
+                .foregroundColor(Color(.secondarySystemBackground))
+                .frame(width: 60, height: 60)
+                .overlay(
+                    Image(systemName: generateStar())
+                        .font(.system(size: 28))
+                        .foregroundColor(Color(.systemYellow))
+                )
+        }
+        .accessibilityIdentifier("AddToFavorites")
+    }
+    
+    /*
+     If currency pair is favorite:
+     * Button action is to remove from favorites
+     else:
+     * Button action is to add to favorites
+     */
+    private func favoriteAction() {
+        let favoriteCurrencyPairs = favorites.map { $0.currencyPair }
+        let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
+        if favoriteCurrencyPairs.contains(currencyPair) {
+            removeFromFavorites()
+        } else {
+            addToFavorites()
+        }
+        
+        let haptics = Haptics()
+        haptics.simpleSuccess()
+    }
+    
+    /*
+     if currency pair is favorite:
+     * Return "star.fill" symbol
+     else:
+     * Return "star"
+     */
+    private func generateStar() -> String {
+        let favoriteCurrencyPairs = favorites.map { $0.currencyPair }
+        let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
+        if favoriteCurrencyPairs.contains(currencyPair) {
+            return "star.fill"
+        } else {
+            return "star"
+        }
+    }
+    
+    /*
+     * Get first favorite core data object that matches the specified currency pair
+     * Delete it
+     */
+    private func removeFromFavorites() {
+        let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
+        withAnimation {
+            let favoriteObject = favorites.first(where: { $0.currencyPair == currencyPair })
+            viewContext.delete(favoriteObject ?? Favorite())
+
+            do {
+                try viewContext.save()
+            } catch {
+                let nsError = error as NSError
+                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
+            }
+        }
+    }
+    
+    /*
+     * Create a favorite core data object
+     * Save it
+     */
+    private func addToFavorites() {
+        let currencyPair = "(\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
+        withAnimation {
+            let favorite = Favorite(context: viewContext)
+            favorite.currencyPair = currencyPair
+
+            do {
+                try viewContext.save()
+            } catch {
+                let nsError = error as NSError
+                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
+            }
+        }
+    }
+}
+
+struct FavoriteButton_Previews: PreviewProvider {
+    static var previews: some View {
+        FavoriteButton(currencyPair: CurrencyPairModel(baseSymbol: "USD", quoteSymbol: "EUR"))
+    }
+}