Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion TablePro/Core/Storage/GroupStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@ final class GroupStorage {
private let defaults = UserDefaults.standard
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
private var cachedGroups: [ConnectionGroup]?

private init() {}

// MARK: - Group CRUD

/// Load all groups
func loadGroups() -> [ConnectionGroup] {
if let cached = cachedGroups { return cached }

guard let data = defaults.data(forKey: groupsKey) else {
cachedGroups = []
return []
}

do {
return try decoder.decode([ConnectionGroup].self, from: data)
let groups = try decoder.decode([ConnectionGroup].self, from: data)
cachedGroups = groups
return groups
} catch {
Self.logger.error("Failed to load groups: \(error)")
cachedGroups = []
return []
}
}
Expand All @@ -39,6 +46,7 @@ final class GroupStorage {
do {
let data = try encoder.encode(groups)
defaults.set(data, forKey: groupsKey)
cachedGroups = nil
SyncChangeTracker.shared.markDirty(.group, ids: groups.map { $0.id.uuidString })
} catch {
Self.logger.error("Failed to save groups: \(error)")
Expand Down
13 changes: 11 additions & 2 deletions TablePro/Core/Storage/TagStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class TagStorage {
private let defaults = UserDefaults.standard
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
private var cachedTags: [ConnectionTag]?

private init() {
// Initialize with presets on first launch
Expand All @@ -29,16 +30,23 @@ final class TagStorage {

/// Load all tags (presets + custom)
func loadTags() -> [ConnectionTag] {
if let cached = cachedTags { return cached }

guard let data = defaults.data(forKey: tagsKey) else {
return ConnectionTag.presets
let tags = ConnectionTag.presets
cachedTags = tags
return tags
}

do {
let tags = try decoder.decode([ConnectionTag].self, from: data)
cachedTags = tags
return tags
} catch {
Self.logger.error("Failed to load tags: \(error)")
return ConnectionTag.presets
let tags = ConnectionTag.presets
cachedTags = tags
return tags
}
}

Expand All @@ -47,6 +55,7 @@ final class TagStorage {
do {
let data = try encoder.encode(tags)
defaults.set(data, forKey: tagsKey)
cachedTags = nil
SyncChangeTracker.shared.markDirty(.tag, ids: tags.map { $0.id.uuidString })
} catch {
Self.logger.error("Failed to save tags: \(error)")
Expand Down
Loading
Loading