changeset 166:e4cbb1eea394

Implement FavoritesView
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 11 Sep 2021 16:30:32 +0200
parents 6f024e6a3b19
children 1940db1ef321
files Simoleon/FavoritesView.swift Simoleon/Helpers/ListModifier.swift Simoleon/UI/FavoritePairRow.swift Simoleon/UI/FavoritesPlaceholder.swift
diffstat 4 files changed, 170 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/Simoleon/FavoritesView.swift	Sat Sep 11 16:29:57 2021 +0200
+++ b/Simoleon/FavoritesView.swift	Sat Sep 11 16:30:32 2021 +0200
@@ -1,105 +1,100 @@
-////
-////  FavoritesView.swift
-////  Simoleon
-////
-////  Created by Dennis Concepción Martín on 19/07/2021.
-////
 //
-//import SwiftUI
+//  FavoritesView.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 19/07/2021.
 //
-//struct FavoritesView: View {
-//    @Environment(\.managedObjectContext) private var viewContext
-//    @FetchRequest(
-//        sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.currencyPair, ascending: true)],
-//        animation: .default) private var favorites: FetchedResults<Favorite>
-//    
-//    var body: some View {
-//        VStack {
-//            if favorites.isEmpty {
-//                Group {
-//                    Image(systemName: "star")
-//                        .font(.title)
-//                    
-//                    Text("Search a currency pair and add it to favorites.")
-//                        .padding(.top, 5)
-//                }
-//                .multilineTextAlignment(.center)
-//                .foregroundColor(.secondary)
-//                .padding(.horizontal, 50)
-//            } else {
-//                List {
-//                    ForEach(favorites) { favorite in
-//                        NavigationLink(destination: Conversion()) {
-////                            CurrencyRow(currencyPairName: favorite.currencyPair)
-//                        }
-//                    }
-//                    .onDelete(perform: removeFromFavorites)
-//                }
-//                .listStyle(PlainListStyle())
-//                .accessibilityIdentifier("FavoritesList")
-//            }
-//        }
-//        .navigationTitle("Favorites")
-//        .toolbar {
-//            #if os(iOS)
-//            EditButton()
-//            #endif
-//        }
-//        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
-//            NavigationView { content }
-//        }
-//        .onAppear {
-//            #if SCREENSHOTS
-//            generateFavoritesForScreenshots()
-//            #endif
-//        }
-//    }
-//    
-//    private func removeFromFavorites(offsets: IndexSet) {
-//        withAnimation {
-//            offsets.map { favorites[$0] }.forEach(viewContext.delete)
-//            
-//            do {
-//                try viewContext.save()
-//            } catch {
-//                let nsError = error as NSError
-//                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
-//            }
-//        }
-//    }
-//
-//    #if SCREENSHOTS
-//    /*
-//     Save currencies to favourites to take screenshots for the App Store
-//     */
-//    private func generateFavoritesForScreenshots() {
-//        let favoriteCurrencies = [
-//            "EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD",
-//            "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD"
-//        ]
-//        
-//        let coreDataCurrencyPairs = favorites.map { $0.currencyPair }
-//        
-//        for favoriteCurrency in favoriteCurrencies {
-//            if !coreDataCurrencyPairs.contains(favoriteCurrency) {
-//                let favorites = Favorite(context: viewContext)
-//                favorites.currencyPair = favoriteCurrency
-//                
-//                do {
-//                    try viewContext.save()
-//                } catch {
-//                    let nsError = error as NSError
-//                    fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
-//                }
-//            }
-//        }
-//    }
-//    #endif
-//}
-//
-//struct FavoritesView_Previews: PreviewProvider {
-//    static var previews: some View {
-//        FavoritesView()
-//            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
-//    }
-//}
+
+import SwiftUI
+
+struct FavoritesView: View {
+    @Environment(\.managedObjectContext) private var viewContext
+    @FetchRequest(
+        sortDescriptors: [NSSortDescriptor(keyPath: \FavoritePair.baseSymbol, ascending: true)],
+        animation: .default)
+    private var favoritePairs: FetchedResults<FavoritePair>
+    
+    var body: some View {
+        VStack {
+            if favoritePairs.isEmpty {
+                FavoritesPlaceholder()
+            } else {
+                List {
+                    ForEach(favoritePairs, id:\.self) { favoritePair in
+                        FavoritePairRow(favoritePair: favoritePair)
+                            .padding(.vertical, 4)
+                    }
+                    .onDelete(perform: removeFromFavorites)
+                }
+                .listStyle(InsetGroupedListStyle())
+            }
+        }
+        .navigationTitle("Favorites")
+        .toolbar {
+            #if os(iOS)
+            EditButton()
+            #endif
+        }
+        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
+            NavigationView { content }
+        }
+        .onAppear {
+            #if SCREENSHOTS
+            generateFavoritesForScreenshots()
+            #endif
+        }
+    }
+    
+    private func removeFromFavorites(offsets: IndexSet) {
+        withAnimation {
+            offsets.map { favoritePairs[$0] }.forEach(viewContext.delete)
+
+            do {
+                try viewContext.save()
+            } catch {
+                let nsError = error as NSError
+                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
+            }
+        }
+    }
+
+    #if SCREENSHOTS
+    /*
+     Save currencies to favourites to take screenshots for the App Store
+     */
+    private func generateFavoritesForScreenshots() {
+        let pairs = ["EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD",
+                                  "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD"]
+        
+        for pair in pairs {
+            let symbols = pair.components(separatedBy: "/")
+            let favoritePair = favoritePairs.first(
+                where: {
+                    $0.baseSymbol == symbols[0] && $0.quoteSymbol == symbols[1]
+            })
+            
+            if let _ = favoritePair {
+                // Do not save to core data again
+            } else {
+                let favoritePair = FavoritePair(context: viewContext)
+                favoritePair.baseSymbol = symbols[0]
+                favoritePair.quoteSymbol = symbols[1]
+            }
+            
+            do {
+                try viewContext.save()
+            } catch {
+                let nsError = error as NSError
+                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
+            }
+        }
+    }
+    #endif
+}
+
+struct FavoritesView_Previews: PreviewProvider {
+    static var previews: some View {
+        FavoritesView()
+            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
+    }
+}
--- a/Simoleon/Helpers/ListModifier.swift	Sat Sep 11 16:29:57 2021 +0200
+++ b/Simoleon/Helpers/ListModifier.swift	Sat Sep 11 16:30:32 2021 +0200
@@ -11,7 +11,7 @@
     func body(content: Content) -> some View {
             content
                 .id(UUID())
-                .listStyle(PlainListStyle())
+                .listStyle(InsetGroupedListStyle())
                 .gesture(
                     DragGesture()
                             .onChanged({ _ in
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/UI/FavoritePairRow.swift	Sat Sep 11 16:30:32 2021 +0200
@@ -0,0 +1,37 @@
+//
+//  FavoritePairRow.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 5/9/21.
+//
+
+import SwiftUI
+
+struct FavoritePairRow: View {
+    var favoritePair: FavoritePair
+    let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json")
+    
+    var body: some View {
+        HStack {
+            let baseCurrencyDetails = currencyDetails[favoritePair.baseSymbol]
+            let quoteCurrencyDetails = currencyDetails[favoritePair.quoteSymbol]
+            
+            Flag(flag: baseCurrencyDetails!.flag)
+            Flag(flag: quoteCurrencyDetails!.flag)
+                .offset(x: -25)
+                .padding(.trailing, -25)
+            
+            Group {
+                Text("From \(baseCurrencyDetails!.symbol)")
+                Text("to \(quoteCurrencyDetails!.symbol)")
+            }
+            .font(.headline)
+        }
+    }
+}
+
+struct FavoritePairRow_Previews: PreviewProvider {
+    static var previews: some View {
+        FavoritePairRow(favoritePair: FavoritePair())
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/UI/FavoritesPlaceholder.swift	Sat Sep 11 16:30:32 2021 +0200
@@ -0,0 +1,34 @@
+//
+//  FavoritesPlaceholder.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 5/9/21.
+//
+
+import SwiftUI
+
+struct FavoritesPlaceholder: View {
+    var body: some View {
+        VStack {
+            Group {
+                Group {
+                    Image(systemName: "star")
+                        .padding(.bottom)
+                    
+                    Text("No Favorites")
+                        .padding(.bottom, 5)
+                }
+                .font(.system(size: 30))
+                
+                Text("Your favorite currencies will appear here.")
+            }
+            .foregroundColor(.secondary)
+        }
+    }
+}
+
+struct FavoritesPlaceholder_Previews: PreviewProvider {
+    static var previews: some View {
+        FavoritesPlaceholder()
+    }
+}