view Simoleon/Helpers/CurrencyList.swift @ 185:2fc95efcb1ee

connect backend
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Wed, 22 Dec 2021 16:12:23 +0100
parents d2398f02e1ce
children 1ebd1c5dd302
line wrap: on
line source

//
//  CurrencyList.swift
//  Simoleon
//
//  Created by Dennis Concepción Martín on 20/12/21.
//

import SwiftUI


struct CurrencyList: View {
    @Binding var baseCurrency: SupportedCurrencyResult
    @Binding var quoteCurrency: SupportedCurrencyResult
    var selecting: Selection
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        NavigationView {
            List {
                let currencies = getCurrencies()
                ForEach(currencies, id: \.self) { currency in
                    CurrencyRow(currency: currency)
                }
            }
            .navigationTitle("Currencies")
            .toolbar {
                ToolbarItem(placement: .destructiveAction) {
                    Button(action: { dismiss() }) {
                        Image(systemName: "multiply.circle.fill")
                    }
                }
            }
        }
    }
    
    // Get compatible currencies given currency code
    func getCurrencies() -> [SupportedCurrencyResult] {
        let pairs: SupportedPairResponse = readJson(from: "SupportedCurrencies.json")
        let currencies: SupportedCurrencyResponse = readJson(from: "SupportedCurrencies.json")
        var supportedCurrencies = [SupportedCurrencyResult]()
        
        if selecting == .baseCurrency {
            // Get base currencies compatible with quote currency
            let currencyPair = pairs.pairs.filter { $0.toCurrency == quoteCurrency.code }
            for currencyPair in currencyPair {
                let currency = currencies.currencies.filter { $0.code == currencyPair.fromCurrency }
                supportedCurrencies.append(contentsOf: currency)
            }
        } else {
            // Get quote currencies compatible with base currencies
            let currencyPair = pairs.pairs.filter { $0.fromCurrency == baseCurrency.code }
            for currencyPair in currencyPair {
                let currency = currencies.currencies.filter { $0.code == currencyPair.toCurrency }
                supportedCurrencies.append(contentsOf: currency)
            }
        }
        
        
        return supportedCurrencies
    }
}

struct CurrencyList_Previews: PreviewProvider {
    static var previews: some View {
        CurrencyList(
            baseCurrency: .constant(SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)),
            quoteCurrency: .constant(SupportedCurrencyResult(code: "CHF", name: "Swiss Franc", isCrypto: 0)),
            selecting: .baseCurrency
        )
    }
}