Ability to cache currency selection

This commit is contained in:
2022-09-07 00:14:23 +01:00
parent 3c1ecd76c2
commit c0942903ba
13 changed files with 415 additions and 172 deletions

View File

@@ -19,7 +19,7 @@ abstract class BaseStatelessWidget<T extends BaseViewmodel>
Widget build(BuildContext parent) {
return Scaffold(
body: Container(
padding: const EdgeInsets.all(PADDING_GLOBAL),
padding: const EdgeInsets.all(paddingGlobal),
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,

View File

@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'BaseStatelessWidget.dart';
import 'Utils/Constants.dart';
class HomePage extends BaseStatelessWidget<MainViewModel> {
@override
@@ -15,11 +16,13 @@ class HomePage extends BaseStatelessWidget<MainViewModel> {
}
@override
Widget displayWidget(
BuildContext context, MainViewModel model, Widget? child) {
Widget displayWidget(BuildContext context, MainViewModel model, Widget? child) {
TextEditingController controller1 = TextEditingController();
TextEditingController controller2 = TextEditingController();
String selected1 = model.getConversionPair(SelectionType.conversionFrom);
String selected2 = model.getConversionPair(SelectionType.conversionTo);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
@@ -27,7 +30,10 @@ class HomePage extends BaseStatelessWidget<MainViewModel> {
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
DropDownBox(model.data, (selected) {}),
DropDownBox(listOfCurrencies, selected1, (selected) {
selected1 = selected!;
model.setConversionPair(selected1, selected2);
}),
ConverterEditText("Enter conversion from", controller1, (input) => {
controller2.text = model.convertInput(input, SelectionType.conversionFrom)
})
@@ -38,7 +44,10 @@ class HomePage extends BaseStatelessWidget<MainViewModel> {
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
DropDownBox(model.data, (selected) {}),
DropDownBox(listOfCurrencies, selected2, (selected) {
selected2 = selected!;
model.setConversionPair(selected1, selected2);
}),
ConverterEditText("Enter conversion from", controller2, (input) => {
controller1.text = model.convertInput(input, SelectionType.conversionTo)
})

View File

@@ -1,165 +1,33 @@
import 'package:easy_cc_flutter/BaseViewModel.dart';
import 'package:easy_cc_flutter/Utils/SelectionType.dart';
import 'Utils/Constants.dart';
import 'data/prefs/CurrencyPair.dart';
import 'data/repository/Repository.dart';
import 'data/repository/RepositoryImpl.dart';
import 'locator.dart';
class MainViewModel extends BaseViewmodel {
final Repository _repository = locator<RepositoryImpl>();
double conversionRate = 1.4;
final List<String> data = <String>['ALL - Albanian Lek',
'AFN - Afghan Afghani',
'DZD - Algerian Dinar',
'AOA - Angolan Kwanza',
'ARS - Argentine Peso',
'AMD - Armenian Dram',
'AWG - Aruban Florin',
'AUD - Australian Dollar',
'AZN - Azerbaijani Manat',
'BSD - Bahamian Dollar',
'BHD - Bahraini Dinar',
'BDT - Bangladeshi Taka',
'BBD - Barbadian Dollar',
'BYR - Belarusian Ruble',
'BZD - Belize Dollar',
'BTN - Bhutanese Ngultrum',
'BTC - Bitcoin',
'BOB - Bolivian Boliviano',
'BAM - Bosnia And Herzegovina Konvertibilna Marka',
'BWP - Botswana Pula',
'BRL - Brazilian Real',
'GBP - British Pound',
'BND - Brunei Dollar',
'BGN - Bulgarian Lev',
'BIF - Burundi Franc',
'KHR - Cambodian Riel',
'CAD - Canadian Dollar',
'CVE - Cape Verdean Escudo',
'KYD - Cayman Islands Dollar',
'XAF - Central African CFA Franc',
'XPF - CFP Franc',
'CLP - Chilean Peso',
'CNY - Chinese Yuan',
'COP - Colombian Peso',
'KMF - Comorian Franc',
'CDF - Congolese Franc',
'CRC - Costa Rican Colon',
'HRK - Croatian Kuna',
'CUP - Cuban Peso',
'CZK - Czech Koruna',
'DKK - Danish Krone',
'DJF - Djiboutian Franc',
'DOP - Dominican Peso',
'XCD - East Caribbean Dollar',
'EGP - Egyptian Pound',
'ERN - Eritrean Nakfa',
'ETB - Ethiopian Birr',
'EUR - Euro',
'FKP - Falkland Islands Pound',
'FJD - Fijian Dollar',
'GMD - Gambian Dalasi',
'GEL - Georgian Lari',
'GHS - Ghanaian Cedi',
'GIP - Gibraltar Pound',
'GTQ - Guatemalan Quetzal',
'GNF - Guinean Franc',
'GYD - Guyanese Dollar',
'HTG - Haitian Gourde',
'HNL - Honduran Lempira',
'HKD - Hong Kong Dollar',
'HUF - Hungarian Forint',
'ISK - Icelandic Kr\u00f3na',
'INR - Indian Rupee',
'IDR - Indonesian Rupiah',
'IRR - Iranian Rial',
'IQD - Iraqi Dinar',
'ILS - Israeli New Sheqel',
'JMD - Jamaican Dollar',
'JPY - Japanese Yen',
'JOD - Jordanian Dinar',
'KZT - Kazakhstani Tenge',
'KES - Kenyan Shilling',
'KWD - Kuwaiti Dinar',
'KGS - Kyrgyzstani Som',
'LAK - Lao Kip',
'LVL - Latvian Lats',
'LBP - Lebanese Lira',
'LSL - Lesotho Loti',
'LRD - Liberian Dollar',
'LYD - Libyan Dinar',
'MOP - Macanese Pataca',
'MKD - Macedonian Denar',
'MGA - Malagasy Ariary',
'MWK - Malawian Kwacha',
'MYR - Malaysian Ringgit',
'MVR - Maldivian Rufiyaa',
'MRO - Mauritanian Ouguiya',
'MUR - Mauritian Rupee',
'MXN - Mexican Peso',
'MDL - Moldovan Leu',
'MNT - Mongolian Tugrik',
'MAD - Moroccan Dirham',
'MZN - Mozambican Metical',
'MMK - Myanma Kyat',
'NAD - Namibian Dollar',
'NPR - Nepalese Rupee',
'ANG - Netherlands Antillean Gulden',
'TWD - New Taiwan Dollar',
'NZD - New Zealand Dollar',
'NIO - Nicaraguan Cordoba',
'NGN - Nigerian Naira',
'KPW - North Korean Won',
'NOK - Norwegian Krone',
'OMR - Omani Rial',
'TOP - Paanga',
'PKR - Pakistani Rupee',
'PAB - Panamanian Balboa',
'PGK - Papua New Guinean Kina',
'PYG - Paraguayan Guarani',
'PEN - Peruvian Nuevo Sol',
'PHP - Philippine Peso',
'PLN - Polish Zloty',
'QAR - Qatari Riyal',
'RON - Romanian Leu',
'RUB - Russian Ruble',
'RWF - Rwandan Franc',
'SHP - Saint Helena Pound',
'WST - Samoan Tala',
'STD - Sao Tome And Principe Dobra',
'SAR - Saudi Riyal',
'RSD - Serbian Dinar',
'SCR - Seychellois Rupee',
'SLL - Sierra Leonean Leone',
'SGD - Singapore Dollar',
'SBD - Solomon Islands Dollar',
'SOS - Somali Shilling',
'ZAR - South African Rand',
'KRW - South Korean Won',
'XDR - Special Drawing Rights',
'LKR - Sri Lankan Rupee',
'SDG - Sudanese Pound',
'SRD - Surinamese Dollar',
'SZL - Swazi Lilangeni',
'SEK - Swedish Krona',
'CHF - Swiss Franc',
'SYP - Syrian Pound',
'TJS - Tajikistani Somoni',
'TZS - Tanzanian Shilling',
'THB - Thai Baht',
'TTD - Trinidad and Tobago Dollar',
'TND - Tunisian Dinar',
'TRY - Turkish New Lira',
'TMT - Turkmenistan Manat',
'AED - UAE Dirham',
'UGX - Ugandan Shilling',
'UAH - Ukrainian Hryvnia',
'USD - United States Dollar',
'UYU - Uruguayan Peso',
'UZS - Uzbekistani Som',
'VUV - Vanuatu Vatu',
'VEF - Venezuelan Bolivar',
'VND - Vietnamese Dong',
'XOF - West African CFA Franc',
'YER - Yemeni Rial',
'ZMW - Zambian Kwacha'];
String getConversionPair(SelectionType type) {
CurrencyPair pair = _repository.getConversionPair();
switch (type) {
case SelectionType.conversionFrom:
return pair.currencyOne != null ? pair.currencyOne! : listOfCurrencies[0];
case SelectionType.conversionTo:
return pair.currencyTwo != null ? pair.currencyTwo! : listOfCurrencies[0];
default:
throw NullThrownError();
}
}
void setConversionPair(String fromCurrency, String toCurrency) {
_repository.setConversionPair(fromCurrency, toCurrency);
}
String convertInput(String? input, SelectionType type) {
if (input == null || input.isEmpty) {

View File

@@ -6,4 +6,162 @@ const Color colourThree = Color(0xFF2978A0);
const Color colourFour = Color(0xFF8549ff);
const Color colourFive = Color(0xFFC6E0FF);
const double PADDING_GLOBAL = 12;
const double paddingGlobal = 12;
final List<String> listOfCurrencies = <String>[
'ALL - Albanian Lek',
'AFN - Afghan Afghani',
'DZD - Algerian Dinar',
'AOA - Angolan Kwanza',
'ARS - Argentine Peso',
'AMD - Armenian Dram',
'AWG - Aruban Florin',
'AUD - Australian Dollar',
'AZN - Azerbaijani Manat',
'BSD - Bahamian Dollar',
'BHD - Bahraini Dinar',
'BDT - Bangladeshi Taka',
'BBD - Barbadian Dollar',
'BYR - Belarusian Ruble',
'BZD - Belize Dollar',
'BTN - Bhutanese Ngultrum',
'BTC - Bitcoin',
'BOB - Bolivian Boliviano',
'BAM - Bosnia And Herzegovina Konvertibilna Marka',
'BWP - Botswana Pula',
'BRL - Brazilian Real',
'GBP - British Pound',
'BND - Brunei Dollar',
'BGN - Bulgarian Lev',
'BIF - Burundi Franc',
'KHR - Cambodian Riel',
'CAD - Canadian Dollar',
'CVE - Cape Verdean Escudo',
'KYD - Cayman Islands Dollar',
'XAF - Central African CFA Franc',
'XPF - CFP Franc',
'CLP - Chilean Peso',
'CNY - Chinese Yuan',
'COP - Colombian Peso',
'KMF - Comorian Franc',
'CDF - Congolese Franc',
'CRC - Costa Rican Colon',
'HRK - Croatian Kuna',
'CUP - Cuban Peso',
'CZK - Czech Koruna',
'DKK - Danish Krone',
'DJF - Djiboutian Franc',
'DOP - Dominican Peso',
'XCD - East Caribbean Dollar',
'EGP - Egyptian Pound',
'ERN - Eritrean Nakfa',
'ETB - Ethiopian Birr',
'EUR - Euro',
'FKP - Falkland Islands Pound',
'FJD - Fijian Dollar',
'GMD - Gambian Dalasi',
'GEL - Georgian Lari',
'GHS - Ghanaian Cedi',
'GIP - Gibraltar Pound',
'GTQ - Guatemalan Quetzal',
'GNF - Guinean Franc',
'GYD - Guyanese Dollar',
'HTG - Haitian Gourde',
'HNL - Honduran Lempira',
'HKD - Hong Kong Dollar',
'HUF - Hungarian Forint',
'ISK - Icelandic Kr\u00f3na',
'INR - Indian Rupee',
'IDR - Indonesian Rupiah',
'IRR - Iranian Rial',
'IQD - Iraqi Dinar',
'ILS - Israeli New Sheqel',
'JMD - Jamaican Dollar',
'JPY - Japanese Yen',
'JOD - Jordanian Dinar',
'KZT - Kazakhstani Tenge',
'KES - Kenyan Shilling',
'KWD - Kuwaiti Dinar',
'KGS - Kyrgyzstani Som',
'LAK - Lao Kip',
'LVL - Latvian Lats',
'LBP - Lebanese Lira',
'LSL - Lesotho Loti',
'LRD - Liberian Dollar',
'LYD - Libyan Dinar',
'MOP - Macanese Pataca',
'MKD - Macedonian Denar',
'MGA - Malagasy Ariary',
'MWK - Malawian Kwacha',
'MYR - Malaysian Ringgit',
'MVR - Maldivian Rufiyaa',
'MRO - Mauritanian Ouguiya',
'MUR - Mauritian Rupee',
'MXN - Mexican Peso',
'MDL - Moldovan Leu',
'MNT - Mongolian Tugrik',
'MAD - Moroccan Dirham',
'MZN - Mozambican Metical',
'MMK - Myanma Kyat',
'NAD - Namibian Dollar',
'NPR - Nepalese Rupee',
'ANG - Netherlands Antillean Gulden',
'TWD - New Taiwan Dollar',
'NZD - New Zealand Dollar',
'NIO - Nicaraguan Cordoba',
'NGN - Nigerian Naira',
'KPW - North Korean Won',
'NOK - Norwegian Krone',
'OMR - Omani Rial',
'TOP - Paanga',
'PKR - Pakistani Rupee',
'PAB - Panamanian Balboa',
'PGK - Papua New Guinean Kina',
'PYG - Paraguayan Guarani',
'PEN - Peruvian Nuevo Sol',
'PHP - Philippine Peso',
'PLN - Polish Zloty',
'QAR - Qatari Riyal',
'RON - Romanian Leu',
'RUB - Russian Ruble',
'RWF - Rwandan Franc',
'SHP - Saint Helena Pound',
'WST - Samoan Tala',
'STD - Sao Tome And Principe Dobra',
'SAR - Saudi Riyal',
'RSD - Serbian Dinar',
'SCR - Seychellois Rupee',
'SLL - Sierra Leonean Leone',
'SGD - Singapore Dollar',
'SBD - Solomon Islands Dollar',
'SOS - Somali Shilling',
'ZAR - South African Rand',
'KRW - South Korean Won',
'XDR - Special Drawing Rights',
'LKR - Sri Lankan Rupee',
'SDG - Sudanese Pound',
'SRD - Surinamese Dollar',
'SZL - Swazi Lilangeni',
'SEK - Swedish Krona',
'CHF - Swiss Franc',
'SYP - Syrian Pound',
'TJS - Tajikistani Somoni',
'TZS - Tanzanian Shilling',
'THB - Thai Baht',
'TTD - Trinidad and Tobago Dollar',
'TND - Tunisian Dinar',
'TRY - Turkish New Lira',
'TMT - Turkmenistan Manat',
'AED - UAE Dirham',
'UGX - Ugandan Shilling',
'UAH - Ukrainian Hryvnia',
'USD - United States Dollar',
'UYU - Uruguayan Peso',
'UZS - Uzbekistani Som',
'VUV - Vanuatu Vatu',
'VEF - Venezuelan Bolivar',
'VND - Vietnamese Dong',
'XOF - West African CFA Franc',
'YER - Yemeni Rial',
'ZMW - Zambian Kwacha'
];

View File

@@ -0,0 +1,6 @@
class CurrencyPair {
String? currencyOne;
String? currencyTwo;
CurrencyPair(this.currencyOne, this.currencyTwo);
}

View File

@@ -0,0 +1,27 @@
import 'dart:ffi';
import 'package:shared_preferences/shared_preferences.dart';
import 'CurrencyPair.dart';
const String CURRENCY_ONE = "conversion_one";
const String CURRENCY_TWO = "conversion_two";
class PreferenceProvider {
late final SharedPreferences _prefs;
Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
}
Future<void> saveConversionPair(String s1, String s2) async {
await _prefs.setString(CURRENCY_ONE, s1);
await _prefs.setString(CURRENCY_TWO, s2);
}
CurrencyPair getConversionPair() {
String? s1 = _prefs.getString(CURRENCY_ONE);
String? s2 = _prefs.getString(CURRENCY_TWO);
return CurrencyPair(s1, s2);
}
}

View File

@@ -0,0 +1,6 @@
import '../prefs/CurrencyPair.dart';
abstract class Repository {
CurrencyPair getConversionPair();
Future<void> setConversionPair(String fromCurrency, String toCurrency);
}

View File

@@ -0,0 +1,20 @@
import 'package:easy_cc_flutter/data/prefs/CurrencyPair.dart';
import 'package:easy_cc_flutter/data/prefs/PreferenceProvider.dart';
import '../../locator.dart';
import 'Repository.dart';
class RepositoryImpl extends Repository {
final PreferenceProvider _prefs = locator<PreferenceProvider>();
@override
CurrencyPair getConversionPair() {
return _prefs.getConversionPair();
}
@override
Future<void> setConversionPair(String fromCurrency, String toCurrency) {
return _prefs.saveConversionPair(fromCurrency, toCurrency);
}
}

14
lib/locator.dart Normal file
View File

@@ -0,0 +1,14 @@
import 'package:easy_cc_flutter/MainViewModel.dart';
import 'package:easy_cc_flutter/data/repository/RepositoryImpl.dart';
import 'package:get_it/get_it.dart';
import 'data/prefs/PreferenceProvider.dart';
GetIt locator = GetIt.instance;
void setupLocator() {
locator.registerLazySingleton(() => PreferenceProvider());
locator.registerLazySingleton(() => RepositoryImpl());
locator.registerFactory(() => MainViewModel());
}

View File

@@ -1,8 +1,13 @@
import 'package:flutter/material.dart';
import 'Home.dart';
import 'data/prefs/PreferenceProvider.dart';
import 'locator.dart';
void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
setupLocator();
await locator<PreferenceProvider>().init();
runApp(const MyApp());
}

View File

@@ -1,14 +1,15 @@
import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../Utils/Constants.dart';
class DropDownBox extends StatelessWidget {
final List<String> _selection;
final String _hintText;
final Function(String?) _onChanged;
const DropDownBox(this._selection, this._onChanged, {super.key});
const DropDownBox(this._selection, this._hintText, this._onChanged,
{super.key});
@override
Widget build(BuildContext context) {
@@ -18,20 +19,21 @@ class DropDownBox extends StatelessWidget {
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(22))),
child: DropdownSearch<String>(
popupProps: const PopupProps.dialog(showSearchBox: true),
popupProps: const PopupProps.dialog(
showSearchBox: true,
dialogProps: DialogProps(backgroundColor: colourThree)),
items: _selection,
dropdownDecoratorProps: const DropDownDecoratorProps(
dropdownDecoratorProps: DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
contentPadding: EdgeInsets.all(14),
contentPadding: const EdgeInsets.all(14),
border: InputBorder.none,
filled: true,
fillColor: Colors.transparent,
hintText: "Select a currency",
hintText: _hintText,
),
),
onChanged: _onChanged,
),
);
}
}
}

View File

@@ -71,6 +71,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
flutter:
dependency: "direct main"
description: flutter
@@ -101,7 +115,7 @@ packages:
source: hosted
version: "4.6.5"
get_it:
dependency: transitive
dependency: "direct main"
description:
name: get_it
url: "https://pub.dartlang.org"
@@ -163,6 +177,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
provider:
dependency: transitive
description:
@@ -177,6 +233,62 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.13.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.15"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.12"
shared_preferences_ios:
dependency: transitive
description:
name: shared_preferences_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
sky_engine:
dependency: transitive
description: flutter
@@ -280,6 +392,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+2"
sdks:
dart: ">=2.18.0-190.0.dev <3.0.0"
flutter: ">=1.20.0"
flutter: ">=3.0.0"

View File

@@ -42,6 +42,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
shared_preferences: ^2.0.15
get_it: ^7.2.0
dev_dependencies:
flutter_test: