Skip to content

Commit 8408625

Browse files
r1b2nsRubens
authored andcommitted
feat: added formatted in BalanceDisplayFormat
1 parent 3317d2b commit 8408625

File tree

6 files changed

+73
-20
lines changed

6 files changed

+73
-20
lines changed

BDKSwiftExampleWallet/Model/BalanceDisplayFormat.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ enum BalanceDisplayFormat: String, CaseIterable, Codable {
2323
case .fiat: return "USD"
2424
}
2525
}
26+
27+
func formatted(_ value: UInt64) -> String {
28+
switch self {
29+
case .sats:
30+
return value.formatted(.number)
31+
case .bitcoin:
32+
return String(format: "%.8f", Double(value) / 100_000_000)
33+
case .bitcoinSats:
34+
return value.formattedSatoshis()
35+
case .bip21q:
36+
return value.formatted(.number)
37+
case .fiat:
38+
return ""
39+
// return satsPrice.formatted(.number.precision(.fractionLength(2)))
40+
case .bip177:
41+
return value.formattedBip177()
42+
}
43+
}
44+
2645
}
2746

2847
extension BalanceDisplayFormat {

BDKSwiftExampleWallet/View/Activity/ActivityListView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ struct ActivityListView: View {
2626
TransactionListView(
2727
viewModel: .init(),
2828
transactions: viewModel.transactions,
29-
walletSyncState: viewModel.walletSyncState
29+
walletSyncState: viewModel.walletSyncState,
30+
format: .bip177
3031
)
3132
.transition(.blurReplace)
3233
} else {

BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ struct TransactionItemView: View {
1313
@Environment(\.dynamicTypeSize) var dynamicTypeSize
1414
let txDetails: TxDetails
1515
let isRedacted: Bool
16+
private let format: BalanceDisplayFormat
17+
18+
init(
19+
txDetails: TxDetails,
20+
isRedacted: Bool,
21+
format: BalanceDisplayFormat
22+
) {
23+
self.txDetails = txDetails
24+
self.isRedacted = isRedacted
25+
self.format = format
26+
}
1627

1728
var body: some View {
1829

@@ -120,7 +131,8 @@ struct TransactionItemView: View {
120131
#Preview {
121132
TransactionItemView(
122133
txDetails: .mock,
123-
isRedacted: false
134+
isRedacted: false,
135+
format: .bip177
124136
)
125137
}
126138
#endif

BDKSwiftExampleWallet/View/Activity/TransactionListView.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,28 @@ struct TransactionListView: View {
1313
@Bindable var viewModel: TransactionListViewModel
1414
let transactions: [CanonicalTx]
1515
let walletSyncState: WalletSyncState
16-
16+
private let format: BalanceDisplayFormat
17+
18+
init(
19+
viewModel: TransactionListViewModel,
20+
transactions: [CanonicalTx],
21+
walletSyncState: WalletSyncState,
22+
format: BalanceDisplayFormat
23+
) {
24+
self.viewModel = viewModel
25+
self.transactions = transactions
26+
self.walletSyncState = walletSyncState
27+
self.format = format
28+
}
29+
1730
var body: some View {
1831

1932
List {
2033
if transactions.isEmpty && walletSyncState == .syncing {
2134
TransactionItemView(
2235
txDetails: .mock,
23-
isRedacted: true
36+
isRedacted: true,
37+
format: format
2438
)
2539
.listRowInsets(EdgeInsets())
2640
.listRowSeparator(.hidden)
@@ -102,7 +116,8 @@ struct TransactionListView: View {
102116
) {
103117
TransactionItemView(
104118
txDetails: txDetails,
105-
isRedacted: false
119+
isRedacted: false,
120+
format: format
106121
)
107122
}
108123

@@ -142,7 +157,8 @@ struct TransactionListView: View {
142157
transactions: [
143158
.mock
144159
],
145-
walletSyncState: .synced
160+
walletSyncState: .synced,
161+
format: .bip177
146162
)
147163
}
148164
#Preview {
@@ -151,7 +167,8 @@ struct TransactionListView: View {
151167
bdkClient: .mock
152168
),
153169
transactions: [],
154-
walletSyncState: .synced
170+
walletSyncState: .synced,
171+
format: .bip177
155172
)
156173
}
157174
#endif

BDKSwiftExampleWallet/View/Home/BalanceView.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@ struct BalanceView: View {
3737

3838
@MainActor
3939
private var formattedBalance: String {
40-
switch format {
41-
case .sats:
42-
return balance.formatted(.number)
43-
case .bitcoin:
44-
return String(format: "%.8f", Double(balance) / 100_000_000)
45-
case .bitcoinSats:
46-
return balance.formattedSatoshis()
47-
case .fiat:
48-
return satsPrice.formatted(.number.precision(.fractionLength(2)))
49-
case .bip177:
50-
return balance.formattedBip177()
51-
}
40+
return format.formatted(balance)
41+
// switch format {
42+
// case .sats:
43+
// return balance.formatted(.number)
44+
// case .bitcoin:
45+
// return String(format: "%.8f", Double(balance) / 100_000_000)
46+
// case .bitcoinSats:
47+
// return balance.formattedSatoshis()
48+
// case .bip21q:
49+
// return balance.formatted(.number)
50+
// case .fiat:
51+
// return satsPrice.formatted(.number.precision(.fractionLength(2)))
52+
// case .bip177:
53+
// return balance.formattedBip177()
54+
// }
5255
}
5356

5457
@MainActor

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ struct WalletView: View {
5555
TransactionListView(
5656
viewModel: .init(),
5757
transactions: viewModel.recentTransactions,
58-
walletSyncState: viewModel.walletSyncState
58+
walletSyncState: viewModel.walletSyncState,
59+
format: balanceFormat
5960
)
6061
.refreshable {
6162
await viewModel.syncOrFullScan()

0 commit comments

Comments
 (0)