Branchwell

Jotley / app · jotley.ai / iOS / Views / iOSNotesListView+Data.swift

Raw 5.7 KB on c22c50f3614b055549c1ecbf54af0165e0bc8df1
1import SwiftUI
2import CoreData
3
4extension NotesListView {
5 func noteMatchesSearch(_ note: Note) -> Bool {
6 let search = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
7 if search.isEmpty {
8 return true
9 }
10 return (note.title?.localizedCaseInsensitiveContains(search) ?? false) ||
11 (note.summary?.localizedCaseInsensitiveContains(search) ?? false) ||
12 (note.tagsAsString?.localizedCaseInsensitiveContains(search) ?? false)
13 }
14
15 func noteMatchesFolder(_ note: Note) -> Bool {
16 switch filterState.selectedFolder {
17 case .all:
18 return true
19 case .unfiled:
20 return note.folder == nil
21 case .folder(let id):
22 return note.folder?.id == id
23 }
24 }
25
26 func noteMatchesTagFilter(_ note: Note) -> Bool {
27 if filterState.selectedTags.isEmpty {
28 return true
29 }
30 let noteTags = Set(note.tagNames)
31 switch filterState.tagMatchMode {
32 case .any:
33 return !filterState.selectedTags.isDisjoint(with: noteTags)
34 case .all:
35 return filterState.selectedTags.isSubset(of: noteTags)
36 }
37 }
38
39 var filteredNotes: [Note] {
40 let base = notes.filter { note in
41 noteMatchesSearch(note) && noteMatchesFolder(note) && noteMatchesTagFilter(note)
42 }
43 return sortNotes(base)
44 }
45
46 /// Notes that belong to the currently selected folder, ignoring tag filters and search.
47 /// Used to distinguish "the folder is empty" from "filters hid everything in the folder".
48 var notesInSelectedFolder: [Note] {
49 notes.filter { noteMatchesFolder($0) }
50 }
51
52 /// Which empty-state message the notes list should render right now, or `.none`
53 /// if the list has content to show. Mirrors the macOS filter-state resolver so
54 /// both platforms share the "no notes in folder" vs. "filters hiding notes" logic.
55 var currentEmptyState: NotesListEmptyState {
56 let isSpecificFolder: Bool
57 switch filterState.selectedFolder {
58 case .all:
59 isSpecificFolder = false
60 case .unfiled, .folder:
61 isSpecificFolder = true
62 }
63 return NotesListEmptyState.resolve(
64 totalNoteCount: notes.count,
65 notesInSelectedFolderCount: notesInSelectedFolder.count,
66 filteredNoteCount: combinedListItems.count,
67 searchText: searchText,
68 isSpecificFolderSelected: isSpecificFolder,
69 hasTagFilter: filterState.hasTagFilter
70 )
71 }
72
73 func sortNotes(_ notes: [Note]) -> [Note] {
74 switch filterState.sortOption {
75 case .dateNewest:
76 return notes.sorted { $0.timestampOrNow > $1.timestampOrNow }
77 case .dateOldest:
78 return notes.sorted { $0.timestampOrNow < $1.timestampOrNow }
79 case .titleAZ:
80 return notes.sorted { $0.titleOrUntitled.localizedCaseInsensitiveCompare($1.titleOrUntitled) == .orderedAscending }
81 case .titleZA:
82 return notes.sorted { $0.titleOrUntitled.localizedCaseInsensitiveCompare($1.titleOrUntitled) == .orderedDescending }
83 case .mostTags:
84 return notes.sorted {
85 let lhs = $0.tagNames.count
86 let rhs = $1.tagNames.count
87 if lhs != rhs { return lhs > rhs }
88 return $0.timestampOrNow > $1.timestampOrNow
89 }
90 }
91 }
92
93 func getAllUniqueTags() -> [String] {
94 var allTags = Set<String>()
95 for note in notes {
96 allTags.formUnion(note.tagNames)
97 }
98 return Array(allTags).sorted()
99 }
100
101 /// Count tags within search-matched notes, independent of selected tag filters.
102 func getTagCountsForVisibleResults() -> [String: Int] {
103 var counts: [String: Int] = [:]
104 for note in notes where noteMatchesSearch(note) {
105 for tag in Set(note.tagNames) {
106 counts[tag, default: 0] += 1
107 }
108 }
109 return counts
110 }
111
112 var combinedListItems: [NotesListItem] { filteredNotes.map { .note($0) } }
113
114 /// Group items by date for section headers
115 var groupedItems: [(Date, [NotesListItem])] {
116 // Non-date sort modes collapse into a single unlabeled group.
117 if filterState.sortOption == .titleAZ
118 || filterState.sortOption == .titleZA
119 || filterState.sortOption == .mostTags {
120 return combinedListItems.isEmpty ? [] : [(Date.distantFuture, combinedListItems)]
121 }
122
123 let calendar = Calendar.current
124 let grouped = Dictionary(grouping: combinedListItems) { item in
125 calendar.startOfDay(for: item.date)
126 }
127 let ascending = filterState.sortOption == .dateOldest
128 return grouped.sorted { ascending ? $0.key < $1.key : $0.key > $1.key }
129 }
130
131 func formatDateSectionHeader(_ date: Date) -> String {
132 // Sentinel used by non-date sort modes — render an empty header label.
133 if date == .distantFuture { return "" }
134 let calendar = Calendar.current
135 if calendar.isDateInToday(date) {
136 return String(localized: "Today")
137 } else if calendar.isDateInYesterday(date) {
138 return String(localized: "Yesterday")
139 } else {
140 let formatter = DateFormatter()
141 formatter.dateFormat = "E, MMM d"
142 return formatter.string(from: date)
143 }
144 }
145
146 func deleteItemsInGroup(at offsets: IndexSet, from itemsInGroup: [NotesListItem]) {
147 for index in offsets {
148 if case .note(let note) = itemsInGroup[index] {
149 do { try transcriptionManager.delete(note) } catch { transcriptionManager.errorMessage = error.localizedDescription }
150 }
151 }
152 }
153}