From b0f066be1b61f975126e8acece3c4eb8de099ecb Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 8 Feb 2024 19:27:39 +0800 Subject: [PATCH] Backend,Frontend.XF: refactor CryptoSubUnit Move it to Backend in case we use it in Frontend.Console, respect DRY (for the caption) and improve func name. --- .../UtxoCoin/TransactionTypes.fs | 16 +++++++++++ src/GWallet.Frontend.XF/FrontendHelpers.fs | 27 +++++++------------ src/GWallet.Frontend.XF/ReceivePage.xaml.fs | 18 ++++++------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/GWallet.Backend/UtxoCoin/TransactionTypes.fs b/src/GWallet.Backend/UtxoCoin/TransactionTypes.fs index 098219630..e67387415 100644 --- a/src/GWallet.Backend/UtxoCoin/TransactionTypes.fs +++ b/src/GWallet.Backend/UtxoCoin/TransactionTypes.fs @@ -4,6 +4,22 @@ open GWallet.Backend open NBitcoin +type SubUnit = + { + Multiplier: int + Caption: string + } + static member Bits = + { + Multiplier = 1000000 + Caption = "bits" + } + static member Sats = + { + Multiplier = 100000000 + Caption = "sats" + } + type TransactionInputOutpointInfo = { TransactionHash: string; diff --git a/src/GWallet.Frontend.XF/FrontendHelpers.fs b/src/GWallet.Frontend.XF/FrontendHelpers.fs index 0d01cbf18..09e5d5f94 100644 --- a/src/GWallet.Frontend.XF/FrontendHelpers.fs +++ b/src/GWallet.Frontend.XF/FrontendHelpers.fs @@ -38,15 +38,6 @@ 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 = [] abstract member Resumed: IEvent with get @@ -118,7 +109,7 @@ module FrontendHelpers = let UpdateBalance (balance: MaybeCached) currency usdRate (maybeFrame: Option) (balanceLabel: Label) (fiatBalanceLabel: Label) - (cryptoSubUnit: CryptoSubUnit) + (cryptoSubUnit: Option) : MaybeCached = let maybeBalanceAmount = match balance with @@ -141,16 +132,16 @@ module FrontendHelpers = | Some balanceAmount -> let adjustedBalance = match cryptoSubUnit with - | No -> balanceAmount - | Specific (multiplier, _caption) -> - balanceAmount * decimal multiplier + | None -> balanceAmount + | Some unit -> + balanceAmount * decimal unit.Multiplier let cryptoAmount = Formatting.DecimalAmountRounding CurrencyType.Crypto adjustedBalance let cryptoAmountStr = match cryptoSubUnit with - | No -> + | None -> SPrintF2 "%A %s" currency cryptoAmount - | Specific (_multiplier, caption) -> - SPrintF2 "%s %A" cryptoAmount caption + | Some unit -> + SPrintF2 "%s %A" cryptoAmount unit.Caption let fiatAmount,fiatAmountStr = BalanceInUsdString balanceAmount usdRate cryptoAmountStr,fiatAmount,fiatAmountStr MainThread.BeginInvokeOnMainThread(fun _ -> @@ -179,7 +170,7 @@ module FrontendHelpers = (Some balanceSet.Widgets.Frame) balanceSet.Widgets.CryptoLabel balanceSet.Widgets.FiatLabel - CryptoSubUnit.No + None return { BalanceSet = balanceSet FiatAmount = fiatAmount @@ -208,7 +199,7 @@ module FrontendHelpers = (Some balanceSet.Widgets.Frame) balanceSet.Widgets.CryptoLabel balanceSet.Widgets.FiatLabel - CryptoSubUnit.No + None return { BalanceSet = balanceSet FiatAmount = fiatAmount diff --git a/src/GWallet.Frontend.XF/ReceivePage.xaml.fs b/src/GWallet.Frontend.XF/ReceivePage.xaml.fs index 57c251701..2261ac27b 100644 --- a/src/GWallet.Frontend.XF/ReceivePage.xaml.fs +++ b/src/GWallet.Frontend.XF/ReceivePage.xaml.fs @@ -36,14 +36,14 @@ type ReceivePage(account: IAccount, let balanceLabel = mainLayout.FindByName