Skip to content

Commit

Permalink
feat(@desktop/settings): Integrate token advanced settings with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuteivist committed Feb 19, 2024
1 parent 01095e0 commit 9068cb7
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type
tokenService: token_service.Service
walletAccountService: wallet_account_service.Service
settingsService: settings_service.Service
displayAssetsBelowBalanceThreshold: CurrencyAmount

proc newController*(
delegate: io_interface.AccessInterface,
Expand Down Expand Up @@ -113,7 +114,8 @@ proc toggleDisplayAssetsBelowBalance*(self: Controller): bool =

proc getDisplayAssetsBelowBalanceThreshold*(self: Controller): CurrencyAmount =
let amount = float64(self.settingsService.displayAssetsBelowBalanceThreshold())
return newCurrencyAmount(amount, self.tokenService.getCurrency(), 9, true)
self.displayAssetsBelowBalanceThreshold = newCurrencyAmount(amount, self.tokenService.getCurrency(), 9, true)
return self.displayAssetsBelowBalanceThreshold

proc setDisplayAssetsBelowBalanceThreshold*(self: Controller, threshold: int64): bool =
return self.settingsService.setDisplayAssetsBelowBalanceThreshold(threshold)
4 changes: 2 additions & 2 deletions src/app/modules/main/wallet_section/all_tokens/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ QtObject:
proc getDisplayAssetsBelowBalanceThreshold(self: View): QVariant {.slot.} =
return newQVariant(self.delegate.getDisplayAssetsBelowBalanceThreshold())

proc setDisplayAssetsBelowBalanceThreshold(self: View, threshold: QVariant) {.slot.} =
if not self.delegate.setDisplayAssetsBelowBalanceThreshold(threshold.int64Val()):
proc setDisplayAssetsBelowBalanceThreshold(self: View, threshold: int) {.slot.} =
if not self.delegate.setDisplayAssetsBelowBalanceThreshold(int64(threshold)):
error "Failed to set displayAssetsBelowBalanceThreshold"
return
self.displayAssetsBelowBalanceThresholdChanged()
Expand Down
18 changes: 12 additions & 6 deletions ui/app/AppLayouts/Profile/views/WalletView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,24 @@ SettingsContentBase {
}

dirty: manageTokensView.dirty
ignoreDirty: stackContainer.currentIndex === manageTokensViewIndex
ignoreDirty: stackContainer.currentIndex === manageTokensViewIndex && !manageTokensView.advancedTabVisible

saveChangesButtonEnabled: dirty
toast.type: SettingsDirtyToastMessage.Type.Info
toast.cancelButtonVisible: false
toast.saveForLaterButtonVisible: dirty
toast.saveChangesText: qsTr("Apply to my Wallet")
toast.changesDetectedText: qsTr("New custom sort order created")
toast.cancelButtonVisible: manageTokensView.advancedTabVisible
toast.saveForLaterButtonVisible: !manageTokensView.advancedTabVisible
toast.saveChangesText: manageTokensView.advancedTabVisible ? toast.defaultSaveChangesText : qsTr("Apply to my Wallet")
toast.changesDetectedText: manageTokensView.advancedTabVisible ? toast.defaultChangesDetectedText : qsTr("New custom sort order created")

onSaveForLaterClicked: {
manageTokensView.saveChanges()
}
onSaveChangesClicked: {
manageTokensView.saveChanges()
if (manageTokensView.advancedTabVisible) {
// Save changes only when tab is active
manageTokensView.saveChanges()
return
}

let sectionLink = "%1/%2/".arg(Constants.appSection.wallet).arg(WalletLayout.LeftPanelSelection.AllAddresses)

Expand Down Expand Up @@ -313,6 +318,7 @@ SettingsContentBase {
implicitHeight: root.availableHeight
Layout.fillWidth: true

tokensStore: root.tokensStore
tokenListUpdatedAt: tokensStore.tokenListUpdatedAt
assetsController: root.assetsStore.assetsController
collectiblesController: root.collectiblesStore.collectiblesController
Expand Down
76 changes: 59 additions & 17 deletions ui/app/AppLayouts/Profile/views/wallet/ManageTokensView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import utils 1.0

import AppLayouts.Profile.panels 1.0
import AppLayouts.Wallet.panels 1.0
import AppLayouts.Wallet.stores 1.0


Item {
id: root

required property TokensStore tokensStore

required property double tokenListUpdatedAt
required property var assetsController
required property var collectiblesController
Expand All @@ -35,22 +39,17 @@ Item {

property alias currentIndex: tabBar.currentIndex

readonly property bool dirty: {
if (!loader.item)
return false
if (tabBar.currentIndex > d.hiddenTabIndex)
return false
// FIXME take advanced settings into account here too (#13178)
return loader.item && loader.item.dirty
}
readonly property bool dirty: !!loader.item && loader.item.dirty
readonly property bool advancedTabVisible: tabBar.currentIndex === d.advancedTabIndex

function saveChanges() {
if (tabBar.currentIndex > d.hiddenTabIndex)
return
// FIXME save advanced settings (#13178)
loader.item.saveSettings()
}

function resetChanges() {
loader.item.resetChanges()
}

QtObject {
id: d

Expand Down Expand Up @@ -147,7 +146,34 @@ Item {
Component {
id: advancedTab
ColumnLayout {
id: advancedTabColumn
id: advancedSettings

function saveSettings() {
if (showCommunityAssetsSwitch.checked !== root.tokensStore.showCommunityAssetsInSend)
root.tokensStore.toggleShowCommunityAssetsInSend()
if (displayThresholdSwitch.checked !== root.tokensStore.displayAssetsBelowBalance)
root.tokensStore.toggleDisplayAssetsBelowBalance()
const rawAmount = currencyAmount.value * Math.pow(10, thresholdCurrency.displayDecimals)
if (rawAmount !== thresholdCurrency.amount) {
root.tokensStore.setDisplayAssetsBelowBalanceThreshold(rawAmount)
}
dirty = false
}

function resetChanges() {
showCommunityAssetsSwitch.checked = root.tokensStore.showCommunityAssetsInSend
displayThresholdSwitch.checked = root.tokensStore.displayAssetsBelowBalance
currencyAmount.value = getDisplayThresholdAmount()
dirty = false
}

function getDisplayThresholdAmount() {
return thresholdCurrency.amount / Math.pow(10, thresholdCurrency.displayDecimals)
}

property bool dirty: false

property var thresholdCurrency: root.tokensStore.getDisplayAssetsBelowBalanceThresholdCurrency()

spacing: 8
StatusListItem {
Expand All @@ -157,9 +183,13 @@ Item {
components: [
StatusSwitch {
id: showCommunityAssetsSwitch
checked: true // FIXME integrate with backend (#13178)
checked: root.tokensStore.showCommunityAssetsInSend
onCheckedChanged: {
// FIXME integrate with backend (#13178)
if (!advancedSettings.dirty && checked === root.tokensStore.showCommunityAssetsInSend) {
// Skipping initial value
return
}
advancedSettings.dirty = true
}
}
]
Expand All @@ -176,15 +206,27 @@ Item {

components: [
CurrencyAmountInput {
id: currencyAmount
enabled: displayThresholdSwitch.checked
currencySymbol: SharedStores.RootStore.currencyStore.currentCurrency
value: 0.10 // FIXME integrate with backend (#13178)
value: advancedSettings.getDisplayThresholdAmount()
onValueChanged: {
if (!advancedSettings.dirty && advancedSettings.getDisplayThresholdAmount() === value) {
// Skipping initial value
return
}
advancedSettings.dirty = true
}
},
StatusSwitch {
id: displayThresholdSwitch
checked: false // FIXME integrate with backend (#13178)
checked: root.tokensStore.displayAssetsBelowBalance
onCheckedChanged: {
// FIXME integrate with backend (#13178)
if (!advancedSettings.dirty && checked === root.tokensStore.displayAssetsBelowBalance) {
// Skipping initial value
return
}
advancedSettings.dirty = true
}
}
]
Expand Down
35 changes: 33 additions & 2 deletions ui/app/AppLayouts/Wallet/stores/TokensStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ QtObject {

// Property and methods below are used to apply advanced token management settings to the SendModal
readonly property bool showCommunityAssetsInSend: root._allTokensModule.showCommunityAssetWhenSendingTokens
readonly property bool balanceThresholdEnabled: root._allTokensModule.displayAssetsBelowBalance
readonly property real balanceThresholdAmount: root._allTokensModule.displayAssetsBelowBalanceThreshold
readonly property bool displayAssetsBelowBalance: root._allTokensModule.displayAssetsBelowBalance

signal displayAssetsBelowBalanceThresholdChanged()

function getDisplayAssetsBelowBalanceThresholdCurrency() {
return root._allTokensModule.displayAssetsBelowBalanceThreshold
}

function getDisplayAssetsBelowBalanceThresholdDisplayAmount() {
const thresholdCurrency = getDisplayAssetsBelowBalanceThresholdCurrency()
return thresholdCurrency.amount / Math.pow(10, thresholdCurrency.displayDecimals)
}

function setDisplayAssetsBelowBalanceThreshold(rawValue) {
// rawValue - raw amount (multiplied by displayDecimals)`
root._allTokensModule.setDisplayAssetsBelowBalanceThreshold(rawValue)
}

function toggleShowCommunityAssetsInSend() {
root._allTokensModule.toggleShowCommunityAssetWhenSendingTokens()
}

function toggleDisplayAssetsBelowBalance() {
root._allTokensModule.toggleDisplayAssetsBelowBalance()
}

readonly property Connections allTokensConnections: Connections {
target: root._allTokensModule

function onDisplayAssetsBelowBalanceThresholdChanged() {
root.displayAssetsBelowBalanceThresholdChanged()
}
}
}
13 changes: 9 additions & 4 deletions ui/imports/shared/popups/SettingsDirtyToastMessage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Rectangle {
property alias cancelChangesText: cancelChangesButton.text
property alias changesDetectedText: changesDetectedTextItem.text

readonly property string defaultChangesDetectedText: qsTr("Changes detected")
readonly property string defaultSaveChangesText: qsTr("Save changes")
readonly property string defaultSaveForLaterText: qsTr("Save for later")
readonly property string defaultCancelChangesText: qsTr("Cancel")

property Flickable flickable: null

enum Type {
Expand Down Expand Up @@ -123,12 +128,12 @@ Rectangle {
padding: 8
horizontalAlignment: Text.AlignHCenter
color: Theme.palette.directColor1
text: qsTr("Changes detected")
text: root.defaultChangesDetectedText
}

StatusButton {
id: cancelChangesButton
text: qsTr("Cancel")
text: root.defaultCancelChangesText
enabled: root.active
visible: root.cancelButtonVisible
type: StatusBaseButton.Type.Danger
Expand All @@ -137,7 +142,7 @@ Rectangle {

StatusFlatButton {
id: saveForLaterButton
text: qsTr("Save for later")
text: root.defaultSaveForLaterText
enabled: root.active && root.saveChangesButtonEnabled
visible: root.saveForLaterButtonVisible
onClicked: root.saveForLaterClicked()
Expand All @@ -147,7 +152,7 @@ Rectangle {
id: saveChangesButton
objectName: "settingsDirtyToastMessageSaveButton"
buttonType: DisabledTooltipButton.Normal
text: qsTr("Save changes")
text: root.defaultSaveChangesText
enabled: root.active && root.saveChangesButtonEnabled
interactive: root.active && root.saveChangesButtonEnabled
onClicked: root.saveChangesClicked()
Expand Down
12 changes: 10 additions & 2 deletions ui/imports/shared/stores/send/TransactionStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,16 @@ QtObject {
}
}

readonly property Connections tokensStoreConnections: Connections {
target: tokensStore
function onDisplayAssetsBelowBalanceThresholdChanged() {
processedAssetsModel.displayAssetsBelowBalanceThresholdAmount = tokensStore.getDisplayAssetsBelowBalanceThresholdDisplayAmount()
}
}

// Model prepared to provide filtered and sorted assets as per the advanced Settings in token management
property var processedAssetsModel: SortFilterProxyModel {
property real displayAssetsBelowBalanceThresholdAmount: tokensStore.getDisplayAssetsBelowBalanceThresholdDisplayAmount()
sourceModel: __assetsWithFilteredBalances
proxyRoles: [
FastExpressionRole {
Expand Down Expand Up @@ -317,10 +325,10 @@ QtObject {
expression: {
if (model.isCommunityAsset)
return true
return model.currentCurrencyBalance > tokensStore.balanceThresholdAmount
return model.currentCurrencyBalance > processedAssetsModel.displayAssetsBelowBalanceThresholdAmount
}
expectedRoles: ["isCommunityAsset", "currentCurrencyBalance"]
enabled: tokensStore.balanceThresholdEnabled
enabled: tokensStore.displayAssetsBelowBalance
}
]
sorters: RoleSorter {
Expand Down

0 comments on commit 9068cb7

Please sign in to comment.