Jotley / app · jotley.ai / iOS / Views / iOSFolderDrawer.swift
| 1 | import SwiftUI |
| 2 | import CoreData |
| 3 | |
| 4 | /// Vertical folder drawer presented as a sheet. Replaces the horizontal |
| 5 | /// chip row. Tapping a row scopes the notes list to that folder and |
| 6 | /// dismisses the drawer. |
| 7 | struct iOSFolderDrawer: View { |
| 8 | @ObservedObject var filterState: FilterAndSortState |
| 9 | @Binding var showingFolderManagement: Bool |
| 10 | var onDone: () -> Void |
| 11 | |
| 12 | @FetchRequest( |
| 13 | sortDescriptors: [ |
| 14 | NSSortDescriptor(keyPath: \Folder.sortOrder, ascending: true), |
| 15 | NSSortDescriptor(keyPath: \Folder.name, ascending: true) |
| 16 | ], |
| 17 | animation: .default |
| 18 | ) |
| 19 | private var folders: FetchedResults<Folder> |
| 20 | |
| 21 | var body: some View { |
| 22 | VStack(spacing: 0) { |
| 23 | header |
| 24 | |
| 25 | ScrollView { |
| 26 | VStack(spacing: 2) { |
| 27 | row( |
| 28 | title: String(localized: "All Notes"), |
| 29 | systemImage: "tray.full", |
| 30 | count: nil, |
| 31 | isSelected: filterState.selectedFolder == .all |
| 32 | ) { |
| 33 | select(.all) |
| 34 | } |
| 35 | |
| 36 | row( |
| 37 | title: String(localized: "Unfiled"), |
| 38 | systemImage: "tray", |
| 39 | count: nil, |
| 40 | isSelected: filterState.selectedFolder == .unfiled |
| 41 | ) { |
| 42 | select(.unfiled) |
| 43 | } |
| 44 | |
| 45 | if !folders.isEmpty { |
| 46 | Divider() |
| 47 | .overlay(Color.themeTextSecondary.opacity(0.2)) |
| 48 | .padding(.vertical, 8) |
| 49 | .padding(.horizontal, 16) |
| 50 | |
| 51 | ForEach(folders) { folder in |
| 52 | folderRow(folder) |
| 53 | } |
| 54 | } else { |
| 55 | folderEmptyState |
| 56 | } |
| 57 | } |
| 58 | .padding(.vertical, 8) |
| 59 | } |
| 60 | |
| 61 | Divider() |
| 62 | .overlay(Color.themeTextSecondary.opacity(0.2)) |
| 63 | |
| 64 | Button { |
| 65 | showingFolderManagement = true |
| 66 | onDone() |
| 67 | } label: { |
| 68 | HStack(spacing: 10) { |
| 69 | Image(systemName: "folder.badge.gearshape") |
| 70 | .font(.system(size: 16)) |
| 71 | Text("Manage Folders") |
| 72 | .font(.system(size: 16, weight: .medium)) |
| 73 | Spacer() |
| 74 | Image(systemName: "chevron.right") |
| 75 | .font(.system(size: 13, weight: .semibold)) |
| 76 | .foregroundColor(Color.themeTextSecondary) |
| 77 | } |
| 78 | .foregroundColor(Color.themeTextPrimary) |
| 79 | .padding(.horizontal, 20) |
| 80 | .padding(.vertical, 16) |
| 81 | .contentShape(Rectangle()) |
| 82 | } |
| 83 | .buttonStyle(.plain) |
| 84 | .accessibilityIdentifier("folderDrawerManageButton") |
| 85 | } |
| 86 | .background(Color.themeBackground) |
| 87 | } |
| 88 | |
| 89 | private var header: some View { |
| 90 | HStack { |
| 91 | Text("Folders") |
| 92 | .font(.system(size: 20, weight: .semibold)) |
| 93 | .foregroundColor(Color.themeTextPrimary) |
| 94 | Spacer() |
| 95 | Button("Done", action: onDone) |
| 96 | .jotleyDismissButtonStyle() |
| 97 | .accessibilityIdentifier("folderDrawerDoneButton") |
| 98 | } |
| 99 | .padding(.horizontal, 20) |
| 100 | .padding(.top, 28) |
| 101 | .padding(.bottom, 12) |
| 102 | } |
| 103 | |
| 104 | private var folderEmptyState: some View { |
| 105 | VStack(spacing: 8) { |
| 106 | Image(systemName: "folder.badge.plus") |
| 107 | .font(.system(size: 30)) |
| 108 | .foregroundColor(Color.themeTextSecondary) |
| 109 | Text("No folders yet") |
| 110 | .font(.system(size: 15, weight: .semibold)) |
| 111 | .foregroundColor(Color.themeTextPrimary) |
| 112 | Text("Tap “Manage Folders” below to create your first folder, then file notes into it from any note.") |
| 113 | .font(.system(size: 13)) |
| 114 | .foregroundColor(Color.themeTextSecondary) |
| 115 | .multilineTextAlignment(.center) |
| 116 | .fixedSize(horizontal: false, vertical: true) |
| 117 | } |
| 118 | .frame(maxWidth: .infinity) |
| 119 | .padding(.horizontal, 32) |
| 120 | .padding(.top, 24) |
| 121 | .padding(.bottom, 8) |
| 122 | } |
| 123 | |
| 124 | private func row(title: String, systemImage: String, count: Int?, isSelected: Bool, action: @escaping () -> Void) -> some View { |
| 125 | Button(action: action) { |
| 126 | HStack(spacing: 12) { |
| 127 | Image(systemName: systemImage) |
| 128 | .font(.system(size: 16)) |
| 129 | .frame(width: 24) |
| 130 | .foregroundColor(isSelected ? Color.themePrimaryAccent : Color.themeTextSecondary) |
| 131 | Text(title) |
| 132 | .font(.system(size: 16, weight: isSelected ? .semibold : .regular)) |
| 133 | .foregroundColor(Color.themeTextPrimary) |
| 134 | Spacer() |
| 135 | if let count { |
| 136 | Text("\(count)") |
| 137 | .font(.system(size: 14, design: .rounded)) |
| 138 | .monospacedDigit() |
| 139 | .foregroundColor(Color.themeTextSecondary) |
| 140 | } |
| 141 | if isSelected { |
| 142 | Image(systemName: "checkmark") |
| 143 | .font(.system(size: 14, weight: .semibold)) |
| 144 | .foregroundColor(Color.themePrimaryAccent) |
| 145 | } |
| 146 | } |
| 147 | .padding(.horizontal, 20) |
| 148 | .padding(.vertical, 12) |
| 149 | .background( |
| 150 | isSelected ? Color.themeSelectionBackground : Color.clear |
| 151 | ) |
| 152 | .contentShape(Rectangle()) |
| 153 | } |
| 154 | .buttonStyle(.plain) |
| 155 | } |
| 156 | |
| 157 | private func folderRow(_ folder: Folder) -> some View { |
| 158 | let isSelected: Bool = { |
| 159 | if case .folder(let id) = filterState.selectedFolder { |
| 160 | return id == folder.id |
| 161 | } |
| 162 | return false |
| 163 | }() |
| 164 | |
| 165 | return Button { |
| 166 | if let id = folder.id { |
| 167 | select(.folder(id)) |
| 168 | } |
| 169 | } label: { |
| 170 | HStack(spacing: 12) { |
| 171 | Image(systemName: isSelected ? "folder.fill" : "folder") |
| 172 | .font(.system(size: 16)) |
| 173 | .frame(width: 24) |
| 174 | .foregroundColor(isSelected ? Color.themePrimaryAccent : Color.themeTextSecondary) |
| 175 | Text(folder.displayName) |
| 176 | .font(.system(size: 16, weight: isSelected ? .semibold : .regular)) |
| 177 | .foregroundColor(Color.themeTextPrimary) |
| 178 | .lineLimit(1) |
| 179 | Spacer() |
| 180 | Text("\(folder.noteCount)") |
| 181 | .font(.system(size: 14, design: .rounded)) |
| 182 | .monospacedDigit() |
| 183 | .foregroundColor(Color.themeTextSecondary) |
| 184 | if isSelected { |
| 185 | Image(systemName: "checkmark") |
| 186 | .font(.system(size: 14, weight: .semibold)) |
| 187 | .foregroundColor(Color.themePrimaryAccent) |
| 188 | } |
| 189 | } |
| 190 | .padding(.horizontal, 20) |
| 191 | .padding(.vertical, 12) |
| 192 | .background( |
| 193 | isSelected ? Color.themeSelectionBackground : Color.clear |
| 194 | ) |
| 195 | .contentShape(Rectangle()) |
| 196 | } |
| 197 | .buttonStyle(.plain) |
| 198 | .accessibilityIdentifier("folderDrawerRow-\(folder.displayName)") |
| 199 | } |
| 200 | |
| 201 | private func select(_ selection: FolderSelection) { |
| 202 | filterState.selectedFolder = selection |
| 203 | onDone() |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// Single horizontally-scrollable row of pills shown in the notes list header |
| 208 | /// when a folder scope and/or tag filters are active. Folder pill (if any) is |
| 209 | /// rendered first, followed by a pill for each selected tag. Tapping a pill |
| 210 | /// opens the relevant sheet; tapping its X clears just that item. |
| 211 | struct iOSCurrentScopeIndicator: View { |
| 212 | @ObservedObject var filterState: FilterAndSortState |
| 213 | var onFolderTap: () -> Void |
| 214 | var onTagTap: () -> Void |
| 215 | |
| 216 | @FetchRequest( |
| 217 | sortDescriptors: [NSSortDescriptor(keyPath: \Folder.name, ascending: true)] |
| 218 | ) |
| 219 | private var folders: FetchedResults<Folder> |
| 220 | |
| 221 | private var folderLabel: (name: String, systemImage: String)? { |
| 222 | switch filterState.selectedFolder { |
| 223 | case .all: |
| 224 | return nil |
| 225 | case .unfiled: |
| 226 | return ("Unfiled", "tray") |
| 227 | case .folder(let id): |
| 228 | guard let folder = folders.first(where: { $0.id == id }) else { |
| 229 | return nil |
| 230 | } |
| 231 | return (folder.displayName, "folder.fill") |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | var body: some View { |
| 236 | if folderLabel != nil || !filterState.selectedTags.isEmpty { |
| 237 | ScrollView(.horizontal, showsIndicators: false) { |
| 238 | HStack(spacing: 8) { |
| 239 | if let folderLabel { |
| 240 | scopePill( |
| 241 | systemImage: folderLabel.systemImage, |
| 242 | text: folderLabel.name, |
| 243 | onTap: onFolderTap, |
| 244 | onClear: { filterState.selectedFolder = .all }, |
| 245 | clearIdentifier: "currentFolderClearButton" |
| 246 | ) |
| 247 | .accessibilityIdentifier("currentFolderIndicator") |
| 248 | } |
| 249 | |
| 250 | ForEach(filterState.selectedTags.sorted(), id: \.self) { tag in |
| 251 | scopePill( |
| 252 | systemImage: "tag.fill", |
| 253 | text: tag, |
| 254 | onTap: onTagTap, |
| 255 | onClear: { filterState.selectedTags.remove(tag) }, |
| 256 | clearIdentifier: "currentTagClearButton-\(tag)" |
| 257 | ) |
| 258 | } |
| 259 | } |
| 260 | .padding(.horizontal, 16) |
| 261 | } |
| 262 | .accessibilityIdentifier("currentScopeIndicator") |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | @ViewBuilder |
| 267 | private func scopePill( |
| 268 | systemImage: String, |
| 269 | text: String, |
| 270 | onTap: @escaping () -> Void, |
| 271 | onClear: @escaping () -> Void, |
| 272 | clearIdentifier: String |
| 273 | ) -> some View { |
| 274 | HStack(spacing: 8) { |
| 275 | Button(action: onTap) { |
| 276 | HStack(spacing: 6) { |
| 277 | Image(systemName: systemImage) |
| 278 | .font(.system(size: 12, weight: .semibold)) |
| 279 | Text(text) |
| 280 | .font(.system(size: 13, weight: .semibold)) |
| 281 | .lineLimit(1) |
| 282 | } |
| 283 | .foregroundColor(Color.themeTextOnAccent) |
| 284 | .padding(.leading, 12) |
| 285 | .padding(.trailing, 8) |
| 286 | .padding(.vertical, 6) |
| 287 | } |
| 288 | .buttonStyle(.plain) |
| 289 | |
| 290 | Button(action: onClear) { |
| 291 | Image(systemName: "xmark") |
| 292 | .font(.system(size: 11, weight: .bold)) |
| 293 | .foregroundColor(Color.themeTextOnAccent.opacity(0.9)) |
| 294 | .padding(.trailing, 10) |
| 295 | .padding(.vertical, 6) |
| 296 | .contentShape(Rectangle()) |
| 297 | } |
| 298 | .buttonStyle(.plain) |
| 299 | .accessibilityIdentifier(clearIdentifier) |
| 300 | } |
| 301 | .background( |
| 302 | Capsule() |
| 303 | .fill(Color.themePrimaryAccent) |
| 304 | ) |
| 305 | } |
| 306 | } |