Mercurial > public > simoleon
comparison Simoleon/UI/CurrencyList.swift @ 157:8c3bbd640103
Implement Currency Selector
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 28 Aug 2021 11:15:41 +0100 |
parents | |
children | 0c589138a6f3 |
comparison
equal
deleted
inserted
replaced
156:84137052813d | 157:8c3bbd640103 |
---|---|
1 // | |
2 // CurrencyList.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 24/8/21. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 struct CurrencyList: View { | |
11 var currencies: [String] | |
12 @Binding var selectedCurrency: String | |
13 @State private var searchCurrency = "" | |
14 @Environment(\.presentationMode) private var presentation | |
15 let currencyDetails: [String: CurrencyModel] = try! read(json: "Currencies.json") | |
16 | |
17 var searchResults: [String] { | |
18 if searchCurrency.isEmpty { | |
19 return currencies.sorted() | |
20 } else { | |
21 return currencies.filter {$0.contains(searchCurrency.uppercased())} | |
22 } | |
23 } | |
24 | |
25 var body: some View { | |
26 NavigationView { | |
27 List { | |
28 SearchBar(placeholder: "Search...", text: $searchCurrency) | |
29 .padding(.vertical) | |
30 .accessibilityIdentifier("CurrencySearchBar") | |
31 | |
32 ForEach(searchResults, id: \.self) { symbol in | |
33 Button(action: {selectedCurrency = symbol; presentation.wrappedValue.dismiss()}) { | |
34 let currency = currencyDetails[symbol]! | |
35 CurrencyRow(currency: currency) | |
36 } | |
37 } | |
38 } | |
39 .listStyle() | |
40 .navigationTitle("Currencies") | |
41 .navigationBarTitleDisplayMode(.inline) | |
42 .toolbar { | |
43 ToolbarItem(placement: .cancellationAction) { | |
44 Button(action: { presentation.wrappedValue.dismiss() }) { | |
45 Text("Cancel") | |
46 } | |
47 } | |
48 } | |
49 } | |
50 } | |
51 } | |
52 extension View { | |
53 func listStyle() -> some View { | |
54 self.modifier(ListModifier()) | |
55 } | |
56 } | |
57 | |
58 struct CurrencyList_Previews: PreviewProvider { | |
59 static var previews: some View { | |
60 CurrencyList(currencies: ["USD"], selectedCurrency: .constant("USD")) | |
61 } | |
62 } |