comparison Simoleon/Helpers/CurrencySelector.swift @ 28:4f862c618b44

Implemented RevenueCat
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Thu, 22 Jul 2021 19:06:01 +0100
parents c3dda63f50ed
children c52966834f83
comparison
equal deleted inserted replaced
27:d95582268b44 28:4f862c618b44
4 // 4 //
5 // Created by Dennis Concepción Martín on 18/07/2021. 5 // Created by Dennis Concepción Martín on 18/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import Purchases
9 10
10 struct CurrencySelector: View { 11 struct CurrencySelector: View {
11 @Binding var currencyPair: String 12 @Binding var currencyPair: String
12 @Binding var showingCurrencySelector: Bool 13 @Binding var showingCurrencySelector: Bool
14 @EnvironmentObject var subscriptionController: SubscriptionController
15
13 @State private var searchCurrency = "" 16 @State private var searchCurrency = ""
14 @State private var searching = false 17 @State private var searching = false
18 @State private var showingSubscriptionPaywall = false
15 19
16 var body: some View { 20 var body: some View {
17 NavigationView { 21 NavigationView {
18 Form { 22 Form {
19 TextField("Search ...", text: $searchCurrency) { startedEditing in 23 TextField("Search ...", text: $searchCurrency) { startedEditing in
28 } 32 }
29 } 33 }
30 34
31 Section(header: Text("All currencies")) { 35 Section(header: Text("All currencies")) {
32 ForEach(currencyPairs(), id: \.self) { currencyPair in 36 ForEach(currencyPairs(), id: \.self) { currencyPair in
33 Button(action: { 37 Button(action: { select(currencyPair) }) {
34 self.currencyPair = currencyPair
35 showingCurrencySelector = false
36 }) {
37 CurrencyRow(currencyPair: currencyPair) 38 CurrencyRow(currencyPair: currencyPair)
38 } 39 }
39 } 40 }
40 } 41 }
41 } 42 }
42 .gesture(DragGesture() 43 .gesture(DragGesture()
43 .onChanged({ _ in 44 .onChanged({ _ in
44 UIApplication.shared.dismissKeyboard() 45 UIApplication.shared.dismissKeyboard()
46 searching = false
45 }) 47 })
46 ) 48 )
47 .navigationTitle("Currencies") 49 .navigationTitle("Currencies")
48 .navigationBarTitleDisplayMode(.inline) 50 .navigationBarTitleDisplayMode(.inline)
49 .toolbar { 51 .toolbar {
50 ToolbarItem(placement: .confirmationAction) { 52 ToolbarItem(placement: .cancellationAction) {
51 Button("OK", action: { showingCurrencySelector = false }) 53 Button("Cancel", action: { showingCurrencySelector = false })
52 } 54 }
53 55
54 ToolbarItem(placement: .cancellationAction) { 56 ToolbarItem(placement: .confirmationAction) {
55 if searching { 57 if searching {
56 Button("Cancel") { 58 Button("OK") {
57 searchCurrency = "" 59 searchCurrency = ""
58 withAnimation { 60 withAnimation {
59 searching = false 61 searching = false
60 UIApplication.shared.dismissKeyboard() 62 UIApplication.shared.dismissKeyboard()
61 } 63 }
62 } 64 }
63 } 65 }
64 } 66 }
65 } 67 }
68 }
69 .sheet(isPresented: $showingSubscriptionPaywall) {
70 Subscription(showingSubscriptionPaywall: $showingSubscriptionPaywall)
66 } 71 }
67 } 72 }
68 73
69 private func currencyPairs() -> [String] { 74 private func currencyPairs() -> [String] {
70 let currencyPairs: [String] = parseJson("CurrencyPairs.json") 75 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
73 return currencyPairs 78 return currencyPairs
74 } else { 79 } else {
75 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) } 80 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) }
76 } 81 }
77 } 82 }
83
84
85 private func select(_ currencyPair: String) {
86 if subscriptionController.isActive {
87 self.currencyPair = currencyPair
88 showingCurrencySelector = false
89 } else {
90 showingSubscriptionPaywall = true
91 }
92 }
78 } 93 }
79 94
80 95
81 struct CurrencySelector_Previews: PreviewProvider { 96 struct CurrencySelector_Previews: PreviewProvider {
82 static var previews: some View { 97 static var previews: some View {
83 CurrencySelector(currencyPair: .constant("USD/GBP"), showingCurrencySelector: .constant(false)) 98 CurrencySelector(currencyPair: .constant("USD/GBP"), showingCurrencySelector: .constant(false))
99 .environmentObject(SubscriptionController())
84 } 100 }
85 } 101 }