comparison Simoleon/Settings.swift @ 60:7b98dd60381c

Added search bar to default currency picker
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Tue, 27 Jul 2021 18:45:20 +0100
parents 7a6a7c677851
children 1d438bede031
comparison
equal deleted inserted replaced
59:1303c1e50843 60:7b98dd60381c
16 @State private var showingSubscriptionPaywall = false 16 @State private var showingSubscriptionPaywall = false
17 @State private var entitlementIsActive = false 17 @State private var entitlementIsActive = false
18 @State private var alertTitle = "" 18 @State private var alertTitle = ""
19 @State private var alertMessage = "" 19 @State private var alertMessage = ""
20 @State private var showingAlert = false 20 @State private var showingAlert = false
21 @State private var searchCurrency = ""
21 22
22 let currencyPairs: [String] = parseJson("CurrencyPairs.json") 23 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
23 24
24 var body: some View { 25 var body: some View {
25 List { 26 List {
26 Section(header: Text("Preferences", comment: "Section header in settings")) { 27 Section(header: Text("Preferences")) {
27 if entitlementIsActive { 28 if entitlementIsActive {
28 Picker(selection: $selectedDefaultCurrency, label: Text("Default currency", comment: "Picker to select default currency"), content: { 29 Picker("Default currency", selection: $selectedDefaultCurrency) {
29 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in 30 SearchBar(placeholder: "Search...", text: $searchCurrency)
31 .padding(5)
32
33 ForEach(searchResults, id: \.self) { currencyPair in
30 Text(currencyPair) 34 Text(currencyPair)
31 } 35 }
32 }) 36 }
33 } else { 37 } else {
34 LockedCurrencyPicker() 38 LockedCurrencyPicker()
35 .contentShape(Rectangle()) 39 .contentShape(Rectangle())
36 .onTapGesture { showingSubscriptionPaywall = true } 40 .onTapGesture { showingSubscriptionPaywall = true }
37 } 41 }
38 } 42 }
39 43
40 Section(header: Text("Stay in touch", comment: "Section header in settings")) { 44 Section(header: Text("Stay in touch")) {
41 Link(destination: URL(string: "https://itunes.apple.com/app/id1576390953?action=write-review")!) { 45 Link(destination: URL(string: "https://itunes.apple.com/app/id1576390953?action=write-review")!) {
42 HStack { 46 HStack {
43 Image(systemName: "heart.fill") 47 Image(systemName: "heart.fill")
44 .foregroundColor(Color(.systemRed)) 48 .foregroundColor(Color(.systemRed))
45 .imageScale(.large) 49 .imageScale(.large)
46 50
47 Text("Rate Simoleon", comment: "Button to rate app in Settings") 51 Text("Rate Simoleon")
48 } 52 }
49 } 53 }
50 54
51 Link(destination: URL(string: "https://twitter.com/dennisconcep")!) { 55 Link(destination: URL(string: "https://twitter.com/dennisconcep")!) {
52 HStack { 56 HStack {
53 Image("TwitterLogo") 57 Image("TwitterLogo")
54 .resizable() 58 .resizable()
55 .frame(width: 30, height: 30) 59 .frame(width: 30, height: 30)
56 60
57 Text("Developer's Twitter", comment: "Button to go to Twitter in Settings") 61 Text("Developer's Twitter")
58 } 62 }
59 } 63 }
60 64
61 Link(destination: URL(string: "https://dennistech.io/contact")!) { 65 Link(destination: URL(string: "https://dennistech.io/contact")!) {
62 HStack { 66 HStack {
63 Image(systemName: "envelope.circle.fill") 67 Image(systemName: "envelope.circle.fill")
64 .renderingMode(.original) 68 .renderingMode(.original)
65 .imageScale(.large) 69 .imageScale(.large)
66 70
67 Text("Contact", comment: "Button to contact in Settings") 71 Text("Contact")
68 } 72 }
69 } 73 }
70 } 74 }
71 75
72 Section(header: Text("About")) { 76 Section(header: Text("About")) {
73 Link(destination: URL(string: "https://dennistech.io")!) { 77 Link(destination: URL(string: "https://dennistech.io")!) {
74 Text("Website", comment: "Button to go to Dennis Tech website") 78 Text("Website")
75 } 79 }
76 80
77 Link(destination: URL(string: "https://dennistech.io/privacy-policy")!) { 81 Link(destination: URL(string: "https://dennistech.io/privacy-policy")!) {
78 Text("Privacy Policy", comment: "Button to go to app privacy policy") 82 Text("Privacy Policy")
79 } 83 }
80 84
81 Link(destination: URL(string: "https://dennistech.io/terms-of-use")!) { 85 Link(destination: URL(string: "https://dennistech.io/terms-of-use")!) {
82 Text("Terms of Use", comment: "Button to go to app terms of use") 86 Text("Terms of Use")
83 } 87 }
84 } 88 }
85 } 89 }
86 .alert(isPresented: $showingAlert) { 90 .alert(isPresented: $showingAlert) {
87 Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok"))) 91 Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok")))
101 } else { 105 } else {
102 setCoreData() 106 setCoreData()
103 } 107 }
104 } 108 }
105 .listStyle(InsetGroupedListStyle()) 109 .listStyle(InsetGroupedListStyle())
106 .navigationTitle(Text("Settings", comment: "Navigation title")) 110 .navigationTitle("Settings")
107 .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) { 111 .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) {
108 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall) 112 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall)
109 } 113 }
110 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in 114 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
111 NavigationView { content } 115 NavigationView { content }
116 }
117 }
118
119 /*
120 If searched currency string is empty:
121 * Show all currencies
122 else:
123 * Show filtered list of currencies containing searched currency string
124 */
125 var searchResults: [String] {
126 if searchCurrency.isEmpty {
127 return currencyPairs.sorted()
128 } else {
129 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) }
112 } 130 }
113 } 131 }
114 132
115 133
116 // Save default currency to core data 134 // Save default currency to core data
138 #else 156 #else
139 // We're in physical device 157 // We're in physical device
140 Purchases.shared.purchaserInfo { (purchaserInfo, error) in 158 Purchases.shared.purchaserInfo { (purchaserInfo, error) in
141 if purchaserInfo?.entitlements["all"]?.isActive == true { 159 if purchaserInfo?.entitlements["all"]?.isActive == true {
142 entitlementIsActive = true 160 entitlementIsActive = true
143 print("Entitlement is active")
144 } else { 161 } else {
145 entitlementIsActive = false 162 entitlementIsActive = false
146 print("Entitlement is NOT active")
147 } 163 }
148 164
149 if let error = error as NSError? { 165 if let error = error as NSError? {
150 alertTitle = error.localizedDescription 166 alertTitle = error.localizedDescription
151 alertMessage = error.localizedFailureReason ?? "" 167 alertMessage = error.localizedFailureReason ?? ""