diff --git a/lib/ui/ui_helpers.dart b/lib/ui/ui_helpers.dart index 26c619b025f9058e7e4470c2648a9f7b1ac86a6f..d5373b0bb7b197b0382dd09af45636814f5aa12e 100644 --- a/lib/ui/ui_helpers.dart +++ b/lib/ui/ui_helpers.dart @@ -144,8 +144,9 @@ String formatAmountWithLocale(String locale, double amount) { String formatKAmount(BuildContext context, double amount) => formatAmount(context, amount / 100); -double parseToDoubleLocalized(String locale, String double) => - NumberFormat.decimalPattern(locale).parse(double).toDouble(); +double parseToDoubleLocalized( + {required String locale, required String number}) => + NumberFormat.decimalPattern(locale).parse(number).toDouble(); String localizeNumber(BuildContext context, double amount) => NumberFormat.decimalPattern(context.locale.toString()).format(amount); @@ -241,3 +242,37 @@ ImageIcon get g1nkgoIcon => ImageIcon( String get ginkgoIconLocation => assets('img/favicon.png'); String capitalize(String s) => s[0].toUpperCase() + s.substring(1); + +double calculate({required String textInTerminal, required String decimalSep}) { + String operation = textInTerminal; + double sum = 0.0; + operation = operation.replaceAll( + decimalSep, '.'); // change decimal separator to a dot + final RegExp regex = RegExp(r'[\d.]+'); // regular expression to find numbers + final Iterable<Match> matches = + regex.allMatches(operation); // find all numbers in the input + for (final Match? match in matches) { + try { + if (match != null) { + final String? g1 = match.group(0); + if (g1 != null) { + sum += double.parse(g1); // add the number to the sum + } + } + } catch (e) { + // could not convert the number to a double value, ignore it + } + } + // logger(numberFormat.format(sum)); // print the formatted sum + return sum; +} + +String decimalSep(BuildContext context) { + return NumberFormat.decimalPattern(context.locale.toString()) + .symbols + .DECIMAL_SEP; +} + +Color selectedPatternLock(BuildContext context) => Colors.red; + +Color notSelectedPatternLock(BuildContext context) => Colors.amber;