mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-10 12:44:31 +00:00
feat: amount formatter and associated providers + prefs
This commit is contained in:
parent
838e623ff2
commit
86cd5bea3e
3 changed files with 118 additions and 0 deletions
55
lib/utilities/amount/amount_formatter.dart
Normal file
55
lib/utilities/amount/amount_formatter.dart
Normal file
|
@ -0,0 +1,55 @@
|
|||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:stackwallet/providers/global/locale_provider.dart';
|
||||
import 'package:stackwallet/providers/global/prefs_provider.dart';
|
||||
import 'package:stackwallet/utilities/amount/amount.dart';
|
||||
import 'package:stackwallet/utilities/amount/amount_unit.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
|
||||
final pAmountUnit = Provider.family<AmountUnit, Coin>(
|
||||
(ref, coin) => ref.watch(
|
||||
prefsChangeNotifierProvider.select(
|
||||
(value) => value.amountUnit(coin),
|
||||
),
|
||||
),
|
||||
);
|
||||
final pMaxDecimals = Provider.family<int, Coin>(
|
||||
(ref, coin) => ref.watch(
|
||||
prefsChangeNotifierProvider.select(
|
||||
(value) => value.maxDecimals(coin),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final pAmountFormatter = Provider.family<AmountFormatter, Coin>((ref, coin) {
|
||||
return AmountFormatter(
|
||||
unit: ref.watch(pAmountUnit(coin)),
|
||||
locale: ref.watch(
|
||||
localeServiceChangeNotifierProvider.select((value) => value.locale),
|
||||
),
|
||||
coin: coin,
|
||||
maxDecimals: ref.watch(pMaxDecimals(coin)),
|
||||
);
|
||||
});
|
||||
|
||||
class AmountFormatter {
|
||||
final AmountUnit unit;
|
||||
final String locale;
|
||||
final Coin coin;
|
||||
final int maxDecimals;
|
||||
|
||||
AmountFormatter({
|
||||
required this.unit,
|
||||
required this.locale,
|
||||
required this.coin,
|
||||
required this.maxDecimals,
|
||||
});
|
||||
|
||||
String format(Amount amount) {
|
||||
return unit.displayAmount(
|
||||
amount: amount,
|
||||
locale: locale,
|
||||
coin: coin,
|
||||
maxDecimalPlaces: maxDecimals,
|
||||
);
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import 'package:intl/number_symbols_data.dart';
|
|||
import 'package:stackwallet/utilities/amount/amount.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
|
||||
// preserve index order as index is used to store value in preferences
|
||||
enum AmountUnit {
|
||||
normal(0),
|
||||
milli(3),
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:stackwallet/db/hive/db.dart';
|
||||
import 'package:stackwallet/utilities/amount/amount_unit.dart';
|
||||
import 'package:stackwallet/utilities/constants.dart';
|
||||
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
import 'package:stackwallet/utilities/enums/languages_enum.dart';
|
||||
import 'package:stackwallet/utilities/enums/sync_type_enum.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
@ -46,6 +48,8 @@ class Prefs extends ChangeNotifier {
|
|||
_themeId = await _getThemeId();
|
||||
_systemBrightnessLightThemeId = await _getSystemBrightnessLightThemeId();
|
||||
_systemBrightnessDarkThemeId = await _getSystemBrightnessDarkTheme();
|
||||
await _setAmountUnits();
|
||||
await _setMaxDecimals();
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
@ -796,4 +800,62 @@ class Prefs extends ChangeNotifier {
|
|||
) as String? ??
|
||||
"dark";
|
||||
}
|
||||
|
||||
// coin amount unit settings
|
||||
|
||||
final Map<Coin, AmountUnit> _amountUnits = {};
|
||||
|
||||
AmountUnit amountUnit(Coin coin) => _amountUnits[coin] ?? AmountUnit.normal;
|
||||
|
||||
void updateAmountUnit({required Coin coin, required AmountUnit amountUnit}) {
|
||||
if (this.amountUnit(coin) != amountUnit) {
|
||||
DB.instance.put<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "amountUnitFor${coin.name}",
|
||||
value: amountUnit.index,
|
||||
);
|
||||
_amountUnits[coin] = amountUnit;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _setAmountUnits() async {
|
||||
for (final coin in Coin.values) {
|
||||
final unitIndex = await DB.instance.get<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "amountUnitFor${coin.name}",
|
||||
) as int? ??
|
||||
0; // 0 is "normal"
|
||||
_amountUnits[coin] = AmountUnit.values[unitIndex];
|
||||
}
|
||||
}
|
||||
|
||||
// coin precision setting (max decimal places to show)
|
||||
|
||||
final Map<Coin, int> _amountDecimals = {};
|
||||
|
||||
int maxDecimals(Coin coin) => _amountDecimals[coin] ?? coin.decimals;
|
||||
|
||||
void updateMaxDecimals({required Coin coin, required int maxDecimals}) {
|
||||
if (this.maxDecimals(coin) != maxDecimals) {
|
||||
DB.instance.put<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "maxDecimalsFor${coin.name}",
|
||||
value: maxDecimals,
|
||||
);
|
||||
_amountDecimals[coin] = maxDecimals;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _setMaxDecimals() async {
|
||||
for (final coin in Coin.values) {
|
||||
final decimals = await DB.instance.get<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "maxDecimalsFor${coin.name}",
|
||||
) as int? ??
|
||||
coin.decimals;
|
||||
_amountDecimals[coin] = decimals;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue