Skip to content

feat: add balance formatting across the app #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions BDKSwiftExampleWallet/Model/BalanceDisplayFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ enum BalanceDisplayFormat: String, CaseIterable, Codable {
case fiat = "usd"
case bip177 = "bip177"

var displayPrefix: String {
switch self {
case .bitcoinSats, .bitcoin, .bip177: return "₿"
case .fiat: return "$"
default : return ""
}
}

var displayText: String {
switch self {
case .sats, .bitcoinSats: return "sats"
Expand All @@ -23,6 +31,22 @@ enum BalanceDisplayFormat: String, CaseIterable, Codable {
case .fiat: return "USD"
}
}

func formatted(_ btcAmount: UInt64, fiatPrice: Double) -> String {
switch self {
case .sats:
return btcAmount.formatted(.number)
case .bitcoin:
return String(format: "%.8f", Double(btcAmount) / 100_000_000)
case .bitcoinSats:
return btcAmount.formattedSatoshis()
case .fiat:
let satsPrice = Double(btcAmount).valueInUSD(price: fiatPrice)
return satsPrice.formatted(.number.precision(.fractionLength(2)))
case .bip177:
return btcAmount.formattedBip177()
}
}
}

extension BalanceDisplayFormat {
Expand Down
5 changes: 3 additions & 2 deletions BDKSwiftExampleWallet/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@
}
}
},
"%@%lld sats" : {
"%@%@ %@" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "%1$@%2$lld sats"
"value" : "%1$@%2$@ %3$@"
}
}
}
Expand Down Expand Up @@ -288,6 +288,7 @@
}
},
"%llu sats" : {
"extractionState" : "stale",
"localizations" : {
"fr" : {
"stringUnit" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ActivityListViewModel {
var totalScripts: UInt64 = 0
var walletSyncState: WalletSyncState
var walletViewError: AppError?
var fiatPrice: Double

private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
{ [weak self] inspected, total in
Expand All @@ -41,11 +42,13 @@ class ActivityListViewModel {
init(
bdkClient: BDKClient = .live,
transactions: [CanonicalTx] = [],
walletSyncState: WalletSyncState = .notStarted
walletSyncState: WalletSyncState = .notStarted,
fiatPrice: Double
) {
self.bdkClient = bdkClient
self.transactions = transactions
self.walletSyncState = walletSyncState
self.fiatPrice = fiatPrice

// Preload cached data synchronously so UI has content before first render
// transactions + listUnspent items are available from the persisted wallet db
Expand Down
11 changes: 8 additions & 3 deletions BDKSwiftExampleWallet/View/Activity/ActivityListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import BitcoinDevKit
import SwiftUI

struct ActivityListView: View {
@AppStorage("balanceDisplayFormat") private var balanceFormat: BalanceDisplayFormat =
.bitcoinSats
@Bindable var viewModel: ActivityListViewModel

var body: some View {
Expand All @@ -26,13 +28,16 @@ struct ActivityListView: View {
TransactionListView(
viewModel: .init(),
transactions: viewModel.transactions,
walletSyncState: viewModel.walletSyncState
walletSyncState: viewModel.walletSyncState,
format: balanceFormat,
fiatPrice: viewModel.fiatPrice
)
.transition(.blurReplace)
} else {
LocalOutputListView(
localOutputs: viewModel.localOutputs,
walletSyncState: viewModel.walletSyncState
walletSyncState: viewModel.walletSyncState,
fiatPrice: viewModel.fiatPrice
)
.transition(.blurReplace)
}
Expand Down Expand Up @@ -80,5 +85,5 @@ struct CustomSegmentedControl: View {
}

#Preview {
ActivityListView(viewModel: .init())
ActivityListView(viewModel: .init(fiatPrice: 714.23))
}
32 changes: 23 additions & 9 deletions BDKSwiftExampleWallet/View/Activity/LocalOutputItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import BitcoinDevKit
import SwiftUI

struct LocalOutputItemView: View {
@AppStorage("balanceDisplayFormat") private var balanceFormat: BalanceDisplayFormat =
.bitcoinSats
@Environment(\.dynamicTypeSize) var dynamicTypeSize
let isRedacted: Bool
let output: LocalOutput
let fiatPrice: Double

var body: some View {
HStack(spacing: 15) {
Expand Down Expand Up @@ -53,14 +56,24 @@ struct LocalOutputItemView: View {
.redacted(reason: isRedacted ? .placeholder : [])

Spacer()

Text("\(output.txout.value.toSat()) sats")
.font(.subheadline)
.fontWeight(.semibold)
.fontDesign(.rounded)
.lineLimit(1)
.redacted(reason: isRedacted ? .placeholder : [])


Group {
HStack {
Text(balanceFormat.displayPrefix)
Text(
balanceFormat.formatted(
output.txout.value.toSat(),
fiatPrice: fiatPrice
)
)
Text(balanceFormat.displayText)
}
}
.font(.subheadline)
.fontWeight(.semibold)
.fontDesign(.rounded)
.lineLimit(1)
.redacted(reason: isRedacted ? .placeholder : [])
}
.padding(.vertical, 15.0)
.padding(.vertical, 5.0)
Expand All @@ -71,6 +84,7 @@ struct LocalOutputItemView: View {
#Preview {
LocalOutputItemView(
isRedacted: false,
output: .mock
output: .mock,
fiatPrice: 714.23
)
}
9 changes: 6 additions & 3 deletions BDKSwiftExampleWallet/View/Activity/LocalOutputListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import SwiftUI
struct LocalOutputListView: View {
let localOutputs: [LocalOutput]
let walletSyncState: WalletSyncState
let fiatPrice: Double

var body: some View {
List {
if localOutputs.isEmpty && walletSyncState == .syncing {
LocalOutputItemView(
isRedacted: true,
output: .mock
output: .mock,
fiatPrice: .zero
)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
Expand All @@ -30,7 +32,8 @@ struct LocalOutputListView: View {
ForEach(localOutputs, id: \.outpoint) { output in
LocalOutputItemView(
isRedacted: false,
output: output
output: output,
fiatPrice: fiatPrice
)
}
.listRowInsets(EdgeInsets())
Expand All @@ -43,5 +46,5 @@ struct LocalOutputListView: View {
}

#Preview {
LocalOutputListView(localOutputs: [.mock], walletSyncState: .synced)
LocalOutputListView(localOutputs: [.mock], walletSyncState: .synced, fiatPrice: 714.23)
}
16 changes: 13 additions & 3 deletions BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import BitcoinUI
import SwiftUI

struct TransactionDetailView: View {
@AppStorage("balanceDisplayFormat") private var balanceFormat: BalanceDisplayFormat =
.bitcoinSats
@Bindable var viewModel: TransactionDetailViewModel
@State private var isCopied = false
@State private var showCheckmark = false
let txDetails: TxDetails
let fiatPrice: Double

var body: some View {

Expand Down Expand Up @@ -55,8 +58,14 @@ struct TransactionDetailView: View {

VStack(spacing: 8) {
HStack {
Text(abs(txDetails.balanceDelta).delimiter)
Text("sats")
Text(balanceFormat.displayPrefix)
Text(
balanceFormat.formatted(
UInt64(abs(txDetails.balanceDelta)),
fiatPrice: fiatPrice
)
)
Text(balanceFormat.displayText)
}
.lineLimit(1)
.minimumScaleFactor(0.5)
Expand Down Expand Up @@ -168,7 +177,8 @@ struct TransactionDetailView: View {
viewModel: .init(
bdkClient: .mock
),
txDetails: .mock
txDetails: .mock,
fiatPrice: 714.23
)
}
#endif
25 changes: 21 additions & 4 deletions BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ struct TransactionItemView: View {
@Environment(\.dynamicTypeSize) var dynamicTypeSize
let txDetails: TxDetails
let isRedacted: Bool
private let format: BalanceDisplayFormat
private var fiatPrice: Double

init(
txDetails: TxDetails,
isRedacted: Bool,
format: BalanceDisplayFormat,
fiatPrice: Double
) {
self.txDetails = txDetails
self.isRedacted = isRedacted
self.format = format
self.fiatPrice = fiatPrice
}

var body: some View {

Expand Down Expand Up @@ -98,10 +112,11 @@ struct TransactionItemView: View {
Spacer()

let delta = txDetails.balanceDelta
let prefix = delta >= 0 ? "+ " : "- "
let amount = abs(delta)
let prefix = (delta >= 0 ? "+ " : "- ").appending("\(format.displayPrefix) ")
let amount = format.formatted(UInt64(abs(delta)), fiatPrice: fiatPrice)
let suffix = format.displayText

Text("\(prefix)\(amount) sats")
Text("\(prefix)\(amount) \(suffix)")
.font(.subheadline)
.fontWeight(.semibold)
.fontDesign(.rounded)
Expand All @@ -120,7 +135,9 @@ struct TransactionItemView: View {
#Preview {
TransactionItemView(
txDetails: .mock,
isRedacted: false
isRedacted: false,
format: .bip177,
fiatPrice: 714.23
)
}
#endif
37 changes: 31 additions & 6 deletions BDKSwiftExampleWallet/View/Activity/TransactionListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ struct TransactionListView: View {
@Bindable var viewModel: TransactionListViewModel
let transactions: [CanonicalTx]
let walletSyncState: WalletSyncState

private let format: BalanceDisplayFormat
private let fiatPrice: Double

init(
viewModel: TransactionListViewModel,
transactions: [CanonicalTx],
walletSyncState: WalletSyncState,
format: BalanceDisplayFormat,
fiatPrice: Double
) {
self.viewModel = viewModel
self.transactions = transactions
self.walletSyncState = walletSyncState
self.format = format
self.fiatPrice = fiatPrice
}

var body: some View {

List {
if transactions.isEmpty && walletSyncState == .syncing {
TransactionItemView(
txDetails: .mock,
isRedacted: true
isRedacted: true,
format: format,
fiatPrice: fiatPrice
)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
Expand Down Expand Up @@ -94,12 +112,15 @@ struct TransactionListView: View {
viewModel: .init(
bdkClient: .live
),
txDetails: txDetails
txDetails: txDetails,
fiatPrice: fiatPrice
)
) {
TransactionItemView(
txDetails: txDetails,
isRedacted: false
isRedacted: false,
format: format,
fiatPrice: fiatPrice
)
}

Expand Down Expand Up @@ -139,7 +160,9 @@ struct TransactionListView: View {
transactions: [
.mock
],
walletSyncState: .synced
walletSyncState: .synced,
format: .bip177,
fiatPrice: 714.23
)
}
#Preview {
Expand All @@ -148,7 +171,9 @@ struct TransactionListView: View {
bdkClient: .mock
),
transactions: [],
walletSyncState: .synced
walletSyncState: .synced,
format: .bip177,
fiatPrice: 714.23
)
}
#endif
Loading