Mercurial > public > simoleon
comparison Simoleon/Settings.swift @ 47:75c1a05176f6
Refactor code
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 26 Jul 2021 20:08:20 +0100 |
parents | ce4eb7416b41 |
children | 7a6a7c677851 |
comparison
equal
deleted
inserted
replaced
46:ce4eb7416b41 | 47:75c1a05176f6 |
---|---|
81 } | 81 } |
82 } | 82 } |
83 .onAppear { | 83 .onAppear { |
84 checkEntitlement() | 84 checkEntitlement() |
85 /* | 85 /* |
86 if selectedDefaultCurrency is empty -> view is appearing for the first time -> set initial default curency for picker | 86 if selectedDefaultCurrency is empty: |
87 else -> view is appearing after user selected another default currency -> save it to core data | 87 * View is appearing for the first time |
88 * Set initial default curency for picker | |
89 else: | |
90 * View is appearing after user selected another default currency | |
91 * Save it to core data | |
88 */ | 92 */ |
89 if selectedDefaultCurrency == "" { | 93 if selectedDefaultCurrency == "" { |
90 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP" | 94 selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP" |
91 } else { | 95 } else { |
92 setCoreData() | 96 setCoreData() |
93 } | 97 } |
94 } | 98 } |
95 .listStyle(InsetGroupedListStyle()) | 99 .listStyle(InsetGroupedListStyle()) |
100 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in | 104 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in |
101 NavigationView { content } | 105 NavigationView { content } |
102 } | 106 } |
103 } | 107 } |
104 | 108 |
105 /* | 109 |
106 Save default currency to core data | 110 // Save default currency to core data |
107 */ | |
108 private func setCoreData() { | 111 private func setCoreData() { |
109 if self.defaultCurrency.isEmpty { // If it's empty -> add record | 112 if defaultCurrency.isEmpty { // If it's empty -> add record |
110 let defaultCurrency = DefaultCurrency(context: viewContext) | 113 let defaultCurrency = DefaultCurrency(context: viewContext) |
111 defaultCurrency.pair = selectedDefaultCurrency | 114 defaultCurrency.pair = selectedDefaultCurrency |
112 | 115 |
113 do { | 116 do { |
114 try viewContext.save() | 117 try viewContext.save() |
115 } catch { | 118 } catch { |
116 print(error.localizedDescription) | 119 print(error.localizedDescription) |
117 } | 120 } |
118 } else { // If not, update record | 121 } else { // If not, update record |
119 self.defaultCurrency.first?.pair = selectedDefaultCurrency | 122 defaultCurrency.first?.pair = selectedDefaultCurrency |
120 try? viewContext.save() | 123 try? viewContext.save() |
121 } | 124 } |
122 } | 125 } |
123 | 126 |
124 /* | 127 // Check if user subscription is active |
125 Check if user subscription is active | |
126 */ | |
127 private func checkEntitlement() { | 128 private func checkEntitlement() { |
128 #if targetEnvironment(simulator) | 129 #if targetEnvironment(simulator) |
129 // We're in simulator | 130 // We're in simulator |
130 entitlementIsActive = true | 131 entitlementIsActive = true |
131 #else | 132 #else |
132 Purchases.shared.purchaserInfo { (purchaserInfo, error) in | 133 Purchases.shared.purchaserInfo { (purchaserInfo, error) in |
133 if purchaserInfo?.entitlements["all"]?.isActive == true { | 134 if purchaserInfo?.entitlements["all"]?.isActive == true { |
134 entitlementIsActive = true | 135 entitlementIsActive = true |
135 print("Entitlement is active") | 136 print("Entitlement is active") |
136 } else { | 137 } else { |
137 entitlementIsActive = false | 138 entitlementIsActive = false |
138 print("Entitlement is NOT active") | 139 print("Entitlement is NOT active") |
139 } | |
140 } | 140 } |
141 } | |
141 #endif | 142 #endif |
142 } | 143 } |
143 } | 144 } |
144 | 145 |
145 struct Settings_Previews: PreviewProvider { | 146 struct Settings_Previews: PreviewProvider { |