Skip to content

Commit

Permalink
Frontend.XF/{ReceivePage,FEHelpers}: add bits/sats
Browse files Browse the repository at this point in the history
With this new TapGestureRecognizer, we allow the user to tap on
the BTC amount and switch to bits and sats (and back to BTC if
she taps a 3rd time).
  • Loading branch information
knocte committed Feb 8, 2024
1 parent 46d5ef7 commit e9b862b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/GWallet.Frontend.XF/FrontendHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ type internal CurrencyImageSize =

module FrontendHelpers =

type CryptoSubUnit =
| No
| Specific of multiplier:int * caption:string

let Sats =
CryptoSubUnit.Specific(100_000_000, "sats")
let Bits =
CryptoSubUnit.Specific(1_000_000, "bits")

type IGlobalAppState =
[<CLIEvent>]
abstract member Resumed: IEvent<unit> with get
Expand Down Expand Up @@ -109,6 +118,7 @@ module FrontendHelpers =

let UpdateBalance (balance: MaybeCached<decimal>) currency usdRate
(maybeFrame: Option<Frame>) (balanceLabel: Label) (fiatBalanceLabel: Label)
(cryptoSubUnit: CryptoSubUnit)
: MaybeCached<decimal> =
let maybeBalanceAmount =
match balance with
Expand All @@ -129,8 +139,18 @@ module FrontendHelpers =
| None ->
SPrintF1 "%A (?)" currency, NotFresh(NotAvailable), SPrintF1 "(?) %s" defaultFiatCurrency
| Some balanceAmount ->
let cryptoAmount = Formatting.DecimalAmountRounding CurrencyType.Crypto balanceAmount
let cryptoAmountStr = SPrintF2 "%A %s" currency cryptoAmount
let adjustedBalance =
match cryptoSubUnit with
| No -> balanceAmount
| Specific (multiplier, _caption) ->
balanceAmount * decimal multiplier
let cryptoAmount = Formatting.DecimalAmountRounding CurrencyType.Crypto adjustedBalance
let cryptoAmountStr =
match cryptoSubUnit with
| No ->
SPrintF2 "%A %s" currency cryptoAmount
| Specific (_multiplier, caption) ->
SPrintF2 "%s %A" cryptoAmount caption
let fiatAmount,fiatAmountStr = BalanceInUsdString balanceAmount usdRate
cryptoAmountStr,fiatAmount,fiatAmountStr
MainThread.BeginInvokeOnMainThread(fun _ ->
Expand Down Expand Up @@ -159,6 +179,7 @@ module FrontendHelpers =
(Some balanceSet.Widgets.Frame)
balanceSet.Widgets.CryptoLabel
balanceSet.Widgets.FiatLabel
CryptoSubUnit.No
return {
BalanceSet = balanceSet
FiatAmount = fiatAmount
Expand Down Expand Up @@ -187,6 +208,7 @@ module FrontendHelpers =
(Some balanceSet.Widgets.Frame)
balanceSet.Widgets.CryptoLabel
balanceSet.Widgets.FiatLabel
CryptoSubUnit.No
return {
BalanceSet = balanceSet
FiatAmount = fiatAmount
Expand Down
40 changes: 37 additions & 3 deletions src/GWallet.Frontend.XF/ReceivePage.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ type ReceivePage(account: IAccount,
let currencyImg =
mainLayout.FindByName<Image> "currencyImage"

let balanceLabel = mainLayout.FindByName<Label> "balanceLabel"
let fiatBalanceLabel = mainLayout.FindByName<Label> "fiatBalanceLabel"

let tapCryptoLabel accountBalance =
let cryptoSubUnit =
if balanceLabel.Text.StartsWith "BTC" then
FrontendHelpers.Bits
elif balanceLabel.Text.EndsWith "bits" then
FrontendHelpers.Sats
else
FrontendHelpers.CryptoSubUnit.No
FrontendHelpers.UpdateBalance
(NotFresh accountBalance)
account.Currency
usdRate
None
balanceLabel
fiatBalanceLabel
cryptoSubUnit
|> ignore

do
self.Init()

Expand All @@ -44,12 +65,17 @@ type ReceivePage(account: IAccount,
{ CryptoLabel = null; FiatLabel = null ; Frame = null })

member __.Init() =
let balanceLabel = mainLayout.FindByName<Label>("balanceLabel")
let fiatBalanceLabel = mainLayout.FindByName<Label>("fiatBalanceLabel")

let accountBalance =
Caching.Instance.RetrieveLastCompoundBalance account.PublicAddress account.Currency
FrontendHelpers.UpdateBalance (NotFresh accountBalance) account.Currency usdRate None balanceLabel fiatBalanceLabel
FrontendHelpers.UpdateBalance
(NotFresh accountBalance)
account.Currency
usdRate
None
balanceLabel
fiatBalanceLabel
FrontendHelpers.CryptoSubUnit.No
|> ignore

// this below is for the case when a new ReceivePage() instance is suddenly created after sending a transaction
Expand All @@ -60,11 +86,19 @@ type ReceivePage(account: IAccount,
(Some balanceWidgetsFromBalancePage.Frame)
balanceWidgetsFromBalancePage.CryptoLabel
balanceWidgetsFromBalancePage.FiatLabel
FrontendHelpers.CryptoSubUnit.No
|> ignore

balanceLabel.FontSize <- FrontendHelpers.BigFontSize
fiatBalanceLabel.FontSize <- FrontendHelpers.MediumFontSize

if account.Currency = Currency.BTC then
let cryptoTapGestureRecognizer = TapGestureRecognizer()
cryptoTapGestureRecognizer.Tapped.Subscribe(
fun _ -> tapCryptoLabel accountBalance
) |> ignore
balanceLabel.GestureRecognizers.Add cryptoTapGestureRecognizer

let qrCode = mainLayout.FindByName<ZXingBarcodeImageView> "qrCode"
if isNull qrCode then
failwith "Couldn't find QR code"
Expand Down

0 comments on commit e9b862b

Please sign in to comment.