dev
dart 270 lines 8.51 KB
Raw
1 import 'package:cake_wallet/entities/balance_display_mode.dart';
2 import 'package:cake_wallet/entities/bitcoin_amount_display_mode.dart';
3 import 'package:cake_wallet/entities/fiat_currency.dart';
4 import 'package:cake_wallet/entities/sync_status_display_mode.dart';
5 import 'package:cake_wallet/store/app_store.dart';
6 import 'package:cake_wallet/store/settings_store.dart';
7 import 'package:cake_wallet/themes/core/material_base_theme.dart';
8 import 'package:cake_wallet/themes/theme_classes/black_theme.dart';
9 import 'package:cake_wallet/themes/utils/theme_list.dart';
10 import 'package:cw_core/wallet_type.dart';
11 import 'package:mobx/mobx.dart';
12 import 'package:cake_wallet/entities/fiat_api_mode.dart';
13 import 'package:cake_wallet/themes/core/theme_store.dart';
14 import 'package:flutter/material.dart';
15
16 part 'display_settings_view_model.g.dart';
17
18 class DisplaySettingsViewModel = DisplaySettingsViewModelBase with _$DisplaySettingsViewModel;
19
20 abstract class DisplaySettingsViewModelBase with Store {
21 DisplaySettingsViewModelBase(this._appStore, this._themeStore);
22
23 final AppStore _appStore;
24 SettingsStore get _settingsStore => _appStore.settingsStore;
25 final ThemeStore _themeStore;
26
27 @computed
28 bool get disableTradeOption => _settingsStore.disableTradeOption;
29
30 @computed
31 FiatCurrency get fiatCurrency => _settingsStore.fiatCurrency;
32
33 @computed
34 String get languageCode => _settingsStore.languageCode;
35
36 @computed
37 BalanceDisplayMode get balanceDisplayMode => _settingsStore.balanceDisplayMode;
38
39 @computed
40 bool get shouldDisplayBalance => balanceDisplayMode == BalanceDisplayMode.displayableBalance;
41
42 @computed
43 bool get shouldShowMarketPlaceInDashboard => _settingsStore.shouldShowMarketPlaceInDashboard;
44
45 @computed
46 BitcoinAmountDisplayMode get displayAmountsInSatoshi => _settingsStore.displayAmountsInSatoshi;
47
48 @computed
49 MaterialThemeBase get currentTheme => _themeStore.currentTheme;
50
51 @computed
52 ThemeMode get themeMode => _themeStore.themeMode;
53
54 @computed
55 bool get isBlackThemeOledEnabled => _themeStore.currentTheme is BlackTheme && _themeStore.isOled;
56
57 @computed
58 bool get disabledFiatApiMode => _settingsStore.fiatApiMode == FiatApiMode.disabled;
59
60 @computed
61 bool get showAddressBookPopup => _settingsStore.showAddressBookPopupEnabled;
62
63 @computed
64 bool get showZcashCard => _settingsStore.showZcashMissingFundsCard;
65
66 @computed
67 SyncStatusDisplayMode get syncStatusDisplayMode => _settingsStore.syncStatusDisplayMode;
68
69 @computed
70 String get backgroundImage => _settingsStore.backgroundImage;
71
72 @computed
73 List<MaterialThemeBase> get availableThemes {
74 List<MaterialThemeBase> themes;
75 switch (themeMode) {
76 case ThemeMode.light:
77 themes = ThemeList.all.where((theme) => theme.brightness == Brightness.light).toList();
78 break;
79 case ThemeMode.dark:
80 themes = ThemeList.all.where((theme) => theme.brightness == Brightness.dark).toList();
81 break;
82 case ThemeMode.system:
83 final systemBrightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
84 themes = ThemeList.all.where((theme) => theme.brightness == systemBrightness).toList();
85 break;
86 }
87
88 List<MaterialThemeBase> groupedThemes = [];
89 Set<String> addedThemeFamilies = {};
90
91 for (final theme in themes) {
92 if (theme.hasAccentColors) {
93 final family = theme.themeFamily!;
94 if (!addedThemeFamilies.contains(family)) {
95 groupedThemes.add(theme);
96 addedThemeFamilies.add(family);
97 }
98 } else {
99 groupedThemes.add(theme);
100 }
101 }
102
103 return groupedThemes;
104 }
105
106 @computed
107 List<ThemeAccentColor> get availableAccentColors {
108 if (!currentTheme.hasAccentColors) return [];
109
110 if (currentTheme.themeFamily == 'BlackTheme') {
111 return BlackThemeAccentColor.values;
112 }
113 return [];
114 }
115
116 @action
117 void setDisableTradeOption(bool value) => _settingsStore.disableTradeOption = value;
118
119 @action
120 void setBalanceDisplayMode(BalanceDisplayMode value) => _settingsStore.balanceDisplayMode = value;
121
122 @computed
123 bool get showDisplayAmountsInSatoshiSetting => _appStore.wallet?.type == WalletType.bitcoin;
124
125 @computed
126 bool get showZcashCardSetting => _appStore.wallet?.type == WalletType.zcash;
127
128 @action
129 void setDisplayAmountsInSatoshi(BitcoinAmountDisplayMode value) =>
130 _settingsStore.displayAmountsInSatoshi = value;
131
132 @action
133 void setShowZcashCard(bool value) => _settingsStore.showZcashMissingFundsCard = value;
134
135 @action
136 void setShouldDisplayBalance(bool value) {
137 if (value) {
138 _settingsStore.balanceDisplayMode = BalanceDisplayMode.displayableBalance;
139 } else {
140 _settingsStore.balanceDisplayMode = BalanceDisplayMode.hiddenBalance;
141 }
142 }
143
144 @action
145 void onLanguageSelected(String code) {
146 _settingsStore.languageCode = code;
147 }
148
149 @action
150 Future<void> onThemeSelected(MaterialThemeBase newTheme) async {
151 if (newTheme is BlackTheme && _themeStore.isOled) {
152 await setTheme(BlackTheme(newTheme.accentColor, isOled: true));
153 return;
154 }
155 await setTheme(newTheme);
156 }
157
158 @action
159 Future<void> onAccentColorSelected(String accentColorId) async {
160 if (!currentTheme.hasAccentColors) return;
161
162 try {
163 final newTheme = ThemeList.all.firstWhere(
164 (theme) =>
165 theme.hasAccentColors &&
166 theme.themeFamily == currentTheme.themeFamily &&
167 theme.accentColorId == accentColorId,
168 );
169
170 if (newTheme != currentTheme) {
171 if (newTheme is BlackTheme && _themeStore.isOled) {
172 await onThemeSelected(BlackTheme(newTheme.accentColor, isOled: true));
173 } else {
174 await onThemeSelected(newTheme);
175 }
176 }
177 } catch (_) {}
178 }
179
180 @action
181 Future<void> setBlackThemeOled(bool value) async {
182 if (_themeStore.currentTheme is BlackTheme) {
183 await _themeStore.setOledEnabled(value);
184 }
185 }
186
187 bool isThemeSelected(MaterialThemeBase theme) {
188 if (!theme.hasAccentColors) return currentTheme == theme;
189
190 return currentTheme.hasAccentColors && currentTheme.themeFamily == theme.themeFamily;
191 }
192
193 bool isAccentColorSelected(String accentColorId) {
194 if (!currentTheme.hasAccentColors) return false;
195
196 return currentTheme.accentColorId == accentColorId;
197 }
198
199 @action
200 Future<void> setTheme(MaterialThemeBase newTheme) async {
201 await _themeStore.setTheme(newTheme);
202 }
203
204 MaterialThemeBase? getFirstMatchingTheme(ThemeMode mode) {
205 List<MaterialThemeBase> themes;
206 switch (mode) {
207 case ThemeMode.light:
208 themes = ThemeList.all.where((theme) => theme.brightness == Brightness.light).toList();
209 break;
210 case ThemeMode.dark:
211 themes = ThemeList.all.where((theme) => theme.brightness == Brightness.dark).toList();
212 break;
213 case ThemeMode.system:
214 final systemBrightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
215 themes = ThemeList.all.where((theme) => theme.brightness == systemBrightness).toList();
216 break;
217 }
218
219 return themes.isNotEmpty ? themes.first : null;
220 }
221
222 @action
223 Future<void> setThemeMode(ThemeMode value) async {
224 await _themeStore.setThemeMode(value);
225 // We won't override the saved custom theme when switching to system mode.
226 // We'll just let the ThemeStore resolve it based on system brightness and saved theme.
227 if (value != ThemeMode.system) {
228 final matchingTheme = getFirstMatchingTheme(value);
229 if (matchingTheme != null) {
230 await setTheme(matchingTheme);
231 }
232 }
233 }
234
235 @action
236 void setFiatCurrency(FiatCurrency value) => _settingsStore.fiatCurrency = value;
237
238 @action
239 void setShouldShowMarketPlaceInDashbaord(bool value) {
240 _settingsStore.shouldShowMarketPlaceInDashboard = value;
241 }
242
243 @action
244 void setShowAddressBookPopup(bool value) => _settingsStore.showAddressBookPopupEnabled = value;
245
246 @action
247 void setSyncStatusDisplayMode(SyncStatusDisplayMode value) =>
248 _settingsStore.syncStatusDisplayMode = value;
249
250 @action
251 void setBackgroundImage(String path) => _settingsStore.backgroundImage = path;
252
253 String getImageForTheme(MaterialThemeBase theme) {
254 switch (theme.title) {
255 case 'Dark Theme':
256 return 'assets/new-ui/dark.svg';
257 case 'Light Theme':
258 return 'assets/new-ui/light.svg';
259 case 'Black Theme (Cake Primary)':
260 case 'Black Theme (BCH Green)':
261 case 'Black Theme (Bitcoin Yellow)':
262 case 'Black Theme (Monero Orange)':
263 case 'Black Theme (Tron Red)':
264 case 'Black Theme (Frosting Purple)':
265 return 'assets/new-ui/black_accent.svg';
266 default:
267 return 'assets/new-ui/dark.svg';
268 }
269 }
270 }