mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
Use baseunit provider (#3582)
* feat: introduce BaseUnit provider for money settings with aspect-based notifications and add unit tests * test: expand use_base_unit_provider_test with comprehensive scenarios for BaseUnitConfig behaviors
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import "package:cake_wallet/new-ui/widgets/money/money_settings_cubit.dart";
|
||||
import "package:cake_wallet/new-ui/widgets/money/use_base_unit_provider.dart";
|
||||
import "package:cake_wallet/store/settings_store.dart";
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_bloc/flutter_bloc.dart";
|
||||
|
||||
class MoneySettingsProvider extends StatelessWidget {
|
||||
const MoneySettingsProvider({required this.settingsStore, this.child, super.key});
|
||||
const MoneySettingsProvider({required this.settingsStore, required this.child, super.key});
|
||||
|
||||
final Widget? child;
|
||||
final Widget child;
|
||||
final SettingsStore settingsStore;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
BlocProvider(create: (_) => MoneySettingsCubit(settingsStore), child: child);
|
||||
Widget build(BuildContext context) => BlocProvider(
|
||||
create: (_) => MoneySettingsCubit(settingsStore),
|
||||
child: BaseUnitScope(
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import "dart:async";
|
||||
|
||||
import "package:cake_wallet/new-ui/widgets/money/money_settings_cubit.dart";
|
||||
import "package:cw_core/currency.dart";
|
||||
import "package:flutter/widgets.dart";
|
||||
import "package:flutter_bloc/flutter_bloc.dart";
|
||||
|
||||
/// Read-only view of the money display settings for the current subtree.
|
||||
///
|
||||
/// ```dart
|
||||
/// final useBaseUnit = BaseUnit.useBaseUnitOf(context, CryptoCurrency.btc);
|
||||
/// ```
|
||||
class BaseUnit extends InheritedModel<Currency> {
|
||||
const BaseUnit({required this.state, required super.child});
|
||||
|
||||
final MoneySettingsState state;
|
||||
|
||||
static bool useBaseUnitOf(BuildContext context, Currency currency) =>
|
||||
InheritedModel.inheritFrom<BaseUnit>(context, aspect: currency)!.state.useBaseUnit(currency);
|
||||
|
||||
static String getSymbolOf(BuildContext context, Currency currency) =>
|
||||
InheritedModel.inheritFrom<BaseUnit>(context, aspect: currency)!.state.getSymbol(currency);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(BaseUnit old) =>
|
||||
state.bitcoinAmountDisplayMode != old.state.bitcoinAmountDisplayMode;
|
||||
|
||||
@override
|
||||
bool updateShouldNotifyDependent(BaseUnit old, Set<Currency> aspects) => aspects.any((c) =>
|
||||
state.useBaseUnit(c) != old.state.useBaseUnit(c) ||
|
||||
state.getSymbol(c) != old.state.getSymbol(c));
|
||||
}
|
||||
|
||||
class BaseUnitScope extends StatefulWidget {
|
||||
const BaseUnitScope({required this.child, super.key});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<BaseUnitScope> createState() => _BaseUnitScopeState();
|
||||
}
|
||||
|
||||
class _BaseUnitScopeState extends State<BaseUnitScope> {
|
||||
late MoneySettingsState _state;
|
||||
late final StreamSubscription<MoneySettingsState> _sub;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final cubit = context.read<MoneySettingsCubit>();
|
||||
_state = cubit.state;
|
||||
_sub = cubit.stream.listen((s) => setState(() => _state = s));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_sub.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => BaseUnit(state: _state, child: widget.child);
|
||||
}
|
||||
Reference in New Issue
Block a user