diff --git a/apps/swift-ios/DesignSystem/T3Theme.swift b/apps/swift-ios/DesignSystem/T3Theme.swift index a723b7db47..0ad9b001a2 100644 --- a/apps/swift-ios/DesignSystem/T3Theme.swift +++ b/apps/swift-ios/DesignSystem/T3Theme.swift @@ -7,12 +7,15 @@ enum T3Colors { static let input = Color(white: 0.075) static let border = Color.white.opacity(0.09) static let separator = Color.white.opacity(0.07) + static let ledgerSurface = Color(white: 0.055) + static let ledgerSelected = Color(white: 0.051) static let textPrimary = Color.white static let textSecondary = Color.white.opacity(0.62) - static let textTertiary = Color.white.opacity(0.4) + static let textTertiary = Color.white.opacity(0.5) static let accent = Color(red: 0.04, green: 0.52, blue: 1) + static let statusRunning = Color(red: 0.13, green: 0.83, blue: 0.93) static let success = Color(red: 0.19, green: 0.82, blue: 0.35) static let warning = Color(red: 1, green: 0.62, blue: 0.04) static let danger = Color(red: 1, green: 0.27, blue: 0.23) diff --git a/apps/swift-ios/Features/Workspace/DailyUXModels.swift b/apps/swift-ios/Features/Workspace/DailyUXModels.swift index bd3350825c..c0b1317368 100644 --- a/apps/swift-ios/Features/Workspace/DailyUXModels.swift +++ b/apps/swift-ios/Features/Workspace/DailyUXModels.swift @@ -80,13 +80,22 @@ struct DailyUXSidebarIndex { let settled: [FeatureThread] let searchResults: [FeatureThread] + var needsInput: [FeatureThread] { + active.filter { + $0.state == .waitingForApproval || $0.state == .waitingForInput + } + } + + var failed: [FeatureThread] { + active.filter { $0.state == .failed } + } + init( snapshot: FeatureSnapshot, query: String, projectID: String? = nil, now: Date = .now ) { - let projectByID = Dictionary(uniqueKeysWithValues: snapshot.projects.map { ($0.id, $0) }) let visible = snapshot.threads.filter { thread in guard !thread.isArchived else { return false } return projectID == nil || thread.projectID == projectID @@ -122,13 +131,22 @@ struct DailyUXSidebarIndex { return lhs.id < rhs.id } - let normalizedQuery = query.trimmingCharacters(in: .whitespacesAndNewlines) - guard !normalizedQuery.isEmpty else { - searchResults = [] - return - } + searchResults = Self.matchingThreads( + active + snoozed + settled, + snapshot: snapshot, + query: query + ) + } - searchResults = (active + snoozed + settled).filter { thread in + static func matchingThreads( + _ candidates: [FeatureThread], + snapshot: FeatureSnapshot, + query: String + ) -> [FeatureThread] { + let normalizedQuery = query.trimmingCharacters(in: .whitespacesAndNewlines) + guard !normalizedQuery.isEmpty else { return [] } + let projectByID = Dictionary(uniqueKeysWithValues: snapshot.projects.map { ($0.id, $0) }) + return candidates.filter { thread in let project = projectByID[thread.projectID] return [ thread.title, @@ -140,6 +158,48 @@ struct DailyUXSidebarIndex { } } +enum SidebarRelativeAge { + static func compact(since date: Date, now: Date) -> String { + let seconds = max(0, Int(now.timeIntervalSince(date))) + switch seconds { + case ..<60: + return "now" + case ..<3_600: + return "\(seconds / 60)m" + case ..<86_400: + return "\(seconds / 3_600)h" + case ..<604_800: + return "\(seconds / 86_400)d" + case ..<31_536_000: + return "\(seconds / 604_800)w" + default: + return "\(seconds / 31_536_000)y" + } + } + + static func accessibility(since date: Date, now: Date) -> String { + let seconds = max(0, Int(now.timeIntervalSince(date))) + switch seconds { + case ..<60: + return "Updated just now" + case ..<3_600: + return "Updated \(unit(seconds / 60, singular: "minute")) ago" + case ..<86_400: + return "Updated \(unit(seconds / 3_600, singular: "hour")) ago" + case ..<604_800: + return "Updated \(unit(seconds / 86_400, singular: "day")) ago" + case ..<31_536_000: + return "Updated \(unit(seconds / 604_800, singular: "week")) ago" + default: + return "Updated \(unit(seconds / 31_536_000, singular: "year")) ago" + } + } + + private static func unit(_ value: Int, singular: String) -> String { + "\(value) \(singular)\(value == 1 ? "" : "s")" + } +} + extension FeatureThread { var needsAttention: Bool { state == .waitingForApproval || state == .waitingForInput || state == .failed diff --git a/apps/swift-ios/Features/Workspace/ProjectAndArchiveViews.swift b/apps/swift-ios/Features/Workspace/ProjectAndArchiveViews.swift index 72f4c850b0..43e89c9480 100644 --- a/apps/swift-ios/Features/Workspace/ProjectAndArchiveViews.swift +++ b/apps/swift-ios/Features/Workspace/ProjectAndArchiveViews.swift @@ -53,83 +53,3 @@ public struct AddProjectView: View { } } } - -public struct ArchivedThreadsView: View { - @SwiftUI.Environment(\.dismiss) private var dismiss - @Bindable var model: FeatureRootModel - let onSelect: (FeatureThread) -> Void - - public init(model: FeatureRootModel, onSelect: @escaping (FeatureThread) -> Void) { - self.model = model - self.onSelect = onSelect - } - - public var body: some View { - NavigationStack { - List { - if archived.isEmpty { - ContentUnavailableView( - "No archived threads", - systemImage: "archivebox", - description: Text("Archived work will appear here.") - ) - .listRowBackground(Color.clear) - } - ForEach(archived) { thread in - HStack(spacing: 12) { - Button { - onSelect(thread) - } label: { - FeatureThreadRow( - thread: thread, - projectName: projectName(for: thread.projectID) - ) - .frame(maxWidth: .infinity, alignment: .leading) - } - .buttonStyle(.plain) - - Button { - Task { await model.setArchived(thread.id, archived: false) } - } label: { - Image(systemName: "arrow.uturn.backward") - } - .buttonStyle(.borderless) - .accessibilityLabel("Restore \(thread.title)") - } - .swipeActions { - Button(role: .destructive) { - Task { await model.deleteThread(thread.id) } - } label: { - Label("Delete", systemImage: "trash") - } - Button { - Task { await model.setArchived(thread.id, archived: false) } - } label: { - Label("Restore", systemImage: "arrow.uturn.backward") - } - .tint(.blue) - } - } - } - .scrollContentBackground(.hidden) - .background(Color.black) - .navigationTitle("Archived") - .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .confirmationAction) { - Button("Done") { dismiss() } - } - } - } - } - - private var archived: [FeatureThread] { - model.snapshot.threads - .filter(\.isArchived) - .sorted { $0.updatedAt > $1.updatedAt } - } - - private func projectName(for id: String) -> String { - model.snapshot.projects.first(where: { $0.id == id })?.name ?? "Unknown project" - } -} diff --git a/apps/swift-ios/Features/Workspace/WorkspaceView.swift b/apps/swift-ios/Features/Workspace/WorkspaceView.swift index 86a161bd06..1ad155831f 100644 --- a/apps/swift-ios/Features/Workspace/WorkspaceView.swift +++ b/apps/swift-ios/Features/Workspace/WorkspaceView.swift @@ -1,6 +1,8 @@ import SwiftUI public struct WorkspaceView: View { + @SwiftUI.Environment(\.dynamicTypeSize) private var dynamicTypeSize + @Bindable var model: FeatureRootModel private let submitNewTask: (NewTaskRequest) async -> FeatureThread? private let submitMessage: (FeatureMessageSubmission) async -> Bool @@ -8,14 +10,15 @@ public struct WorkspaceView: View { @State private var selectedThreadID: String? @State private var searchText = "" @State private var selectedProjectID: String? + @State private var selectedScope: SidebarScope = .active @State private var settledLimit = 10 @State private var showingNewTask = false @State private var showingAddProject = false @State private var showingSettings = false - @State private var showingArchived = false @State private var renamingThread: FeatureThread? @State private var renameTitle = "" @State private var sidebarClock = Date.now + @FocusState private var isSearchFocused: Bool public init( model: FeatureRootModel, @@ -89,12 +92,6 @@ public struct WorkspaceView: View { .sheet(isPresented: $showingSettings) { SettingsView(model: model) } - .sheet(isPresented: $showingArchived) { - ArchivedThreadsView(model: model) { thread in - selectedThreadID = thread.id - showingArchived = false - } - } .alert( "Rename thread", isPresented: Binding( @@ -120,6 +117,12 @@ public struct WorkspaceView: View { self.selectedThreadID = nil } } + .onChange(of: model.snapshot.projects.map(\.id)) { + if let selectedProjectID, + !model.snapshot.projects.contains(where: { $0.id == selectedProjectID }) { + self.selectedProjectID = nil + } + } .task(id: sidebarBoundarySignature) { while !Task.isCancelled { sidebarClock = .now @@ -134,47 +137,45 @@ public struct WorkspaceView: View { } private var sidebar: some View { - List(selection: $selectedThreadID) { - connectionHeader - newTaskAction + let presentation = SidebarPresentation( + snapshot: model.snapshot, + query: searchText, + projectID: selectedProjectID, + scope: selectedScope, + now: sidebarClock + ) - if searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { - projectFilter - activeSection - snoozedSection - settledSection - archiveAction - } else { - searchResults + return VStack(spacing: 0) { + sidebarNavigationBar + connectionHeader + List(selection: $selectedThreadID) { + scopePicker(presentation) + projectFilter(presentation) + + if searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + scopedThreadSection(presentation) + } else { + searchResults(presentation) + } } + .listStyle(.plain) + .environment(\.defaultMinListRowHeight, 0) + .scrollContentBackground(.hidden) + .scrollDismissesKeyboard(.interactively) + .contentMargins(.top, 0, for: .scrollContent) + .background(T3Colors.background) } - .listStyle(.sidebar) - .scrollContentBackground(.hidden) .background(T3Colors.background) - .searchable(text: $searchText, prompt: "Search tasks and projects") - .navigationTitle("T3") - .toolbar { - ToolbarItem(placement: .topBarLeading) { - Button { - showingSettings = true - } label: { - Image(systemName: "slider.horizontal.3") - } - .accessibilityLabel("Settings") - } - ToolbarItem(placement: .topBarTrailing) { - Button { - showingAddProject = true - } label: { - Image(systemName: "folder.badge.plus") - } - .accessibilityLabel("Add project") - } + .safeAreaInset(edge: .bottom, spacing: 0) { + commandBar } - .t3NavigationChrome() + .toolbar(.hidden, for: .navigationBar) .onChange(of: selectedProjectID) { settledLimit = 10 } + .onChange(of: selectedScope) { + settledLimit = 10 + } } @ViewBuilder @@ -212,6 +213,46 @@ public struct WorkspaceView: View { } } + private var sidebarNavigationBar: some View { + ZStack { + Text("T3 Code") + .font(.headline) + .foregroundStyle(T3Colors.textPrimary) + + HStack { + Button { + showingSettings = true + } label: { + Image(systemName: "gearshape") + .font(.system(size: 19, weight: .medium)) + .frame(width: T3Metrics.minimumTapTarget, height: T3Metrics.minimumTapTarget) + } + .buttonStyle(.plain) + .foregroundStyle(T3Colors.textSecondary) + .accessibilityLabel("Settings") + .accessibilityIdentifier("sidebar-settings-button") + + Spacer() + + Button { + showingAddProject = true + } label: { + Image(systemName: "plus") + .font(.system(size: 20, weight: .medium)) + .frame(width: T3Metrics.minimumTapTarget, height: T3Metrics.minimumTapTarget) + } + .buttonStyle(.plain) + .foregroundStyle(T3Colors.statusRunning) + .accessibilityLabel("Add project") + .accessibilityHint("Adds a repository to this environment") + .accessibilityIdentifier("sidebar-add-project-button") + } + .padding(.horizontal, 6) + } + .frame(height: T3Metrics.minimumTapTarget) + .background(T3Colors.background) + } + private var connectionHeader: some View { Menu { ForEach(model.snapshot.environments) { environment in @@ -232,59 +273,243 @@ public struct WorkspaceView: View { Label("Manage devices", systemImage: "externaldrive.connected.to.line.below") } } label: { - HStack(spacing: 9) { - Circle() - .fill(connectionColor) - .frame(width: 7, height: 7) - VStack(alignment: .leading, spacing: 1) { - Text(model.snapshot.connection.environmentName ?? "T3 environment") - .font(.subheadline.weight(.semibold)) - .foregroundStyle(T3Colors.textPrimary) - .lineLimit(1) - Text(connectionLabel) - .font(.caption2) - .foregroundStyle(T3Colors.textSecondary) + Group { + if dynamicTypeSize.isAccessibilitySize { + HStack(alignment: .top, spacing: 9) { + Circle() + .fill(connectionColor) + .frame(width: 7, height: 7) + .padding(.top, 7) + VStack(alignment: .leading, spacing: 2) { + HStack(spacing: 6) { + Text(model.snapshot.connection.environmentName ?? "T3 environment") + .font(.subheadline.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + .lineLimit(1) + Image(systemName: "chevron.up.chevron.down") + .font(.system(size: 9, weight: .semibold)) + .foregroundStyle(T3Colors.textTertiary) + } + Text(connectionLabel) + .font(.caption) + .foregroundStyle(T3Colors.textSecondary) + } + Spacer() + } + .padding(.vertical, 8) + .frame(minHeight: 60) + } else { + HStack(spacing: 9) { + Circle() + .fill(connectionColor) + .frame(width: 7, height: 7) + Text(model.snapshot.connection.environmentName ?? "T3 environment") + .font(.subheadline.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + .lineLimit(1) + Image(systemName: "chevron.up.chevron.down") + .font(.system(size: 9, weight: .semibold)) + .foregroundStyle(T3Colors.textTertiary) + Spacer() + Text(connectionLabel) + .font(.caption) + .foregroundStyle(T3Colors.textSecondary) + } + .frame(minHeight: T3Metrics.minimumTapTarget) } - Spacer() - Image(systemName: "chevron.up.chevron.down") - .font(.caption2.weight(.semibold)) - .foregroundStyle(T3Colors.textTertiary) } + .padding(.horizontal, 16) .contentShape(Rectangle()) - } - .buttonStyle(.plain) - .listRowSeparator(.hidden) - } - - private var newTaskAction: some View { - Button { - showingNewTask = true - } label: { - HStack(spacing: 10) { - Image(systemName: "plus") - .font(.system(size: 14, weight: .bold)) - Text("New task") - .font(.subheadline.weight(.semibold)) - Spacer() - Image(systemName: "arrow.up.right") - .font(.caption.weight(.bold)) - .foregroundStyle(.black.opacity(0.48)) + .overlay(alignment: .bottom) { + Rectangle() + .fill(T3Colors.separator) + .frame(height: 0.5) } - .foregroundStyle(.black) - .padding(.horizontal, 12) - .frame(height: 42) - .background(.white, in: RoundedRectangle(cornerRadius: 9)) } .buttonStyle(.plain) - .disabled(model.snapshot.projects.isEmpty) - .opacity(model.snapshot.projects.isEmpty ? 0.35 : 1) - .listRowInsets(EdgeInsets(top: 7, leading: 12, bottom: 9, trailing: 12)) - .listRowBackground(Color.clear) - .listRowSeparator(.hidden) - .accessibilityHint("Compose a message and start a thread") + .accessibilityElement(children: .combine) + .accessibilityLabel("Environment") + .accessibilityValue( + "\(model.snapshot.connection.environmentName ?? "T3 environment"), \(connectionLabel)" + ) + .accessibilityHint("Switch environments or manage devices") + .accessibilityIdentifier("sidebar-environment-menu") } - private var projectFilter: some View { + private var commandBar: some View { + HStack(spacing: 10) { + HStack(spacing: 8) { + Image(systemName: "magnifyingglass") + .font(.system(size: 14, weight: .medium)) + .foregroundStyle(T3Colors.textTertiary) + + TextField("Search tasks", text: $searchText) + .font(.subheadline) + .foregroundStyle(T3Colors.textPrimary) + .focused($isSearchFocused) + .submitLabel(.search) + .textInputAutocapitalization(.never) + .autocorrectionDisabled() + .accessibilityLabel("Search tasks and projects") + .accessibilityIdentifier("sidebar-search-field") + + if !searchText.isEmpty { + Button { + searchText = "" + } label: { + Image(systemName: "xmark.circle.fill") + .font(.system(size: 15)) + .foregroundStyle(T3Colors.textTertiary) + .frame(width: T3Metrics.minimumTapTarget, height: T3Metrics.minimumTapTarget) + } + .buttonStyle(.plain) + .accessibilityLabel("Clear search") + } + } + .padding(.horizontal, 12) + .frame(height: T3Metrics.minimumTapTarget) + .background(T3Colors.ledgerSurface, in: RoundedRectangle(cornerRadius: 10)) + .overlay { + RoundedRectangle(cornerRadius: 10) + .stroke(T3Colors.border, lineWidth: 0.5) + } + + Button { + isSearchFocused = false + showingNewTask = true + } label: { + Image(systemName: "square.and.pencil") + .font(.system(size: 20, weight: .medium)) + .frame(width: T3Metrics.minimumTapTarget, height: T3Metrics.minimumTapTarget) + } + .buttonStyle(.plain) + .foregroundStyle(T3Colors.statusRunning) + .disabled(model.snapshot.projects.isEmpty) + .opacity(model.snapshot.projects.isEmpty ? 0.35 : 1) + .accessibilityLabel("New task") + .accessibilityHint("Compose a message and start a thread") + .accessibilityIdentifier("sidebar-new-task-button") + } + .padding(.leading, 16) + .padding(.trailing, 12) + .padding(.vertical, 4) + .background(T3Colors.background) + .overlay(alignment: .top) { + Rectangle() + .fill(T3Colors.separator) + .frame(height: 0.5) + } + } + + private func scopePicker(_ presentation: SidebarPresentation) -> some View { + Group { + if dynamicTypeSize.isAccessibilitySize { + Menu { + ForEach(SidebarScope.allCases) { scope in + Button { + selectedScope = scope + } label: { + if selectedScope == scope { + Label( + "\(scope.title), \(presentation.count(for: scope))", + systemImage: "checkmark" + ) + } else { + Text("\(scope.title), \(presentation.count(for: scope))") + } + } + } + } label: { + HStack(spacing: 8) { + Text("Status filter") + .font(.subheadline) + .foregroundStyle(T3Colors.textSecondary) + Spacer() + Text(selectedScope.title) + .font(.subheadline.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + Text("\(presentation.count(for: selectedScope))") + .font(.subheadline.monospacedDigit()) + .foregroundStyle(T3Colors.textTertiary) + Image(systemName: "chevron.down") + .font(.system(size: 9, weight: .semibold)) + .foregroundStyle(T3Colors.textTertiary) + } + .padding(.horizontal, 16) + .frame(minHeight: T3Metrics.minimumTapTarget) + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .accessibilityLabel("Status filter") + .accessibilityValue( + "\(selectedScope.title), \(presentation.count(for: selectedScope)) tasks" + ) + .accessibilityHint("Filters the task list") + } else { + ScrollViewReader { proxy in + ScrollView(.horizontal, showsIndicators: false) { + HStack(spacing: 22) { + ForEach(SidebarScope.allCases) { scope in + Button { + selectedScope = scope + } label: { + VStack(spacing: 0) { + Spacer(minLength: 0) + HStack(spacing: 4) { + Text(scope.title) + if presentation.count(for: scope) > 0 { + Text("\(presentation.count(for: scope))") + .foregroundStyle(T3Colors.textTertiary) + .monospacedDigit() + } + } + .font( + .caption.weight( + selectedScope == scope ? .semibold : .medium + ) + ) + .foregroundStyle( + selectedScope == scope + ? T3Colors.textPrimary + : T3Colors.textSecondary + ) + Spacer(minLength: 0) + Rectangle() + .fill( + selectedScope == scope + ? T3Colors.textPrimary + : Color.clear + ) + .frame(width: 20, height: 2) + } + .frame(height: T3Metrics.minimumTapTarget) + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .accessibilityLabel(scope.title) + .accessibilityValue("\(presentation.count(for: scope)) tasks") + .accessibilityHint("Filters the task list") + .accessibilityAddTraits( + selectedScope == scope ? .isSelected : [] + ) + .id(scope) + } + } + .padding(.horizontal, 16) + } + .onChange(of: selectedScope) { + proxy.scrollTo(selectedScope, anchor: .center) + } + } + } + } + .frame(minHeight: T3Metrics.minimumTapTarget) + .listRowInsets(EdgeInsets()) + .listRowSeparator(.hidden) + .listRowBackground(Color.clear) + } + + private func projectFilter(_ presentation: SidebarPresentation) -> some View { Menu { Button { selectedProjectID = nil @@ -309,126 +534,135 @@ public struct WorkspaceView: View { } label: { ProjectFilterRow( project: model.snapshot.projects.first { $0.id == selectedProjectID }, - totalCount: sidebarIndex.active.count - + sidebarIndex.snoozed.count - + sidebarIndex.settled.count + totalCount: presentation.scopedThreads.count ) } .buttonStyle(.plain) + .listRowInsets(EdgeInsets()) + .listRowBackground(Color.clear) .listRowSeparator(.hidden) + .accessibilityLabel("Project filter") + .accessibilityValue( + "\(model.snapshot.projects.first { $0.id == selectedProjectID }?.name ?? "All projects"), \(presentation.scopedThreads.count) tasks" + ) + .accessibilityHint("Filters tasks by repository") + .accessibilityIdentifier("sidebar-project-filter") } @ViewBuilder - private var snoozedSection: some View { - if !sidebarIndex.snoozed.isEmpty { - Section("Snoozed") { - ForEach(sidebarIndex.snoozed) { thread in - threadLink(thread, showsProject: selectedProjectID == nil) - .opacity(0.76) - } - } - } - } + private func scopedThreadSection(_ presentation: SidebarPresentation) -> some View { + sidebarSectionHeader( + selectedScope.sectionTitle, + count: presentation.scopedThreads.count + ) - @ViewBuilder - private var activeSection: some View { - Section("Active") { - if sidebarIndex.active.isEmpty { - Text(selectedProjectID == nil ? "No active tasks" : "No active tasks in this project") + if visibleScopedThreads(in: presentation).isEmpty { + HStack { + Text(emptyScopeMessage) .font(.footnote) .foregroundStyle(T3Colors.textSecondary) - } else { - ForEach(sidebarIndex.active) { thread in - threadLink(thread, showsProject: selectedProjectID == nil) - } + .padding(.horizontal, 16) + .frame(minHeight: 60, alignment: .leading) + } + .listRowInsets(EdgeInsets()) + .listRowBackground(Color.clear) + .listRowSeparator(.hidden) + } else { + ForEach(visibleScopedThreads(in: presentation)) { thread in + threadLink( + thread, + showsProject: selectedProjectID == nil, + isArchived: selectedScope == .archived + ) } - } - } - @ViewBuilder - private var settledSection: some View { - if !sidebarIndex.settled.isEmpty { - Section("Settled") { - ForEach(sidebarIndex.settled.prefix(settledLimit)) { thread in - threadLink(thread, showsProject: selectedProjectID == nil) - .opacity(0.76) - } - if sidebarIndex.settled.count > settledLimit { - Button { - settledLimit += 25 - } label: { - HStack { - Text("Show more") - Spacer() - Text("\(sidebarIndex.settled.count - settledLimit)") - .font(.caption.monospacedDigit()) - .foregroundStyle(T3Colors.textTertiary) - } - .font(.subheadline) + if selectedScope == .settled, + presentation.scopedThreads.count > settledLimit { + Button { + settledLimit += 25 + } label: { + HStack { + Text("Show more") + Spacer() + Text("\(presentation.scopedThreads.count - settledLimit)") + .font(.caption.monospacedDigit()) + .foregroundStyle(T3Colors.textTertiary) } - .buttonStyle(.plain) - } - } - } - } - - private var archiveAction: some View { - Button { - showingArchived = true - } label: { - HStack { - Label("Archived", systemImage: "archivebox") + .padding(.horizontal, 16) + .frame(height: T3Metrics.minimumTapTarget) .font(.subheadline) - .foregroundStyle(T3Colors.textSecondary) - Spacer() - } - .contentShape(Rectangle()) - .accessibilityElement(children: .ignore) - .accessibilityLabel("Archived") - .accessibilityAddTraits(.isButton) - .accessibilityAction { - showingArchived = true + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .listRowInsets(EdgeInsets()) + .listRowBackground(Color.clear) + .listRowSeparator(.hidden) } } - .buttonStyle(.plain) - .accessibilityLabel("Archived") - .accessibilityIdentifier("archived-threads-button") } @ViewBuilder - private var searchResults: some View { - Section("Results") { - if sidebarIndex.searchResults.isEmpty { - ContentUnavailableView.search(text: searchText) - .listRowBackground(Color.clear) - } else { - ForEach(sidebarIndex.searchResults) { thread in - threadLink(thread, showsProject: true) - } + private func searchResults(_ presentation: SidebarPresentation) -> some View { + sidebarSectionHeader("Results", count: presentation.searchThreads.count) + + if presentation.searchThreads.isEmpty { + ContentUnavailableView.search(text: searchText) + .listRowInsets(EdgeInsets()) + .listRowBackground(Color.clear) + .listRowSeparator(.hidden) + } else { + ForEach(presentation.searchThreads) { thread in + threadLink( + thread, + showsProject: true, + isArchived: thread.isArchived + ) } } } - private func threadLink(_ thread: FeatureThread, showsProject: Bool) -> some View { + private func threadLink( + _ thread: FeatureThread, + showsProject: Bool, + isArchived: Bool = false + ) -> some View { NavigationLink(value: thread.id) { FeatureThreadRow( thread: thread, - projectName: showsProject ? projectName(for: thread.projectID) : nil + projectName: showsProject ? projectName(for: thread.projectID) : nil, + now: sidebarClock, + isSelected: selectedThreadID == thread.id ) } .tag(thread.id) + .buttonStyle(.plain) + .navigationLinkIndicatorVisibility(.hidden) + .listRowInsets(EdgeInsets()) + .listRowBackground( + selectedThreadID == thread.id ? T3Colors.ledgerSelected : Color.clear + ) + .listRowSeparator(.hidden) .swipeActions(edge: .trailing, allowsFullSwipe: false) { Button(role: .destructive) { Task { await model.deleteThread(thread.id) } } label: { Label("Delete", systemImage: "trash") } - Button { - Task { await model.setArchived(thread.id, archived: true) } - } label: { - Label("Archive", systemImage: "archivebox") + if isArchived { + Button { + Task { await model.setArchived(thread.id, archived: false) } + } label: { + Label("Restore", systemImage: "arrow.uturn.backward") + } + .tint(.blue) + } else { + Button { + Task { await model.setArchived(thread.id, archived: true) } + } label: { + Label("Archive", systemImage: "archivebox") + } + .tint(.orange) } - .tint(.orange) } .contextMenu { Button { @@ -437,44 +671,54 @@ public struct WorkspaceView: View { } label: { Label("Rename", systemImage: "pencil") } - Button { - Task { await model.setArchived(thread.id, archived: true) } - } label: { - Label("Archive", systemImage: "archivebox") - } - if thread.isEffectivelySettled(at: .now) { + if isArchived { Button { - Task { await model.setSettled(thread.id, settled: false) } + Task { await model.setArchived(thread.id, archived: false) } } label: { - Label("Reopen", systemImage: "arrow.counterclockwise") + Label("Restore", systemImage: "arrow.uturn.backward") } } else { Button { - Task { await model.setSettled(thread.id, settled: true) } + Task { await model.setArchived(thread.id, archived: true) } } label: { - Label("Mark done", systemImage: "checkmark") + Label("Archive", systemImage: "archivebox") } - } - Button { - let isSnoozed = thread.isEffectivelySnoozed(at: sidebarClock) - Task { - await model.setSnoozed( - thread.id, - until: isSnoozed ? nil : Date().addingTimeInterval(60 * 60) + + if thread.isEffectivelySettled(at: .now) { + Button { + Task { await model.setSettled(thread.id, settled: false) } + } label: { + Label("Reopen", systemImage: "arrow.counterclockwise") + } + } else { + Button { + Task { await model.setSettled(thread.id, settled: true) } + } label: { + Label("Mark done", systemImage: "checkmark") + } + } + + Button { + let isSnoozed = thread.isEffectivelySnoozed(at: sidebarClock) + Task { + await model.setSnoozed( + thread.id, + until: isSnoozed ? nil : Date().addingTimeInterval(60 * 60) + ) + } + } label: { + let isSnoozed = thread.isEffectivelySnoozed(at: sidebarClock) + Label( + isSnoozed ? "Unsnooze" : "Snooze 1 hour", + systemImage: isSnoozed ? "bell" : "clock" ) } - } label: { - let isSnoozed = thread.isEffectivelySnoozed(at: sidebarClock) - Label( - isSnoozed ? "Unsnooze" : "Snooze 1 hour", - systemImage: isSnoozed ? "bell" : "clock" + .disabled( + thread.state == .queued + || thread.state == .waitingForApproval + || thread.state == .waitingForInput ) } - .disabled( - thread.state == .queued - || thread.state == .waitingForApproval - || thread.state == .waitingForInput - ) Button(role: .destructive) { Task { await model.deleteThread(thread.id) } } label: { @@ -483,13 +727,45 @@ public struct WorkspaceView: View { } } - private var sidebarIndex: DailyUXSidebarIndex { - DailyUXSidebarIndex( - snapshot: model.snapshot, - query: searchText, - projectID: selectedProjectID, - now: sidebarClock - ) + private func visibleScopedThreads(in presentation: SidebarPresentation) -> [FeatureThread] { + if selectedScope == .settled { + return Array(presentation.scopedThreads.prefix(settledLimit)) + } + return presentation.scopedThreads + } + + private var emptyScopeMessage: String { + let projectSuffix = selectedProjectID == nil ? "" : " in this project" + return switch selectedScope { + case .active: "No active tasks\(projectSuffix)" + case .needsInput: "Nothing needs input\(projectSuffix)" + case .failed: "No failed tasks\(projectSuffix)" + case .snoozed: "No snoozed tasks\(projectSuffix)" + case .settled: "No settled tasks\(projectSuffix)" + case .archived: "No archived tasks\(projectSuffix)" + } + } + + private func sidebarSectionHeader(_ title: String, count: Int) -> some View { + HStack(spacing: 6) { + Text(title) + .font(.caption.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + Text("\(count)") + .font(.caption.monospacedDigit()) + .foregroundStyle(T3Colors.textTertiary) + Spacer() + } + .padding(.horizontal, 16) + .frame(height: 31, alignment: .bottom) + .padding(.bottom, 7) + .contentShape(Rectangle()) + .accessibilityElement(children: .ignore) + .accessibilityLabel("\(title), \(count) tasks") + .accessibilityAddTraits(.isHeader) + .listRowInsets(EdgeInsets()) + .listRowBackground(Color.clear) + .listRowSeparator(.hidden) } private var sidebarBoundarySignature: String { @@ -543,100 +819,294 @@ private extension FeatureDraftAttachment { } } +private enum SidebarScope: String, CaseIterable, Identifiable { + case active + case needsInput + case failed + case snoozed + case settled + case archived + + var id: Self { self } + + var title: String { + switch self { + case .active: "Active" + case .needsInput: "Needs input" + case .failed: "Failed" + case .snoozed: "Snoozed" + case .settled: "Settled" + case .archived: "Archive" + } + } + + var sectionTitle: String { + self == .archived ? "Archived" : title + } +} + +private struct SidebarPresentation { + let index: DailyUXSidebarIndex + let archivedThreads: [FeatureThread] + let scopedThreads: [FeatureThread] + let searchThreads: [FeatureThread] + + init( + snapshot: FeatureSnapshot, + query: String, + projectID: String?, + scope: SidebarScope, + now: Date + ) { + let index = DailyUXSidebarIndex( + snapshot: snapshot, + query: "", + projectID: projectID, + now: now + ) + let archivedThreads = snapshot.threads + .filter { thread in + guard thread.isArchived else { return false } + return projectID == nil || thread.projectID == projectID + } + .sorted { lhs, rhs in + if lhs.updatedAt != rhs.updatedAt { + return lhs.updatedAt > rhs.updatedAt + } + return lhs.id < rhs.id + } + let scopedThreads = switch scope { + case .active: index.active + case .needsInput: index.needsInput + case .failed: index.failed + case .snoozed: index.snoozed + case .settled: index.settled + case .archived: archivedThreads + } + + self.index = index + self.archivedThreads = archivedThreads + self.scopedThreads = scopedThreads + searchThreads = DailyUXSidebarIndex.matchingThreads( + scopedThreads, + snapshot: snapshot, + query: query + ) + } + + func count(for scope: SidebarScope) -> Int { + switch scope { + case .active: index.active.count + case .needsInput: index.needsInput.count + case .failed: index.failed.count + case .snoozed: index.snoozed.count + case .settled: index.settled.count + case .archived: archivedThreads.count + } + } +} + private struct ProjectFilterRow: View { let project: FeatureProject? let totalCount: Int var body: some View { - HStack(spacing: 10) { - Image(systemName: "folder") - .font(.system(size: 13, weight: .medium)) - .foregroundStyle(T3Colors.textSecondary) - .frame(width: 18) - VStack(alignment: .leading, spacing: 1) { - Text(project?.name ?? "All projects") - .font(.subheadline.weight(.medium)) - .foregroundStyle(T3Colors.textPrimary) - .lineLimit(1) - Text(project?.path ?? "Filter tasks by repository") - .font(.caption2) - .foregroundStyle(T3Colors.textTertiary) - .lineLimit(1) - } + HStack(spacing: 7) { + Text(project?.name ?? "All projects") + .font(.subheadline) + .foregroundStyle(T3Colors.textPrimary) + .lineLimit(1) Spacer() - if totalCount > 0 { - Text("\(totalCount)") - .font(.caption2.monospacedDigit()) - .foregroundStyle(T3Colors.textTertiary) - } - Image(systemName: "chevron.up.chevron.down") - .font(.caption2.weight(.semibold)) + Text("\(totalCount)") + .font(.subheadline.monospacedDigit()) + .foregroundStyle(T3Colors.textTertiary) + Image(systemName: "chevron.down") + .font(.system(size: 9, weight: .semibold)) .foregroundStyle(T3Colors.textTertiary) } + .padding(.horizontal, 16) + .frame(minHeight: T3Metrics.minimumTapTarget) .contentShape(Rectangle()) - .accessibilityElement(children: .combine) + .overlay(alignment: .bottom) { + Rectangle() + .fill(T3Colors.separator) + .frame(height: 0.5) + } + .accessibilityElement(children: .ignore) } } struct FeatureThreadRow: View { + @SwiftUI.Environment(\.dynamicTypeSize) private var dynamicTypeSize + let thread: FeatureThread let projectName: String? + let now: Date + let isSelected: Bool + + init( + thread: FeatureThread, + projectName: String?, + now: Date = .now, + isSelected: Bool = false + ) { + self.thread = thread + self.projectName = projectName + self.now = now + self.isSelected = isSelected + } var body: some View { - HStack(alignment: .top, spacing: 9) { - Image(systemName: statusIcon) - .font(.system(size: 8, weight: .bold)) - .foregroundStyle(statusColor) - .frame(width: 10, height: 18) - VStack(alignment: .leading, spacing: 3) { - HStack(spacing: 6) { - Text(thread.title) - .font(.subheadline.weight(thread.needsAttention ? .semibold : .regular)) - .foregroundStyle(T3Colors.textPrimary) - .lineLimit(1) - Spacer(minLength: 4) - Text(thread.updatedAt, format: .relative(presentation: .numeric)) - .font(.system(size: 10)) - .foregroundStyle(T3Colors.textTertiary) - .lineLimit(1) - } - HStack(spacing: 5) { - if let projectName { - Text(projectName) - } - if let preview = thread.preview, !preview.isEmpty { - if projectName != nil { Text("·") } - Text(preview) - } else if thread.needsAttention { - if projectName != nil { Text("·") } - Text(statusText) - } - } - .font(.caption) - .foregroundStyle(thread.needsAttention ? statusColor : T3Colors.textSecondary) - .lineLimit(1) + Group { + if dynamicTypeSize.isAccessibilitySize { + accessibleRow + } else { + regularRow } } - .padding(.vertical, 2) - .accessibilityElement(children: .combine) - .accessibilityValue(statusText) + .background(isSelected ? T3Colors.ledgerSelected : Color.clear) + .overlay(alignment: .leading) { + if isSelected { + Rectangle() + .fill(T3Colors.statusRunning) + .frame(width: 2) + } + } + .overlay(alignment: .bottom) { + Rectangle() + .fill(T3Colors.separator) + .frame(height: 0.5) + .padding(.leading, 48) + .padding(.trailing, 16) + } + .contentShape(Rectangle()) + .accessibilityElement(children: .ignore) + .accessibilityLabel(thread.title) + .accessibilityValue(accessibilityValue) + .accessibilityHint("Opens task") + .accessibilityIdentifier("thread-\(thread.id)") + .accessibilityAddTraits(isSelected ? .isSelected : []) + } + + private var regularRow: some View { + HStack(spacing: 0) { + statusGlyph + .frame(width: 32) + + VStack(alignment: .leading, spacing: 3) { + Text(thread.title) + .font(.subheadline.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + .lineLimit(1) + metadata + .lineLimit(1) + } + + Text(compactAge) + .font(.caption.monospacedDigit()) + .foregroundStyle(T3Colors.textTertiary) + .lineLimit(1) + .frame(width: 42, alignment: .trailing) + } + .padding(.leading, 16) + .padding(.trailing, 12) + .frame(minHeight: 60) + } + + private var accessibleRow: some View { + HStack(alignment: .top, spacing: 0) { + statusGlyph + .frame(width: 32, height: 28, alignment: .center) + + VStack(alignment: .leading, spacing: 5) { + Text(thread.title) + .font(.subheadline.weight(.semibold)) + .foregroundStyle(T3Colors.textPrimary) + .lineLimit(2) + + metadata + .lineLimit(2) + + HStack(spacing: 6) { + Text(statusText) + .foregroundStyle(statusColor) + Text("·") + .foregroundStyle(T3Colors.textTertiary) + Text("Updated \(compactAge)") + .foregroundStyle(T3Colors.textSecondary) + } + .font(.caption) + } + } + .padding(.leading, 16) + .padding(.trailing, 16) + .padding(.vertical, 10) + .frame(minHeight: 72) + } + + @ViewBuilder + private var metadata: some View { + HStack(spacing: 5) { + if let projectName { + Text(projectName) + .foregroundStyle(T3Colors.textSecondary) + } + if let preview = visiblePreview { + if projectName != nil { + Text("·") + .foregroundStyle(T3Colors.textTertiary) + } + Text(preview) + .foregroundStyle(thread.needsAttention ? statusColor : T3Colors.textSecondary) + } + } + .font(.caption) + } + + private var statusGlyph: some View { + Image(systemName: statusIcon) + .font(.caption.weight(.semibold)) + .foregroundStyle(statusColor) + .accessibilityHidden(true) + } + + private var visiblePreview: String? { + if let preview = thread.preview?.trimmingCharacters(in: .whitespacesAndNewlines), + !preview.isEmpty { + return preview.replacingOccurrences(of: "\n", with: " ") + } + return thread.needsAttention ? statusText : nil } private var statusIcon: String { - switch thread.state { - case .idle, .completed: "circle.fill" - case .queued, .working: "bolt.fill" - case .waitingForApproval: "exclamationmark.circle.fill" + if thread.isEffectivelySnoozed(at: now) { + return "moon.zzz.fill" + } + if thread.isEffectivelySettled(at: now) { + return "checkmark.circle.fill" + } + return switch thread.state { + case .idle: "circle" + case .completed: "checkmark.circle.fill" + case .queued: "clock" + case .working: "bolt.fill" + case .waitingForApproval: "exclamationmark.triangle.fill" case .waitingForInput: "questionmark.circle.fill" case .failed: "xmark.circle.fill" } } private var statusColor: Color { - switch thread.state { + if thread.isEffectivelySnoozed(at: now) { + return T3Colors.textSecondary + } + if thread.isEffectivelySettled(at: now) { + return T3Colors.success.opacity(0.7) + } + return switch thread.state { case .idle, .completed: T3Colors.textTertiary - case .queued: T3Colors.accent - case .working: .cyan + case .queued: T3Colors.textTertiary + case .working: T3Colors.statusRunning case .waitingForApproval: T3Colors.warning case .waitingForInput: .indigo case .failed: T3Colors.danger @@ -644,7 +1114,13 @@ struct FeatureThreadRow: View { } private var statusText: String { - switch thread.state { + if thread.isEffectivelySnoozed(at: now) { + return "Snoozed" + } + if thread.isEffectivelySettled(at: now) { + return "Settled" + } + return switch thread.state { case .waitingForApproval: "Approval needed" case .waitingForInput: "Answer needed" case .working: "Working" @@ -653,4 +1129,20 @@ struct FeatureThreadRow: View { case .idle, .completed: thread.state.rawValue.capitalized } } + + private var compactAge: String { + SidebarRelativeAge.compact(since: thread.updatedAt, now: now) + } + + private var accessibilityValue: String { + var parts = [statusText] + if let projectName { + parts.append("Project \(projectName)") + } + if let visiblePreview, visiblePreview != statusText { + parts.append(visiblePreview) + } + parts.append(SidebarRelativeAge.accessibility(since: thread.updatedAt, now: now)) + return parts.joined(separator: ". ") + } } diff --git a/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift b/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift index 2ee338a1f8..a8b74de062 100644 --- a/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift @@ -167,6 +167,87 @@ struct DailyUXSidebarTests { #expect(searched.searchResults.map(\.id) == ["server"]) } + @Test + func attentionScopesRemainFocusedSubsetsOfActive() { + let approval = thread( + id: "approval", + title: "Approve schema", + created: -10, + updated: -5, + state: .waitingForApproval + ) + let input = thread( + id: "input", + title: "Answer migration question", + created: -20, + updated: -5, + state: .waitingForInput + ) + let failed = thread( + id: "failed", + title: "Failed build", + created: -30, + updated: -5, + state: .failed + ) + let working = thread( + id: "working", + title: "Build application", + created: -40, + updated: -5, + state: .working + ) + + let snapshot = FeatureSnapshot(threads: [approval, input, failed, working]) + let index = DailyUXSidebarIndex(snapshot: snapshot, query: "", now: now) + + #expect(index.active.map(\.id) == ["approval", "input", "failed", "working"]) + #expect(index.needsInput.map(\.id) == ["approval", "input"]) + #expect(index.failed.map(\.id) == ["failed"]) + #expect( + DailyUXSidebarIndex.matchingThreads( + index.failed, + snapshot: snapshot, + query: "build" + ).map(\.id) == ["failed"] + ) + #expect( + DailyUXSidebarIndex.matchingThreads( + index.needsInput, + snapshot: snapshot, + query: "build" + ).isEmpty + ) + } + + @Test + func compactRelativeAgeClampsFutureDatesAndUsesStableUnits() { + #expect( + SidebarRelativeAge.compact( + since: now.addingTimeInterval(5), + now: now + ) == "now" + ) + #expect( + SidebarRelativeAge.compact( + since: now.addingTimeInterval(-125), + now: now + ) == "2m" + ) + #expect( + SidebarRelativeAge.compact( + since: now.addingTimeInterval(-7_300), + now: now + ) == "2h" + ) + #expect( + SidebarRelativeAge.accessibility( + since: now.addingTimeInterval(-3_600), + now: now + ) == "Updated 1 hour ago" + ) + } + private func makeIndex(_ threads: [FeatureThread]) -> DailyUXSidebarIndex { DailyUXSidebarIndex( snapshot: FeatureSnapshot(threads: threads),