comparison Simoleon/Helpers/CurrencySelector.swift @ 19:94fd7ac93060

Redesign
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 18 Jul 2021 20:00:05 +0100
parents
children c3dda63f50ed
comparison
equal deleted inserted replaced
18:a3512b689bbe 19:94fd7ac93060
1 //
2 // CurrencySelector.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 18/07/2021.
6 //
7
8 import SwiftUI
9
10 struct CurrencySelector: View {
11 @Binding var mainCurrencySelected: String
12 @Binding var secondaryCurrencySelected: String
13 @Binding var showingCurrencySelector: Bool
14 @Binding var selectingMainCurrency: Bool
15
16 var body: some View {
17 NavigationView {
18 Form {
19 ForEach(generateCurrencyList(), id: \.self) { currency in
20 Button(action: { select(currency) }) {
21 CurrencyRow(currency: currency)
22 }
23 }
24 }
25 .navigationTitle("Currencies")
26 .navigationBarTitleDisplayMode(.inline)
27 .toolbar {
28 ToolbarItem(placement: .confirmationAction) {
29 Button("OK", action: { showingCurrencySelector = false })
30 }
31 }
32 }
33 }
34
35 private func generateCurrencyList() -> [String] {
36 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
37 var currencies: [String] = []
38
39 for currencyPair in currencyPairs {
40 let splittedCurrencies = currencyPair.split(separator: "/")
41 let mainCurrency = String(splittedCurrencies[0])
42 if !currencies.contains(mainCurrency) {
43 currencies.append(mainCurrency)
44 }
45 }
46
47 return currencies
48 }
49
50 private func select(_ currency: String) {
51 if selectingMainCurrency {
52 self.mainCurrencySelected = currency
53 } else {
54 self.secondaryCurrencySelected = currency
55 }
56
57 showingCurrencySelector = false
58 }
59 }
60
61 struct CurrencySelector_Previews: PreviewProvider {
62 static var previews: some View {
63 CurrencySelector(mainCurrencySelected: .constant(""), secondaryCurrencySelected: .constant(""), showingCurrencySelector: .constant(false), selectingMainCurrency: .constant(true))
64 }
65 }