comparison Simoleon/FavoritesView.swift @ 166:e4cbb1eea394

Implement FavoritesView
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 11 Sep 2021 16:30:32 +0200
parents 84137052813d
children
comparison
equal deleted inserted replaced
165:6f024e6a3b19 166:e4cbb1eea394
1 ////
2 //// FavoritesView.swift
3 //// Simoleon
4 ////
5 //// Created by Dennis Concepción Martín on 19/07/2021.
6 ////
7 // 1 //
8 //import SwiftUI 2 // FavoritesView.swift
3 // Simoleon
9 // 4 //
10 //struct FavoritesView: View { 5 // Created by Dennis Concepción Martín on 19/07/2021.
11 // @Environment(\.managedObjectContext) private var viewContext
12 // @FetchRequest(
13 // sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.currencyPair, ascending: true)],
14 // animation: .default) private var favorites: FetchedResults<Favorite>
15 //
16 // var body: some View {
17 // VStack {
18 // if favorites.isEmpty {
19 // Group {
20 // Image(systemName: "star")
21 // .font(.title)
22 //
23 // Text("Search a currency pair and add it to favorites.")
24 // .padding(.top, 5)
25 // }
26 // .multilineTextAlignment(.center)
27 // .foregroundColor(.secondary)
28 // .padding(.horizontal, 50)
29 // } else {
30 // List {
31 // ForEach(favorites) { favorite in
32 // NavigationLink(destination: Conversion()) {
33 //// CurrencyRow(currencyPairName: favorite.currencyPair)
34 // }
35 // }
36 // .onDelete(perform: removeFromFavorites)
37 // }
38 // .listStyle(PlainListStyle())
39 // .accessibilityIdentifier("FavoritesList")
40 // }
41 // }
42 // .navigationTitle("Favorites")
43 // .toolbar {
44 // #if os(iOS)
45 // EditButton()
46 // #endif
47 // }
48 // .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
49 // NavigationView { content }
50 // }
51 // .onAppear {
52 // #if SCREENSHOTS
53 // generateFavoritesForScreenshots()
54 // #endif
55 // }
56 // }
57 //
58 // private func removeFromFavorites(offsets: IndexSet) {
59 // withAnimation {
60 // offsets.map { favorites[$0] }.forEach(viewContext.delete)
61 //
62 // do {
63 // try viewContext.save()
64 // } catch {
65 // let nsError = error as NSError
66 // fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
67 // }
68 // }
69 // }
70 // 6 //
71 // #if SCREENSHOTS 7
72 // /* 8 import SwiftUI
73 // Save currencies to favourites to take screenshots for the App Store 9
74 // */ 10 struct FavoritesView: View {
75 // private func generateFavoritesForScreenshots() { 11 @Environment(\.managedObjectContext) private var viewContext
76 // let favoriteCurrencies = [ 12 @FetchRequest(
77 // "EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD", 13 sortDescriptors: [NSSortDescriptor(keyPath: \FavoritePair.baseSymbol, ascending: true)],
78 // "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD" 14 animation: .default)
79 // ] 15 private var favoritePairs: FetchedResults<FavoritePair>
80 // 16
81 // let coreDataCurrencyPairs = favorites.map { $0.currencyPair } 17 var body: some View {
82 // 18 VStack {
83 // for favoriteCurrency in favoriteCurrencies { 19 if favoritePairs.isEmpty {
84 // if !coreDataCurrencyPairs.contains(favoriteCurrency) { 20 FavoritesPlaceholder()
85 // let favorites = Favorite(context: viewContext) 21 } else {
86 // favorites.currencyPair = favoriteCurrency 22 List {
87 // 23 ForEach(favoritePairs, id:\.self) { favoritePair in
88 // do { 24 FavoritePairRow(favoritePair: favoritePair)
89 // try viewContext.save() 25 .padding(.vertical, 4)
90 // } catch { 26 }
91 // let nsError = error as NSError 27 .onDelete(perform: removeFromFavorites)
92 // fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 28 }
93 // } 29 .listStyle(InsetGroupedListStyle())
94 // } 30 }
95 // } 31 }
96 // } 32 .navigationTitle("Favorites")
97 // #endif 33 .toolbar {
98 //} 34 #if os(iOS)
99 // 35 EditButton()
100 //struct FavoritesView_Previews: PreviewProvider { 36 #endif
101 // static var previews: some View { 37 }
102 // FavoritesView() 38 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
103 // .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) 39 NavigationView { content }
104 // } 40 }
105 //} 41 .onAppear {
42 #if SCREENSHOTS
43 generateFavoritesForScreenshots()
44 #endif
45 }
46 }
47
48 private func removeFromFavorites(offsets: IndexSet) {
49 withAnimation {
50 offsets.map { favoritePairs[$0] }.forEach(viewContext.delete)
51
52 do {
53 try viewContext.save()
54 } catch {
55 let nsError = error as NSError
56 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
57 }
58 }
59 }
60
61 #if SCREENSHOTS
62 /*
63 Save currencies to favourites to take screenshots for the App Store
64 */
65 private func generateFavoritesForScreenshots() {
66 let pairs = ["EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD",
67 "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD"]
68
69 for pair in pairs {
70 let symbols = pair.components(separatedBy: "/")
71 let favoritePair = favoritePairs.first(
72 where: {
73 $0.baseSymbol == symbols[0] && $0.quoteSymbol == symbols[1]
74 })
75
76 if let _ = favoritePair {
77 // Do not save to core data again
78 } else {
79 let favoritePair = FavoritePair(context: viewContext)
80 favoritePair.baseSymbol = symbols[0]
81 favoritePair.quoteSymbol = symbols[1]
82 }
83
84 do {
85 try viewContext.save()
86 } catch {
87 let nsError = error as NSError
88 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
89 }
90 }
91 }
92 #endif
93 }
94
95 struct FavoritesView_Previews: PreviewProvider {
96 static var previews: some View {
97 FavoritesView()
98 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
99 }
100 }