forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenedFileRocoverableStorage.swift
More file actions
39 lines (33 loc) · 1.25 KB
/
OpenedFileRocoverableStorage.swift
File metadata and controls
39 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Foundation
import Preferences
public final class OpenedFileRecoverableStorage {
let projectRootURL: URL
let userDefault = UserDefaults.shared
let key = "OpenedFileRecoverableStorage"
init(projectRootURL: URL) {
self.projectRootURL = projectRootURL
}
public func openFile(fileURL: URL) {
var dict = userDefault.dictionary(forKey: key) ?? [:]
var openedFiles = Set(dict[projectRootURL.path] as? [String] ?? [])
openedFiles.insert(fileURL.path)
dict[projectRootURL.path] = Array(openedFiles)
Task { @MainActor [dict] in
userDefault.set(dict, forKey: key)
}
}
public func closeFile(fileURL: URL) {
var dict = userDefault.dictionary(forKey: key) ?? [:]
var openedFiles = dict[projectRootURL.path] as? [String] ?? []
openedFiles.removeAll(where: { $0 == fileURL.path })
dict[projectRootURL.path] = openedFiles
Task { @MainActor [dict] in
userDefault.set(dict, forKey: key)
}
}
public var openedFiles: [URL] {
let dict = userDefault.dictionary(forKey: key) ?? [:]
let openedFiles = dict[projectRootURL.path] as? [String] ?? []
return openedFiles.map { URL(fileURLWithPath: $0) }
}
}