Projects tab: create via +, sort by activity/name/count

Declared-but-empty projects persist locally (UserDefaults) until a
note carries the name, at which point the project syncs via notes as
usual. Empty declared projects can be swipe-deleted.

Claude-Session: https://claude.ai/code/session_014esDWi42URLEC6Cj17hGQ3
This commit is contained in:
2026-07-16 23:22:25 -04:00
parent f877941c0d
commit c5e8aea522
4 changed files with 137 additions and 12 deletions
+89 -9
View File
@@ -12,46 +12,126 @@ struct ProjectsView: View {
@AppStorage(ProjectsNaming.storageKey) private var namingRaw = ProjectsNaming.projects.rawValue
private var naming: ProjectsNaming { ProjectsNaming(rawValue: namingRaw) ?? .projects }
@AppStorage(ProjectsPrefs.declaredKey) private var declaredRaw = ""
@AppStorage(ProjectsPrefs.sortKey) private var sortRaw = ProjectsSort.activity.rawValue
private var sort: ProjectsSort { ProjectsSort(rawValue: sortRaw) ?? .activity }
@State private var creating = false
@State private var newName = ""
var body: some View {
NavigationStack {
List {
ForEach(projectCounts, id: \.project) { entry in
NavigationLink(value: entry.project) {
ForEach(projects, id: \.name) { entry in
NavigationLink(value: entry.name) {
HStack {
Label(entry.project, systemImage: "folder")
Label(entry.name, systemImage: "folder")
Spacer()
Text("\(entry.count)")
.foregroundStyle(.secondary)
.monospacedDigit()
}
}
.swipeActions(edge: .trailing) {
// Only a declared-but-empty project can be deleted here
// one a note carries exists because of that note, not
// because of a local declaration.
if entry.count == 0 {
Button(role: .destructive) {
removeDeclared(entry.name)
} label: {
Label("Delete", systemImage: "trash")
}
}
}
}
}
.navigationTitle(naming.plural)
.navigationDestination(for: String.self) { project in
ProjectNotesView(project: project)
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
creating = true
} label: {
Label("New \(naming.singular)", systemImage: "plus")
}
}
ToolbarItem(placement: .primaryAction) {
Menu {
Picker("Sort By", selection: $sortRaw) {
ForEach(ProjectsSort.allCases) { option in
Text(option.label).tag(option.rawValue)
}
}
} label: {
Label("Sort", systemImage: "arrow.up.arrow.down")
}
}
}
.overlay {
if projectCounts.isEmpty {
if projects.isEmpty {
ContentUnavailableView(
"No \(naming.plural.lowercased()) yet",
systemImage: "folder",
description: Text("Swipe a note in the list to file it under a \(naming.singular.lowercased()).")
description: Text("Swipe a note in the list to file it under a \(naming.singular.lowercased()), or tap + to create one.")
)
}
}
.alert("New \(naming.singular)", isPresented: $creating) {
TextField("Name", text: $newName)
Button("Cancel", role: .cancel) {
newName = ""
}
Button("Create") {
create()
}
}
}
}
private var projectCounts: [(project: String, count: Int)] {
/// Live projects (from notes that carry them) unioned with names
/// declared-but-not-yet-used, sorted per `sort`.
private var projects: [(name: String, count: Int, lastActivity: Date?)] {
var counts: [String: Int] = [:]
var lastActivity: [String: Date] = [:]
for note in notes where note.isLive {
guard let project = note.project else { continue }
counts[project, default: 0] += 1
lastActivity[project] = max(lastActivity[project] ?? note.createdAt, note.createdAt)
}
return counts
.map { (project: $0.key, count: $0.value) }
.sorted { $0.count == $1.count ? $0.project < $1.project : $0.count > $1.count }
for name in ProjectsPrefs.decodeDeclared(declaredRaw) where counts[name] == nil {
counts[name] = 0
}
let entries = counts.map { (name: $0.key, count: $0.value, lastActivity: lastActivity[$0.key]) }
switch sort {
case .activity:
return entries.sorted { lhs, rhs in
switch (lhs.lastActivity, rhs.lastActivity) {
case let (l?, r?): l > r
case (nil, nil): lhs.name.localizedCaseInsensitiveCompare(rhs.name) == .orderedAscending
case (nil, _): false
case (_, nil): true
}
}
case .name:
return entries.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
case .count:
return entries.sorted { $0.count == $1.count ? $0.name < $1.name : $0.count > $1.count }
}
}
private func create() {
let trimmed = newName.trimmingCharacters(in: .whitespacesAndNewlines)
newName = ""
guard !trimmed.isEmpty, !projects.contains(where: { $0.name == trimmed }) else { return }
declaredRaw = ProjectsPrefs.encodeDeclared(ProjectsPrefs.decodeDeclared(declaredRaw) + [trimmed])
}
private func removeDeclared(_ name: String) {
declaredRaw = ProjectsPrefs.encodeDeclared(ProjectsPrefs.decodeDeclared(declaredRaw).filter { $0 != name })
}
}