1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import SwiftUI
/// The explore page, as on grain.social: chips for the top cameras and places
/// over a grid of the recent feed. Lives under the search field and shows
/// whenever nothing has been typed.
struct ExploreView: View {
@Environment(AuthManager.self) private var auth
@Environment(Router.self) private var router
@State private var viewModel: ExploreViewModel
@State private var width: CGFloat = 0
let client: XRPCClient
/// How many chips a group shows before deferring to its index screen.
private static let chipLimit = 10
private static let columns = 3
private static let spacing: CGFloat = 2
init(client: XRPCClient, viewModel: ExploreViewModel? = nil) {
self.client = client
_viewModel = State(initialValue: viewModel ?? ExploreViewModel(client: client))
}
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
if !viewModel.cameras.isEmpty {
ChipGroup(
label: "Cameras",
chips: viewModel.cameras.prefix(Self.chipLimit).map { camera in
Chip(title: camera.camera) { router.push(.camera(camera.camera)) }
},
seeAll: viewModel.cameras.count > Self.chipLimit ? { router.push(.cameras) } : nil
)
}
if !viewModel.locations.isEmpty {
ChipGroup(
label: "Locations",
chips: viewModel.locations.prefix(Self.chipLimit).map { place in
Chip(title: place.name) {
router.push(.locations(cell: place.h3Index, name: place.name, galleryCount: place.galleryCount))
}
},
seeAll: viewModel.locations.count > Self.chipLimit ? { router.push(.locations()) } : nil
)
}
grid
.padding(.top, 8)
}
}
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width = $0 }
.refreshable { await viewModel.refresh(auth: auth.authContext()) }
.task {
guard !isPreview else { return }
await viewModel.loadIfNeeded(auth: auth.authContext())
}
}
@ViewBuilder
private var grid: some View {
if viewModel.galleries.isEmpty {
Group {
if viewModel.isLoading {
ProgressView()
} else {
Text("Nothing to explore yet.")
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity)
.padding(.vertical, 60)
} else {
let cellWidth = max((width - Self.spacing * CGFloat(Self.columns - 1)) / CGFloat(Self.columns), 0)
let cellSize = CGSize(width: cellWidth, height: cellWidth * 4 / 3)
LazyVGrid(
columns: Array(repeating: GridItem(.flexible(), spacing: Self.spacing), count: Self.columns),
spacing: Self.spacing
) {
ForEach(viewModel.galleries) { gallery in
GalleryThumbnailCell(gallery: gallery, cellSize: cellSize) {
router.push(.gallery(uri: gallery.uri))
}
.aspectRatio(3 / 4, contentMode: .fit)
.onAppear {
if gallery.id == viewModel.galleries.last?.id {
Task { await viewModel.loadMore(auth: auth.authContext()) }
}
}
}
}
if viewModel.isLoadingMore {
ProgressView()
.frame(maxWidth: .infinity)
.padding()
}
}
}
}
private struct Chip: Identifiable {
let id = UUID()
let title: String
let action: () -> Void
}
/// A small uppercase label over a wrapping row of chips, with an optional
/// "See all" at the end when the row is only the top of a longer list.
private struct ChipGroup: View {
let label: String
let chips: [Chip]
let seeAll: (() -> Void)?
var body: some View {
VStack(alignment: .leading, spacing: 6) {
Text(label.uppercased())
.font(.system(size: 11, weight: .bold))
.kerning(0.9)
.foregroundStyle(.secondary)
FlowLayout(spacing: 8) {
ForEach(chips) { chip in
Button(chip.title, action: chip.action)
.buttonStyle(ChipStyle(prominent: false))
}
if let seeAll {
Button("See all", action: seeAll)
.buttonStyle(ChipStyle(prominent: true))
}
}
}
.padding(.horizontal)
.padding(.top, 14)
.padding(.bottom, 8)
}
}
private struct ChipStyle: ButtonStyle {
let prominent: Bool
func makeBody(configuration: Configuration) -> some View {
configuration.label
.font(.footnote.weight(.semibold))
.foregroundStyle(prominent ? Color.accentColor : Color.primary.opacity(0.8))
.padding(.horizontal, 14)
.padding(.vertical, 7)
.background(prominent ? Color.clear : Color(.secondarySystemBackground), in: Capsule())
.opacity(configuration.isPressed ? 0.6 : 1)
}
}