Skip to content

Commit

Permalink
More complicated sorting.
Browse files Browse the repository at this point in the history
- Display accounts on same seed first in order
- Retain old function of adding missing indexes before adding new ones
- Sort adhoc accounts at bottom
- Name adhoc accounts on primary key
  • Loading branch information
bbedward committed Mar 6, 2024
1 parent ae1205f commit fd392a0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
39 changes: 25 additions & 14 deletions lib/model/db/appdb.dart
Expand Up @@ -178,8 +178,17 @@ class DBHelper {
// Accounts
Future<List<Account>> getAccounts() async {
var dbClient = await db;
List<Map> list =
await dbClient.rawQuery('SELECT * FROM Accounts ORDER BY id');
List<Map> list = await dbClient.rawQuery("""SELECT * FROM Accounts
ORDER BY
CASE
WHEN acct_index >= 0 THEN 0
ELSE 1
END,
CASE
WHEN acct_index >= 0 THEN acct_index
ELSE id
END;
""");
List<Account> accounts = new List();
for (int i = 0; i < list.length; i++) {
accounts.add(Account(
Expand Down Expand Up @@ -230,15 +239,16 @@ class DBHelper {
int newAccountId;
Account account;
await dbClient.transaction((Transaction txn) async {
int nextIndex;
int nextIndex = 1; // Default starting index
List<Map> accounts = await txn.rawQuery(
'SELECT * from Accounts WHERE acct_index >=0 ORDER BY acct_index ASC');
if (accounts.isEmpty ||
(accounts.length == 1 && accounts[0]["acct_index"] == 0)) {
nextIndex = 1;
} else {
int maxAcctIndex = accounts.last["acct_index"];
nextIndex = maxAcctIndex + 1;
'SELECT * FROM Accounts WHERE acct_index >= 0 ORDER BY acct_index ASC');

for (int i = 0; i < accounts.length; i++) {
if (accounts[i]["acct_index"] > nextIndex) {
break;
} else {
nextIndex = accounts[i]["acct_index"] + 1;
}
}

String nextName = nameBuilder.replaceAll("%1", nextIndex.toString());
Expand Down Expand Up @@ -281,11 +291,12 @@ class DBHelper {
int newAccountId;
await dbClient.transaction((Transaction txn) async {
int nextID = 1;
List<Map> accounts =
await txn.rawQuery('SELECT * from Accounts WHERE acct_index == -1');
for (int i = 0; i < accounts.length; i++) {
nextID++;
var result = await txn.rawQuery('SELECT MAX(id) AS max_id FROM Accounts');
if (result[0]["max_id"] != null) {
int maxId = result[0]["max_id"];
nextID = maxId + 1;
}

String nextName = nameBuilder.replaceAll("%1", "${nextID.toString()}");
String address = NanoUtil.privateToAddress(privateKey);
account = Account(
Expand Down
53 changes: 49 additions & 4 deletions lib/ui/accounts/accounts_sheet.dart
Expand Up @@ -102,7 +102,22 @@ class _AppAccountsWidgetState extends State<AppAccountsWidget> {
setState(() {
widget.accounts.removeWhere((a) => a.id == event.account.id);
widget.accounts.add(event.account);
widget.accounts.sort((a, b) => a.id.compareTo(b.id));
widget.accounts.sort((a, b) {
// Sort by acct_index first, then by id for adhoc accounts
if ((a.index >= 0 && b.index >= 0) ||
(a.index < 0 && b.index < 0)) {
int indexCompare = a.index.compareTo(b.index);
if (indexCompare != 0) return indexCompare;
// Sort by ID
return a.id.compareTo(b.id);
} else if (a.index < 0 && b.index >= 0) {
// a should come after b
return 1;
} else {
// a should come before b
return -1;
}
});
});
}
});
Expand All @@ -120,7 +135,21 @@ class _AppAccountsWidgetState extends State<AppAccountsWidget> {
widget.accounts.add(newAccount);
setState(() {
_addingAccount = false;
widget.accounts.sort((a, b) => a.id.compareTo(b.id));
widget.accounts.sort((a, b) {
// Sort by acct_index first, then by id for adhoc accounts
if ((a.index >= 0 && b.index >= 0) || (a.index < 0 && b.index < 0)) {
int indexCompare = a.index.compareTo(b.index);
if (indexCompare != 0) return indexCompare;
// Sort by ID
return a.id.compareTo(b.id);
} else if (a.index < 0 && b.index >= 0) {
// a should come after b
return 1;
} else {
// a should come before b
return -1;
}
});
// Scroll if list is full
if (expandedKey.currentContext != null) {
RenderBox box = expandedKey.currentContext.findRenderObject();
Expand Down Expand Up @@ -352,8 +381,24 @@ class _AppAccountsWidgetState extends State<AppAccountsWidget> {
widget.accounts.add(newAccount);
setState(() {
_addingAccount = false;
widget.accounts
.sort((a, b) => a.id.compareTo(b.id));
widget.accounts.sort((a, b) {
// Sort by acct_index first, then by id for adhoc accounts
if ((a.index >= 0 && b.index >= 0) ||
(a.index < 0 && b.index < 0)) {
int indexCompare =
a.index.compareTo(b.index);
if (indexCompare != 0)
return indexCompare;
// Sort by ID
return a.id.compareTo(b.id);
} else if (a.index < 0 && b.index >= 0) {
// a should come after b
return 1;
} else {
// a should come before b
return -1;
}
});
// Scroll if list is full
if (expandedKey.currentContext != null) {
RenderBox box = expandedKey.currentContext
Expand Down

0 comments on commit fd392a0

Please sign in to comment.