Jotley / app · jotley.ai / iOS / Views / iOSNotesListView.swift
| 1 | import SwiftUI |
| 2 | import CoreData |
| 3 | import UIKit |
| 4 | |
| 5 | struct NotesListView: View { |
| 6 | let onSelectNote: (UUID) -> Void |
| 7 | let showsNewTranscriptionButton: Bool |
| 8 | let usesSplitView: Bool |
| 9 | var bottomContentInset: CGFloat = 0 |
| 10 | @Binding var showingSettings: Bool |
| 11 | @Binding var selectedNoteId: UUID? |
| 12 | @Binding var showingTranscriptionModal: Bool |
| 13 | @Binding var columnVisibility: NavigationSplitViewVisibility |
| 14 | |
| 15 | @EnvironmentObject var persistenceController: SharedPersistenceController |
| 16 | @EnvironmentObject var transcriptionManager: LiveTranscriptionManager |
| 17 | @ObservedObject var themeManager = ThemeManager.shared |
| 18 | @FetchRequest( |
| 19 | sortDescriptors: [NSSortDescriptor(keyPath: \Note.timestamp, ascending: false)], |
| 20 | predicate: SharedPersistenceController.activeNotesPredicate, |
| 21 | animation: .default |
| 22 | ) |
| 23 | var notes: FetchedResults<Note> |
| 24 | |
| 25 | @State var searchText = "" |
| 26 | @State var isSearchFocused = false |
| 27 | @State var showingTagFilter = false |
| 28 | @StateObject var filterState = FilterAndSortState() |
| 29 | @State var showingFolderManagement = false |
| 30 | @State var showingFolderDrawer = false |
| 31 | @State private var showingTrash = false |
| 32 | @Binding var newTranscriptionNote: Note? |
| 33 | @Environment(\.colorScheme) var colorScheme |
| 34 | |
| 35 | |
| 36 | var body: some View { |
| 37 | let isSearching = isSearchFocused || !searchText.isEmpty |
| 38 | ZStack { |
| 39 | VStack(spacing: 0) { |
| 40 | if usesIPadLayout && usesSplitView && columnVisibility != .detailOnly { |
| 41 | // Keep the control out of the sidebar's system toolbar, |
| 42 | // which adds its own glass treatment to custom buttons. |
| 43 | JotleySidebarToggle(visibility: $columnVisibility) |
| 44 | .frame(height: 54) |
| 45 | .frame(maxWidth: .infinity, alignment: .trailing) |
| 46 | .padding(.trailing, 10) |
| 47 | .background(Color.themeBackground) |
| 48 | } |
| 49 | // Settings stays in the footer on every mobile layout. |
| 50 | HStack(spacing: 12) { |
| 51 | // Search field |
| 52 | HStack(spacing: 8) { |
| 53 | Image(systemName: "magnifyingglass") |
| 54 | .foregroundColor(Color(white: 0.5)) |
| 55 | LocalSearchField(text: $searchText, focused: $isSearchFocused) |
| 56 | .frame(height: 44) |
| 57 | |
| 58 | // Clear button |
| 59 | if !searchText.isEmpty { |
| 60 | Button(action: { searchText = "" }) { |
| 61 | Image(systemName: "xmark.circle.fill") |
| 62 | .foregroundColor(Color(white: 0.5)) |
| 63 | } |
| 64 | .accessibilityIdentifier("clearSearchButton") |
| 65 | } |
| 66 | } |
| 67 | .padding(.horizontal, 12) |
| 68 | .frame(height: 44) |
| 69 | .background(Color.themeContentBackground) |
| 70 | .cornerRadius(10) |
| 71 | |
| 72 | // Cancel button — only while searching |
| 73 | if isSearching { |
| 74 | Button("Cancel") { |
| 75 | withAnimation(.easeInOut(duration: 0.2)) { |
| 76 | searchText = "" |
| 77 | isSearchFocused = false |
| 78 | } |
| 79 | } |
| 80 | .jotleyDismissButtonStyle() |
| 81 | .accessibilityIdentifier("cancelSearchButton") |
| 82 | .transition(.opacity.combined(with: .scale)) |
| 83 | } |
| 84 | |
| 85 | // Folder drawer button |
| 86 | if !isSearching { |
| 87 | Button(action: { showingFolderDrawer = true }) { |
| 88 | Image(systemName: filterState.selectedFolder == .all ? "folder" : "folder.fill") |
| 89 | .font(.system(size: 20, weight: .medium)) |
| 90 | } |
| 91 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 92 | .accessibilityLabel("Folders") |
| 93 | .accessibilityIdentifier("notesListFolderDrawerButton") |
| 94 | .transition(.opacity.combined(with: .scale)) |
| 95 | } |
| 96 | |
| 97 | // Filter & sort button |
| 98 | if !isSearching { |
| 99 | Button(action: { showingTagFilter = true }) { |
| 100 | Image(systemName: filterState.isActive ? "line.3.horizontal.decrease.circle.fill" : "line.3.horizontal.decrease.circle") |
| 101 | .font(.system(size: 20, weight: .medium)) |
| 102 | } |
| 103 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 104 | .accessibilityLabel("Filter and sort notes") |
| 105 | .accessibilityIdentifier("notesListTagFilterButton") |
| 106 | .transition(.opacity.combined(with: .scale)) |
| 107 | .sheet(isPresented: $showingTagFilter) { |
| 108 | FilterAndSortPopoverView( |
| 109 | state: filterState, |
| 110 | allTags: getAllUniqueTags(), |
| 111 | tagCounts: getTagCountsForVisibleResults(), |
| 112 | onDone: { showingTagFilter = false } |
| 113 | ) |
| 114 | .presentationDetents([.large]) |
| 115 | .presentationDragIndicator(.visible) |
| 116 | .presentationBackground(Color.themeContentBackground) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | .animation(.easeInOut(duration: 0.2), value: isSearching) |
| 121 | // Align the custom header with the unfolded iPhone's status row. |
| 122 | // The search, folder and filter controls move together. |
| 123 | .padding(.top, !showsNewTranscriptionButton && !usesIPadLayout ? 26 : 8) |
| 124 | .padding(.bottom, 12) |
| 125 | // The glyph has ten points inside its 44-point touch target. |
| 126 | // A smaller phone inset aligns it with the notes below. |
| 127 | .padding(.leading, usesIPadLayout ? 16 : 8) |
| 128 | .padding(.trailing, 16) |
| 129 | .background(Color.themeBackground) |
| 130 | |
| 131 | // Combined folder + tag scope indicator (single scrollable row, |
| 132 | // folder pill first when present, then one pill per selected tag). |
| 133 | if filterState.selectedFolder != .all || !filterState.selectedTags.isEmpty { |
| 134 | iOSCurrentScopeIndicator( |
| 135 | filterState: filterState, |
| 136 | onFolderTap: { showingFolderDrawer = true }, |
| 137 | onTagTap: { showingTagFilter = true } |
| 138 | ) |
| 139 | .padding(.bottom, 8) |
| 140 | .background(Color.themeBackground) |
| 141 | } |
| 142 | |
| 143 | List { |
| 144 | // Title (scrolls with list) |
| 145 | Section { |
| 146 | Text("Notes") |
| 147 | .font(.system(size: 34, weight: .bold)) |
| 148 | .foregroundColor(Color.themeTextPrimary) |
| 149 | .frame(maxWidth: .infinity, alignment: .leading) |
| 150 | .padding(.top, 4) |
| 151 | .accessibilityIdentifier("notesListTitle") |
| 152 | } |
| 153 | .listRowBackground(Color.themeBackground) |
| 154 | .listRowSeparator(.hidden) |
| 155 | .listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)) |
| 156 | if transcriptionManager.transcriptionState != .idle { |
| 157 | Section { |
| 158 | Button { |
| 159 | showingTranscriptionModal = true |
| 160 | } label: { |
| 161 | ActiveTranscriptionJumpRowView( |
| 162 | recorder: transcriptionManager |
| 163 | ) |
| 164 | .contentShape(Rectangle()) |
| 165 | } |
| 166 | .buttonStyle(.plain) |
| 167 | .accessibilityIdentifier("activeRecordingJumpRow") |
| 168 | } |
| 169 | .listRowBackground(Color.clear) |
| 170 | .listRowSeparator(.hidden) |
| 171 | .listRowInsets(EdgeInsets(top: 2, leading: 16, bottom: 8, trailing: 16)) |
| 172 | } |
| 173 | |
| 174 | switch currentEmptyState { |
| 175 | case .noSearchResults: |
| 176 | Section { |
| 177 | ContentUnavailableView { |
| 178 | Label("No Results for \"\(searchText)\"", systemImage: "magnifyingglass") |
| 179 | } description: { |
| 180 | Text("Try searching with different keywords") |
| 181 | } |
| 182 | .foregroundColor(Color.themeTextPrimary) |
| 183 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 184 | .padding(.vertical, 36) |
| 185 | } |
| 186 | .listRowBackground(Color.themeBackground) |
| 187 | .listRowSeparator(.hidden) |
| 188 | case .noNotesYet: |
| 189 | Section { |
| 190 | if usesSplitView { |
| 191 | iOSNotesListEmptyStateView() |
| 192 | } else { |
| 193 | JotleyLibraryWelcome(canStart: transcriptionManager.canStart || transcriptionManager.isTranscribing, |
| 194 | onStart: handleCreateNewNoteTapped) |
| 195 | .padding(.top, 34) |
| 196 | .accessibilityElement(children: .contain) |
| 197 | .accessibilityIdentifier("notesListEmptyState") |
| 198 | } |
| 199 | } |
| 200 | .listRowBackground(Color.themeBackground) |
| 201 | .listRowSeparator(.hidden) |
| 202 | .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)) |
| 203 | case .noNotesInFolder: |
| 204 | Section { |
| 205 | iOSNoNotesInFolderView() |
| 206 | } |
| 207 | .listRowBackground(Color.themeBackground) |
| 208 | .listRowSeparator(.hidden) |
| 209 | .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)) |
| 210 | case .filtersHidingNotes: |
| 211 | Section { |
| 212 | iOSFiltersHidingNotesView(filterState: filterState) |
| 213 | } |
| 214 | .listRowBackground(Color.themeBackground) |
| 215 | .listRowSeparator(.hidden) |
| 216 | .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)) |
| 217 | case .none: |
| 218 | ForEach(groupedItems, id: \.0) { date, itemsInGroup in |
| 219 | Section { |
| 220 | if usesInlineDateHeaders { |
| 221 | dateSectionHeader(date) |
| 222 | .listRowBackground(Color.themeBackground) |
| 223 | .listRowSeparator(.hidden) |
| 224 | .listRowInsets(EdgeInsets()) |
| 225 | } |
| 226 | ForEach(itemsInGroup) { item in |
| 227 | switch item { |
| 228 | case .note(let note): |
| 229 | let rowIsSelected = usesSplitView && note.id != nil && selectedNoteId == note.id |
| 230 | Button { |
| 231 | // A note synced from another device (or a legacy note) can |
| 232 | // arrive without an `id`. Guaranteeing one here is what makes |
| 233 | // the row openable — the old `if let note.id` silently dropped |
| 234 | // the tap, so the note looked unselectable. |
| 235 | let noteId = ensureStableNoteID(for: note) |
| 236 | onSelectNote(noteId) |
| 237 | } label: { |
| 238 | NoteRowView(note: note, isSelected: rowIsSelected) |
| 239 | .contentShape(Rectangle()) |
| 240 | } |
| 241 | .buttonStyle(.plain) |
| 242 | .accessibilityAddTraits(rowIsSelected ? .isSelected : []) |
| 243 | .accessibilityIdentifier("notesListNoteRow-\(note.id?.uuidString ?? "unknown")") |
| 244 | .listRowBackground(Color.clear) |
| 245 | .listRowSeparator(.hidden) |
| 246 | .listRowInsets(EdgeInsets(top: 2, leading: 16, bottom: 2, trailing: 16)) |
| 247 | .swipeActions(edge: .trailing) { |
| 248 | Button(role: .destructive) { |
| 249 | if let index = itemsInGroup.firstIndex(of: item) { |
| 250 | deleteItemsInGroup(at: IndexSet(integer: index), from: itemsInGroup) |
| 251 | } |
| 252 | } label: { |
| 253 | Label("Move to Trash", systemImage: "trash") |
| 254 | } |
| 255 | // Keep the destructive fill independent of the list's readable text tint. |
| 256 | .tint(.red) |
| 257 | } |
| 258 | |
| 259 | |
| 260 | } |
| 261 | } |
| 262 | } header: { |
| 263 | if !usesInlineDateHeaders { |
| 264 | dateSectionHeader(date) |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | Section { |
| 270 | NotesTrashLink(showingTrash: $showingTrash) |
| 271 | } |
| 272 | .listRowBackground(Color.themeBackground) |
| 273 | .listRowSeparator(.hidden) |
| 274 | .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 4, trailing: 16)) |
| 275 | } |
| 276 | .listStyle(PlainListStyle()) |
| 277 | .scrollContentBackground(.hidden) |
| 278 | .scrollDismissesKeyboard(.interactively) |
| 279 | .contentMargins(.bottom, max(bottomContentInset, 84), for: .scrollContent) |
| 280 | .background(Color.themeBackground) |
| 281 | .navigationBarTitleDisplayMode(.inline) |
| 282 | } // End of VStack |
| 283 | |
| 284 | // Floating bottom controls |
| 285 | VStack { |
| 286 | Spacer() |
| 287 | HStack(alignment: .center) { |
| 288 | settingsButton.padding(.leading, 20) |
| 289 | Spacer() |
| 290 | if showsNewTranscriptionButton { |
| 291 | NotesNewTranscriptionButton(action: handleCreateNewNoteTapped) |
| 292 | .padding(.trailing, 20) |
| 293 | } |
| 294 | } |
| 295 | .padding(.bottom, usesIPadLayout ? MobileNoteFooterInsets.splitBottom : MobileNoteFooterInsets.phoneBottom) |
| 296 | } |
| 297 | // Match the phone capture control's window-bottom placement. |
| 298 | .ignoresSafeArea(.container, edges: usesIPadLayout ? [] : .bottom) |
| 299 | .ignoresSafeArea(.keyboard, edges: .bottom) |
| 300 | } |
| 301 | // The custom header occupies the status/toolbar row on the inner display. |
| 302 | // Otherwise the portrait drawer reserves that row again above the gear. |
| 303 | .ignoresSafeArea(.container, edges: usesSplitView && !usesIPadLayout ? .top : []) |
| 304 | // Apply to the sidebar root, including its custom header. |
| 305 | .toolbar(.hidden, for: .navigationBar) |
| 306 | .sheet(isPresented: $showingTrash) { |
| 307 | NotesTrashView(store: persistenceController) |
| 308 | } |
| 309 | .sheet(isPresented: $showingFolderManagement) { |
| 310 | FolderManagementSheet() |
| 311 | } |
| 312 | .sheet(isPresented: $showingFolderDrawer) { |
| 313 | iOSFolderDrawer(filterState: filterState, showingFolderManagement: $showingFolderManagement, |
| 314 | onDone: { showingFolderDrawer = false }) |
| 315 | .presentationDetents([.medium, .large]) |
| 316 | .presentationDragIndicator(.visible) |
| 317 | .presentationBackground(Color.themeBackground) |
| 318 | } |
| 319 | .observeThemeChanges() |
| 320 | .tint(.themeActionText) |
| 321 | } |
| 322 | // UIKit's pinned List section-header material can remain over earlier rows |
| 323 | // in Designed for iPad on Mac. Plain date rows avoid that material layer; |
| 324 | // keep native sticky headers on iPhone and iPad. |
| 325 | private var usesInlineDateHeaders: Bool { |
| 326 | #if DEBUG && targetEnvironment(simulator) |
| 327 | if ProcessInfo.processInfo.environment["JOTLEY_TEST_INLINE_NOTE_HEADERS"] == "1" { return true } |
| 328 | #endif |
| 329 | return ProcessInfo.processInfo.isiOSAppOnMac |
| 330 | } |
| 331 | |
| 332 | private func dateSectionHeader(_ date: Date) -> some View { |
| 333 | HStack { |
| 334 | Text(formatDateSectionHeader(date)) |
| 335 | .font(.subheadline) |
| 336 | .fontWeight(.medium) |
| 337 | .foregroundColor(Color(white: 0.5)) |
| 338 | .textCase(nil) |
| 339 | Spacer() |
| 340 | } |
| 341 | .padding(.horizontal) |
| 342 | .padding(.vertical, 8) |
| 343 | .frame(maxWidth: .infinity) |
| 344 | .background(Color.themeBackground) |
| 345 | .listRowInsets(EdgeInsets()) |
| 346 | } |
| 347 | |
| 348 | // Hardware idiom is intentional: a narrow iPad window still has window |
| 349 | // controls, even when its horizontal size class becomes compact. |
| 350 | private var usesIPadLayout: Bool { UIDevice.current.userInterfaceIdiom == .pad } |
| 351 | |
| 352 | private var settingsButton: some View { |
| 353 | Button(action: { showingSettings = true }) { |
| 354 | Image(systemName: "gearshape") |
| 355 | .font(.system(size: 24, weight: .medium)) |
| 356 | .frame(width: 44, height: 44) |
| 357 | } |
| 358 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 359 | .accessibilityLabel("Settings") |
| 360 | .accessibilityIdentifier("notesListSettingsButton") |
| 361 | } |
| 362 | |
| 363 | func ensureStableNoteID(for note: Note) -> UUID { |
| 364 | if let id = note.id { return id } |
| 365 | let id = UUID(); note.id = id; transcriptionManager.saveEdits(); return id |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | private struct LocalSearchField: UIViewRepresentable { |
| 371 | @Binding var text: String |
| 372 | @Binding var focused: Bool |
| 373 | final class Field: UITextField { |
| 374 | override var intrinsicContentSize: CGSize { |
| 375 | let size = super.intrinsicContentSize |
| 376 | return CGSize(width: size.width, height: max(44, size.height)) |
| 377 | } |
| 378 | } |
| 379 | final class Coordinator: NSObject, UITextFieldDelegate { |
| 380 | var owner: LocalSearchField |
| 381 | init(_ owner: LocalSearchField) { self.owner = owner } |
| 382 | @objc func changed(_ field: UITextField) { owner.text = field.text ?? "" } |
| 383 | func textFieldDidBeginEditing(_ field: UITextField) { if !owner.focused { owner.focused = true } } |
| 384 | func textFieldDidEndEditing(_ field: UITextField) { if owner.focused { owner.focused = false } } |
| 385 | func textFieldShouldReturn(_ field: UITextField) -> Bool { field.resignFirstResponder(); return true } |
| 386 | } |
| 387 | func makeCoordinator() -> Coordinator { Coordinator(self) } |
| 388 | func makeUIView(context: Context) -> Field { |
| 389 | let field = Field() |
| 390 | field.placeholder = String(localized: "Search notes") |
| 391 | field.accessibilityIdentifier = "notesListSearchField" |
| 392 | field.font = .preferredFont(forTextStyle: .body) |
| 393 | field.adjustsFontForContentSizeCategory = true |
| 394 | field.returnKeyType = .search |
| 395 | field.autocorrectionType = .no |
| 396 | field.delegate = context.coordinator |
| 397 | field.addTarget(context.coordinator, action: #selector(Coordinator.changed(_:)), for: .editingChanged) |
| 398 | field.setContentHuggingPriority(.defaultLow, for: .horizontal) |
| 399 | field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) |
| 400 | return field |
| 401 | } |
| 402 | func updateUIView(_ field: Field, context: Context) { |
| 403 | context.coordinator.owner = self |
| 404 | if field.text != text { field.text = text } |
| 405 | field.textColor = UIColor(Color.themeTextPrimary) |
| 406 | if !focused && field.isFirstResponder { field.resignFirstResponder() } |
| 407 | } |
| 408 | } |