fix(Accounts): Avoid model reset on every account change

This fixes the issues where the accounts model gets reset on every minor model change triggering full UI refresh.
This is not fixing the root cause, but the symptoms in the upper-most level I could find in order to keep the regression risk to a low level.
This commit is contained in:
Alex Jbanca 2024-09-03 14:45:27 +03:00 committed by Alex Jbanca
parent 186b5f8798
commit 99a209635b
1 changed files with 48 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import NimQml, Tables, strutils, stew/shims/strformat import NimQml, Tables, strutils, stew/shims/strformat, std/sequtils
import ./item import ./item
import ../../../shared_models/currency_amount import ../../../shared_models/currency_amount
@ -75,11 +75,55 @@ QtObject:
ModelRole.CanSend.int: "canSend" ModelRole.CanSend.int: "canSend"
}.toTable }.toTable
proc removeItemWithIndex(self: Model, index: int) =
if (index < 0 or index >= self.items.len):
return
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
self.beginRemoveRows(parentModelIndex, index, index)
self.items.delete(index)
self.endRemoveRows()
proc insertItem(self: Model, item: Item, index: int) =
if (index < 0 or index > self.items.len):
return
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
self.beginInsertRows(parentModelIndex, index, index)
self.items.insert(item, index)
self.endInsertRows()
proc findAccountIndex(self: Model, account: Item): int =
for i in 0 ..< self.items.len:
if self.items[i].address() == account.address():
return i
return -1
proc setItems*(self: Model, items: seq[Item]) = proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel() var indexesToRemove: seq[int]
self.items = items
self.endResetModel() #remove
for i in 0 ..< self.items.len:
if not items.anyIt(it.address() == self.items[i].address()):
indexesToRemove.add(i)
while indexesToRemove.len > 0:
let index = pop(indexesToRemove)
self.removeItemWithIndex(index)
# Update or insert
for i in 0 ..< items.len:
var account = items[i]
let index = self.findAccountIndex(account)
if index >= 0:
let qIndex = self.createIndex(i, 0, nil)
defer: qIndex.delete
self.items[index] = account
self.dataChanged(qIndex, qIndex)
continue
self.insertItem(account, i)
self.countChanged() self.countChanged()
for item in items: for item in items: