Skip to content
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

ui: add addressformattedview #95

Merged
merged 1 commit into from
Jun 22, 2024
Merged
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
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")
}
Loading