From fd392a0393f53bb59285ab7f5653e2842921de82 Mon Sep 17 00:00:00 2001 From: Brandon Berhent Date: Wed, 6 Mar 2024 09:35:03 -0500 Subject: [PATCH] More complicated sorting. - 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 --- lib/model/db/appdb.dart | 39 +++++++++++++-------- lib/ui/accounts/accounts_sheet.dart | 53 ++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/lib/model/db/appdb.dart b/lib/model/db/appdb.dart index 7b4397b..6096797 100644 --- a/lib/model/db/appdb.dart +++ b/lib/model/db/appdb.dart @@ -178,8 +178,17 @@ class DBHelper { // Accounts Future> getAccounts() async { var dbClient = await db; - List list = - await dbClient.rawQuery('SELECT * FROM Accounts ORDER BY id'); + List 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 accounts = new List(); for (int i = 0; i < list.length; i++) { accounts.add(Account( @@ -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 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()); @@ -281,11 +291,12 @@ class DBHelper { int newAccountId; await dbClient.transaction((Transaction txn) async { int nextID = 1; - List 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( diff --git a/lib/ui/accounts/accounts_sheet.dart b/lib/ui/accounts/accounts_sheet.dart index 0e55fbd..a51f6d2 100644 --- a/lib/ui/accounts/accounts_sheet.dart +++ b/lib/ui/accounts/accounts_sheet.dart @@ -102,7 +102,22 @@ class _AppAccountsWidgetState extends State { 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; + } + }); }); } }); @@ -120,7 +135,21 @@ class _AppAccountsWidgetState extends State { 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(); @@ -352,8 +381,24 @@ class _AppAccountsWidgetState extends State { 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