From 7c64a8524422c11a8842a3ef96fd17e4e6df439e Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Thu, 1 Feb 2024 03:36:46 +0100 Subject: [PATCH] allow users cancel dex active orders/expired bonds password modal Signed-off-by: Philemon Ukane --- ui/page/root/home_page.go | 151 +++++++++++++++++++----------------- ui/values/localizable/en.go | 1 + ui/values/strings.go | 1 + 3 files changed, 82 insertions(+), 71 deletions(-) diff --git a/ui/page/root/home_page.go b/ui/page/root/home_page.go index 6719d8bfb..4fc9e6db1 100644 --- a/ui/page/root/home_page.go +++ b/ui/page/root/home_page.go @@ -220,6 +220,80 @@ func (hp *HomePage) initDEX() { dexPassEditor := hp.Theme.EditorPassword(new(widget.Editor), values.String(values.StrDexPassword)) dexPassEditor.Editor.SingleLine, dexPassEditor.IsRequired = true, true + showWalletToSyncModal := func() bool { + // DEX client has active orders or expired bonds, retrieve the + // wallets involved and ensure they are synced or syncing. + walletsToSyncMap := make(map[uint32]*struct{}) + for _, orders := range activeOrders { + for _, ord := range orders { + walletsToSyncMap[ord.BaseID] = &struct{}{} + walletsToSyncMap[ord.QuoteID] = &struct{}{} + } + } + + for _, bond := range expiredBonds { + walletsToSyncMap[bond.AssetID] = &struct{}{} + } + + var walletsToSyncStr string + var walletsToSync []sharedW.Asset + for assetID := range walletsToSyncMap { + walletID, err := dexClient.WalletIDForAsset(assetID) + if err != nil { + log.Errorf("dexClient.WalletIDForAsset(%d) error: %w", assetID, err) + continue + } + + if walletID == nil { + continue // impossible but better safe than sorry + } + + wallet := hp.AssetsManager.WalletWithID(*walletID) + if wallet == nil { // impossible but better safe than sorry + log.Error("dex wallet with ID %d is missing", walletID) + continue + } + + if wallet.IsSynced() || wallet.IsSyncing() { + continue // ok + } + + walletsToSync = append(walletsToSync, wallet) + walletsToSyncStr += "," + fmt.Sprintf("%s (%s)", wallet.GetWalletName(), wallet.GetAssetType()) + } + + if len(walletsToSync) == 0 { + return true + } + + walletSyncRequestModal := modal.NewCustomModal(hp.Load). + Title(values.String(values.StrWalletsNeedToSync)). + Body(values.StringF(values.StrWalletsNeedToSyncMsg, strings.Trim(walletsToSyncStr, ","))). + SetNegativeButtonText(values.String(values.StrIWillSyncLater)). + SetPositiveButtonText(values.String(values.StrOkaySync)). + SetPositiveButtonCallback(func(isChecked bool, im *modal.InfoModal) bool { + if !hp.isConnected.Load() { + // Notify user and return. + hp.Toast.NotifyError(values.String(values.StrNotConnected)) + return false + } + + for _, w := range walletsToSync { + err := w.SpvSync() + if err != nil { + log.Error(err) + } else { + w.SaveUserConfigValue(sharedW.AutoSyncConfigKey, true) + } + } + + hp.ParentWindow().Reload() + return true + }).SetCancelable(false) + hp.ParentWindow().ShowModal(walletSyncRequestModal) + return true + } + loginModal := modal.NewCustomModal(hp.Load). UseCustomWidget(func(gtx C) D { return layout.Flex{Axis: layout.Vertical}.Layout(gtx, @@ -231,6 +305,11 @@ func (hp *HomePage) initDEX() { }), ) }). + SetNegativeButtonText(values.String(values.StrLoginLater)). + NegativeButtonStyle(hp.Theme.Color.OrangeRipple, hp.Theme.Color.Surface). + SetNegativeButtonCallback(func() { + showWalletToSyncModal() + }). SetPositiveButtonText(values.String(values.StrLogin)). SetPositiveButtonCallback(func(isChecked bool, im *modal.InfoModal) bool { dexPassEditor.SetError("") @@ -240,77 +319,7 @@ func (hp *HomePage) initDEX() { return false } - // DEX client has active orders or expired bonds, retrieve the - // wallets involved and ensure they are synced or syncing. - walletsToSyncMap := make(map[uint32]*struct{}) - for _, orders := range activeOrders { - for _, ord := range orders { - walletsToSyncMap[ord.BaseID] = &struct{}{} - walletsToSyncMap[ord.QuoteID] = &struct{}{} - } - } - - for _, bond := range expiredBonds { - walletsToSyncMap[bond.AssetID] = &struct{}{} - } - - var walletsToSyncStr string - var walletsToSync []sharedW.Asset - for assetID := range walletsToSyncMap { - walletID, err := dexClient.WalletIDForAsset(assetID) - if err != nil { - log.Errorf("dexClient.WalletIDForAsset(%d) error: %w", assetID, err) - continue - } - - if walletID == nil { - continue // impossible but better safe than sorry - } - - wallet := hp.AssetsManager.WalletWithID(*walletID) - if wallet == nil { // impossible but better safe than sorry - log.Error("dex wallet with ID %d is missing", walletID) - continue - } - - if wallet.IsSynced() || wallet.IsSyncing() { - continue // ok - } - - walletsToSync = append(walletsToSync, wallet) - walletsToSyncStr += "," + fmt.Sprintf("%s (%s)", wallet.GetWalletName(), wallet.GetAssetType()) - } - - if len(walletsToSync) == 0 { - return true - } - - walletSyncRequestModal := modal.NewCustomModal(hp.Load). - Title(values.String(values.StrWalletsNeedToSync)). - Body(values.StringF(values.StrWalletsNeedToSyncMsg, strings.Trim(walletsToSyncStr, ","))). - SetNegativeButtonText(values.String(values.StrIWillSyncLater)). - SetPositiveButtonText(values.String(values.StrOkaySync)). - SetPositiveButtonCallback(func(isChecked bool, im *modal.InfoModal) bool { - if !hp.isConnected.Load() { - // Notify user and return. - hp.Toast.NotifyError(values.String(values.StrNotConnected)) - return false - } - - for _, w := range walletsToSync { - err := w.SpvSync() - if err != nil { - log.Error(err) - } else { - w.SaveUserConfigValue(sharedW.AutoSyncConfigKey, true) - } - } - - hp.ParentWindow().Reload() - return true - }) - hp.ParentWindow().ShowModal(walletSyncRequestModal) - return true + return showWalletToSyncModal() }). SetCancelable(false) hp.ParentWindow().ShowModal(loginModal) diff --git a/ui/values/localizable/en.go b/ui/values/localizable/en.go index 0561deb94..8be83d7ae 100644 --- a/ui/values/localizable/en.go +++ b/ui/values/localizable/en.go @@ -930,4 +930,5 @@ const EN = ` "backupDEXSeed" = "Backup DEX Seed" "dexSeed" = "DEX Seed" "optionalRestorationSeed" = "Restoration Seed (optional)" +"loginLater" = "I'll login later" ` diff --git a/ui/values/strings.go b/ui/values/strings.go index 9b90c53ac..df675c534 100644 --- a/ui/values/strings.go +++ b/ui/values/strings.go @@ -1038,6 +1038,7 @@ const ( StrBackupDEXSeed = "backupDEXSeed" StrDEXSeed = "dexSeed" StrOptionalRestorationSeed = "optionalRestorationSeed" + StrLoginLater = "loginLater" StrLoginDEXForActiveOrdersOrExpiredBonds = "loginDEXForActiveOrdersOrExpiredBonds" )