# HG changeset patch # User Dennis Concepción Martín # Date 1627332735 -3600 # Node ID 7a6a7c677851d4f6c533a5124c755cae685c10c0 # Parent 67e76ce661a11f23d63d72138c94c92b033f2b06 Handle errors with alerts diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file Simoleon.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/ContentView.swift --- a/Simoleon/ContentView.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/ContentView.swift Mon Jul 26 21:52:15 2021 +0100 @@ -8,10 +8,11 @@ import SwiftUI struct ContentView: View { - @State private var tab: Tab = .convert @Environment(\.managedObjectContext) private var viewContext @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults + @State private var tab: Tab = .convert + var body: some View { TabView(selection: $tab) { Conversion(currencyPair: defaultCurrency.first?.pair ?? "USD/GBP") diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/Conversion.swift --- a/Simoleon/Conversion.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/Conversion.swift Mon Jul 26 21:52:15 2021 +0100 @@ -10,6 +10,7 @@ struct Conversion: View { var showNavigationView: Bool? + @State var currencyPair: String @State private var amountToConvert = "1000" @State private var price: Double = 1.00 diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/Helpers/CurrencyRow.swift --- a/Simoleon/Helpers/CurrencyRow.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/Helpers/CurrencyRow.swift Mon Jul 26 21:52:15 2021 +0100 @@ -9,6 +9,7 @@ struct CurrencyRow: View { var currencyPair: String + let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") var body: some View { diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/Helpers/CurrencySelector.swift --- a/Simoleon/Helpers/CurrencySelector.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/Helpers/CurrencySelector.swift Mon Jul 26 21:52:15 2021 +0100 @@ -14,6 +14,9 @@ @State private var searchCurrency = "" @State private var showingSubscriptionPaywall = false + @State private var alertTitle = "" + @State private var alertMessage = "" + @State private var showingAlert = false var body: some View { NavigationView { @@ -47,6 +50,9 @@ } } } + .alert(isPresented: $showingAlert) { + Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok"))) + } } /* @@ -86,6 +92,12 @@ } else { showingSubscriptionPaywall = true } + + if let error = error as NSError? { + alertTitle = error.localizedDescription + alertMessage = error.localizedFailureReason ?? "" + showingAlert = true + } } #endif } diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/Helpers/FavouriteButton.swift --- a/Simoleon/Helpers/FavouriteButton.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/Helpers/FavouriteButton.swift Mon Jul 26 21:52:15 2021 +0100 @@ -10,10 +10,11 @@ struct FavouriteButton: View { var currencyPair: String - @State private var starSymbol = "star" @Environment(\.managedObjectContext) private var viewContext @FetchRequest(sortDescriptors: []) private var favourite: FetchedResults + @State private var starSymbol = "star" + var body: some View { let favouriteCurrencyPairs = favourite.map { $0.currencyPair } Button(action: { favouriteAction(favouriteCurrencyPairs) }) { diff -r 67e76ce661a1 -r 7a6a7c677851 Simoleon/Settings.swift --- a/Simoleon/Settings.swift Mon Jul 26 21:22:54 2021 +0100 +++ b/Simoleon/Settings.swift Mon Jul 26 21:52:15 2021 +0100 @@ -15,6 +15,9 @@ @State private var selectedDefaultCurrency = "" @State private var showingSubscriptionPaywall = false @State private var entitlementIsActive = false + @State private var alertTitle = "" + @State private var alertMessage = "" + @State private var showingAlert = false let currencyPairs: [String] = parseJson("CurrencyPairs.json") @@ -80,6 +83,9 @@ } } } + .alert(isPresented: $showingAlert) { + Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok"))) + } .onAppear { checkEntitlement() /* @@ -130,6 +136,7 @@ // We're in simulator entitlementIsActive = true #else + // We're in physical device Purchases.shared.purchaserInfo { (purchaserInfo, error) in if purchaserInfo?.entitlements["all"]?.isActive == true { entitlementIsActive = true @@ -138,6 +145,12 @@ entitlementIsActive = false print("Entitlement is NOT active") } + + if let error = error as NSError? { + alertTitle = error.localizedDescription + alertMessage = error.localizedFailureReason ?? "" + showingAlert = true + } } #endif }