Skip to content

Commit

Permalink
ui: balance styling
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Sep 14, 2023
1 parent 68b8bf6 commit a27e71b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
6 changes: 2 additions & 4 deletions LDKNodeMonday/Bitcoin/View Model/BitcoinViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class BitcoinViewModel: ObservableObject {
func getTotalOnchainBalanceSats() async {
do {
let balance = try await LightningNodeService.shared.getTotalOnchainBalanceSats()
let intBalance = Int(balance)
let stringIntBalance = String(intBalance)
let stringIntBalance = balance.formattedSatoshis()
DispatchQueue.main.async {
self.totalBalance = stringIntBalance
self.isTotalBalanceFinished = true
Expand All @@ -44,8 +43,7 @@ class BitcoinViewModel: ObservableObject {
func getSpendableOnchainBalanceSats() async {
do {
let balance = try await LightningNodeService.shared.getSpendableOnchainBalanceSats()
let intBalance = Int(balance)
let stringIntBalance = String(intBalance)
let stringIntBalance = balance.formattedSatoshis()
DispatchQueue.main.async {
self.spendableBalance = stringIntBalance
self.isSpendableBalanceFinished = true
Expand Down
45 changes: 33 additions & 12 deletions LDKNodeMonday/Bitcoin/View/BitcoinView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,56 @@ struct BitcoinView: View {
VStack(spacing: 10) {

HStack {
Spacer()
if viewModel.isTotalBalanceFinished {
Text(viewModel.totalBalance.formattedAmount())
.bold()
withAnimation {
HStack(spacing: 15) {
Image(systemName: "bitcoinsign")
.foregroundColor(.secondary)
.font(.title)
.fontWeight(.thin)
Text(viewModel.totalBalance)
.contentTransition(.numericText())
.fontWeight(.semibold)
.fontDesign(.rounded)
Text("sats")
.foregroundColor(.secondary)
.fontWeight(.thin)
}
.font(.largeTitle)
.lineLimit(1)
.minimumScaleFactor(0.5)
}
} else {
ProgressView()
.padding(.all, 5)
}
Text("Total Sats")
.foregroundColor(.secondary)
.bold()
.font(.title3)
Spacer()
}
.lineLimit(1)
.minimumScaleFactor(0.5)
.animation(.spring(), value: viewModel.totalBalance)

HStack(spacing: 4) {
Spacer()
if viewModel.isSpendableBalanceFinished {
Text(viewModel.spendableBalance.formattedAmount())
withAnimation {
HStack(spacing: 5) {
Image(systemName: "bitcoinsign")
.foregroundColor(.secondary)
.fontWeight(.thin)
Text(viewModel.spendableBalance)
.contentTransition(.numericText())
.fontWeight(.semibold)
.fontDesign(.rounded)
Text("sats spendable")
.foregroundColor(.secondary)
.fontWeight(.thin)
}
.lineLimit(1)
.minimumScaleFactor(0.5)
}
} else {
ProgressView()
.padding(.all, 5)
}
Text("Spendable Sats")
Spacer()
}
.lineLimit(1)
.animation(.spring(), value: viewModel.spendableBalance)
Expand Down
27 changes: 27 additions & 0 deletions LDKNodeMonday/Extensions/UInt64+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,30 @@ extension UInt64 {
}
}
}

extension UInt64 {
func formattedSatoshis() -> String {
if self == 0 {
return "0.00 000 000"
} else {
let balanceString = String(format: "%010d", self)

let zero = balanceString.prefix(2)
let first = balanceString.dropFirst(2).prefix(2)
let second = balanceString.dropFirst(4).prefix(3)
let third = balanceString.dropFirst(7).prefix(3)

var formattedZero = zero

if zero == "00" {
formattedZero = zero.dropFirst()
} else if zero.hasPrefix("0") {
formattedZero = zero.suffix(1)
}

let formattedBalance = "\(formattedZero).\(first) \(second) \(third)"

return formattedBalance
}
}
}

0 comments on commit a27e71b

Please sign in to comment.