comparison Simoleon/ContentView.swift @ 16:aec2e86e5dbd

Change design and icon
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Thu, 15 Jul 2021 19:03:24 +0100
parents a02f463aa906
children 4a81e39186f1
comparison
equal deleted inserted replaced
15:a02f463aa906 16:aec2e86e5dbd
1 // 1 //
2 // ContentView.swift 2 // ContentView.swift
3 // Simoleon 3 // Simoleon
4 // 4 //
5 // Created by Dennis Concepción Martín on 08/07/2021. 5 // Created by Dennis Concepción Martín on 15/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import Alamofire
10 9
11 struct ContentView: View { 10 struct ContentView: View {
12 @State private var showingView = false 11 @State var searchText = ""
13 @State private var text = "" 12 @State var searching = false
14 @State private var isEditing = false 13 @State private var refreshView = 0
15 @State private var popularCurrencyPairsQuote = [CurrencyQuoteModel()] 14 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
16 @State private var popularSelectedCurrencyPairQuote: CurrencyQuoteModel? = nil
17
18 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
19 15
20 var body: some View { 16 var body: some View {
21 NavigationView { 17 VStack {
22 if showingView { 18 SearchBar(searchText: $searchText, searching: $searching)
23 ScrollView(showsIndicators: false) { 19 List(filterCurrencies(), id: \.self) { currency in
24 VStack(spacing: 20) { 20 NavigationLink(destination: CurrencyConversion(currency: currency)) {
25 SearchBar(text: $text, isEditing: $isEditing) 21 CurrencyRow(currency: currency)
26 .padding(.top) 22 .padding(.vertical, 7)
27
28 if text.isEmpty {
29 ForEach(popularCurrencyPairsQuote, id: \.self) { currencyQuote in
30 CurrencyRow(currencyQuote: currencyQuote)
31 .onTapGesture { self.popularSelectedCurrencyPairQuote = currencyQuote }
32 }
33 } else {
34 SearchedCurrencyList(text: $text)
35 }
36 }
37 .padding(.vertical)
38 .sheet(item: self.$popularSelectedCurrencyPairQuote) { currencyQuote in
39 CurrencyConversion(currencyQuote: currencyQuote)
40 }
41 } 23 }
42 .navigationTitle("Currencies")
43 .toolbar {
44 ToolbarItem(placement: .cancellationAction) {
45 if isEditing {
46 Button("Cancel", action: {
47 text = ""
48 isEditing = false
49 UIApplication.shared.dismissKeyboard()
50 })
51 }
52 }
53 }
54 } else {
55 ProgressView()
56 .onAppear(perform: requestCurrencyPairsQuote)
57 } 24 }
25 .id(UUID())
26 .listStyle(InsetListStyle())
27 .gesture(DragGesture()
28 .onChanged({ _ in
29 UIApplication.shared.dismissKeyboard()
30 })
31 )
32 }
33 .navigationTitle(searching ? "Search" : "Popular currencies")
34 .toolbar {
35 if searching {
36 Button("Cancel") { searchText = ""
37 withAnimation {
38 searching = false
39 UIApplication.shared.dismissKeyboard()
40 }
41 }
42 }
58 } 43 }
59 } 44 }
60 45
61 /* 46 private func filterCurrencies() -> [String] {
62 Request API 47 if searchText.isEmpty {
63 */ 48 return currencyPairs
64 private func requestCurrencyPairsQuote() { 49 } else {
65 let popularCurrencyPairsArray: [String] = parseJson("PopularCurrencyPairs.json") 50 return currencyPairs.filter { $0.contains(searchText.uppercased()) }
66 let popularCurrencyPairsString = popularCurrencyPairsArray.joined(separator: ",")
67 let quotes = popularCurrencyPairsString.replacingOccurrences(of: "/", with: "-")
68 let url = "https://api.simoleon.app/quotes=\(quotes)"
69
70 // Request popular currencies
71 AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in
72 if let currencyPairsQuote = response.value {
73 self.popularCurrencyPairsQuote = currencyPairsQuote
74 self.showingView = true
75 } else {
76 // Handle error
77 }
78 } 51 }
79 } 52 }
80 } 53 }
81 /*
82 Dismiss keyboard on cancel textfield
83 */
84 extension UIApplication { 54 extension UIApplication {
85 func dismissKeyboard() { 55 func dismissKeyboard() {
86 sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) 56 sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
57 }
87 } 58 }
88 }
89
90 59
91 struct ContentView_Previews: PreviewProvider { 60 struct ContentView_Previews: PreviewProvider {
92 static var previews: some View { 61 static var previews: some View {
93 ContentView() 62 ContentView()
94 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
95 } 63 }
96 } 64 }