comparison Simoleon/Favorites.swift @ 146:f10b0e188905 v1.3.1

Merge pull request #16 from DennisTechnologies/development Release v1.3.1 committer: GitHub <noreply@github.com>
author Dennis C. M. <dennis@denniscm.com>
date Tue, 17 Aug 2021 22:14:58 +0100
parents 87f02d4f9c26
children 738fbd3ca671
comparison
equal deleted inserted replaced
117:a6df002a0a5c 146:f10b0e188905
9 9
10 struct Favorites: View { 10 struct Favorites: View {
11 @Environment(\.managedObjectContext) private var viewContext 11 @Environment(\.managedObjectContext) private var viewContext
12 @FetchRequest( 12 @FetchRequest(
13 sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.currencyPair, ascending: true)], 13 sortDescriptors: [NSSortDescriptor(keyPath: \Favorite.currencyPair, ascending: true)],
14 animation: .default) private var favorite: FetchedResults<Favorite> 14 animation: .default) private var favorites: FetchedResults<Favorite>
15 15
16 var body: some View { 16 var body: some View {
17 VStack { 17 VStack {
18 if favorite.isEmpty { 18 if favorites.isEmpty {
19 Group { 19 Group {
20 Image(systemName: "star") 20 Image(systemName: "star")
21 .font(.title) 21 .font(.title)
22
22 Text("Search a currency pair and add it to favourites.") 23 Text("Search a currency pair and add it to favourites.")
23 .padding(.top, 5) 24 .padding(.top, 5)
24 } 25 }
25 .multilineTextAlignment(.center) 26 .multilineTextAlignment(.center)
26 .foregroundColor(.secondary) 27 .foregroundColor(.secondary)
27 .padding(.horizontal, 50) 28 .padding(.horizontal, 50)
28 } else { 29 } else {
29 List { 30 List {
30 ForEach(favorite) { favorite in 31 ForEach(favorites) { favorite in
31 NavigationLink(destination: Conversion(showNavigationView: false, currencyPair: favorite.currencyPair)) { 32 NavigationLink(destination: Conversion(showNavigationView: false, currencyPair: favorite.currencyPair)) {
32 CurrencyRow(currencyPairName: favorite.currencyPair) 33 CurrencyRow(currencyPairName: favorite.currencyPair)
33 } 34 }
34 } 35 }
35 .onDelete(perform: removeFromFavorites) 36 .onDelete(perform: removeFromFavorites)
45 } 46 }
46 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in 47 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
47 NavigationView { content } 48 NavigationView { content }
48 } 49 }
49 .onAppear { 50 .onAppear {
50 #if DEBUG 51 #if SCREENSHOTS
51 generateFavoritesToScreenshots() 52 generateFavoritesForScreenshots()
52 #endif 53 #endif
53 } 54 }
54 } 55 }
55 56
56 private func removeFromFavorites(offsets: IndexSet) { 57 private func removeFromFavorites(offsets: IndexSet) {
57 withAnimation { 58 withAnimation {
58 offsets.map { favorite[$0] }.forEach(viewContext.delete) 59 offsets.map { favorites[$0] }.forEach(viewContext.delete)
59 60
60 do { 61 do {
61 try viewContext.save() 62 try viewContext.save()
62 } catch { 63 } catch {
63 let nsError = error as NSError 64 let nsError = error as NSError
64 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 65 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
65 } 66 }
66 } 67 }
67 } 68 }
68 69
69 #if DEBUG 70 #if SCREENSHOTS
70 private func generateFavoritesToScreenshots() { 71 /*
71 if favorite.isEmpty { 72 Save currencies to favourites to take screenshots for the App Store
72 let favoriteCurrencies = [ 73 */
73 "EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD", 74 private func generateFavoritesForScreenshots() {
74 "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD" 75 let favoriteCurrencies = [
75 ] 76 "EUR/USD", "BTC/USD", "USD/HKD", "USD/JPY", "AUD/USD",
76 77 "XAU/GBP", "DASH/ETH", "EUR/USD", "XAG/CAD"
77 for favoriteCurrency in favoriteCurrencies { 78 ]
78 let favorite = Favorite(context: viewContext) 79
79 favorite.currencyPair = favoriteCurrency 80 let coreDataCurrencyPairs = favorites.map { $0.currencyPair }
81
82 for favoriteCurrency in favoriteCurrencies {
83 if !coreDataCurrencyPairs.contains(favoriteCurrency) {
84 let favorites = Favorite(context: viewContext)
85 favorites.currencyPair = favoriteCurrency
80 86
81 do { 87 do {
82 try viewContext.save() 88 try viewContext.save()
83 } catch { 89 } catch {
84 let nsError = error as NSError 90 let nsError = error as NSError