Skip to content

Commit e96509e

Browse files
authored
ui: add addressformattedview
1 parent 1c10c79 commit e96509e

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// AddressFormattedView.swift
3+
//
4+
//
5+
// Created by Matthew Ramsden on 6/21/24.
6+
//
7+
8+
import SwiftUI
9+
10+
public struct AddressFormattedView: View {
11+
public let address: String
12+
public let columns: Int
13+
public let spacing: CGFloat
14+
public let gridItemSize: CGFloat
15+
16+
public init(
17+
address: String,
18+
columns: Int = 3,
19+
spacing: CGFloat = 10,
20+
gridItemSize: CGFloat = 80
21+
) {
22+
self.address = address
23+
self.columns = columns
24+
self.spacing = spacing
25+
self.gridItemSize = gridItemSize
26+
}
27+
28+
public var body: some View {
29+
LazyVGrid(
30+
columns: Array(
31+
repeating: GridItem(
32+
.fixed(gridItemSize),
33+
spacing: spacing
34+
),
35+
count: columns
36+
),
37+
spacing: spacing
38+
) {
39+
let chunks = chunkedAddress()
40+
ForEach(chunks.indices, id: \.self) { index in
41+
Text(chunks[index])
42+
.font(.system(size: 20, weight: .medium, design: .monospaced))
43+
.foregroundColor(index % 2 == 0 ? .primary : .secondary)
44+
}
45+
}
46+
}
47+
}
48+
49+
extension AddressFormattedView {
50+
private func chunkedAddress() -> [String] {
51+
let chunkSize = 4
52+
return stride(from: 0, to: address.count, by: chunkSize).map {
53+
let start = address.index(address.startIndex, offsetBy: $0)
54+
let end =
55+
address.index(start, offsetBy: chunkSize, limitedBy: address.endIndex)
56+
?? address.endIndex
57+
return String(address[start..<end])
58+
}
59+
}
60+
}
61+
62+
struct AddressFormattedView_Previews: PreviewProvider {
63+
static var previews: some View {
64+
AddressFormattedView(
65+
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl"
66+
)
67+
}
68+
}
69+
70+
#Preview {
71+
AddressFormattedView(address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl")
72+
}

0 commit comments

Comments
 (0)