Skip to content

Commit f96bdc6

Browse files
committed
Backend,Frontend.XF: refactor CryptoSubUnit
Move it to Backend in case we use it in Frontend.Console, and respect DRY (for the caption).
1 parent e9b862b commit f96bdc6

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/GWallet.Backend/UtxoCoin/TransactionTypes.fs

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ open GWallet.Backend
44

55
open NBitcoin
66

7+
type SubUnit =
8+
{
9+
Multiplier: int
10+
Caption: string
11+
}
12+
static member Bits =
13+
{
14+
Multiplier = 1000000
15+
Caption = "bits"
16+
}
17+
static member Sats =
18+
{
19+
Multiplier = 100000000
20+
Caption = "sats"
21+
}
22+
723
type TransactionInputOutpointInfo =
824
{
925
TransactionHash: string;

src/GWallet.Frontend.XF/FrontendHelpers.fs

+9-18
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ type internal CurrencyImageSize =
3838

3939
module FrontendHelpers =
4040

41-
type CryptoSubUnit =
42-
| No
43-
| Specific of multiplier:int * caption:string
44-
45-
let Sats =
46-
CryptoSubUnit.Specific(100_000_000, "sats")
47-
let Bits =
48-
CryptoSubUnit.Specific(1_000_000, "bits")
49-
5041
type IGlobalAppState =
5142
[<CLIEvent>]
5243
abstract member Resumed: IEvent<unit> with get
@@ -118,7 +109,7 @@ module FrontendHelpers =
118109

119110
let UpdateBalance (balance: MaybeCached<decimal>) currency usdRate
120111
(maybeFrame: Option<Frame>) (balanceLabel: Label) (fiatBalanceLabel: Label)
121-
(cryptoSubUnit: CryptoSubUnit)
112+
(cryptoSubUnit: Option<UtxoCoin.SubUnit>)
122113
: MaybeCached<decimal> =
123114
let maybeBalanceAmount =
124115
match balance with
@@ -141,16 +132,16 @@ module FrontendHelpers =
141132
| Some balanceAmount ->
142133
let adjustedBalance =
143134
match cryptoSubUnit with
144-
| No -> balanceAmount
145-
| Specific (multiplier, _caption) ->
146-
balanceAmount * decimal multiplier
135+
| None -> balanceAmount
136+
| Some unit ->
137+
balanceAmount * decimal unit.Multiplier
147138
let cryptoAmount = Formatting.DecimalAmountRounding CurrencyType.Crypto adjustedBalance
148139
let cryptoAmountStr =
149140
match cryptoSubUnit with
150-
| No ->
141+
| None ->
151142
SPrintF2 "%A %s" currency cryptoAmount
152-
| Specific (_multiplier, caption) ->
153-
SPrintF2 "%s %A" cryptoAmount caption
143+
| Some unit ->
144+
SPrintF2 "%s %A" cryptoAmount unit.Caption
154145
let fiatAmount,fiatAmountStr = BalanceInUsdString balanceAmount usdRate
155146
cryptoAmountStr,fiatAmount,fiatAmountStr
156147
MainThread.BeginInvokeOnMainThread(fun _ ->
@@ -179,7 +170,7 @@ module FrontendHelpers =
179170
(Some balanceSet.Widgets.Frame)
180171
balanceSet.Widgets.CryptoLabel
181172
balanceSet.Widgets.FiatLabel
182-
CryptoSubUnit.No
173+
None
183174
return {
184175
BalanceSet = balanceSet
185176
FiatAmount = fiatAmount
@@ -208,7 +199,7 @@ module FrontendHelpers =
208199
(Some balanceSet.Widgets.Frame)
209200
balanceSet.Widgets.CryptoLabel
210201
balanceSet.Widgets.FiatLabel
211-
CryptoSubUnit.No
202+
None
212203
return {
213204
BalanceSet = balanceSet
214205
FiatAmount = fiatAmount

src/GWallet.Frontend.XF/ReceivePage.xaml.fs

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ type ReceivePage(account: IAccount,
3838

3939
let tapCryptoLabel accountBalance =
4040
let cryptoSubUnit =
41-
if balanceLabel.Text.StartsWith "BTC" then
42-
FrontendHelpers.Bits
43-
elif balanceLabel.Text.EndsWith "bits" then
44-
FrontendHelpers.Sats
41+
if balanceLabel.Text.Contains UtxoCoin.SubUnit.Bits.Caption then
42+
Some UtxoCoin.SubUnit.Sats
43+
elif balanceLabel.Text.Contains UtxoCoin.SubUnit.Sats.Caption then
44+
None
4545
else
46-
FrontendHelpers.CryptoSubUnit.No
46+
Some UtxoCoin.SubUnit.Bits
4747
FrontendHelpers.UpdateBalance
4848
(NotFresh accountBalance)
4949
account.Currency
@@ -75,7 +75,7 @@ type ReceivePage(account: IAccount,
7575
None
7676
balanceLabel
7777
fiatBalanceLabel
78-
FrontendHelpers.CryptoSubUnit.No
78+
None
7979
|> ignore
8080

8181
// this below is for the case when a new ReceivePage() instance is suddenly created after sending a transaction
@@ -86,7 +86,7 @@ type ReceivePage(account: IAccount,
8686
(Some balanceWidgetsFromBalancePage.Frame)
8787
balanceWidgetsFromBalancePage.CryptoLabel
8888
balanceWidgetsFromBalancePage.FiatLabel
89-
FrontendHelpers.CryptoSubUnit.No
89+
None
9090
|> ignore
9191

9292
balanceLabel.FontSize <- FrontendHelpers.BigFontSize

0 commit comments

Comments
 (0)