Repository Public
1// SP Notes iOS2// File: Features/Search/SearchCriteria.swift3// Description: What the search screen asks for (text and combinable filters) and its translation into the store's filters;4// date ranges and the list of recent searches. Pure values, tested.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPNotesCore1011enum DateRangeFilter: Equatable, Sendable {12 case any, today, last7Days, last30Days13 case custom(from: Date, to: Date)1415 /// Inclusive bounds on the modification date. Days are whole calendar days: "7 jours" is today and the six days before.16 func bounds(now: Date = .now, calendar: Calendar = .current) -> (after: Date?, before: Date?) {17 let startOfToday = calendar.startOfDay(for: now)18 switch self {19 case .any: return (nil, nil)20 case .today: return (startOfToday, nil)21 case .last7Days: return (calendar.date(byAdding: .day, value: -6, to: startOfToday), nil)22 case .last30Days: return (calendar.date(byAdding: .day, value: -29, to: startOfToday), nil)23 case .custom(let from, let to):24 let first = min(from, to), last = max(from, to)25 let endOfLast = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: last)).map { $0.addingTimeInterval(-0.001) }26 return (calendar.startOfDay(for: first), endOfLast)27 }28 }2930 var label: String {31 switch self {32 case .any: return String(localized: "Date")33 case .today: return String(localized: "Aujourd’hui")34 case .last7Days: return String(localized: "7 jours")35 case .last30Days: return String(localized: "30 jours")36 case .custom(let from, let to):37 return "\(min(from, to).formatted(date: .abbreviated, time: .omitted)) – \(max(from, to).formatted(date: .abbreviated, time: .omitted))"38 }39 }40}4142struct SearchCriteria: Equatable, Sendable {43 var query = ""44 var section: String?45 var folderId: String?46 var folderName: String?47 var tags: [String] = []48 var dateRange = DateRangeFilter.any49 var favoritesOnly = false50 var pinnedOnly = false51 var includeArchived = false5253 /// True when a filter narrows the results (including archives widens them, it is not a filter on its own).54 var hasFilters: Bool { section != nil || folderId != nil || !tags.isEmpty || dateRange != .any || favoritesOnly || pinnedOnly }55 var hasQuery: Bool { !query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }56 /// With neither text nor filter there is nothing to search: the screen shows the recent searches.57 var isActive: Bool { hasQuery || hasFilters }5859 func storeFilters(now: Date = .now, calendar: Calendar = .current) -> SearchFilters {60 let bounds = dateRange.bounds(now: now, calendar: calendar)61 return SearchFilters(section: section, folderId: folderId, tags: tags, updatedAfter: bounds.after, updatedBefore: bounds.before,62 favoritesOnly: favoritesOnly, pinnedOnly: pinnedOnly, includeArchived: includeArchived, includeTrash: false)63 }6465 mutating func clearFilters() {66 section = nil; folderId = nil; folderName = nil; tags = []; dateRange = .any; favoritesOnly = false; pinnedOnly = false; includeArchived = false67 }68}6970enum RecentSearches {71 static let key = "search.recents"72 static let limit = 87374 /// Most recent first, no duplicate (case and accents ignored), bounded.75 static func adding(_ query: String, to recents: [String]) -> [String] {76 let clean = query.trimmingCharacters(in: .whitespacesAndNewlines)77 guard !clean.isEmpty else { return recents }78 let rest = recents.filter { $0.compare(clean, options: [.caseInsensitive, .diacriticInsensitive]) != .orderedSame }79 return Array(([clean] + rest).prefix(limit))80 }8182 static func load(_ defaults: UserDefaults = .standard) -> [String] { defaults.stringArray(forKey: key) ?? [] }83 static func save(_ recents: [String], _ defaults: UserDefaults = .standard) { defaults.set(recents, forKey: key) }84}85