| 1 | import 'package:bloc/bloc.dart'; |
| 2 | import 'package:cake_wallet/monero/monero.dart'; |
| 3 | import 'package:cake_wallet/wownero/wownero.dart'; |
| 4 | import "package:cw_core/balance_card_style_settings.dart"; |
| 5 | import 'package:cw_core/card_design.dart'; |
| 6 | import 'package:cw_core/crypto_currency.dart'; |
| 7 | import 'package:cw_core/wallet_base.dart'; |
| 8 | import "package:cw_core/wallet_type.dart"; |
| 9 | import 'package:flutter/src/painting/gradient.dart'; |
| 10 | import 'package:meta/meta.dart'; |
| 11 | |
| 12 | part 'card_customizer_event.dart'; |
| 13 | part 'card_customizer_state.dart'; |
| 14 | |
| 15 | class CardCustomizerBloc extends Bloc<CardCustomizerEvent, CardCustomizerState> { |
| 16 | final WalletBase _wallet; |
| 17 | final bool lightningMode; |
| 18 | final bool displaySats; |
| 19 | |
| 20 | CardCustomizerBloc(this._wallet, {this.lightningMode = false, this.displaySats = false}) |
| 21 | : super(CardCustomizerNotLoaded( |
| 22 | 0, 0, [CardDesign.genericDefault], [], "", -1, displaySats, 0)) { |
| 23 | on<_Init>(_init); |
| 24 | on<CardDesignSelected>(_onDesignSelected); |
| 25 | on<ColorSelected>(_onColorSelected); |
| 26 | on<IconStyleSelected>(_onIconStyleSelected); |
| 27 | on<DesignSaved>(_onDesignSaved); |
| 28 | on<AccountNameChanged>(_onAccountNameChanged); |
| 29 | |
| 30 | add(_Init()); |
| 31 | } |
| 32 | |
| 33 | List<Gradient> _updateAvailableColors(CardDesign currentDesign) { |
| 34 | final list = List<Gradient>.from(CardDesign.allGradients, growable: true); |
| 35 | if (CardDesign.specialDesignsForCurrencies[_wallet.currency] != null) { |
| 36 | list.add(CardDesign.specialDesignsForCurrencies[_wallet.currency]!.gradient); |
| 37 | } |
| 38 | return list; |
| 39 | } |
| 40 | |
| 41 | Future<BalanceCardStyleSettings?> _loadCurrentDesignSettings(int accountIndex) async { |
| 42 | return (await BalanceCardStyleSettings.get(_wallet.walletInfo.internalId, accountIndex)); |
| 43 | } |
| 44 | |
| 45 | List<CardDesign> _initAvailableDesigns({bool lightningMode = false}) { |
| 46 | final List<CardDesign> ret = List<CardDesign>.empty(growable: true); |
| 47 | final curr = lightningMode ? CryptoCurrency.btcln : _wallet.currency; |
| 48 | |
| 49 | ret.add(CardDesign.gradientOnlyDesign); |
| 50 | ret.add(CardDesign.forCurrencyIcon(curr)); |
| 51 | |
| 52 | if (CardDesign.specialDesignsForCurrencies[curr] != null) |
| 53 | ret.add(CardDesign.forCurrencySpecial(curr)); |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | int _initSelectedDesign(CardDesign currentDesign) { |
| 59 | if (currentDesign.backgroundType == CardDesignBackgroundTypes.gradientOnly) { |
| 60 | return 0; |
| 61 | } |
| 62 | if (currentDesign.backgroundType == CardDesignBackgroundTypes.svgIcon) { |
| 63 | return 1; |
| 64 | } |
| 65 | if (currentDesign.backgroundType == CardDesignBackgroundTypes.svgFull) { |
| 66 | return 2; |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int _initSelectedIconIndex( |
| 72 | BalanceCardStyleSettings? settings, |
| 73 | List<CardIconPath> availableIconPaths, |
| 74 | ) { |
| 75 | if (settings == null || availableIconPaths.isEmpty) return 0; |
| 76 | if (settings.iconStyleIndex >= availableIconPaths.length) return 0; |
| 77 | return settings.iconStyleIndex; |
| 78 | } |
| 79 | |
| 80 | int _initSelectedColor(CardDesign currentDesign) { |
| 81 | final ret = CardDesign.allGradients.indexOf(currentDesign.gradient); |
| 82 | return ret == -1 ? CardDesign.allGradients.length : ret; |
| 83 | } |
| 84 | |
| 85 | void _init(_Init event, Emitter<CardCustomizerState> emit) async { |
| 86 | late final account; |
| 87 | if (_wallet.type == WalletType.monero) { |
| 88 | account = monero!.getCurrentAccount(_wallet); |
| 89 | } else if (_wallet.type == WalletType.wownero) { |
| 90 | account = wownero!.getCurrentAccount(_wallet); |
| 91 | } else { |
| 92 | account = null; |
| 93 | } |
| 94 | final accountName = (account?.label ?? "") as String; |
| 95 | late final int accountIndex; |
| 96 | if (account != null) { |
| 97 | accountIndex = account.id as int; |
| 98 | } else if (lightningMode) { |
| 99 | accountIndex = 0; |
| 100 | } else { |
| 101 | accountIndex = -1; |
| 102 | } |
| 103 | final curr = lightningMode ? CryptoCurrency.btcln : _wallet.currency; |
| 104 | final currentDesignSettings = await _loadCurrentDesignSettings(accountIndex); |
| 105 | final currentDesign = CardDesign.fromStyleSettings(currentDesignSettings, curr); |
| 106 | final availableDesigns = _initAvailableDesigns(lightningMode: lightningMode); |
| 107 | final availableColors = _updateAvailableColors(currentDesign); |
| 108 | final selectedDesignIndex = _initSelectedDesign(currentDesign); |
| 109 | final selectedColor = _initSelectedColor(currentDesign); |
| 110 | final availableIconPaths = CardDesign.iconPathsForWalletType(curr); |
| 111 | final selectedIconIndex = _initSelectedIconIndex(currentDesignSettings, availableIconPaths); |
| 112 | |
| 113 | emit(CardCustomizerInitial( |
| 114 | selectedDesignIndex, |
| 115 | selectedColor, |
| 116 | availableDesigns, |
| 117 | availableColors, |
| 118 | accountName, |
| 119 | accountIndex, |
| 120 | displaySats, |
| 121 | currentDesignSettings?.cardOrder ?? 0, |
| 122 | availableIconPaths: availableIconPaths, |
| 123 | selectedIconIndex: selectedIconIndex)); |
| 124 | } |
| 125 | |
| 126 | void _onDesignSelected(CardDesignSelected event, Emitter<CardCustomizerState> emit) { |
| 127 | final newColors = _updateAvailableColors(state.availableDesigns[event.newDesignIndex]); |
| 128 | late final int newColorIndex; |
| 129 | if (newColors.isEmpty) { |
| 130 | newColorIndex = 0; |
| 131 | } else if (newColors.length < state.availableColors.length) { |
| 132 | newColorIndex = 0; |
| 133 | } else { |
| 134 | newColorIndex = state.selectedColorIndex.clamp(0, newColors.length - 1); |
| 135 | } |
| 136 | |
| 137 | emit(state.copyWith( |
| 138 | selectedDesignIndex: event.newDesignIndex, |
| 139 | availableColors: newColors, |
| 140 | selectedColorIndex: newColorIndex)); |
| 141 | } |
| 142 | |
| 143 | void _onColorSelected(ColorSelected event, Emitter<CardCustomizerState> emit) { |
| 144 | emit(state.copyWith(selectedColorIndex: event.newColorIndex)); |
| 145 | } |
| 146 | |
| 147 | void _onIconStyleSelected(IconStyleSelected event, Emitter<CardCustomizerState> emit) { |
| 148 | emit(state.copyWith(selectedIconIndex: event.iconIndex)); |
| 149 | } |
| 150 | |
| 151 | void _onAccountNameChanged(AccountNameChanged event, Emitter<CardCustomizerState> emit) { |
| 152 | emit(state.copyWith(accountName: event.newAccountName)); |
| 153 | } |
| 154 | |
| 155 | void _onDesignSaved(DesignSaved event, Emitter<CardCustomizerState> emit) { |
| 156 | BalanceCardStyleSettings.fromCardDesign( |
| 157 | walletInfoId: _wallet.walletInfo.internalId, |
| 158 | accountIndex: state.accountIndex, |
| 159 | cardOrder: state.cardOrder, |
| 160 | design: state.selectedDesign, |
| 161 | iconStyleIndex: state.selectedIconIndex, |
| 162 | gradientIndexOverride: state.selectedColorIndex) |
| 163 | .insert() |
| 164 | .then((value) { |
| 165 | emit(CardCustomizerSaved( |
| 166 | state.selectedDesignIndex, |
| 167 | state.selectedColorIndex, |
| 168 | state.availableDesigns, |
| 169 | state.availableColors, |
| 170 | state.accountName, |
| 171 | state.accountIndex, |
| 172 | state.displaySats, |
| 173 | state.cardOrder, |
| 174 | availableIconPaths: state.availableIconPaths, |
| 175 | selectedIconIndex: state.selectedIconIndex)); |
| 176 | }); |
| 177 | saveAccountName(); |
| 178 | } |
| 179 | |
| 180 | Future<void> saveAccountName() async { |
| 181 | if (_wallet.type == WalletType.monero) { |
| 182 | await saveMoneroAccountName(); |
| 183 | } |
| 184 | |
| 185 | if (_wallet.type == WalletType.wownero) { |
| 186 | await saveWowneroAccountName(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | Future<void> saveMoneroAccountName() async { |
| 191 | final MoneroAccountList moneroAccountList = monero!.getAccountList(_wallet); |
| 192 | await moneroAccountList.setLabelAccount(_wallet, |
| 193 | accountIndex: state.accountIndex, label: state.accountName); |
| 194 | |
| 195 | await _wallet.save(); |
| 196 | } |
| 197 | |
| 198 | Future<void> saveWowneroAccountName() async { |
| 199 | final WowneroAccountList wowneroAccountList = wownero!.getAccountList(_wallet); |
| 200 | await wowneroAccountList.setLabelAccount(_wallet, |
| 201 | accountIndex: state.accountIndex, label: state.accountName); |
| 202 | |
| 203 | await _wallet.save(); |
| 204 | } |
| 205 | } |