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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import SwiftUI
struct FeedsManagementView: View {
@Environment(AuthManager.self) private var auth
@Bindable var prefsViewModel: FeedPreferencesViewModel
let client: XRPCClient
@State private var cameras: [CameraItem] = []
@State private var locations: [LocationItem] = []
@State private var isLoadingDiscovery = false
@State private var selectedCamera: String?
@State private var selectedLocation: LocationDestination?
private var coreIds: Set<String> {
Set(PinnedFeed.defaults.map(\.id))
}
private var unpinnedDefaults: [PinnedFeed] {
let pinnedIds = Set(prefsViewModel.pinnedFeeds.map(\.id))
return PinnedFeed.defaults.filter { !pinnedIds.contains($0.id) }
}
var body: some View {
List {
Section {
ForEach(prefsViewModel.pinnedFeeds) { feed in
feedRow(feed: feed, showPin: true)
}
.onMove { from, to in
var feeds = prefsViewModel.pinnedFeeds
feeds.move(fromOffsets: from, toOffset: to)
Task { await prefsViewModel.reorderFeeds(feeds, auth: auth.authContext()) }
}
} header: {
Text("Pinned feeds")
}
if !unpinnedDefaults.isEmpty {
Section {
ForEach(unpinnedDefaults) { feed in
feedRow(feed: feed, showPin: false)
}
} header: {
Text("Available feeds")
}
}
if !cameras.isEmpty {
Section {
ForEach(cameras) { camera in
cameraRow(camera: camera)
}
} header: {
Text("Cameras")
}
}
if !locations.isEmpty {
Section {
ForEach(locations) { location in
locationRow(location: location)
}
} header: {
Text("Locations")
}
}
}
.navigationTitle("My feeds")
.navigationBarTitleDisplayMode(.inline)
.environment(\.editMode, .constant(.active))
.navigationDestination(item: $selectedCamera) { camera in
CameraFeedView(client: client, camera: camera)
}
.navigationDestination(item: $selectedLocation) { loc in
LocationFeedView(client: client, h3Index: loc.h3Index, locationName: loc.name)
}
.task {
guard !isLoadingDiscovery else { return }
isLoadingDiscovery = true
async let camerasReq = client.getCameras(auth: auth.authContext())
async let locationsReq = client.getLocations(auth: auth.authContext())
do {
let (cameraList, locationList) = try await (camerasReq, locationsReq)
cameras = cameraList.cameras ?? []
locations = locationList.locations ?? []
} catch {}
isLoadingDiscovery = false
}
}
private func feedRow(feed: PinnedFeed, showPin: Bool) -> some View {
HStack(spacing: 12) {
Image(systemName: iconName(for: feed))
.font(.body)
.foregroundStyle(Color.accentColor)
.frame(width: 32, height: 32)
.background(Color(.secondarySystemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 8))
Text(feed.label)
.font(.body)
Spacer()
if showPin {
Button {
Task { await prefsViewModel.unpinFeed(feed.id, auth: auth.authContext()) }
} label: {
Image(systemName: "pin.slash")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
.accessibilityLabel("Unpin feed")
} else {
Button {
Task { await prefsViewModel.pinFeed(feed, auth: auth.authContext()) }
} label: {
Image(systemName: "pin")
.font(.subheadline)
.foregroundStyle(Color.accentColor)
}
.buttonStyle(.plain)
.accessibilityLabel("Pin feed")
}
}
.moveDisabled(!showPin)
}
private func cameraRow(camera: CameraItem) -> some View {
let feedId = "camera:\(camera.camera)"
let pinned = prefsViewModel.isPinned(feedId)
return Button {
selectedCamera = camera.camera
} label: {
HStack(spacing: 12) {
Image(systemName: "camera")
.font(.body)
.foregroundStyle(Color.accentColor)
.frame(width: 32, height: 32)
.background(Color(.secondarySystemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 8))
VStack(alignment: .leading) {
Text(camera.camera)
.font(.body)
Text("\(camera.photoCount) photos")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
if pinned {
Image(systemName: "pin.fill")
.font(.caption)
.foregroundStyle(Color.accentColor)
}
Image(systemName: "chevron.right")
.font(.caption.weight(.semibold))
.foregroundStyle(.tertiary)
}
}
.buttonStyle(.plain)
.moveDisabled(true)
}
private func locationRow(location: LocationItem) -> some View {
let feedId = "location:\(location.h3Index)"
let pinned = prefsViewModel.isPinned(feedId)
return Button {
selectedLocation = LocationDestination(h3Index: location.h3Index, name: location.name)
} label: {
HStack(spacing: 12) {
Image(systemName: "mappin.and.ellipse")
.font(.body)
.foregroundStyle(Color.accentColor)
.frame(width: 32, height: 32)
.background(Color(.secondarySystemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 8))
VStack(alignment: .leading) {
Text(location.name)
.font(.body)
Text("\(location.galleryCount) galleries")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
if pinned {
Image(systemName: "pin.fill")
.font(.caption)
.foregroundStyle(Color.accentColor)
}
Image(systemName: "chevron.right")
.font(.caption.weight(.semibold))
.foregroundStyle(.tertiary)
}
}
.buttonStyle(.plain)
.moveDisabled(true)
}
private func iconName(for feed: PinnedFeed) -> String {
switch feed.id {
case "recent": "clock"
case "following": "person.2"
case "foryou": "sparkles"
default:
switch feed.type {
case "camera": "camera"
case "location": "mappin.and.ellipse"
case "hashtag": "number"
default: "pin"
}
}
}
}
#Preview {
NavigationStack {
FeedsManagementView(
prefsViewModel: FeedPreferencesViewModel(client: XRPCClient(baseURL: AuthManager.serverURL)),
client: XRPCClient(baseURL: AuthManager.serverURL)
)
}
.environment(AuthManager())
}