From 9b85a302fc6648341ae58cfd8d3ac9074b8f3f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Tue, 6 Feb 2024 09:16:01 +0100 Subject: [PATCH] fix(showcase): unbreak drag and drop reordering - unify the signature of the method to `move(from, to, count)` so that both ListModel and NIM have it the same - realize the move operation using the proper `begin/endMoveRows` instead of doing a full model reset - simplify signaling `dataChanged()` for all model roles (nimqml now follows the C++ impl) Fixes #13329 --- .../profile_preferences_accounts_model.nim | 32 ++++++++------- .../profile_preferences_assets_model.nim | 34 +++++++-------- ...profile_preferences_collectibles_model.nim | 41 +++++++++---------- .../profile_preferences_communities_model.nim | 36 ++++++++-------- .../Profile/panels/ProfileShowcasePanel.qml | 2 +- 5 files changed, 71 insertions(+), 74 deletions(-) diff --git a/src/app/modules/main/profile_section/profile/models/profile_preferences_accounts_model.nim b/src/app/modules/main/profile_section/profile/models/profile_preferences_accounts_model.nim index fa2e20398c..b02f598f12 100644 --- a/src/app/modules/main/profile_section/profile/models/profile_preferences_accounts_model.nim +++ b/src/app/modules/main/profile_section/profile/models/profile_preferences_accounts_model.nim @@ -113,14 +113,7 @@ QtObject: let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.ShowcaseVisibility.int, - ModelRole.Order.int, - ModelRole.Address.int, - ModelRole.Name.int, - ModelRole.Emoji.int, - ModelRole.ColorId.int, - ]) + self.dataChanged(index, index) proc upsertItemJson(self: ProfileShowcaseAccountsModel, itemJson: string) {.slot.} = self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseAccountItem()) @@ -165,16 +158,25 @@ QtObject: if ind != -1: self.remove(ind) - proc move*(self: ProfileShowcaseAccountsModel, fromIndex: int, toIndex: int) {.slot.} = - if fromIndex < 0 or fromIndex >= self.items.len: + proc move*(self: ProfileShowcaseAccountsModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} = + if fromRow < 0 or fromRow >= self.items.len: return - self.beginResetModel() - let item = self.items[fromIndex] - self.items.delete(fromIndex) - self.items.insert(@[item], toIndex) + let sourceIndex = newQModelIndex() + defer: sourceIndex.delete + let destIndex = newQModelIndex() + defer: destIndex.delete + + var destRow = toRow + if toRow > fromRow: + inc(destRow) + + self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow) + let item = self.items[fromRow] + self.items.delete(fromRow) + self.items.insert(@[item], toRow) self.recalcOrder() - self.endResetModel() + self.endMoveRows() proc setVisibilityByIndex*(self: ProfileShowcaseAccountsModel, ind: int, visibility: int) {.slot.} = if (visibility >= ord(low(ProfileShowcaseVisibility)) and diff --git a/src/app/modules/main/profile_section/profile/models/profile_preferences_assets_model.nim b/src/app/modules/main/profile_section/profile/models/profile_preferences_assets_model.nim index f7c202ee5c..c1f12355b2 100644 --- a/src/app/modules/main/profile_section/profile/models/profile_preferences_assets_model.nim +++ b/src/app/modules/main/profile_section/profile/models/profile_preferences_assets_model.nim @@ -121,16 +121,7 @@ QtObject: let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.ShowcaseVisibility.int, - ModelRole.Order.int, - ModelRole.Address.int, - ModelRole.CommunityId.int, - ModelRole.Symbol.int, - ModelRole.Name.int, - ModelRole.EnabledNetworkBalance.int, - ModelRole.Decimals.int, - ]) + self.dataChanged(index, index) proc upsertItemJson(self: ProfileShowcaseAssetsModel, itemJson: string) {.slot.} = self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseAssetItem()) @@ -175,16 +166,25 @@ QtObject: if ind != -1: self.remove(ind) - proc move*(self: ProfileShowcaseAssetsModel, fromIndex: int, toIndex: int) {.slot.} = - if fromIndex < 0 or fromIndex >= self.items.len: + proc move*(self: ProfileShowcaseAssetsModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} = + if fromRow < 0 or fromRow >= self.items.len: return - self.beginResetModel() - let item = self.items[fromIndex] - self.items.delete(fromIndex) - self.items.insert(@[item], toIndex) + let sourceIndex = newQModelIndex() + defer: sourceIndex.delete + let destIndex = newQModelIndex() + defer: destIndex.delete + + var destRow = toRow + if toRow > fromRow: + inc(destRow) + + self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow) + let item = self.items[fromRow] + self.items.delete(fromRow) + self.items.insert(@[item], toRow) self.recalcOrder() - self.endResetModel() + self.endMoveRows() proc setVisibilityByIndex*(self: ProfileShowcaseAssetsModel, ind: int, visibility: int) {.slot.} = if (visibility >= ord(low(ProfileShowcaseVisibility)) and diff --git a/src/app/modules/main/profile_section/profile/models/profile_preferences_collectibles_model.nim b/src/app/modules/main/profile_section/profile/models/profile_preferences_collectibles_model.nim index cf89537305..713336b19a 100644 --- a/src/app/modules/main/profile_section/profile/models/profile_preferences_collectibles_model.nim +++ b/src/app/modules/main/profile_section/profile/models/profile_preferences_collectibles_model.nim @@ -62,7 +62,7 @@ QtObject: ModelRole.ImageUrl.int: "imageUrl", ModelRole.BackgroundColor.int: "backgroundColor", ModelRole.CollectionName.int: "collectionName", - ModelRole.IsLoading.int:"isLoading", + ModelRole.IsLoading.int: "isLoading", ModelRole.CommunityId.int: "communityId", ModelRole.ShowcaseVisibility.int: "showcaseVisibility", @@ -138,19 +138,7 @@ QtObject: let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.ShowcaseVisibility.int, - ModelRole.Order.int, - ModelRole.ChainId.int, - ModelRole.TokenId.int, - ModelRole.ContractAddress.int, - ModelRole.CommunityId.int, - ModelRole.Name.int, - ModelRole.CollectionName.int, - ModelRole.ImageUrl.int, - ModelRole.BackgroundColor.int, - ModelRole.IsLoading.int - ]) + self.dataChanged(index, index) proc upsertItemJson(self: ProfileShowcaseCollectiblesModel, itemJson: string) {.slot.} = self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCollectibleItem()) @@ -195,16 +183,25 @@ QtObject: if ind != -1: self.remove(ind) - proc move*(self: ProfileShowcaseCollectiblesModel, fromIndex: int, toIndex: int) {.slot.} = - if fromIndex < 0 or fromIndex >= self.items.len: + proc move*(self: ProfileShowcaseCollectiblesModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} = + if fromRow < 0 or fromRow >= self.items.len: return - self.beginResetModel() - let item = self.items[fromIndex] - self.items.delete(fromIndex) - self.items.insert(@[item], toIndex) + let sourceIndex = newQModelIndex() + defer: sourceIndex.delete + let destIndex = newQModelIndex() + defer: destIndex.delete + + var destRow = toRow + if toRow > fromRow: + inc(destRow) + + self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow) + let item = self.items[fromRow] + self.items.delete(fromRow) + self.items.insert(@[item], toRow) self.recalcOrder() - self.endResetModel() + self.endMoveRows() proc setVisibilityByIndex*(self: ProfileShowcaseCollectiblesModel, ind: int, visibility: int) {.slot.} = if (visibility >= ord(low(ProfileShowcaseVisibility)) and @@ -219,4 +216,4 @@ QtObject: proc setVisibility*(self: ProfileShowcaseCollectiblesModel, uid: string, visibility: int) {.slot.} = let index = self.findIndexForCollectible(uid) if index != -1: - self.setVisibilityByIndex(index, visibility) \ No newline at end of file + self.setVisibilityByIndex(index, visibility) diff --git a/src/app/modules/main/profile_section/profile/models/profile_preferences_communities_model.nim b/src/app/modules/main/profile_section/profile/models/profile_preferences_communities_model.nim index c89d90f451..a0f90776f8 100644 --- a/src/app/modules/main/profile_section/profile/models/profile_preferences_communities_model.nim +++ b/src/app/modules/main/profile_section/profile/models/profile_preferences_communities_model.nim @@ -128,18 +128,7 @@ QtObject: let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.ShowcaseVisibility.int, - ModelRole.Order.int, - ModelRole.Id.int, - ModelRole.Name.int, - ModelRole.MemberRole.int, - ModelRole.Image.int, - ModelRole.Color.int, - ModelRole.Description.int, - ModelRole.MembersCount.int, - ModelRole.Loading.int, - ]) + self.dataChanged(index, index) proc upsertItemJson(self: ProfileShowcaseCommunitiesModel, itemJson: string) {.slot.} = self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCommunityItem()) @@ -184,16 +173,25 @@ QtObject: if ind != -1: self.remove(ind) - proc move*(self: ProfileShowcaseCommunitiesModel, fromIndex: int, toIndex: int) {.slot.} = - if fromIndex < 0 or fromIndex >= self.items.len: + proc move*(self: ProfileShowcaseCommunitiesModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} = + if fromRow < 0 or fromRow >= self.items.len: return - self.beginResetModel() - let item = self.items[fromIndex] - self.items.delete(fromIndex) - self.items.insert(@[item], toIndex) + let sourceIndex = newQModelIndex() + defer: sourceIndex.delete + let destIndex = newQModelIndex() + defer: destIndex.delete + + var destRow = toRow + if toRow > fromRow: + inc(destRow) + + self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow) + let item = self.items[fromRow] + self.items.delete(fromRow) + self.items.insert(@[item], toRow) self.recalcOrder() - self.endResetModel() + self.endMoveRows() proc setVisibilityByIndex*(self: ProfileShowcaseCommunitiesModel, ind: int, visibility: int) {.slot.} = if (visibility >= ord(low(ProfileShowcaseVisibility)) and diff --git a/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml b/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml index 8a4fe9d6fd..43e1fddbd5 100644 --- a/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml +++ b/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml @@ -172,7 +172,7 @@ Control { if (to === from) return root.showcaseEntryChanged() - showcaseModel.move(from, to) + showcaseModel.move(from, to, 1) drag.accept() }