Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Nov 28, 2023
1 parent 822bf43 commit 078b56e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
4 changes: 4 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,10 @@ func (mgr *AssetsManager) CalculateTotalAssetsBalance() (map[utils.AssetType]sha
}

func (mgr *AssetsManager) CalculateAssetsUSDBalance(balances map[utils.AssetType]sharedW.AssetAmount) (map[utils.AssetType]float64, error) {
if !mgr.ExchangeRateFetchingEnabled() {
return nil, fmt.Errorf("USD exchange rate is disabled")
}

usdBalance := func(bal sharedW.AssetAmount, market string) (float64, error) {
rate := mgr.RateSource.GetTicker(market)
if rate == nil || rate.LastTradePrice <= 0 {
Expand Down
31 changes: 14 additions & 17 deletions ui/page/components/consensus_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"gioui.org/font"
"gioui.org/layout"
"gioui.org/unit"

"github.com/crypto-power/cryptopower/libwallet/assets/dcr"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
Expand All @@ -14,7 +13,7 @@ import (
)

type ConsensusItem struct {
Agenda dcr.Agenda
Agenda *dcr.Agenda
VoteButton cryptomaterial.Button
}

Expand All @@ -37,7 +36,7 @@ func AgendaItemWidget(gtx C, l *load.Load, consensusItem *ConsensusItem, hasVoti
)
}

func layoutAgendaStatus(gtx C, l *load.Load, agenda dcr.Agenda) D {
func layoutAgendaStatus(gtx C, l *load.Load, agenda *dcr.Agenda) D {
var statusLabel cryptomaterial.Label
var statusIcon *cryptomaterial.Icon
var backgroundColor color.NRGBA
Expand Down Expand Up @@ -135,7 +134,7 @@ func layoutAgendaVoteAction(gtx C, l *load.Load, item *ConsensusItem, hasVotingW
item.VoteButton.Background = l.Theme.Color.Gray3
}

gtx.Constraints.Min.X, gtx.Constraints.Max.X = gtx.Dp(unit.Dp(150)), gtx.Dp(unit.Dp(200))
gtx.Constraints.Min.X, gtx.Constraints.Max.X = gtx.Dp(values.MarginPadding150), gtx.Dp(values.MarginPadding200)
return layout.Inset{Top: values.MarginPadding15}.Layout(gtx, item.VoteButton.Layout)
}

Expand All @@ -160,29 +159,27 @@ func LoadAgendas(l *load.Load, dcrWallet *dcr.Asset, newestFirst bool) []*Consen
return nil
}

var walletChoices map[string]string
if dcrWallet != nil {
walletChoices, err := dcrWallet.AgendaChoices("")
walletChoices, err = dcrWallet.AgendaChoices("")
if err != nil {
return nil
}
// Update the vote preference value in the agendas slice. Where the
// wallet doesn't have a set vote preference, default to "abstain".
for i := range agendas {
agenda := agendas[i]
if voteChoice, ok := walletChoices[agenda.AgendaID]; ok {
agenda.VotingPreference = voteChoice
} else {
agenda.VotingPreference = "abstain"
}
}
}

consensusItems := make([]*ConsensusItem, len(agendas))
for i := 0; i < len(agendas); i++ {
for i := range agendas {
agenda := agendas[i]
if voteChoice, ok := walletChoices[agenda.AgendaID]; ok {
agenda.VotingPreference = voteChoice
} else {
agenda.VotingPreference = "-"
}
consensusItems[i] = &ConsensusItem{
Agenda: *agendas[i],
Agenda: agenda,
VoteButton: l.Theme.Button(values.String(values.StrSetChoice)),
}
}

return consensusItems
}
2 changes: 1 addition & 1 deletion ui/page/governance/consensus_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (pg *ConsensusPage) HandleUserInteractions() {

for _, item := range pg.consensusItems {
if item.VoteButton.Clicked() {
pg.agendaVoteChoiceModal(&item.Agenda)
pg.agendaVoteChoiceModal(item.Agenda)
}
}

Expand Down
18 changes: 4 additions & 14 deletions ui/page/settings/settings_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,25 +621,15 @@ func (pg *SettingPage) updateSettingOptions() {
}

func (pg *SettingPage) updatePrivacySettings() {
pg.setInitialSwitchStatus(pg.privacyActive, pg.AssetsManager.IsPrivacyModeOn())
if !pg.AssetsManager.IsPrivacyModeOn() {
privacyOn := pg.AssetsManager.IsPrivacyModeOn()
pg.setInitialSwitchStatus(pg.privacyActive, privacyOn)
if !privacyOn {
pg.setInitialSwitchStatus(pg.transactionNotification, pg.AssetsManager.IsTransactionNotificationsOn())
pg.setInitialSwitchStatus(pg.governanceAPI, pg.AssetsManager.IsHTTPAPIPrivacyModeOff(libutils.GovernanceHTTPAPI))
pg.setInitialSwitchStatus(pg.exchangeAPI, pg.AssetsManager.IsHTTPAPIPrivacyModeOff(libutils.ExchangeHTTPAPI))
pg.setInitialSwitchStatus(pg.feeRateAPI, pg.AssetsManager.IsHTTPAPIPrivacyModeOff(libutils.FeeRateHTTPAPI))
pg.setInitialSwitchStatus(pg.vspAPI, pg.AssetsManager.IsHTTPAPIPrivacyModeOff(libutils.VspAPI))
} /*else {
// TODO: Re "Clear all the peers saved if the privacy mode is on". Does
// this mean clear all peers for all wallets? The privacy mode isn't on
// for just the selected wallet afterall.
if pg.WL.SelectedWallet != nil {
go func() {
// Clear all the peers saved if the privacy mode is on.
pg.WL.SelectedWallet.Wallet.SetStringConfigValueForKey(sharedW.SpvPersistentPeerAddressesConfigKey, "")
}()
}
}*/
}
}

// OnNavigatedFrom is called when the page is about to be removed from
Expand Down
22 changes: 9 additions & 13 deletions ui/page/transaction/transactions_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ func (pg *TransactionsPage) pageTitle(gtx C) D {
}

func (pg *TransactionsPage) refreshAvailableTxType() {
wal := pg.selectedWallet
items := []cryptomaterial.DropDownItem{}
_, keysinfo := components.TxPageDropDownFields(wal.GetAssetType(), pg.selectedTabIndex)
_, keysinfo := components.TxPageDropDownFields(pg.selectedWallet.GetAssetType(), pg.selectedTabIndex)
for _, name := range keysinfo {
item := cryptomaterial.DropDownItem{}
item.Text = name
Expand All @@ -166,12 +165,12 @@ func (pg *TransactionsPage) refreshAvailableTxType() {

go func() {
countfn := func(fType int32) int {
count, _ := wal.CountTransactions(fType)
count, _ := pg.selectedWallet.CountTransactions(fType)
return count
}

items := []cryptomaterial.DropDownItem{}
mapinfo, keysinfo := components.TxPageDropDownFields(wal.GetAssetType(), pg.selectedTabIndex)
mapinfo, keysinfo := components.TxPageDropDownFields(pg.selectedWallet.GetAssetType(), pg.selectedTabIndex)
for _, name := range keysinfo {
fieldtype := mapinfo[name]
item := cryptomaterial.DropDownItem{}
Expand All @@ -188,18 +187,17 @@ func (pg *TransactionsPage) refreshAvailableTxType() {
}

func (pg *TransactionsPage) loadTransactions(offset, pageSize int32) ([]*sharedW.Transaction, int, bool, error) {
wal := pg.selectedWallet
mapinfo, _ := components.TxPageDropDownFields(wal.GetAssetType(), pg.selectedTabIndex)
mapinfo, _ := components.TxPageDropDownFields(pg.selectedWallet.GetAssetType(), pg.selectedTabIndex)
if len(mapinfo) < 1 {
err := fmt.Errorf("asset type(%v) and tab index(%d) found", wal.GetAssetType(), pg.selectedTabIndex)
err := fmt.Errorf("asset type(%v) and tab index(%d) found", pg.selectedWallet.GetAssetType(), pg.selectedTabIndex)
return nil, -1, false, err
}

selectedVal, _, _ := strings.Cut(pg.txTypeDropDown.Selected(), " ")
txFilter, ok := mapinfo[selectedVal]
if !ok {
err := fmt.Errorf("unsupported field(%v) for asset type(%v) and tab index(%d) found",
selectedVal, wal.GetAssetType(), pg.selectedTabIndex)
selectedVal, pg.selectedWallet.GetAssetType(), pg.selectedTabIndex)
return nil, -1, false, err
}

Expand All @@ -210,7 +208,7 @@ func (pg *TransactionsPage) loadTransactions(offset, pageSize int32) ([]*sharedW
pg.previousTxFilter = txFilter
}

tempTxs, err := wal.GetTransactionsRaw(offset, pageSize, txFilter, true)
tempTxs, err := pg.selectedWallet.GetTransactionsRaw(offset, pageSize, txFilter, true)
if err != nil {
err = fmt.Errorf("Error loading transactions: %v", err)
}
Expand All @@ -229,7 +227,6 @@ func (pg *TransactionsPage) Layout(gtx layout.Context) layout.Dimensions {

func (pg *TransactionsPage) layoutDesktop(gtx layout.Context) layout.Dimensions {
pg.scroll.OnScrollChangeListener(pg.ParentWindow())
wal := pg.selectedWallet

txlisingView := layout.Flexed(1, func(gtx C) D {
return layout.Inset{Top: values.MarginPadding0}.Layout(gtx, func(gtx C) D {
Expand Down Expand Up @@ -265,7 +262,7 @@ func (pg *TransactionsPage) layoutDesktop(gtx layout.Context) layout.Dimensions
tx := wallTxs[index]
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return components.LayoutTransactionRow(gtx, pg.Load, wal, tx, true)
return components.LayoutTransactionRow(gtx, pg.Load, pg.selectedWallet, tx, true)
}),
layout.Rigid(func(gtx C) D {
// No divider for last row
Expand Down Expand Up @@ -308,7 +305,6 @@ func (pg *TransactionsPage) layoutDesktop(gtx layout.Context) layout.Dimensions
}

func (pg *TransactionsPage) layoutMobile(gtx layout.Context) layout.Dimensions {
wal := pg.selectedWallet
container := func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
Expand All @@ -334,7 +330,7 @@ func (pg *TransactionsPage) layoutMobile(gtx layout.Context) layout.Dimensions {
tx := wallTxs[index]
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return components.LayoutTransactionRow(gtx, pg.Load, wal, tx, true)
return components.LayoutTransactionRow(gtx, pg.Load, pg.selectedWallet, tx, true)
}),
layout.Rigid(func(gtx C) D {
// No divider for last row
Expand Down

0 comments on commit 078b56e

Please sign in to comment.