SPB Git

Repository Public MIT

FavoritesStore.swift 3.0 KB 76 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Apps/GlassStudio/Kit/Stores/FavoritesStore.swift8// Module           : GlassStudio9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// 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          : MIT33//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 Foundation42import Observation43import SPGlassKit4445struct FavoritesDocument: Codable, Sendable, Equatable {46    var schemaVersion = 147    var componentIDs: [String] = []48}4950/// Favourite components, stored locally.51@MainActor52@Observable53public final class FavoritesStore {54    public private(set) var componentIDs: [String] = []55    private let store: AtomicFileStore<FavoritesDocument>5657    public init(directory: URL? = nil) {58        store = AtomicFileStore(url: StudioPaths.dataDirectory(override: directory).appendingPathComponent("favorites.json"), maximumBytes: 256 * 1024)59        // Identifiers that no longer exist in the catalogue are dropped on load.60        componentIDs = store.load(default: FavoritesDocument()).document.componentIDs.filter { GlassComponentCatalog.component(id: $0) != nil }61    }6263    public func contains(_ id: String) -> Bool { componentIDs.contains(id) }6465    public func toggle(_ id: String) {66        guard GlassComponentCatalog.component(id: id) != nil else { return }67        if let index = componentIDs.firstIndex(of: id) { componentIDs.remove(at: index) } else { componentIDs.append(id) }68        try? store.save(FavoritesDocument(componentIDs: componentIDs))69    }7071    public func move(fromOffsets source: IndexSet, toOffset destination: Int) {72        componentIDs.move(fromOffsets: source, toOffset: destination)73        try? store.save(FavoritesDocument(componentIDs: componentIDs))74    }75}76
Files main
Menu

Theme