Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved. 2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. 3 : 4 : import 'package:app_finance/_classes/structure/currency_app_data.dart'; 5 : import 'package:app_finance/_classes/storage/app_preferences.dart'; 6 : import 'package:app_finance/_classes/storage/app_data.dart'; 7 : import 'package:flutter_currency_picker/flutter_currency_picker.dart'; 8 : 9 : class Exchange { 10 : AppData store; 11 : static Currency? defaultCurrency; 12 : 13 3 : Exchange({ 14 : required this.store, 15 : }); 16 : 17 1 : Currency? getDefaultCurrency() { 18 2 : defaultCurrency ??= CurrencyProvider.find(AppPreferences.get(AppPreferences.prefCurrency)); 19 1 : defaultCurrency ??= CurrencyProvider.find('EUR'); 20 : CurrencyDefaults.defaultCurrency = defaultCurrency; 21 : return defaultCurrency; 22 : } 23 : 24 3 : double reform(double? amount, Currency? origin, Currency? target) { 25 : amount ??= 0.0; 26 1 : if (origin != null && target != null && origin.code != target.code) { 27 3 : CurrencyAppData? ex = store.getByUuid('${origin.code}-${target.code}'); 28 : if (ex == null) { 29 1 : ex = CurrencyAppData( 30 : currency: target, 31 : currencyFrom: origin, 32 : ); 33 2 : store.add(ex); 34 : } 35 2 : amount *= ex.details; 36 : } 37 : return amount; 38 : } 39 : }