Skip to content

Commit

Permalink
ui: add addressformattedview
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Jun 22, 2024
1 parent 1c10c79 commit e96509e
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Sources/BitcoinUI/AddressFormattedView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// AddressFormattedView.swift
//
//
// Created by Matthew Ramsden on 6/21/24.
//

import SwiftUI

public struct AddressFormattedView: View {
public let address: String
public let columns: Int
public let spacing: CGFloat
public let gridItemSize: CGFloat

public init(
address: String,
columns: Int = 3,
spacing: CGFloat = 10,
gridItemSize: CGFloat = 80
) {
self.address = address
self.columns = columns
self.spacing = spacing
self.gridItemSize = gridItemSize
}

public var body: some View {
LazyVGrid(
columns: Array(
repeating: GridItem(
.fixed(gridItemSize),
spacing: spacing
),
count: columns
),
spacing: spacing
) {
let chunks = chunkedAddress()
ForEach(chunks.indices, id: \.self) { index in
Text(chunks[index])
.font(.system(size: 20, weight: .medium, design: .monospaced))
.foregroundColor(index % 2 == 0 ? .primary : .secondary)
}
}
}
}

extension AddressFormattedView {
private func chunkedAddress() -> [String] {
let chunkSize = 4
return stride(from: 0, to: address.count, by: chunkSize).map {
let start = address.index(address.startIndex, offsetBy: $0)
let end =
address.index(start, offsetBy: chunkSize, limitedBy: address.endIndex)
?? address.endIndex
return String(address[start..<end])
}
}
}

struct AddressFormattedView_Previews: PreviewProvider {
static var previews: some View {
AddressFormattedView(
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl"
)
}
}

#Preview {
AddressFormattedView(address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl")
}

0 comments on commit e96509e

Please sign in to comment.