From 7fe6de785a721a064a34bc58e43f5d9f85130052 Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 23 May 2025 15:06:24 +0700 Subject: [PATCH 1/2] v4.1.6 --- .idea/libraries/Dart_Packages.xml | 210 ++++++++++-------- .idea/libraries/Flutter_Plugins.xml | 8 +- lib/money_formatter/money_formatter.dart | 5 + .../src/flutter_money_formatter_base.dart | 133 +++++++++++ .../src/utils/compact_format_type.dart | 2 + .../src/utils/money_formatter_compare.dart | 23 ++ .../src/utils/money_formatter_output.dart | 36 +++ .../src/utils/money_formatter_settings.dart | 41 ++++ lib/money_formatter/src/utils/utilities.dart | 41 ++++ pubspec.yaml | 1 + 10 files changed, 401 insertions(+), 99 deletions(-) create mode 100644 lib/money_formatter/money_formatter.dart create mode 100644 lib/money_formatter/src/flutter_money_formatter_base.dart create mode 100644 lib/money_formatter/src/utils/compact_format_type.dart create mode 100644 lib/money_formatter/src/utils/money_formatter_compare.dart create mode 100644 lib/money_formatter/src/utils/money_formatter_output.dart create mode 100644 lib/money_formatter/src/utils/money_formatter_settings.dart create mode 100644 lib/money_formatter/src/utils/utilities.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 5056846..dab2ef7 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,30 +5,30 @@ - - - - @@ -49,8 +49,8 @@ + @@ -71,6 +71,7 @@ + @@ -78,21 +79,28 @@ - + + + + + + - - @@ -106,8 +114,8 @@ + @@ -121,14 +129,14 @@ - - @@ -142,7 +150,7 @@ - @@ -156,14 +164,14 @@ - - @@ -191,28 +199,28 @@ - - - - @@ -240,21 +248,28 @@ - - - + + + + + + @@ -268,14 +283,14 @@ - - @@ -338,7 +353,7 @@ - @@ -359,14 +374,14 @@ - - @@ -394,7 +409,7 @@ - @@ -415,14 +430,14 @@ - - @@ -436,8 +451,8 @@ - @@ -458,6 +473,7 @@ + @@ -472,7 +488,7 @@ - @@ -493,7 +509,7 @@ - @@ -507,7 +523,7 @@ - @@ -595,13 +611,6 @@ - - - - - - @@ -654,7 +663,7 @@ - @@ -675,7 +684,7 @@ - @@ -710,7 +719,7 @@ - @@ -738,8 +747,8 @@ + @@ -753,30 +762,31 @@ + + + + @@ -784,6 +794,7 @@ + @@ -791,6 +802,7 @@ + @@ -798,6 +810,7 @@ + @@ -812,7 +825,7 @@ - @@ -910,8 +923,8 @@ + @@ -946,21 +959,21 @@ - - - @@ -985,43 +998,46 @@ - - + + - + - - - + + + + + + - - - + + - + - - + + - - - - + + + + - - - - - + + + + + + @@ -1029,31 +1045,32 @@ - + - - + + - + - - + + + - + - + - + @@ -1066,7 +1083,6 @@ - @@ -1074,32 +1090,36 @@ - + - + - + - + - + - + - + + + + + - + @@ -1114,14 +1134,14 @@ - + - - - + + + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index c4b2248..331e917 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -20,14 +20,14 @@ - - - - + + + + diff --git a/lib/money_formatter/money_formatter.dart b/lib/money_formatter/money_formatter.dart new file mode 100644 index 0000000..eb325a3 --- /dev/null +++ b/lib/money_formatter/money_formatter.dart @@ -0,0 +1,5 @@ +export 'src/utils/compact_format_type.dart'; +export 'src/utils/money_formatter_settings.dart'; +export 'src/utils/money_formatter_output.dart'; +export 'src/utils/money_formatter_compare.dart'; +export 'src/flutter_money_formatter_base.dart'; diff --git a/lib/money_formatter/src/flutter_money_formatter_base.dart b/lib/money_formatter/src/flutter_money_formatter_base.dart new file mode 100644 index 0000000..cbb8817 --- /dev/null +++ b/lib/money_formatter/src/flutter_money_formatter_base.dart @@ -0,0 +1,133 @@ +import 'package:intl/intl.dart'; + +import '/money_formatter/src/utils/compact_format_type.dart'; +import '/money_formatter/src/utils/money_formatter_settings.dart'; +import '/money_formatter/src/utils/money_formatter_output.dart'; +import '/money_formatter/src/utils/money_formatter_compare.dart'; + +part 'utils/utilities.dart'; + +/// [FlutterMoneyFormatter] instance +class MoneyFormatter { + late _Utilities _utilities; + + /// Init instance of [FlutterMoneyFormatter] + /// + /// [amount] (@required) the number that will be formatted + MoneyFormatter({required this.amount, this.settings}) { + settings = settings ?? MoneyFormatterSettings(); + output = _getOutput(); + comparator = MoneyFormatterCompare(amount: amount); + } + + /// Amount number that will be formatted. + double amount; + + /// The formatter settings + MoneyFormatterSettings? settings; + + /// Returns compiled and formatted output in several formats. + late MoneyFormatterOutput output; + + /// Comparator + late MoneyFormatterCompare comparator; + + /// output builder + MoneyFormatterOutput _getOutput() { + _utilities = _Utilities(amount: amount, settings: settings); + + String urs = _utilities.refineSeparator; + int decSepCharPos = urs.indexOf(settings?.decimalSeparator ?? '˘'); + + return MoneyFormatterOutput( + nonSymbol: urs, + symbolOnLeft: '${settings?.symbol ?? ''}${_utilities.spacer}$urs', + symbolOnRight: '$urs${_utilities.spacer}${settings?.symbol ?? ''}', + compactNonSymbol: _compactNonSymbol, + compactSymbolOnLeft: + '${settings?.symbol ?? ''}${_utilities.spacer}$_compactNonSymbol', + compactSymbolOnRight: + '$_compactNonSymbol${_utilities.spacer}${settings?.symbol ?? ''}', + fractionDigitsOnly: + urs.substring((-1 == decSepCharPos ? 0 : decSepCharPos + 1)), + withoutFractionDigits: urs.substring( + 0, -1 == decSepCharPos ? urs.length - 1 : decSepCharPos)); + } + + /// returns FlutterMoneyFormatter after calculating amount. + MoneyFormatter fastCalc( + {required FastCalcType type, required double amount}) { + switch (type) { + case FastCalcType.addition: + this.amount += amount; + break; + + case FastCalcType.substraction: + this.amount -= amount; + break; + + case FastCalcType.multiplication: + this.amount *= amount; + break; + + case FastCalcType.division: + this.amount /= amount; + break; + + case FastCalcType.percentageAddition: + this.amount += (amount / 100) * this.amount; + break; + + case FastCalcType.percentageSubstraction: + this.amount -= (amount / 100) * this.amount; + break; + } + + return this; + } + + /// Copies current instance and change some values to the new instance. + MoneyFormatter copyWith( + {double? amount, + String? symbol, + String? thousandSeparator, + String? decimalSeparator, + int? fractionDigits, + String? symbolAndNumberSeparator, + CompactFormatType? compactFormatType}) { + MoneyFormatterSettings? ts = settings; + + MoneyFormatterSettings mfs = MoneyFormatterSettings( + symbol: symbol ?? ts?.symbol, + thousandSeparator: thousandSeparator ?? ts?.thousandSeparator, + decimalSeparator: decimalSeparator ?? ts?.decimalSeparator, + symbolAndNumberSeparator: + symbolAndNumberSeparator ?? ts?.symbolAndNumberSeparator, + fractionDigits: fractionDigits ?? ts?.fractionDigits, + compactFormatType: compactFormatType ?? ts?.compactFormatType); + + return MoneyFormatter(amount: amount ?? this.amount, settings: mfs); + } + + /// Returns compact format number without currency symbol + String get _compactNonSymbol { + String compacted = _utilities.baseCompact.format(amount); + String numerics = RegExp(r'(\d+\.\d+)|(\d+)') + .allMatches(compacted) + // ignore: no_wildcard_variable_uses + .map((_) => _.group(0)) + .toString() + .replaceAll('(', '') + .replaceAll(')', ''); + + String alphas = compacted.replaceAll(numerics, ''); + + String reformat = NumberFormat.currency( + symbol: '', + decimalDigits: + !numerics.contains('.') ? 0 : settings?.fractionDigits) + .format(num.parse(numerics)); + + return '$reformat$alphas'; + } +} diff --git a/lib/money_formatter/src/utils/compact_format_type.dart b/lib/money_formatter/src/utils/compact_format_type.dart new file mode 100644 index 0000000..e489055 --- /dev/null +++ b/lib/money_formatter/src/utils/compact_format_type.dart @@ -0,0 +1,2 @@ +/// An enum to be used on compact text format +enum CompactFormatType { short, long } diff --git a/lib/money_formatter/src/utils/money_formatter_compare.dart b/lib/money_formatter/src/utils/money_formatter_compare.dart new file mode 100644 index 0000000..9f96fce --- /dev/null +++ b/lib/money_formatter/src/utils/money_formatter_compare.dart @@ -0,0 +1,23 @@ +/// [MoneyFormatterCompare] instance. +/// +/// This instance is used to hold utilities in comparing values held by [FlutterMoneyFormatter] +class MoneyFormatterCompare { + MoneyFormatterCompare({required this.amount}); + + final double amount; + + /// Check current instance amount is lower than [amount] or not + bool isLowerThan(double amount) => this.amount < amount; + + /// Check current instance amount is greater than [amount] or not + bool isGreaterThan(double amount) => this.amount > amount; + + /// Check current instance amount is equal than [amount] or not + bool isEqual(double amount) => this.amount == amount; + + /// Check current instance amount is equal or lower than [amount] or not + bool isEqualOrLowerThan(double amount) => this.amount <= amount; + + /// Check current instance amount is equal or greater than [amount] or not + bool isEqualOrGreaterThan(double amount) => this.amount >= amount; +} diff --git a/lib/money_formatter/src/utils/money_formatter_output.dart b/lib/money_formatter/src/utils/money_formatter_output.dart new file mode 100644 index 0000000..814df62 --- /dev/null +++ b/lib/money_formatter/src/utils/money_formatter_output.dart @@ -0,0 +1,36 @@ +class MoneyFormatterOutput { + /// Init instance of [MoneyFormatterOutput] + MoneyFormatterOutput( + {required this.nonSymbol, + required this.symbolOnLeft, + required this.symbolOnRight, + required this.compactNonSymbol, + required this.compactSymbolOnLeft, + required this.compactSymbolOnRight, + required this.fractionDigitsOnly, + required this.withoutFractionDigits}); + + /// Returns formatted number without currency symbol + final String nonSymbol; + + /// Returns formatted number with currency symbol on the left side. + final String symbolOnLeft; + + /// Returns formatted number with currency symbol on the right side. + final String symbolOnRight; + + /// Returns compact format number without currency symbol + final String compactNonSymbol; + + /// Returns compact format number with currency symbol on the left side. + final String compactSymbolOnLeft; + + /// Returns compact format number with currency symbol on the right side. + final String compactSymbolOnRight; + + /// Returns decimal-only with length as specified on fractionDigits. + final String fractionDigitsOnly; + + /// Returns formatted number without decimal. + final String withoutFractionDigits; +} diff --git a/lib/money_formatter/src/utils/money_formatter_settings.dart b/lib/money_formatter/src/utils/money_formatter_settings.dart new file mode 100644 index 0000000..26f39c9 --- /dev/null +++ b/lib/money_formatter/src/utils/money_formatter_settings.dart @@ -0,0 +1,41 @@ +import '/money_formatter/src/utils/compact_format_type.dart'; + +/// [MoneyFormatterSettings] instance. +/// +/// This instance is used as a configurator for the [FlutterMoneyFormatter] instance. +/// You can change the output of [FlutterMoneyFormatter] through this instance. +class MoneyFormatterSettings { + /// Init instance of [MoneyFormatterSettings] + MoneyFormatterSettings( + {this.symbol, + this.thousandSeparator, + this.decimalSeparator, + this.symbolAndNumberSeparator, + this.fractionDigits, + this.compactFormatType}) { + symbol = symbol ?? '\$'; + thousandSeparator = thousandSeparator ?? ','; + decimalSeparator = decimalSeparator ?? '.'; + symbolAndNumberSeparator = symbolAndNumberSeparator ?? ' '; + fractionDigits = fractionDigits ?? 2; + compactFormatType = compactFormatType ?? CompactFormatType.short; + } + + /// The [symbol] that will be used on formatted output, default value is $ (Dollar Sign) + String? symbol; + + // The character that will be used as thousand separator on formatted output, default value is ',' (comma) + String? thousandSeparator; + + /// The character that will be used as decimal separator on formatted output, default value is '.' (dot) + String? decimalSeparator; + + /// The character that will be used as separator between the numbers and the symbol. + String? symbolAndNumberSeparator; + + /// The fraction digits that will be used on formatted output, default value is 2. + int? fractionDigits; + + /// Compact format type, for example using 'million' or 'M' + CompactFormatType? compactFormatType; +} diff --git a/lib/money_formatter/src/utils/utilities.dart b/lib/money_formatter/src/utils/utilities.dart new file mode 100644 index 0000000..54087e9 --- /dev/null +++ b/lib/money_formatter/src/utils/utilities.dart @@ -0,0 +1,41 @@ +part of '../flutter_money_formatter_base.dart'; + +enum FastCalcType { + addition, + substraction, + multiplication, + division, + percentageAddition, + percentageSubstraction +} + +class _Utilities { + _Utilities({required this.amount, this.settings}) { + settings = settings ?? MoneyFormatterSettings(); + } + + final double amount; + + MoneyFormatterSettings? settings; + + /// Returns formatted number + String get baseFormat => NumberFormat.currency( + symbol: '', decimalDigits: settings?.fractionDigits, locale: 'en_US') + .format(amount); + + /// Returns formatted number with refined separator chars + String get refineSeparator => baseFormat + .replaceAll(',', '(,)') + .replaceAll('.', '(.)') + .replaceAll('(,)', settings?.thousandSeparator ?? ' ') + .replaceAll('(.)', settings?.decimalSeparator ?? ' '); + + /// Returns spacer as `spaceBetweenSymbolAndNumber` value + String get spacer => settings?.symbolAndNumberSeparator ?? ' '; + + /// Returns base compact format + NumberFormat get baseCompact => + settings?.compactFormatType == CompactFormatType.short + ? NumberFormat.compact() + : NumberFormat.compactLong(); +} diff --git a/pubspec.yaml b/pubspec.yaml index 1fa1f0c..94891f0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: dio: ^5.8.0+1 device_meta: ^2.1.7 encrypt: ^5.0.3 + intl: ^0.20.2 flutter: sdk: flutter From b9f8b08b94f9458703bfac7381d44f7cc9d3cd7b Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 23 May 2025 15:07:33 +0700 Subject: [PATCH 2/2] v4.2.0 --- CHANGELOG.md | 4 ++++ README.md | 2 +- lib/woosignal.dart | 2 +- pubspec.yaml | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a35fdc7..fc9ef1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [4.2.0] - 2025-05-23 + +* Add money formatter into library + ## [4.1.6] - 2025-05-23 * Pubspec.yaml dependency updates. diff --git a/README.md b/README.md index 7023070..7b23810 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ In your flutter project add the dependency: ``` dart dependencies: ... - woosignal: ^4.1.6 + woosignal: ^4.2.0 ``` ### Usage example # diff --git a/lib/woosignal.dart b/lib/woosignal.dart index 9384ebe..4b77c1b 100644 --- a/lib/woosignal.dart +++ b/lib/woosignal.dart @@ -58,7 +58,7 @@ import 'package:encrypt/encrypt.dart' as enc; import 'package:encrypt/encrypt.dart'; /// WooSignal Package version -const String _wooSignalVersion = "4.1.6"; +const String _wooSignalVersion = "4.2.0"; class WooSignal { WooSignal._privateConstructor(); diff --git a/pubspec.yaml b/pubspec.yaml index 94891f0..d53cedd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: woosignal description: WooCommerce REST API for dart, connect a WooCommerce store and start developing with our interface for their API endpoints. -version: 4.1.6 +version: 4.2.0 homepage: https://woosignal.com repository: https://github.com/woosignal/flutter-woocommerce-api issue_tracker: https://github.com/woosignal/flutter-woocommerce-api/issues