Branchwell

Jobley / app · jobley.ai / Apps / iPhone / PhoneApp.swift

1import SwiftUI
2import JobleyKit
3import JobleyUI
4
5@main
6struct JobleyPhoneApp: App {
7 @StateObject private var app = PhoneModel()
8
9 var body: some Scene {
10 WindowGroup {
11 PhoneRoot(app: app, curator: app.curator, board: app.board)
12 .tint(Theme.accent)
13 }
14 }
15}
16
17/// Everyone gets the board, saved jobs, "Put me forward" and "Post a job". Curators and moderators
18/// sign in with a passkey in Settings to get the Review tab: the same queue as the Mac desk.
19@MainActor
20final class PhoneModel: ObservableObject {
21 enum Tab: String { case jobs, saved, post, review, settings }
22
23 let board: BoardModel
24 let curator: CuratorModel
25 let saved = SavedJobs()
26 @Published var tab: Tab = .jobs
27
28 init() {
29 board = BoardModel(api: APIClient(client: .iphone))
30 curator = CuratorModel(api: APIClient(client: .iphone, token: TokenStore.load()))
31 #if DEBUG
32 if let start = ProcessInfo.processInfo.environment["JOBLEY_TAB"], let tab = Tab(rawValue: start) { self.tab = tab }
33 #endif
34 }
35}
36
37struct PhoneRoot: View {
38 @ObservedObject var app: PhoneModel
39 @ObservedObject var curator: CuratorModel
40 @ObservedObject var board: BoardModel
41
42 var body: some View {
43 TabView(selection: $app.tab) {
44 Tab("Jobs", systemImage: "list.bullet.rectangle", value: PhoneModel.Tab.jobs) {
45 NavigationStack { BoardScreen(board: board, saved: app.saved) }
46 }
47 Tab("Saved", systemImage: "bookmark", value: PhoneModel.Tab.saved) {
48 NavigationStack { SavedScreen(board: board, saved: app.saved) }
49 }
50 Tab("Post", systemImage: "square.and.pencil", value: PhoneModel.Tab.post) {
51 NavigationStack { PostJobForm(api: board.api) { app.tab = .jobs }.navigationTitle("Post a job") }
52 }
53 if curator.isSignedIn {
54 Tab("Review", systemImage: "tray.full", value: PhoneModel.Tab.review) {
55 NavigationStack { ReviewScreen(curator: curator, labels: board.labels) }
56 }
57 .badge(curator.stats?.jobs(.pending) ?? 0)
58 }
59 Tab("Settings", systemImage: "gearshape", value: PhoneModel.Tab.settings) {
60 NavigationStack { PhoneSettings(curator: curator) }
61 }
62 }
63 .task {
64 await board.loadMeta()
65 await board.reload()
66 await curator.start()
67 }
68 }
69}
70
71struct BoardScreen: View {
72 @ObservedObject var board: BoardModel
73 @ObservedObject var saved: SavedJobs
74
75 var body: some View {
76 List {
77 Section {
78 ScrollView(.horizontal, showsIndicators: false) { BoardFilters(board: board) }
79 .listRowInsets(EdgeInsets(top: 4, leading: 16, bottom: 4, trailing: 16))
80 .listRowBackground(Color.clear)
81 }
82 if let error = board.error, board.jobs.isEmpty {
83 StateMessage("Couldn't load the board", detail: error, systemImage: "wifi.slash") { Task { await board.reload() } }
84 .listRowBackground(Color.clear)
85 } else if board.jobs.isEmpty && !board.loading {
86 StateMessage(board.hasFilters ? "Nothing matches" : "No jobs yet",
87 detail: board.hasFilters ? "Try fewer filters." : "Every job here is read by a person first. Check back soon.",
88 systemImage: "magnifyingglass")
89 .listRowBackground(Color.clear)
90 } else {
91 Section {
92 ForEach(board.jobs) { job in
93 NavigationLink(value: job) { JobRow(job: job, labels: board.labels, saved: saved.contains(job)) }
94 }
95 if board.nextCursor != nil {
96 Button("Load more") { Task { await board.loadMore() } }.frame(maxWidth: .infinity)
97 }
98 } header: {
99 Text("\(board.jobs.count) on the board · newest first").font(Theme.mono(11))
100 }
101 }
102 }
103 .listStyle(.insetGrouped)
104 .scrollContentBackground(.hidden)
105 .background(Theme.background)
106 .navigationTitle("Good jobs")
107 .searchable(text: $board.query, prompt: "Title, company or place")
108 .onSubmit(of: .search) { Task { await board.reload() } }
109 .onChange(of: board.query) { if board.query.isEmpty { Task { await board.reload() } } }
110 .refreshable { await board.reload() }
111 .navigationDestination(for: Job.self) { JobScreen(job: $0, board: board, saved: saved) }
112 .overlay { if board.loading && board.jobs.isEmpty { ProgressView() } }
113 #if DEBUG
114 .task {
115 // Screenshot hooks: open the first job, or its "Put me forward" form.
116 guard let mode = ProcessInfo.processInfo.environment["JOBLEY_OPEN"] else { return }
117 while board.jobs.isEmpty { try? await Task.sleep(for: .milliseconds(200)) }
118 DebugOpen.shared.mode = mode
119 debugJob = board.jobs.first
120 }
121 .navigationDestination(item: $debugJob) { JobScreen(job: $0, board: board, saved: saved) }
122 #endif
123 }
124
125 #if DEBUG
126 @State private var debugJob: Job?
127 #endif
128}
129
130#if DEBUG
131@MainActor
132final class DebugOpen {
133 static let shared = DebugOpen()
134 var mode = ""
135}
136#endif
137
138struct JobScreen: View {
139 let job: Job
140 @ObservedObject var board: BoardModel
141 @ObservedObject var saved: SavedJobs
142 @State private var puttingForward = false
143
144 var body: some View {
145 ScrollView {
146 VStack(alignment: .leading, spacing: 20) {
147 JobBody(job: job, labels: board.labels)
148 }
149 .padding(20)
150 }
151 .background(Theme.background)
152 .safeAreaInset(edge: .bottom) {
153 HStack(spacing: 12) {
154 Button { saved.toggle(job) } label: {
155 Label(saved.contains(job) ? "Saved" : "Save", systemImage: saved.contains(job) ? "bookmark.fill" : "bookmark")
156 }
157 .buttonStyle(SecondaryButtonStyle())
158 Button { puttingForward = true } label: { Text("Put me forward").frame(maxWidth: .infinity) }
159 .buttonStyle(PrimaryButtonStyle())
160 .accessibilityIdentifier("putMeForward")
161 }
162 .padding(.horizontal, 20).padding(.vertical, 12)
163 .background(.bar)
164 }
165 .toolbar { ShareLink(item: URL(string: job.url) ?? AppLinks.website) }
166 .navigationBarTitleDisplayMode(.inline)
167 .sheet(isPresented: $puttingForward) {
168 NavigationStack {
169 PutMeForwardForm(api: board.api, job: job) { puttingForward = false }
170 .navigationTitle("Put me forward")
171 .navigationBarTitleDisplayMode(.inline)
172 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { puttingForward = false } } }
173 }
174 }
175 #if DEBUG
176 .onAppear { if DebugOpen.shared.mode == "forward" { DebugOpen.shared.mode = ""; puttingForward = true } }
177 #endif
178 }
179}
180
181struct SavedScreen: View {
182 @ObservedObject var board: BoardModel
183 @ObservedObject var saved: SavedJobs
184 @State private var askingAnyJob = false
185
186 var body: some View {
187 List {
188 if saved.jobs.isEmpty {
189 StateMessage("Nothing saved", detail: "Tap Save on a job to keep it here, on this iPhone.", systemImage: "bookmark")
190 .listRowBackground(Color.clear)
191 }
192 ForEach(saved.jobs) { job in
193 NavigationLink(value: job) { JobRow(job: job, labels: board.labels, saved: true) }
194 }
195 Section {
196 Button { askingAnyJob = true } label: { Label("Put me forward for a kind of job", systemImage: "person.crop.circle.badge.plus") }
197 } footer: {
198 Text("Tell us what you're looking for. When a good job fits, a person introduces you.")
199 }
200 }
201 .scrollContentBackground(.hidden)
202 .background(Theme.background)
203 .navigationTitle("Saved")
204 .navigationDestination(for: Job.self) { JobScreen(job: $0, board: board, saved: saved) }
205 .sheet(isPresented: $askingAnyJob) {
206 NavigationStack {
207 PutMeForwardForm(api: board.api, job: nil) { askingAnyJob = false }
208 .navigationTitle("Put me forward")
209 .navigationBarTitleDisplayMode(.inline)
210 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { askingAnyJob = false } } }
211 }
212 }
213 }
214}
215
216/// The same review queue as the Mac desk, to decide on the go.
217struct ReviewScreen: View {
218 @ObservedObject var curator: CuratorModel
219 let labels: Labels
220 @State private var queue: CuratorModel.Queue = .pending
221
222 var body: some View {
223 List {
224 Picker("Queue", selection: $queue) {
225 Text("Waiting").tag(CuratorModel.Queue.pending)
226 Text("Approved").tag(CuratorModel.Queue.approved)
227 Text("Published").tag(CuratorModel.Queue.published)
228 Text("Sent back").tag(CuratorModel.Queue.rejected)
229 }
230 .pickerStyle(.segmented)
231 .listRowBackground(Color.clear)
232 let jobs = curator.jobs(queue)
233 if jobs.isEmpty {
234 StateMessage("Nothing here", detail: queue == .pending ? "Every post has been read." : "Posts move here as people decide.", systemImage: queue.systemImage)
235 .listRowBackground(Color.clear)
236 }
237 Section {
238 ForEach(jobs) { job in
239 NavigationLink(value: job.id) { JobRow(job: job, labels: labels, showStatus: queue != .pending) }
240 }
241 } footer: {
242 if queue == .pending && !jobs.isEmpty { Text("Oldest first. A person reads every one.") }
243 }
244 }
245 .scrollContentBackground(.hidden)
246 .background(Theme.background)
247 .navigationTitle("Review")
248 .refreshable { await curator.refreshAll() }
249 .navigationDestination(for: String.self) { id in ReviewItemScreen(curator: curator, id: id, labels: labels) }
250 #if DEBUG
251 .task {
252 guard ProcessInfo.processInfo.environment["JOBLEY_OPEN"] == "review" else { return }
253 while curator.jobs(.pending).isEmpty { try? await Task.sleep(for: .milliseconds(200)) }
254 reviewDebugID = curator.jobs(.pending).first?.id
255 }
256 .navigationDestination(item: $reviewDebugID) { id in ReviewItemScreen(curator: curator, id: id, labels: labels) }
257 #endif
258 }
259
260 #if DEBUG
261 @State private var reviewDebugID: String?
262 #endif
263}
264
265struct ReviewItemScreen: View {
266 @ObservedObject var curator: CuratorModel
267 let id: String
268 let labels: Labels
269
270 var body: some View {
271 Group {
272 if let item = curator.item, item.job.id == id {
273 ReviewDetail(model: curator, item: item, labels: labels, allowsEdit: false)
274 } else {
275 ProgressView()
276 }
277 }
278 .navigationBarTitleDisplayMode(.inline)
279 .task { await curator.open(id) }
280 }
281}