Inkspring / app · inkspring.ai / native-mac / iPhone / MobileIdeas.swift
| 1 | import FoundationModels |
| 2 | import SwiftUI |
| 3 | |
| 4 | struct MobileIdeasView: View { |
| 5 | @Environment(\.mobileWideLayout) private var wideLayout |
| 6 | private enum Field { case title, angle } |
| 7 | |
| 8 | @Environment(\.scenePhase) private var scenePhase |
| 9 | @Environment(\.studioPalette) private var palette |
| 10 | @State private var projectID: String? |
| 11 | @FocusState private var focusedField: Field? |
| 12 | @State private var title = "" |
| 13 | @State private var includesPlan = false |
| 14 | @State private var bodyText = "" |
| 15 | @State private var suggestions: [PostIdeaText] = [] |
| 16 | @State private var availabilityMessage: String? |
| 17 | @State private var generating = false |
| 18 | @State private var message: String? |
| 19 | @State private var savedIdeaID: String? |
| 20 | let library: MobileLibrary |
| 21 | let isSelected: Bool |
| 22 | let onSaved: ((String) -> Void)? |
| 23 | @Environment(\.dismiss) private var dismiss |
| 24 | |
| 25 | init(library: MobileLibrary, isSelected: Bool, initialProject: String? = nil, onSaved: ((String) -> Void)? = nil) { |
| 26 | self.library = library |
| 27 | self.isSelected = isSelected |
| 28 | self.onSaved = onSaved |
| 29 | _projectID = State(initialValue: initialProject) |
| 30 | } |
| 31 | |
| 32 | var body: some View { |
| 33 | NavigationStack { |
| 34 | ideaForm |
| 35 | .frame(maxWidth: 680) |
| 36 | .mobileCanvas() |
| 37 | .mobilePageTitle(onSaved == nil ? "New ideas" : "Draft a new idea", |
| 38 | wide: wideLayout && onSaved == nil, background: palette.canvas) |
| 39 | .toolbar(wideLayout ? .hidden : .visible, for: .tabBar) |
| 40 | .toolbar { |
| 41 | if onSaved != nil { |
| 42 | ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } |
| 43 | } |
| 44 | } |
| 45 | .refreshable { |
| 46 | checkAvailability() |
| 47 | await library.refresh() |
| 48 | } |
| 49 | .navigationDestination(item: $savedIdeaID) { id in |
| 50 | if let snapshot = library.snapshot, let item = library.visibleItems.first(where: { $0.id == id }) { |
| 51 | MobileReviewDetail(item: item, library: library, workspaceID: snapshot.workspaceID) |
| 52 | } else if let idea = library.pendingIdeas.first(where: { $0.id.uuidString.lowercased() == id }) { |
| 53 | Form { |
| 54 | Section { |
| 55 | Text(idea.title).font(.headline) |
| 56 | MobileLastUpdated(date: idea.savedAt) |
| 57 | Text(idea.text).textSelection(.enabled) |
| 58 | } footer: { |
| 59 | Text("Saved on this device. Connect your Mac to create platform drafts from this idea.") |
| 60 | } |
| 61 | .listRowBackground(palette.card) |
| 62 | } |
| 63 | .scrollContentBackground(.hidden) |
| 64 | .mobileCanvas() |
| 65 | .navigationTitle("Idea") |
| 66 | } |
| 67 | } |
| 68 | .alert("Inkspring", isPresented: Binding(get: { message != nil }, set: { if !$0 { message = nil } })) { |
| 69 | Button("OK", role: .cancel) { message = nil } |
| 70 | } message: { |
| 71 | Text(message ?? "") |
| 72 | } |
| 73 | } |
| 74 | .onAppear(perform: checkAvailability) |
| 75 | .onChange(of: editorText) { suggestions = [] } |
| 76 | .modifier(InkspringRatingPrompt(isIdle: isSelected && focusedField == nil && !generating |
| 77 | && message == nil && !library.busy)) |
| 78 | .onChange(of: scenePhase) { _, phase in |
| 79 | if phase == .active { checkAvailability() } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private var ideaForm: some View { |
| 84 | ScrollViewReader { scroll in |
| 85 | Form { |
| 86 | captureForm |
| 87 | if !suggestions.isEmpty { |
| 88 | Section("Suggested ideas") { |
| 89 | ForEach(suggestions, id: \.self) { idea in |
| 90 | Button { |
| 91 | title = idea.title |
| 92 | bodyText = idea.body |
| 93 | focusedField = nil |
| 94 | suggestions = [] |
| 95 | withAnimation { scroll.scrollTo(Field.title, anchor: .top) } |
| 96 | } label: { |
| 97 | VStack(alignment: .leading, spacing: 5) { |
| 98 | Text(idea.title).font(.headline) |
| 99 | Text(idea.body).foregroundStyle(.secondary) |
| 100 | Label("Use this idea", systemImage: "arrow.up.left") |
| 101 | .font(.footnote).foregroundStyle(.tint) |
| 102 | } |
| 103 | .frame(maxWidth: .infinity, alignment: .leading) |
| 104 | .padding(.vertical, 5) |
| 105 | .contentShape(Rectangle()) |
| 106 | } |
| 107 | .buttonStyle(.plain) |
| 108 | .accessibilityIdentifier("idea.suggestion") |
| 109 | } |
| 110 | } |
| 111 | .listRowBackground(palette.card) |
| 112 | } |
| 113 | if onSaved == nil { recentlySaved } |
| 114 | } |
| 115 | .scrollContentBackground(.hidden) |
| 116 | .scrollDismissesKeyboard(.interactively) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @ViewBuilder private var recentlySaved: some View { |
| 121 | if !library.pendingIdeas.isEmpty { |
| 122 | Section("Recently saved") { |
| 123 | ForEach(library.pendingIdeas) { idea in |
| 124 | VStack(alignment: .leading, spacing: 6) { |
| 125 | Text(idea.title).font(.headline) |
| 126 | MobileLastUpdated(date: idea.savedAt) |
| 127 | Text(idea.text).textSelection(.enabled) |
| 128 | if let error = idea.error { |
| 129 | Label(error, systemImage: "exclamationmark.icloud") |
| 130 | } else if library.snapshot?.pending.contains(where: { $0.id == idea.id }) == true { |
| 131 | Label("Saved to iCloud", systemImage: "checkmark") |
| 132 | } else { |
| 133 | Label(library.snapshot == nil ? "Saved on this device" : "Saved · iCloud sync pending", systemImage: "icloud") |
| 134 | } |
| 135 | } |
| 136 | .font(.footnote) |
| 137 | .padding(.vertical, 3) |
| 138 | .swipeActions(edge: .trailing, allowsFullSwipe: false) { |
| 139 | Button("Skip", systemImage: "trash") { |
| 140 | do { try library.skipPendingIdea(idea) } |
| 141 | catch { message = error.localizedDescription } |
| 142 | }.tint(palette.iconAccent) |
| 143 | } |
| 144 | .contextMenu { |
| 145 | Button("Copy text", systemImage: "doc.on.doc") { UIPasteboard.general.string = idea.text } |
| 146 | Button("Skip idea", systemImage: "trash") { |
| 147 | do { try library.skipPendingIdea(idea) } |
| 148 | catch { message = error.localizedDescription } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | .listRowBackground(palette.card) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private var captureForm: some View { |
| 158 | Section { |
| 159 | if let projects = library.snapshot?.projects, projects.count > 1 { |
| 160 | Picker("Project", selection: $projectID) { |
| 161 | Text("Default project").tag(String?.none) |
| 162 | ForEach(projects) { project in |
| 163 | Text(project.name).tag(Optional(project.id)) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | TextField("Title", text: $title, prompt: Text("Give the idea a working title")) |
| 168 | .focused($focusedField, equals: .title) |
| 169 | .submitLabel(.next) |
| 170 | .onSubmit { focusedField = .angle } |
| 171 | .accessibilityIdentifier("idea.new.title") |
| 172 | .id(Field.title) |
| 173 | TextField("Angle or notes", text: $bodyText, |
| 174 | prompt: Text("What do you want to say, and why does it matter?"), axis: .vertical) |
| 175 | .lineLimit(3...8) |
| 176 | .focused($focusedField, equals: .angle) |
| 177 | .accessibilityIdentifier("idea.new.angle") |
| 178 | Button { |
| 179 | focusedField = nil |
| 180 | Task { await generate() } |
| 181 | } label: { |
| 182 | Label("Generate ideas", systemImage: "sparkles") |
| 183 | .frame(maxWidth: .infinity, alignment: .leading) |
| 184 | .contentShape(Rectangle()) |
| 185 | } |
| 186 | .buttonStyle(.plain) |
| 187 | .disabled(generating || availabilityMessage != nil || editorText.isEmpty) |
| 188 | if generating { ProgressView("Thinking on this device…") } |
| 189 | StudioProofreadAction(document: .init(title: title, body: bodyText), |
| 190 | unavailable: MobileProofreading.availabilityMessage, |
| 191 | begin: { focusedField = nil }, reviewInSheet: true, |
| 192 | check: MobileProofreading.check) { title = $0.title; bodyText = $0.body } |
| 193 | if library.snapshot?.supportsContentPlans == true { |
| 194 | Toggle("Include a plan", isOn: $includesPlan) |
| 195 | } |
| 196 | Button("Save idea", systemImage: "plus") { saveIdea() } |
| 197 | .disabled(!editorText.canSaveOnMobile) |
| 198 | } header: { |
| 199 | Text("Save an idea") |
| 200 | } footer: { |
| 201 | Text(availabilityMessage ?? "Apple Intelligence rewrites your notes on this device. Review and edit your selection before saving.") |
| 202 | } |
| 203 | .listRowBackground(palette.card) |
| 204 | } |
| 205 | |
| 206 | private var editorText: PostIdeaText { |
| 207 | PostIdeaText(title: title, body: bodyText) |
| 208 | } |
| 209 | |
| 210 | private func checkAvailability() { |
| 211 | #if DEBUG |
| 212 | if Self.usesSuggestionFixture { |
| 213 | availabilityMessage = nil |
| 214 | return |
| 215 | } |
| 216 | #endif |
| 217 | availabilityMessage = Self.generationAvailabilityMessage |
| 218 | } |
| 219 | |
| 220 | #if DEBUG |
| 221 | private static var usesSuggestionFixture: Bool { |
| 222 | ProcessInfo.processInfo.arguments.contains("--idea-suggestions-fixture") |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | static var generationAvailabilityMessage: String? { |
| 227 | if #available(iOS 26, *) { |
| 228 | switch SystemLanguageModel.default.availability { |
| 229 | case .available: return nil |
| 230 | case .unavailable(.deviceNotEligible): return "This device doesn’t support Apple Intelligence. You can still write and save ideas." |
| 231 | case .unavailable(.appleIntelligenceNotEnabled): return "Turn on Apple Intelligence in Settings to generate ideas." |
| 232 | case .unavailable(.modelNotReady): return "Apple Intelligence is getting ready. Try again when the model finishes downloading." |
| 233 | case .unavailable: return "Apple Intelligence is unavailable. You can still write and save ideas." |
| 234 | } |
| 235 | } else { |
| 236 | return "Idea generation needs Apple Intelligence on iOS or iPadOS 26 or later. You can still write and save ideas." |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private func generate() async { |
| 241 | checkAvailability() |
| 242 | guard #available(iOS 26, *), availabilityMessage == nil, !generating else { return } |
| 243 | let source = editorText |
| 244 | guard !source.isEmpty else { return } |
| 245 | suggestions = [] |
| 246 | generating = true |
| 247 | defer { generating = false } |
| 248 | do { |
| 249 | let generated = try await generateSuggestions(for: source) |
| 250 | guard editorText == source else { return } |
| 251 | suggestions = generated |
| 252 | if generated.isEmpty { |
| 253 | message = "No usable ideas came back. Try again or keep editing your notes." |
| 254 | } |
| 255 | } catch { |
| 256 | checkAvailability() |
| 257 | message = "Apple Intelligence couldn’t generate ideas from these notes. Try again or keep editing." |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | @available(iOS 26, *) |
| 262 | private func generateSuggestions(for source: PostIdeaText) async throws -> [PostIdeaText] { |
| 263 | #if DEBUG |
| 264 | if Self.usesSuggestionFixture { |
| 265 | return [ |
| 266 | PostIdeaText(title: "Private chats sent to the wrong people", body: source.body), |
| 267 | PostIdeaText(title: "Classic Facebook", body: "Classic Facebook. " + source.body) |
| 268 | ] |
| 269 | } |
| 270 | #endif |
| 271 | return try await PostIdeaWriting.onDeviceSuggestions(for: source) |
| 272 | } |
| 273 | |
| 274 | private func saveIdea() { |
| 275 | let idea = editorText |
| 276 | guard idea.canSaveOnMobile else { return } |
| 277 | do { |
| 278 | let id = try library.saveIdea(title: idea.title, text: idea.body, |
| 279 | projectID: projectID, plan: includesPlan ? ContentPlan.starter(idea.title) : nil) |
| 280 | focusedField = nil |
| 281 | title = "" |
| 282 | bodyText = "" |
| 283 | includesPlan = false |
| 284 | InkspringReview.recordActivity() |
| 285 | if let onSaved { onSaved(id) } |
| 286 | else { savedIdeaID = id } |
| 287 | } catch { message = error.localizedDescription } |
| 288 | } |
| 289 | } |