Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Settings/CategoriesView.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// Categories: the five built-in ones and any number of custom ones.44struct CategoriesView: View {45 @Environment(Vault.self) private var vault4647 @State private var editor: CategoryDraft?48 @State private var pendingDelete: CardCategory?4950 private func count(_ category: CardCategory) -> Int { vault.cards.filter { $0.categoryID == category.id }.count }5152 var body: some View {53 ScreenScaffold(eyebrow: "Organisation", title: "Categories", showsBack: true) {54 GlassIconButton(symbol: "plus", label: "New category", size: 40) { editor = CategoryDraft() }55 .accessibilityIdentifier("categories.add")56 } content: {57 PanelSection("Built in") {58 ForEach(Array(CardCategory.builtIn.enumerated()), id: \.element.id) { position, category in59 SettingRow(symbol: category.symbol, tint: Color(category.tint), title: category.name,60 caption: CardKind.kinds(inCategory: category.id).prefix(3).map(\.title).joined(separator: " · "),61 showsDivider: position < CardCategory.builtIn.count - 1) { countLabel(count(category)) }62 .accessibilityElement(children: .combine)63 }64 }6566 VStack(alignment: .leading, spacing: SP.Space.s) {67 SectionHeader("Your categories")68 if vault.customCategories.isEmpty {69 VStack(alignment: .leading, spacing: SP.Space.s) {70 Text("None yet").font(.spHeadline).foregroundStyle(SP.Palette.textPrimary)71 Text("Create a category for anything the built-in ones do not cover: travel, family, a club.")72 .font(.spCallout).foregroundStyle(SP.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)73 Button { editor = CategoryDraft() } label: { Label("New category", systemImage: "plus") }74 .buttonStyle(.spSecondary).accessibilityIdentifier("categories.addFirst")75 }76 .spPanel()77 } else {78 VStack(spacing: 0) {79 ForEach(Array(vault.customCategories.enumerated()), id: \.element.id) { position, category in80 SettingRow(symbol: category.symbol, tint: Color(category.tint), title: category.name, caption: nil,81 showsDivider: position < vault.customCategories.count - 1) {82 HStack(spacing: SP.Space.xxs) {83 countLabel(count(category))84 Menu {85 Button("Rename", systemImage: "pencil") { editor = CategoryDraft(category) }86 Button("Delete", systemImage: "trash", role: .destructive) { pendingDelete = category }87 } label: {88 Image(systemName: "ellipsis").font(.system(size: 15, weight: .semibold)).foregroundStyle(SP.Palette.titanium)89 .frame(width: 40, height: 40).contentShape(Rectangle())90 }91 .accessibilityLabel("Actions for \(category.name)")92 .accessibilityIdentifier("categories.menu.\(category.name)")93 }94 }95 }96 }97 .spPanel(padding: 0)98 }99 }100 }101 .sheet(item: $editor) { draft in102 CategoryEditorSheet(draft: draft) { result in103 if let id = result.existingID { try? vault.renameCategory(id: id, name: result.name) }104 else { try? vault.addCategory(name: result.name, symbol: result.symbol, tint: result.tint) }105 Haptics.shared.play(.success)106 }107 }108 .alert("Delete “\(pendingDelete?.name ?? "")”?", isPresented: Binding(get: { pendingDelete != nil }, set: { if !$0 { pendingDelete = nil } })) {109 Button("Cancel", role: .cancel) {}110 Button("Delete", role: .destructive) {111 if let pendingDelete { try? vault.removeCategory(id: pendingDelete.id) }112 }113 } message: {114 let n = pendingDelete.map(count) ?? 0115 Text(n == 0 ? "No card uses this category." : "Its \(n) card\(n == 1 ? "" : "s") will move back to the category of their type. No card is deleted.")116 }117 }118119 private func countLabel(_ count: Int) -> some View {120 Text(count == 1 ? "1 card" : "\(count) cards").font(.spCaption).monospacedDigit().foregroundStyle(SP.Palette.textSecondary)121 }122}123124struct CategoryDraft: Identifiable {125 static let symbols = ["folder.fill", "airplane", "house.fill", "heart.fill", "figure.2.and.child.holdinghands", "dumbbell.fill", "cart.fill",126 "book.fill", "pawprint.fill", "car.fill", "ticket.fill", "building.columns.fill", "cross.case.fill", "star.fill"]127 static let tints: [RGB] = [RGB(r: 0.16, g: 0.47, b: 1.0), RGB(r: 0.72, g: 0.75, b: 0.80), RGB(r: 0.22, g: 0.70, b: 0.62), RGB(r: 0.55, g: 0.50, b: 0.95),128 RGB(r: 0.85, g: 0.62, b: 0.30), RGB(r: 0.86, g: 0.38, b: 0.42), RGB(r: 0.35, g: 0.72, b: 0.90)]129130 let id = UUID()131 var existingID: String?132 var name = ""133 var symbol = CategoryDraft.symbols[0]134 var tint = CategoryDraft.tints[0]135136 init() {}137 init(_ category: CardCategory) {138 existingID = category.id139 name = category.name140 symbol = category.symbol141 tint = category.tint142 }143}144145private struct CategoryEditorSheet: View {146 @State var draft: CategoryDraft147 var onSave: (CategoryDraft) -> Void148 @Environment(\.dismiss) private var dismiss149150 private var isRenaming: Bool { draft.existingID != nil }151 private var trimmedName: String { draft.name.trimmingCharacters(in: .whitespacesAndNewlines) }152153 var body: some View {154 SheetScaffold(eyebrow: "Category", title: isRenaming ? "Rename category" : "New category") {155 HStack(spacing: SP.Space.m) {156 IconBadge(symbol: draft.symbol, tint: Color(draft.tint), size: 56)157 Text(trimmedName.isEmpty ? "Category name" : trimmedName).font(.spTitle)158 .foregroundStyle(trimmedName.isEmpty ? SP.Palette.textTertiary : SP.Palette.textPrimary).lineLimit(1).minimumScaleFactor(0.6)159 Spacer(minLength: 0)160 }161 .spPanel()162 .accessibilityHidden(true)163164 SPTextField(title: "Name", text: $draft.name, prompt: "Travel", capitalization: .words)165 .accessibilityIdentifier("category.name")166167 if !isRenaming {168 VStack(alignment: .leading, spacing: SP.Space.xs) {169 Text("Symbol").spEyebrow()170 LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: SP.Space.xs), count: 7), spacing: SP.Space.xs) {171 ForEach(CategoryDraft.symbols, id: \.self) { symbol in172 let selected = symbol == draft.symbol173 Button { draft.symbol = symbol; Haptics.shared.play(.select) } label: {174 Image(systemName: symbol).font(.system(size: 16, weight: .semibold))175 .foregroundStyle(selected ? Color(draft.tint) : SP.Palette.titanium)176 .frame(maxWidth: .infinity, minHeight: 44)177 .background(selected ? Color(draft.tint).opacity(0.16) : SP.Palette.surface, in: RoundedRectangle(cornerRadius: SP.Radius.s, style: .continuous))178 .overlay(RoundedRectangle(cornerRadius: SP.Radius.s, style: .continuous)179 .strokeBorder(selected ? Color(draft.tint).opacity(0.6) : SP.Palette.hairlineStrong, lineWidth: 0.75))180 }181 .buttonStyle(.plain)182 .accessibilityLabel(symbol.replacingOccurrences(of: ".fill", with: "").replacingOccurrences(of: ".", with: " "))183 .accessibilityAddTraits(selected ? .isSelected : [])184 }185 }186 }187 VStack(alignment: .leading, spacing: SP.Space.xs) {188 Text("Tint").spEyebrow()189 HStack(spacing: SP.Space.s) {190 ForEach(Array(CategoryDraft.tints.enumerated()), id: \.offset) { position, tint in191 let selected = tint == draft.tint192 Button { draft.tint = tint; Haptics.shared.play(.select) } label: {193 Circle().fill(Color(tint)).frame(width: 30, height: 30)194 .overlay(Circle().strokeBorder(.white.opacity(0.25), lineWidth: 0.75))195 .padding(5)196 .overlay(Circle().strokeBorder(selected ? Color(tint) : .clear, lineWidth: 2))197 .frame(minWidth: 44, minHeight: 44)198 }199 .buttonStyle(.plain)200 .accessibilityLabel("Tint \(position + 1)")201 .accessibilityAddTraits(selected ? .isSelected : [])202 }203 }204 }205 }206207 Button(isRenaming ? "Save" : "Create category") {208 var result = draft209 result.name = trimmedName210 dismiss()211 onSave(result)212 }213 .buttonStyle(.spPrimary)214 .disabled(trimmedName.isEmpty)215 .accessibilityIdentifier("category.save")216 }217 .presentationDetents([.large])218 }219}220