Skip to content

Commit

Permalink
add utils function for display common balance
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy authored and dreacot committed Oct 25, 2023
1 parent dab134b commit bd8906e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
48 changes: 47 additions & 1 deletion ui/page/components/coinformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"

"gioui.org/font"
"gioui.org/layout"
"gioui.org/unit"
"github.com/crypto-power/cryptopower/libwallet/utils"
Expand All @@ -20,7 +21,7 @@ var (
noDecimal = regexp.MustCompile(`([0-9]{1,3},*)+`)
)

func formatBalance(gtx layout.Context, l *load.Load, amount string, mainTextSize unit.Sp, scale float32, col color.NRGBA, displayUnitText bool) D {
func formatBalance(gtx C, l *load.Load, amount string, mainTextSize unit.Sp, scale float32, col color.NRGBA, displayUnitText bool) D {

startIndex := 0
stopIndex := 0
Expand Down Expand Up @@ -66,6 +67,31 @@ func formatBalance(gtx layout.Context, l *load.Load, amount string, mainTextSize
)
}

func formatBalanceWithHiden(gtx C, l *load.Load, amount string, mainTextSize unit.Sp, textFont font.Weight, col color.NRGBA, isUSD bool) D {
isBalanceHidden := l.WL.AssetsManager.IsTotalBalanceVisible()
txt := l.Theme.Label(mainTextSize, amount)
if isUSD {
if !IsFetchExchangeRateAPIAllowed(l.WL) {
txt.Text = "$ --"
}
}
if isBalanceHidden {
unit := ""
if !isUSD {
stopIndex := getIndexUnit(amount)
isUnitExist := stopIndex == -1
if isUnitExist {
stopIndex = len(amount)
}
unit = amount[stopIndex:]
}
txt.Text = "****** " + unit
}
txt.Color = col
txt.Font.Weight = textFont
return txt.Layout(gtx)
}

// getIndexUnit returns index of unit currency in amount and
// helps to break out the unit part from the amount string.
func getIndexUnit(amount string) int {
Expand Down Expand Up @@ -105,3 +131,23 @@ func LayoutBalanceSizeScale(gtx layout.Context, l *load.Load, amount string, mai
func LayoutBalanceColor(gtx layout.Context, l *load.Load, amount string, color color.NRGBA) layout.Dimensions {
return formatBalance(gtx, l, amount, values.TextSize20, defaultScale, color, false)
}

func LayoutBalanceWithState(gtx layout.Context, l *load.Load, amount string) layout.Dimensions {
return formatBalanceWithHiden(gtx, l, amount, values.TextSize16, font.Normal, l.Theme.Color.Text, false)
}

func LayoutBalanceColorWithState(gtx layout.Context, l *load.Load, amount string, color color.NRGBA) layout.Dimensions {
return formatBalanceWithHiden(gtx, l, amount, values.TextSize20, font.Normal, color, false)
}

func LayoutBalanceWithStateSemiBold(gtx layout.Context, l *load.Load, amount string) layout.Dimensions {
return formatBalanceWithHiden(gtx, l, amount, values.TextSize16, font.SemiBold, l.Theme.Color.Text, false)
}

func LayoutBalanceWithStateUSD(gtx layout.Context, l *load.Load, amount string) layout.Dimensions {
return formatBalanceWithHiden(gtx, l, amount, values.TextSize16, font.Normal, l.Theme.Color.Text, true)
}

func LayoutBalanceColorWithStateUSD(gtx layout.Context, l *load.Load, amount string, color color.NRGBA) layout.Dimensions {
return formatBalanceWithHiden(gtx, l, amount, values.TextSize16, font.Normal, color, true)
}
6 changes: 2 additions & 4 deletions ui/page/root/overview_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (pg *OverviewPage) assetBalanceItemLayout(item assetBalanceSliderItem) layo
}),
layout.Rigid(func(gtx C) D {
return pg.centerLayout(gtx, values.MarginPadding0, values.MarginPadding10, func(gtx C) D {
return components.LayoutBalanceColor(gtx, pg.Load, item.totalBalance.String(), col)
return components.LayoutBalanceColorWithState(gtx, pg.Load, item.totalBalance.String(), col)
})
}),
layout.Rigid(func(gtx C) D {
Expand All @@ -406,9 +406,7 @@ func (pg *OverviewPage) assetBalanceItemLayout(item assetBalanceSliderItem) layo
Right: values.MarginPadding8,
Left: values.MarginPadding8,
}.Layout(gtx, func(gtx C) D {
lbl := pg.Theme.Body2(item.totalBalanceUSD)
lbl.Color = col
return lbl.Layout(gtx)
return components.LayoutBalanceColorWithStateUSD(gtx, pg.Load, item.totalBalanceUSD, col)
})
})
})
Expand Down
16 changes: 4 additions & 12 deletions ui/page/root/wallet_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,14 @@ func (pg *WalletSelectorPage) walletWrapper(gtx C, item *load.WalletItem) D {
Alignment: layout.End,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
txt := pg.Theme.Label(values.TextSize16, item.TotalBalance.String())
txt.Color = pg.Theme.Color.Text
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
return components.LayoutBalanceWithStateSemiBold(gtx, pg.Load, item.TotalBalance.String())
}),
layout.Rigid(func(gtx C) D {
usdBalance := ""
if components.IsFetchExchangeRateAPIAllowed(pg.WL) {
usdBalance := utils.FormatAsUSDString(pg.Printer, item.TotalBalance.MulF64(pg.assetRate[item.Wallet.GetAssetType()]).ToCoin())
txt := pg.Theme.Label(values.TextSize16, usdBalance)
txt.Color = pg.Theme.Color.Text
return txt.Layout(gtx)
usdBalance = utils.FormatAsUSDString(pg.Printer, item.TotalBalance.MulF64(pg.assetRate[item.Wallet.GetAssetType()]).ToCoin())
}

txt := pg.Theme.Label(values.TextSize16, "$--")
txt.Color = pg.Theme.Color.Text
return txt.Layout(gtx)
return components.LayoutBalanceWithStateUSD(gtx, pg.Load, usdBalance)
}),
)
})
Expand Down
15 changes: 4 additions & 11 deletions ui/page/root/wallet_selector_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,14 @@ func (pg *WalletSelectorPage) dropdownTitleLayout(gtx C, asset libutils.AssetTyp
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
}
txt := pg.Theme.Label(values.TextSize16, pg.assetsBalance[asset].String())
txt.Color = pg.Theme.Color.Text
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
return components.LayoutBalanceWithStateSemiBold(gtx, pg.Load, pg.assetsBalance[asset].String())
}),
layout.Rigid(func(gtx C) D {
usdBalance := ""
if components.IsFetchExchangeRateAPIAllowed(pg.WL) {
txt := pg.Theme.Label(values.TextSize16, utils.FormatAsUSDString(pg.Printer, pg.assetsTotalUSDBalance[asset]))
txt.Color = pg.Theme.Color.Text
return txt.Layout(gtx)
usdBalance = utils.FormatAsUSDString(pg.Printer, pg.assetsTotalUSDBalance[asset])
}

txt := pg.Theme.Label(values.TextSize16, "$ --")
txt.Color = pg.Theme.Color.Text
return txt.Layout(gtx)
return components.LayoutBalanceWithStateUSD(gtx, pg.Load, usdBalance)
}),
)
}),
Expand Down

0 comments on commit bd8906e

Please sign in to comment.