TeraTodayView.swift (12040B)
1 import SwiftUI 2 import UIKit 3 4 struct TeraTodayView: View { 5 let snapshot: TeraRuntimeSnapshot 6 @ObservedObject var store: TeraTodayStore 7 @ObservedObject var searchStore: TeraSearchStore 8 @ObservedObject var meStore: TeraMeStore 9 @ObservedObject var addStore: TeraAddStore 10 @ObservedObject var settingsStore: TeraSettingsStore 11 @ObservedObject var mediaStore: TeraMediaStore 12 let revise: (TeraTodayCard) -> Void 13 let retract: (TeraTodayCard) -> Void 14 @State private var showsAccount = false 15 @State private var showsContextPicker = false 16 @State private var showsSearch = false 17 18 var body: some View { 19 Group { 20 switch store.presentation.content { 21 case .notLoaded: 22 initialContent 23 case .empty: 24 emptyView 25 case .available: 26 feed 27 } 28 } 29 .navigationTitle("Today") 30 .toolbar { toolbarContent } 31 .sheet(isPresented: $showsContextPicker) { 32 TeraContextPicker(store: store) 33 } 34 .sheet(isPresented: $showsAccount, onDismiss: { meStore.stop() }) { 35 TeraMeSheet( 36 runtimeSnapshot: snapshot, 37 context: store.selectedContext, 38 store: meStore, 39 todayStore: store, 40 addStore: addStore, 41 settingsStore: settingsStore, 42 mediaStore: mediaStore, 43 revise: revise, 44 retract: retract 45 ) 46 } 47 .sheet(isPresented: $showsSearch, onDismiss: { searchStore.stop() }) { 48 TeraSearchSheet( 49 snapshot: snapshot, 50 context: store.selectedContext, 51 store: searchStore, 52 mediaStore: mediaStore, 53 revise: revise, 54 retract: retract 55 ) 56 } 57 .task { await store.start() } 58 } 59 60 @ViewBuilder 61 private var initialContent: some View { 62 if store.hasMorePages { 63 VStack(spacing: 16) { 64 TeraTodayStatusView(presentation: store.presentation) 65 Text("Continue loading saved posts.") 66 continuationButton 67 } 68 .padding() 69 .accessibilityIdentifier("tera.today.more_saved_posts") 70 } else if let failure = store.presentation.readFailure { 71 unavailableView(failure) 72 .accessibilityIdentifier("radroots.today.error") 73 } else { 74 TeraTodayStatusView(presentation: store.presentation) 75 .accessibilityIdentifier("radroots.today.loading") 76 } 77 } 78 79 private var continuationButton: some View { 80 Button("Continue loading") { Task { await store.loadNextPage() } } 81 .buttonStyle(.bordered) 82 .frame(minHeight: 44) 83 .disabled(store.isLoadingNextPage) 84 .accessibilityIdentifier("tera.today.continue_loading") 85 } 86 87 private var emptyView: some View { 88 ScrollView { 89 VStack(spacing: 16) { 90 TeraTodayStatusView(presentation: store.presentation) 91 TeraTodayDiscoveryView(store: store) 92 Image(systemName: "leaf") 93 .font(.largeTitle) 94 .foregroundStyle(.secondary) 95 .accessibilityHidden(true) 96 Text("Nothing here yet") 97 .font(.title2.weight(.semibold)) 98 .multilineTextAlignment(.center) 99 Text("Pull to refresh or add the first update to this local network.") 100 .multilineTextAlignment(.center) 101 Button("Refresh") { Task { await store.reload() } } 102 .buttonStyle(.borderedProminent) 103 .tint(.primary) 104 .frame(minWidth: 44, minHeight: 44) 105 .contentShape(Rectangle()) 106 .accessibilityIdentifier("radroots.today.refresh.empty") 107 } 108 .frame(maxWidth: .infinity) 109 .padding() 110 } 111 } 112 113 private var feed: some View { 114 List { 115 TeraTodayPagingStatusView(store: store) 116 117 ForEach(store.cards) { card in 118 NavigationLink(value: card) { 119 TeraTodayCardView( 120 card: card, 121 context: store.selectedContext, 122 mediaStore: mediaStore 123 ) 124 } 125 .accessibilityIdentifier("radroots.today.card.\(card.id)") 126 .onAppear { 127 guard card.id == store.cards.last?.id, store.canLoadNextPage else { return } 128 Task { await store.loadNextPage() } 129 } 130 } 131 132 if store.isLoadingNextPage { 133 HStack { 134 Spacer() 135 ProgressView("Loading more…") 136 Spacer() 137 } 138 .accessibilityIdentifier("radroots.today.loading_more") 139 } 140 if store.hasMorePages { 141 continuationButton 142 } 143 TeraTodayDiscoveryView(store: store) 144 } 145 .listStyle(.plain) 146 .refreshable { await store.reload() } 147 .navigationDestination(for: TeraTodayCard.self) { card in 148 TeraTodayCurrentDetail( 149 cardID: card.id, store: store, 150 context: store.selectedContext, 151 mediaStore: mediaStore, 152 canRevise: card.authorPublicKey == snapshot.identity.publicKeyHex 153 && card.localOperationID != nil, 154 canRetract: card.authorPublicKey == snapshot.identity.publicKeyHex, 155 revise: revise, 156 retract: retract 157 ) 158 } 159 .environment(\.timeZone, store.viewerCalendar?.timeZone ?? .current) 160 .accessibilityIdentifier("radroots.today.feed") 161 } 162 163 @ToolbarContentBuilder 164 private var toolbarContent: some ToolbarContent { 165 ToolbarItem(placement: .topBarLeading) { 166 Button { 167 showsContextPicker = true 168 } label: { 169 Label(store.selectedContext?.label ?? "Local network", systemImage: "location.circle") 170 } 171 .accessibilityLabel("Choose local network") 172 .accessibilityValue(store.selectedContext?.label ?? "None") 173 .accessibilityIdentifier("radroots.support.context") 174 } 175 ToolbarItem(placement: .topBarTrailing) { 176 Button { 177 Task { await store.reload() } 178 } label: { 179 Label("Refresh", systemImage: "arrow.clockwise") 180 } 181 .accessibilityIdentifier("radroots.today.refresh") 182 } 183 ToolbarItem(placement: .topBarTrailing) { 184 Button { 185 showsSearch = true 186 } label: { 187 Label("Search", systemImage: "magnifyingglass") 188 } 189 .accessibilityIdentifier("radroots.support.search") 190 } 191 ToolbarItem(placement: .topBarTrailing) { 192 Button { 193 showsAccount = true 194 } label: { 195 Label("Account", systemImage: "person.crop.circle") 196 } 197 .accessibilityIdentifier("radroots.support.account") 198 } 199 } 200 201 private func unavailableView(_ failure: TeraTodayFailure) -> some View { 202 ContentUnavailableView { 203 Label("Today is unavailable", systemImage: failure.systemImage) 204 } description: { 205 TeraTodayStatusView(presentation: store.presentation) 206 } actions: { 207 Button("Try again") { Task { await store.reload() } } 208 } 209 } 210 } 211 212 struct TeraTodayCardView: View { 213 @Environment(\.locale) private var locale 214 @Environment(\.timeZone) private var timeZone 215 let card: TeraTodayCard 216 let context: TeraLocalNetwork? 217 @ObservedObject var mediaStore: TeraMediaStore 218 219 var presentation: TeraTodayCardPresentation = .feed 220 221 var body: some View { 222 VStack(alignment: .leading, spacing: 12) { 223 HStack(alignment: .firstTextBaseline) { 224 Text(presentation.label(card.authorName)) 225 .font(.subheadline.weight(.semibold)) 226 Spacer() 227 Text(card.type.label) 228 .font(.caption.weight(.semibold)) 229 .padding(.horizontal, 8) 230 .padding(.vertical, 4) 231 .background(.tint.opacity(0.12), in: Capsule()) 232 } 233 234 if let title = card.title { 235 Text(presentation.label(title)) 236 .font(.headline) 237 } 238 if !card.content.isEmpty { 239 Text(presentation.content(card.content)) 240 .font(card.type == .ask ? .headline : .body) 241 } 242 243 if card.type == .event { 244 TeraCalendarMetadata(card: card, presentation: presentation) 245 } 246 if card.type == .foodAvailability { 247 foodMetadata 248 } 249 250 ForEach(presentation.media(card.media)) { media in 251 TeraTrustedMediaView(media: media, context: context, store: mediaStore) 252 } 253 254 HStack(spacing: 12) { 255 if let date = TeraCalendarPresentation(locale: locale, timeZone: timeZone).authoredDate(card.authoredAtUnixSeconds) { 256 Text(date, style: .relative) 257 } else { 258 Text("Date unavailable") 259 } 260 if card.lifecycle != .active { 261 Label(card.lifecycle.rawValue.capitalized, systemImage: "clock") 262 } 263 if let operationState = card.localOperationState { 264 Label( 265 operationState.replacingOccurrences(of: "_", with: " ").capitalized, 266 systemImage: "arrow.triangle.2.circlepath" 267 ) 268 } 269 } 270 .font(.caption) 271 .foregroundStyle(.secondary) 272 } 273 .padding(.vertical, 8) 274 .accessibilityElement(children: .combine) 275 .accessibilityLabel(presentation.accessibility(card, locale: locale, timeZone: timeZone)) 276 } 277 278 private var foodMetadata: some View { 279 ViewThatFits(in: .horizontal) { 280 HStack(spacing: 12) { 281 foodMetadataLabels 282 } 283 VStack(alignment: .leading, spacing: 4) { 284 foodMetadataLabels 285 } 286 } 287 .font(.subheadline) 288 } 289 290 @ViewBuilder 291 private var foodMetadataLabels: some View { 292 let food = TeraFoodPresentation(locale: locale) 293 if let price = food.price(card) { 294 Label(presentation.label(price), systemImage: "tag") 295 } 296 if let quantity = food.quantity(card) { 297 Label(presentation.label(quantity), systemImage: "basket") 298 } 299 if let location = card.location { 300 Label(presentation.label(location), systemImage: "mappin.and.ellipse") 301 } 302 } 303 } 304 305 struct TeraTrustedMediaView: View { 306 let media: TeraMediaReference 307 let context: TeraLocalNetwork? 308 @ObservedObject var store: TeraMediaStore 309 310 var body: some View { 311 TeraLocalMediaContent(media: media, context: context, store: store) 312 .frame(maxWidth: .infinity, minHeight: 120, maxHeight: 260) 313 .background(.quaternary, in: RoundedRectangle(cornerRadius: 12)) 314 .clipShape(RoundedRectangle(cornerRadius: 12)) 315 } 316 } 317 318 struct TeraLocalMediaContent: View { 319 let media: TeraMediaReference 320 let context: TeraLocalNetwork? 321 @ObservedObject var store: TeraMediaStore 322 323 var body: some View { 324 let state = store.state(for: media, context: context) 325 Group { 326 switch state { 327 case .ready: 328 if let image = store.image(for: media, context: context) { 329 Image(uiImage: image) 330 .resizable() 331 .scaledToFill() 332 } else { 333 mediaState("Saved photo is corrupt", systemImage: "shield.slash", retries: true) 334 } 335 case .pending: 336 mediaState("Verifying photo", systemImage: "hourglass", retries: false) 337 case .loading: 338 ProgressView("Loading verified photo…") 339 case .unavailable: 340 mediaState("Photo is not available locally", systemImage: "photo", retries: true) 341 case .networkUnavailable: 342 mediaState("Photo service unavailable", systemImage: "wifi.slash", retries: true) 343 case .corrupt: 344 mediaState("Saved photo failed verification", systemImage: "shield.slash", retries: true) 345 case .failed: 346 mediaState( 347 "Photo could not be loaded", 348 systemImage: "photo.badge.exclamationmark", 349 retries: true 350 ) 351 } 352 } 353 .task(id: TeraMediaStore.Request(referenceID: media.id, context: context)) { 354 store.load(media: media, context: context) 355 } 356 .accessibilityElement(children: .combine) 357 .accessibilityLabel(accessibilityLabel(for: state)) 358 } 359 360 private func mediaState( 361 _ message: String, 362 systemImage: String, 363 retries: Bool 364 ) -> some View { 365 VStack(spacing: 8) { 366 Label(message, systemImage: systemImage) 367 if retries, context != nil { 368 Button("Retry photo") { 369 store.retry(media: media, context: context) 370 } 371 .buttonStyle(.bordered) 372 } 373 } 374 .font(.caption) 375 } 376 377 private func accessibilityLabel(for state: TeraMediaPresentationState) -> String { 378 guard let alt = media.alt, !alt.isEmpty else { return state.accessibilityLabel } 379 return "\(TeraTodayCardPresentation.feed.label(alt)). \(state.accessibilityLabel)" 380 } 381 }