krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v2.15.0: Hutch/Views/More/MoreView.swift · raw

 1import SwiftUI
 2
 3struct MoreView: View {
 4    @Environment(AppState.self) private var appState
 5
 6    private let unsupportedLinks: [(title: String, url: URL)] = [
 7        ("chat.sr.ht", SRHTWebURL.chat)
 8    ]
 9
10    @State private var viewModel: MoreViewModel?
11    @State private var showAccountSwitcher = false
12
13    var body: some View {
14        List {
15            Section("Search") {
16                NavigationLink(value: MoreRoute.lookup) {
17                    Label("Look Up", systemImage: "magnifyingglass")
18                }
19            }
20
21            Section("Other Services") {
22                NavigationLink(value: MoreRoute.lists) {
23                    Label("Mailing Lists", systemImage: "list.bullet.rectangle")
24                }
25
26                NavigationLink(value: MoreRoute.manPageBrowser) {
27                    Label("Man Pages", systemImage: "book")
28                }
29
30                NavigationLink(value: MoreRoute.pastes) {
31                    Label("Pastes", systemImage: "doc.on.clipboard")
32                }
33
34                NavigationLink(value: MoreRoute.systemStatus) {
35                    SystemStatusSummaryRow(
36                        snapshot: viewModel?.systemStatusSnapshot,
37                        isLoading: viewModel?.isLoadingSystemStatus ?? true,
38                        errorMessage: viewModel?.systemStatusErrorMessage,
39                        isShowingStaleData: viewModel?.isShowingStaleSystemStatus ?? false
40                    )
41                }
42            }
43
44            Section("Meta") {
45                NavigationLink(value: MoreRoute.profile) {
46                    Label("Profile", systemImage: "person.text.rectangle")
47                }
48
49                NavigationLink(value: MoreRoute.settings) {
50                    Label("Settings", systemImage: "gear")
51                }
52            }
53
54            Section {
55                ForEach(unsupportedLinks, id: \.title) { item in
56                    Link(destination: item.url) {
57                        Label(item.title, systemImage: "safari")
58                    }
59                }
60            } header: {
61                Text("External Links")
62            } footer: {
63                Text("These SourceHut services are not supported in-app, as the SourceHut API does not support them, and will open in your browser.")
64            }
65        }
66        .navigationTitle("More")
67        .refreshable {
68            await ensureViewModel().loadSystemStatus(forceRefresh: true)
69        }
70        .task {
71            await ensureViewModel().loadIfNeeded()
72        }
73        .toolbar {
74            ToolbarItem(placement: .topBarTrailing) {
75                Button {
76                    showAccountSwitcher = true
77                } label: {
78                    Image(systemName: "person.crop.circle.badge.plus")
79                }
80            }
81        }
82        .sheet(isPresented: $showAccountSwitcher) {
83            AccountSwitcherView()
84        }
85    }
86
87    @MainActor
88    private func ensureViewModel() -> MoreViewModel {
89        if let viewModel {
90            return viewModel
91        }
92
93        let newViewModel = MoreViewModel(repository: appState.systemStatusRepository)
94        viewModel = newViewModel
95        return newViewModel
96    }
97}