CW-512 wallet sort (#1195)

* add shared preferences key and make list re-orderable

* macos sonoma build fix

* everything (almost) works

* add translations

* more translations

* everything works

* fixes

* translation merge file fix

* fix for mobile platforms

* fix filter icon being invisible on bright theme

* first pass of adding ascending/descending to filter widget

* small fix

* update ascending switch

* fixes

* fix

* reverse creation sort

* review fixes part 1

* refactor into function rather than tuple + color changes

* more fixes

* remove unrelated file

* updated wording

* cleaner callback

* undo sonoma fix

---------

Co-authored-by: fossephate <fosse@book.local>
Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
Matthew Fosse
2023-12-13 23:58:43 +02:00
committed by GitHub
co-authored by fossephate Omar Hatem
parent 049e4def27
commit 2ff81df9e7
37 changed files with 624 additions and 36 deletions
@@ -1,5 +1,6 @@
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/wallet_list_order_types.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/store/app_store.dart';
@@ -19,7 +20,7 @@ abstract class WalletListViewModelBase with Store {
this._walletLoadingService,
this._authService,
) : wallets = ObservableList<WalletListItem>() {
updateList();
setOrderType(_appStore.settingsStore.walletListOrder);
reaction((_) => _appStore.wallet, (_) => updateList());
}
@@ -43,11 +44,14 @@ abstract class WalletListViewModelBase with Store {
@action
Future<void> loadWallet(WalletListItem walletItem) async {
final wallet =
await _walletLoadingService.load(walletItem.type, walletItem.name);
final wallet = await _walletLoadingService.load(walletItem.type, walletItem.name);
_appStore.changeCurrentWallet(wallet);
}
WalletListOrderType? get orderType => _appStore.settingsStore.walletListOrder;
bool get ascending => _appStore.settingsStore.walletListAscending;
@action
void updateList() {
wallets.clear();
@@ -57,14 +61,105 @@ abstract class WalletListViewModelBase with Store {
name: info.name,
type: info.type,
key: info.key,
isCurrent: info.name == _appStore.wallet?.name &&
info.type == _appStore.wallet?.type,
isCurrent: info.name == _appStore.wallet?.name && info.type == _appStore.wallet?.type,
isEnabled: availableWalletTypes.contains(info.type),
),
),
);
}
Future<void> reorderAccordingToWalletList() async {
if (wallets.isEmpty) {
updateList();
return;
}
_appStore.settingsStore.walletListOrder = WalletListOrderType.Custom;
// make a copy of the walletInfoSource:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
// delete all wallets from walletInfoSource:
await _walletInfoSource.clear();
// add wallets from wallets list in order of wallets list, by name:
for (WalletListItem wallet in wallets) {
for (int i = 0; i < walletInfoSourceCopy.length; i++) {
if (walletInfoSourceCopy[i].name == wallet.name) {
await _walletInfoSource.add(walletInfoSourceCopy[i]);
walletInfoSourceCopy.removeAt(i);
break;
}
}
}
updateList();
}
Future<void> sortGroupByType() async {
// sort the wallets by type:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
if (ascending) {
walletInfoSourceCopy.sort((a, b) => a.type.toString().compareTo(b.type.toString()));
} else {
walletInfoSourceCopy.sort((a, b) => b.type.toString().compareTo(a.type.toString()));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
updateList();
}
Future<void> sortAlphabetically() async {
// sort the wallets alphabetically:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
if (ascending) {
walletInfoSourceCopy.sort((a, b) => a.name.compareTo(b.name));
} else {
walletInfoSourceCopy.sort((a, b) => b.name.compareTo(a.name));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
updateList();
}
Future<void> sortByCreationDate() async {
// sort the wallets by creation date:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
if (ascending) {
walletInfoSourceCopy.sort((a, b) => a.date.compareTo(b.date));
} else {
walletInfoSourceCopy.sort((a, b) => b.date.compareTo(a.date));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
updateList();
}
void setAscending(bool ascending) {
_appStore.settingsStore.walletListAscending = ascending;
}
Future<void> setOrderType(WalletListOrderType? type) async {
if (type == null) return;
_appStore.settingsStore.walletListOrder = type;
switch (type) {
case WalletListOrderType.CreationDate:
await sortByCreationDate();
break;
case WalletListOrderType.Alphabetical:
await sortAlphabetically();
break;
case WalletListOrderType.GroupByType:
await sortGroupByType();
break;
case WalletListOrderType.Custom:
default:
await reorderAccordingToWalletList();
break;
}
}
bool checkIfAuthRequired() {
return _authService.requireAuth();
}