From 4a4a5d5c4b18f135710ad17632e80cbd199085a2 Mon Sep 17 00:00:00 2001 From: Alex Jbanca Date: Thu, 29 Feb 2024 09:43:38 +0200 Subject: [PATCH] feat(ProfileShowcase): Adding the MovableModel to the ProfileShowcaseModels to intercept the SFPM events This is needed for the DND. --- storybook/pages/ProfileShowcasePanelPage.qml | 16 ++------- .../helpers/ProfileShowcaseDirtyState.qml | 22 ++++++++++--- .../Profile/helpers/ProfileShowcaseModels.qml | 4 +-- .../VisibilityAndPositionDirtyStateModel.qml | 33 ------------------- .../Profile/panels/ProfileShowcasePanel.qml | 6 ++-- .../Profile/views/MyProfileView.qml | 12 +++---- 6 files changed, 30 insertions(+), 63 deletions(-) diff --git a/storybook/pages/ProfileShowcasePanelPage.qml b/storybook/pages/ProfileShowcasePanelPage.qml index 82e4ad5fa7..0d1ee9aa50 100644 --- a/storybook/pages/ProfileShowcasePanelPage.qml +++ b/storybook/pages/ProfileShowcasePanelPage.qml @@ -70,20 +70,8 @@ SplitView { SplitView.fillHeight: true emptyInShowcasePlaceholderText: "No items in showcase" emptyHiddenPlaceholderText: "No hidden items" - onChangePositionRequested: function (key, to) { - for (var i = 0; i < inShowcaseModelItem.count; i++) { - if (inShowcaseModelItem.get(i).key === key) { - inShowcaseModelItem.move(i, to, 1) - break - } - } - - for (var i = 0; i < hiddenModelItem.count; i++) { - if (hiddenModelItem.get(i).key === key) { - hiddenModelItem.move(from, to, 1) - break - } - } + onChangePositionRequested: function (from, to) { + inShowcaseModelItem.move(from, to, 1) } onSetVisibilityRequested: function (key, toVisibility) { for (var i = 0; i < inShowcaseModelItem.count; i++) { diff --git a/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseDirtyState.qml b/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseDirtyState.qml index 750a36322f..7b1536e93e 100644 --- a/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseDirtyState.qml +++ b/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseDirtyState.qml @@ -34,7 +34,7 @@ QObject { /** * Returns dirty state of the showcase model. */ - readonly property alias dirty: writable.dirty + readonly property bool dirty: writable.dirty || !visibleModel.synced || !hiddenModel.synced function revert() { writable.revert() @@ -48,8 +48,8 @@ QObject { writable.setVisibility(key, visibility) } - function changePosition(key, to) { - writable.changePosition(key, to) + function changePosition(from, to) { + visible.move(from, to) } // internals, debug purpose only @@ -81,7 +81,7 @@ QObject { } SortFilterProxyModel { - id: visible + id: visibleSFPM sourceModel: writable delayed: true @@ -90,12 +90,24 @@ QObject { sorters: RoleSorter { roleName: "position" } } + MovableModel { + id: visible + + sourceModel: visibleSFPM + } + SortFilterProxyModel { - id: hidden + id: hiddenSFPM sourceModel: writable delayed: true filters: HiddenFilter {} } + + MovableModel { + id: hidden + + sourceModel: hiddenSFPM + } } diff --git a/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseModels.qml b/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseModels.qml index efec54a4a4..723480e68a 100644 --- a/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseModels.qml +++ b/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseModels.qml @@ -91,8 +91,8 @@ QObject { collectibles.setVisibility(key, visibility) } - function changeCollectiblePosition(key, to) { - collectibles.changePosition(key, to) + function changeCollectiblePosition(from, to) { + collectibles.changePosition(from, to) } ProfileShowcaseModelAdapter { diff --git a/ui/app/AppLayouts/Profile/helpers/VisibilityAndPositionDirtyStateModel.qml b/ui/app/AppLayouts/Profile/helpers/VisibilityAndPositionDirtyStateModel.qml index 14b879caa3..086174e00d 100644 --- a/ui/app/AppLayouts/Profile/helpers/VisibilityAndPositionDirtyStateModel.qml +++ b/ui/app/AppLayouts/Profile/helpers/VisibilityAndPositionDirtyStateModel.qml @@ -61,39 +61,6 @@ WritableProxyModel { set(sourceIdx, { visibility, position }) } - /* Sets the position of the item. The "to" parameter is expected to be - * a target index in the list and must be in range [0; count - 1]. - */ - function changePosition(key, to) { - const visible = d.getVisibleEntries() - visible.sort((a, b) => a.position - b.position) - - const idx = visible.findIndex(item => item.key === key) - - if (idx === -1) { - console.warn(`Entry with key ${key} not found`) - return - } - - const count = visible.length - - if (to < 0 || to >= count) { - console.warn(`Destination position out of range: ${to}`) - return - } - - // swap - [visible[idx], visible[to]] = [visible[to], visible[idx]] - - visible.forEach((e, i) => { - if (e.position === i) - return - - const idx = d.indexByKey(e.key) - set(idx, { position: i }) - }) - } - readonly property QtObject d_: QtObject { id: d diff --git a/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml b/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml index f05515513f..d721acf9fe 100644 --- a/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml +++ b/ui/app/AppLayouts/Profile/panels/ProfileShowcasePanel.qml @@ -31,7 +31,7 @@ DoubleFlickableWithFolding { property string emptyHiddenPlaceholderText // Signal to requst position change of the visible items - signal changePositionRequested(var key, int to) + signal changePositionRequested(int from, int to) // Signal to request visibility change of the items signal setVisibilityRequested(var key, int toVisibility) @@ -46,7 +46,7 @@ DoubleFlickableWithFolding { Drag.keys: dragKeys - dragParent: root + dragParent: dragParentData visualIndex: visualIndexData dragAxis: Drag.YAxis showcaseVisibility: model ? model.visibility ?? Constants.ShowcaseVisibility.NoOne : @@ -322,7 +322,7 @@ DoubleFlickableWithFolding { var to = visualIndex if (to === from) return - root.changePositionRequested(drag.source.key, to) + root.changePositionRequested(drag.source.visualIndex, to) } drag.accept() } diff --git a/ui/app/AppLayouts/Profile/views/MyProfileView.qml b/ui/app/AppLayouts/Profile/views/MyProfileView.qml index c5e29fa0b3..067f6ba26a 100644 --- a/ui/app/AppLayouts/Profile/views/MyProfileView.qml +++ b/ui/app/AppLayouts/Profile/views/MyProfileView.qml @@ -204,8 +204,8 @@ SettingsContentBase { inShowcaseModel: priv.showcaseModels.communitiesVisibleModel hiddenModel: priv.showcaseModels.communitiesHiddenModel - onChangePositionRequested: function (key, to) { - priv.showcaseModels.changeCommunityPosition(key, to) + onChangePositionRequested: function (from, to) { + priv.showcaseModels.changeCommunityPosition(from, to) } onSetVisibilityRequested: function (key, toVisibility) { priv.showcaseModels.setCommunityVisibility(key, toVisibility) @@ -219,8 +219,8 @@ SettingsContentBase { hiddenModel: priv.showcaseModels.accountsHiddenModel currentWallet: root.walletStore.overview.mixedcaseAddress - onChangePositionRequested: function (key, to) { - priv.showcaseModels.changeAccountPosition(key, to) + onChangePositionRequested: function (from, to) { + priv.showcaseModels.changeAccountPosition(from, to) } onSetVisibilityRequested: function (key, toVisibility) { @@ -238,8 +238,8 @@ SettingsContentBase { inShowcaseModel: priv.showcaseModels.collectiblesVisibleModel hiddenModel: priv.showcaseModels.collectiblesHiddenModel - onChangePositionRequested: function (key, to) { - priv.showcaseModels.changeCollectiblePosition(key, to) + onChangePositionRequested: function (from, to) { + priv.showcaseModels.changeCollectiblePosition(from, to) } onSetVisibilityRequested: function (key, toVisibility) {