Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: Fix bugs and enable wallet creation on the create order page #196

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,27 @@ func (mgr *AssetsManager) WalletWithID(walletID int) sharedW.Asset {
return nil
}

// AssetWallets returns the wallets for the specified asset type(s).
func (mgr *AssetsManager) AssetWallets(assetTypes ...utils.AssetType) []sharedW.Asset {
var wallets []sharedW.Asset
for _, asset := range assetTypes {
switch asset {
case utils.BTCWalletAsset:
wallets = append(wallets, mgr.AllBTCWallets()...)
case utils.DCRWalletAsset:
wallets = append(wallets, mgr.AllDCRWallets()...)
case utils.LTCWalletAsset:
wallets = append(wallets, mgr.AllLTCWallets()...)
}
}

if len(wallets) == 0 && len(assetTypes) == 0 {
wallets = mgr.AllWallets()
}

return wallets
}

func (mgr *AssetsManager) getbadWallet(walletID int) *sharedW.Wallet {
if badWallet, ok := mgr.Assets.BTC.BadWallets[walletID]; ok {
return badWallet
Expand Down
18 changes: 8 additions & 10 deletions ui/page/components/asset_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const AssetTypeSelectorID = "AssetTypeSelectorID"
type AssetTypeSelector struct {
openSelectorDialog *cryptomaterial.Clickable
*assetTypeModal
changed bool

hint string
isDisableBorder bool
Expand All @@ -40,7 +39,7 @@ type assetTypeModal struct {
*cryptomaterial.Modal

selectedAssetType *AssetTypeItem
assetTypeCallback func(*AssetTypeItem)
assetTypeCallback func(*AssetTypeItem) bool
dialogTitle string
onAssetTypeClicked func(*AssetTypeItem)
assetTypeList layout.List
Expand All @@ -58,14 +57,13 @@ func NewAssetTypeSelector(l *load.Load) *AssetTypeSelector {

ats.assetTypeModal = newAssetTypeModal(l).
assetTypeClicked(func(assetType *AssetTypeItem) {
if ats.selectedAssetType != nil {
if ats.selectedAssetType.Type.String() != assetType.Type.String() {
ats.changed = true
}
}
ats.selectedAssetType = assetType
ok := true
if ats.assetTypeCallback != nil {
ats.assetTypeCallback(assetType)
ok = ats.assetTypeCallback(assetType)
}

if ok && (ats.selectedAssetType == nil || ats.selectedAssetType.Type.String() != assetType.Type.String()) {
ats.selectedAssetType = assetType
}
})
ats.assetTypeItems = ats.buildExchangeItems()
Expand Down Expand Up @@ -142,7 +140,7 @@ func (ats *AssetTypeSelector) Title(title string) *AssetTypeSelector {
}

// AssetTypeSelected sets the callback executed when an asset type is selected.
func (ats *AssetTypeSelector) AssetTypeSelected(callback func(*AssetTypeItem)) *AssetTypeSelector {
func (ats *AssetTypeSelector) AssetTypeSelected(callback func(*AssetTypeItem) bool) *AssetTypeSelector {
ats.assetTypeCallback = callback
return ats
}
Expand Down
4 changes: 4 additions & 0 deletions ui/page/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ func TransactionTitleIcon(l *load.Load, wal sharedW.Asset, tx *sharedW.Transacti
func LayoutTransactionRow(gtx layout.Context, l *load.Load, row TransactionRow, isTxPage bool) layout.Dimensions {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
wal := l.WL.AssetsManager.WalletWithID(row.Transaction.WalletID)
if wal == nil {
return D{}
}

txStatus := TransactionTitleIcon(l, wal, &row.Transaction)
amount := wal.ToAmount(row.Transaction.Amount).String()
assetIcon := CoinImageBySymbol(l, wal.GetAssetType(), wal.IsWatchingOnlyWallet())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package info
package components

import (
"image"
Expand All @@ -17,7 +17,6 @@ import (
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/modal"
"github.com/crypto-power/cryptopower/ui/page/components"
"github.com/crypto-power/cryptopower/ui/values"
)

Expand Down Expand Up @@ -60,7 +59,7 @@ func NewRestorePage(l *load.Load, walletName string, walletType libutils.AssetTy
toggleSeedInput: l.Theme.Switch(),
}

pg.backButton, _ = components.SubpageHeaderButtons(l)
pg.backButton, _ = SubpageHeaderButtons(l)
pg.backButton.Icon = pg.Theme.Icons.ContentClear

pg.seedInputEditor = l.Theme.Editor(new(widget.Editor), values.String(values.StrEnterWalletSeed))
Expand Down Expand Up @@ -96,7 +95,7 @@ func (pg *Restore) Layout(gtx C) D {

func (pg *Restore) layoutDesktop(gtx C) D {
body := func(gtx C) D {
sp := components.SubPage{
sp := SubPage{
Load: pg.Load,
Title: values.String(values.StrRestoreWallet),
BackButton: pg.backButton,
Expand All @@ -109,12 +108,12 @@ func (pg *Restore) layoutDesktop(gtx C) D {
}
return sp.Layout(pg.ParentWindow(), gtx)
}
return components.UniformPadding(gtx, body)
return UniformPadding(gtx, body)
}

func (pg *Restore) layoutMobile(gtx C) D {
body := func(gtx C) D {
sp := components.SubPage{
sp := SubPage{
Load: pg.Load,
Title: values.String(values.StrRestoreWallet),
BackButton: pg.backButton,
Expand All @@ -127,11 +126,11 @@ func (pg *Restore) layoutMobile(gtx C) D {
}
return sp.Layout(pg.ParentWindow(), gtx)
}
return components.UniformMobile(gtx, false, false, body)
return UniformMobile(gtx, false, false, body)
}

func (pg *Restore) restoreLayout(gtx layout.Context) layout.Dimensions {
return components.UniformPadding(gtx, func(gtx C) D {
return UniformPadding(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(pg.tabLayout),
layout.Rigid(pg.Theme.Separator().Layout),
Expand Down Expand Up @@ -353,7 +352,7 @@ func (pg *Restore) restoreFromSeedEditor() {

seedOrHex := strings.TrimSpace(pg.seedInputEditor.Editor.Text())
// Check if the user did input a hex or seed. If its a hex set the correct tabindex.
if len(seedOrHex) > components.MaxSeedBytes {
if len(seedOrHex) > MaxSeedBytes {
pg.tabIndex = 0
} else {
pg.tabIndex = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package info
package components

import (
"fmt"
Expand Down
52 changes: 22 additions & 30 deletions ui/page/components/wallet_account_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,38 +142,44 @@ func (ws *WalletAndAccountSelector) SelectFirstValidAccount(wallet *load.WalletM
return errors.New(values.String(values.StrNoValidAccountFound))
}

func (ws *WalletAndAccountSelector) SetSelectedAsset(assetType ...utils.AssetType) {
ws.assetType = assetType
// SetSelectedAsset sets the specified assetType and returns true if the asset
// has at least one wallet.
func (ws *WalletAndAccountSelector) SetSelectedAsset(assetType ...utils.AssetType) bool {
ws.accountSelector = false
ws.selectorModal.setupWallet(assetType[0])
if len(ws.selectorItems) == 0 {
return false
}

ws.assetType = assetType
ws.selectedWallet = ws.selectorItems[0].item.(*load.WalletMapping)
ws.accountSelector = false
return true
}

func (ws *WalletAndAccountSelector) SelectedAsset() utils.AssetType {
return ws.assetType[0]
}

func (ws *WalletAndAccountSelector) SelectAccount(wallet *load.WalletMapping, accountNumber int32) error {
if !ws.accountSelector {
ws.accountSelector = true
}
ws.accountSelector = true
ws.SetSelectedWallet(wallet)

account, err := wallet.GetAccount(accountNumber)
if err != nil {
return err
}

if ws.accountIsValid(account) {
ws.SetSelectedAccount(account)
if ws.accountCallback != nil {
ws.accountCallback(account)
}
return nil
if !ws.accountIsValid(account) {
ws.ResetAccount()
return errors.New(values.String(values.StrNoValidAccountFound))
}

ws.ResetAccount()
return errors.New(values.String(values.StrNoValidAccountFound))
ws.SetSelectedAccount(account)
if ws.accountCallback != nil {
ws.accountCallback(account)
}

return nil
}

func (ws *WalletAndAccountSelector) ResetAccount() {
Expand Down Expand Up @@ -438,22 +444,7 @@ func (sm *selectorModal) OnResume() {
func (sm *selectorModal) setupWallet(assetType ...utils.AssetType) {
selectorItems := make([]*SelectorItem, 0)

var wallets []sharedW.Asset
for _, asset := range assetType {
switch asset {
case utils.BTCWalletAsset:
wallets = append(wallets, sm.WL.AssetsManager.AllBTCWallets()...)
case utils.DCRWalletAsset:
wallets = append(wallets, sm.WL.AssetsManager.AllDCRWallets()...)
case utils.LTCWalletAsset:
wallets = append(wallets, sm.WL.AssetsManager.AllLTCWallets()...)
}
}

if len(wallets) == 0 {
wallets = sm.WL.AssetsManager.AllWallets()
}

wallets := sm.WL.AssetsManager.AssetWallets(assetType...)
for _, wal := range wallets {
if wal.IsWatchingOnlyWallet() && !sm.isWatchOnlyEnabled {
continue
Expand All @@ -463,6 +454,7 @@ func (sm *selectorModal) setupWallet(assetType ...utils.AssetType) {
clickable: sm.Theme.NewClickable(true),
})
}

sm.selectorItems = selectorItems
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package root
package components

import (
"errors"
Expand All @@ -15,8 +15,6 @@ import (
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/modal"
"github.com/crypto-power/cryptopower/ui/page/components"
"github.com/crypto-power/cryptopower/ui/page/info"
"github.com/crypto-power/cryptopower/ui/utils"
"github.com/crypto-power/cryptopower/ui/values"
)
Expand Down Expand Up @@ -46,7 +44,7 @@ type CreateWallet struct {

walletActions []*walletAction

assetTypeSelector *components.AssetTypeSelector
assetTypeSelector *AssetTypeSelector
assetTypeError cryptomaterial.Label
walletName cryptomaterial.Editor
watchOnlyWalletHex cryptomaterial.Editor
Expand All @@ -62,11 +60,13 @@ type CreateWallet struct {

selectedWalletAction int

walletCreationSuccessCallback func()

showLoader bool
isLoading bool
}

func NewCreateWallet(l *load.Load, assetType ...libutils.AssetType) *CreateWallet {
func NewCreateWallet(l *load.Load, walletCreationSuccessCallback func(), assetType ...libutils.AssetType) *CreateWallet {
pg := &CreateWallet{
GenericPageModal: app.NewGenericPageModal(CreateWalletID),
scrollContainer: &widget.List{
Expand All @@ -75,7 +75,7 @@ func NewCreateWallet(l *load.Load, assetType ...libutils.AssetType) *CreateWalle
Alignment: layout.Middle,
},
},
assetTypeSelector: components.NewAssetTypeSelector(l),
assetTypeSelector: NewAssetTypeSelector(l),
list: layout.List{Axis: layout.Vertical},

continueBtn: l.Theme.Button(values.String(values.StrContinue)),
Expand All @@ -85,7 +85,14 @@ func NewCreateWallet(l *load.Load, assetType ...libutils.AssetType) *CreateWalle
selectedWalletAction: -1,
assetTypeError: l.Theme.Body1(""),

Load: l,
Load: l,
walletCreationSuccessCallback: walletCreationSuccessCallback,
}

if walletCreationSuccessCallback == nil {
pg.walletCreationSuccessCallback = func() {
pg.ParentNavigator().CloseCurrentPage()
}
}

bg := l.Theme.Color.White
Expand Down Expand Up @@ -114,7 +121,7 @@ func NewCreateWallet(l *load.Load, assetType ...libutils.AssetType) *CreateWalle

pg.materialLoader = material.Loader(l.Theme.Base)

pg.backButton, _ = components.SubpageHeaderButtons(l)
pg.backButton, _ = SubpageHeaderButtons(l)

return pg
}
Expand Down Expand Up @@ -507,7 +514,7 @@ func (pg *CreateWallet) HandleUserInteractions() {
pg.walletCreationSuccessCallback()
}
ast := pg.assetTypeSelector.SelectedAssetType()
pg.ParentNavigator().Display(info.NewRestorePage(pg.Load, pg.walletName.Editor.Text(), *ast, afterRestore))
pg.ParentNavigator().Display(NewRestorePage(pg.Load, pg.walletName.Editor.Text(), *ast, afterRestore))
}

// imported wallet click action control
Expand Down Expand Up @@ -628,13 +635,3 @@ func (pg *CreateWallet) validRestoreWalletInputs() bool {

return true
}

func (pg *CreateWallet) walletCreationSuccessCallback() {
// display the overview page if the user is creating a wallet
// for the first time (i.e coming from the onboarding page)
if len(pg.WL.AssetsManager.AllWallets()) == 1 {
pg.ParentNavigator().Display(NewHomePage(pg.Load))
return
}
pg.ParentNavigator().Display(NewWalletSelectorPage(pg.Load))
}
Loading
Loading