field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

TeraSupportingViews.swift (11609B)


      1 import RadrootsKit
      2 import SwiftUI
      3 
      4 struct TeraSearchSheet: View {
      5   let snapshot: TeraRuntimeSnapshot
      6   let context: TeraLocalNetwork?
      7   @ObservedObject var store: TeraSearchStore
      8   @ObservedObject var mediaStore: TeraMediaStore
      9   let revise: (TeraTodayCard) -> Void
     10   let retract: (TeraTodayCard) -> Void
     11   @Environment(\.dismiss) private var dismiss
     12 
     13   var body: some View {
     14     NavigationStack {
     15       Group {
     16         switch store.state {
     17         case .idle:
     18           ContentUnavailableView(
     19             "Search your local network",
     20             systemImage: "magnifyingglass",
     21             description: Text("Find current posts and adopted Nostr profiles.")
     22           )
     23         case .loading:
     24           ProgressView("Searching…")
     25         case .empty:
     26           ContentUnavailableView.search(text: store.query)
     27         case let .failed(message):
     28           ContentUnavailableView {
     29             Label("Search unavailable", systemImage: "exclamationmark.triangle")
     30           } description: {
     31             Text(message)
     32           } actions: {
     33             Button("Try again") { Task { await store.search() } }
     34           }
     35         case .loaded:
     36           resultList
     37         }
     38       }
     39       .navigationTitle("Search")
     40       .searchable(text: query, prompt: "Posts and profiles")
     41       .onSubmit(of: .search) { Task { await store.search() } }
     42       .toolbar {
     43         ToolbarItem(placement: .confirmationAction) {
     44           Button("Done") { dismiss() }
     45         }
     46       }
     47     }
     48     .task(id: context) {
     49       store.configure(context: context)
     50       store.start()
     51     }
     52     .accessibilityIdentifier("radroots.support.search.sheet")
     53   }
     54 
     55   private var resultList: some View {
     56     List(store.results) { result in
     57       switch (result.card, result.profile) {
     58       case let (card?, _):
     59         NavigationLink {
     60           TeraSearchCurrentDetail(
     61             resultID: result.id, type: result.type, store: store,
     62             context: context, mediaStore: mediaStore,
     63             publicKey: snapshot.identity.publicKeyHex, revise: revise, retract: retract
     64           )
     65         } label: {
     66           TeraTodayCardView(card: card, context: context, mediaStore: mediaStore)
     67         }
     68         .accessibilityIdentifier("radroots.search.card.\(result.id)")
     69       case let (_, profile?):
     70         NavigationLink {
     71           TeraSearchCurrentDetail(
     72             resultID: result.id, type: result.type, store: store,
     73             context: context, mediaStore: mediaStore,
     74             publicKey: snapshot.identity.publicKeyHex, revise: revise, retract: retract
     75           )
     76         } label: {
     77           TeraProfileRow(profile: profile, context: context, mediaStore: mediaStore)
     78         }
     79         .accessibilityIdentifier("radroots.search.profile.\(result.id)")
     80       default:
     81         EmptyView()
     82       }
     83     }
     84     .listStyle(.plain)
     85     .accessibilityIdentifier("radroots.search.results")
     86   }
     87 
     88   private var query: Binding<String> {
     89     Binding(
     90       get: { store.query },
     91       set: { value in store.updateQuery(value) }
     92     )
     93   }
     94 }
     95 
     96 struct TeraMeSheet: View {
     97   let runtimeSnapshot: TeraRuntimeSnapshot
     98   let context: TeraLocalNetwork?
     99   @ObservedObject var store: TeraMeStore
    100   @ObservedObject var todayStore: TeraTodayStore
    101   @ObservedObject var addStore: TeraAddStore
    102   @ObservedObject var settingsStore: TeraSettingsStore
    103   @ObservedObject var mediaStore: TeraMediaStore
    104   let revise: (TeraTodayCard) -> Void
    105   let retract: (TeraTodayCard) -> Void
    106   @Environment(\.dismiss) private var dismiss
    107   @Environment(\.dynamicTypeSize) private var dynamicTypeSize
    108   @State private var showsDrafts = false
    109 
    110   var body: some View {
    111     NavigationStack {
    112       Group {
    113         switch store.state {
    114         case .idle, .loading:
    115           ProgressView("Loading your profile…")
    116         case let .failed(message) where store.snapshot == nil:
    117           ContentUnavailableView {
    118             Label("Profile unavailable", systemImage: "person.crop.circle.badge.exclamationmark")
    119           } description: {
    120             Text(message)
    121           } actions: {
    122             Button("Try again") { Task { await store.reload() } }
    123           }
    124         default:
    125           content
    126         }
    127       }
    128       .navigationTitle("Me")
    129       .navigationBarTitleDisplayMode(dynamicTypeSize.isAccessibilitySize ? .inline : .automatic)
    130       .teraReadableScrollEdges(dynamicTypeSize.isAccessibilitySize)
    131       .toolbar {
    132         ToolbarItem(placement: .confirmationAction) {
    133           Button("Done") { dismiss() }
    134         }
    135       }
    136     }
    137     .sheet(isPresented: $showsDrafts) {
    138       TeraDraftsSheet(store: addStore)
    139     }
    140     .task(id: context) {
    141       store.configure(context: context)
    142       await store.start()
    143     }
    144     .presentationDetents(dynamicTypeSize.isAccessibilitySize ? [.large] : [.medium, .large])
    145     .accessibilityIdentifier("radroots.support.me.sheet")
    146   }
    147 
    148   private var content: some View {
    149     List {
    150       Section {
    151         if let profile = store.snapshot?.profile {
    152           NavigationLink {
    153             TeraMeCurrentDetail(
    154               cardID: nil, store: store, context: context, mediaStore: mediaStore,
    155               publicKey: runtimeSnapshot.identity.publicKeyHex, revise: revise, retract: retract
    156             )
    157           } label: {
    158             TeraProfileRow(profile: profile, context: context, mediaStore: mediaStore)
    159           }
    160         } else {
    161           TeraProfileRow(
    162             profile: TeraProfileSummary(
    163               authorPublicKey: store.snapshot?.publicKey ?? runtimeSnapshot.identity.publicKeyHex,
    164               name: nil,
    165               displayName: nil,
    166               about: nil,
    167               picture: nil,
    168               banner: nil,
    169               nip05: nil,
    170               website: nil,
    171               lightningAddress: nil
    172             ),
    173             context: context,
    174             mediaStore: mediaStore
    175           )
    176         }
    177       }
    178 
    179       Section("Local work") {
    180         Button {
    181           showsDrafts = true
    182         } label: {
    183           LabeledContent("Drafts & outbox", value: "\(addStore.drafts.count)")
    184         }
    185         .foregroundStyle(.primary)
    186         .accessibilityIdentifier("radroots.support.outbox")
    187       }
    188 
    189       Section("My posts") {
    190         if store.snapshot?.cards.isEmpty != false {
    191           Text("No current posts in this local network.")
    192             .foregroundStyle(.secondary)
    193         }
    194         ForEach(store.snapshot?.cards ?? []) { card in
    195           NavigationLink {
    196             TeraMeCurrentDetail(
    197               cardID: card.id, store: store, context: context, mediaStore: mediaStore,
    198               publicKey: runtimeSnapshot.identity.publicKeyHex, revise: revise, retract: retract
    199             )
    200           } label: {
    201             TeraTodayCardView(card: card, context: context, mediaStore: mediaStore)
    202           }
    203         }
    204       }
    205 
    206       Section {
    207         NavigationLink("Settings") {
    208           TeraSettingsView(
    209             snapshot: runtimeSnapshot,
    210             todayStore: todayStore,
    211             addStore: addStore,
    212             meStore: store,
    213             settingsStore: settingsStore
    214           )
    215         }
    216         .accessibilityIdentifier("radroots.support.settings")
    217       }
    218     }
    219     .refreshable { await store.reload() }
    220   }
    221 }
    222 
    223 struct TeraProfileRow: View {
    224   let profile: TeraProfileSummary
    225   let context: TeraLocalNetwork?
    226   @ObservedObject var mediaStore: TeraMediaStore
    227 
    228   var body: some View {
    229     HStack(spacing: 12) {
    230       TeraStableAvatarView(
    231         profile: profile,
    232         context: context,
    233         mediaStore: mediaStore,
    234         size: 48
    235       )
    236       VStack(alignment: .leading, spacing: 3) {
    237         Text(profile.preferredName)
    238           .font(.headline)
    239         if let nip05 = profile.nip05 {
    240           Text(nip05)
    241             .font(.caption)
    242             .foregroundStyle(.secondary)
    243         } else {
    244           Text(Self.abbreviate(profile.authorPublicKey))
    245             .font(.caption.monospaced())
    246             .foregroundStyle(.secondary)
    247         }
    248       }
    249     }
    250     .accessibilityElement(children: .combine)
    251     .accessibilityLabel("\(profile.preferredName), \(Self.abbreviate(profile.authorPublicKey))")
    252   }
    253 
    254   private static func abbreviate(_ key: String) -> String {
    255     guard key.count > 16 else { return key }
    256     return "\(key.prefix(8))…\(key.suffix(8))"
    257   }
    258 }
    259 
    260 struct TeraProfileView: View {
    261   let profile: TeraProfileSummary
    262   let context: TeraLocalNetwork?
    263   @ObservedObject var mediaStore: TeraMediaStore
    264 
    265   var body: some View {
    266     List {
    267       Section {
    268         VStack(spacing: 12) {
    269           if let banner = profile.banner {
    270             TeraLocalMediaContent(media: banner, context: context, store: mediaStore)
    271               .frame(maxWidth: .infinity, minHeight: 100, maxHeight: 160)
    272               .background(.quaternary, in: RoundedRectangle(cornerRadius: 12))
    273               .clipShape(RoundedRectangle(cornerRadius: 12))
    274           }
    275           TeraStableAvatarView(
    276             profile: profile,
    277             context: context,
    278             mediaStore: mediaStore,
    279             size: 88
    280           )
    281           Text(profile.preferredName)
    282             .font(.title2.weight(.semibold))
    283           Text(abbreviatedPublicKey)
    284             .font(.caption.monospaced())
    285             .foregroundStyle(.secondary)
    286         }
    287         .frame(maxWidth: .infinity)
    288         .accessibilityElement(children: .combine)
    289       }
    290       if let about = profile.about, !about.isEmpty {
    291         Section("About") { Text(about) }
    292       }
    293       Section("Nostr profile") {
    294         if let nip05 = profile.nip05 {
    295           LabeledContent("NIP-05", value: nip05)
    296         }
    297         if let website = safeHTTPS(profile.website) {
    298           Link(destination: website) {
    299             LabeledContent("Website", value: website.host ?? website.absoluteString)
    300           }
    301         }
    302         if let address = profile.lightningAddress {
    303           LabeledContent("Lightning", value: address)
    304         }
    305       }
    306     }
    307     .navigationTitle("Profile")
    308     .toolbar { TeraAuthorVisibilityMenu(author: profile.authorPublicKey) }
    309     .navigationBarTitleDisplayMode(.inline)
    310     .accessibilityIdentifier("radroots.profile.\(profile.authorPublicKey)")
    311   }
    312 
    313   private var abbreviatedPublicKey: String {
    314     guard profile.authorPublicKey.count > 16 else { return profile.authorPublicKey }
    315     return "\(profile.authorPublicKey.prefix(8))…\(profile.authorPublicKey.suffix(8))"
    316   }
    317 
    318   private func safeHTTPS(_ value: String?) -> URL? {
    319     guard let value,
    320       let url = URL(string: value),
    321       url.scheme?.lowercased() == "https",
    322       url.host != nil,
    323       url.user == nil,
    324       url.password == nil
    325     else {
    326       return nil
    327     }
    328     return url
    329   }
    330 }
    331 
    332 struct TeraStableAvatarView: View {
    333   let profile: TeraProfileSummary
    334   let context: TeraLocalNetwork?
    335   @ObservedObject var mediaStore: TeraMediaStore
    336   let size: CGFloat
    337 
    338   var body: some View {
    339     Group {
    340       if let picture = profile.picture {
    341         TeraLocalMediaContent(media: picture, context: context, store: mediaStore)
    342       } else {
    343         fallback
    344       }
    345     }
    346     .frame(width: size, height: size)
    347     .background(.quaternary)
    348     .clipShape(Circle())
    349   }
    350 
    351   private var fallback: some View {
    352     let identity = TeraStableVisualIdentity(publicKeyHex: profile.authorPublicKey)
    353     return ZStack {
    354       Circle().fill(Self.palette[identity.paletteIndex])
    355       Text(String(profile.preferredName.prefix(1)).uppercased())
    356         .font(.system(size: size * 0.4, weight: .semibold, design: .rounded))
    357         .foregroundStyle(.white)
    358     }
    359     .accessibilityHidden(true)
    360   }
    361 
    362   private static let palette: [Color] = [
    363     .indigo, .mint, .orange, .purple, .teal, .pink,
    364     .blue, .green, .red, .cyan, .brown, .yellow,
    365   ]
    366 }