yattee/Model/URLBookmarkModel.swift

71 lines
2.2 KiB
Swift
Raw Normal View History

2022-11-10 17:11:28 +00:00
import Foundation
import Logging
struct URLBookmarkModel {
static var shared = URLBookmarkModel()
var logger = Logger(label: "stream.yattee.url-bookmark")
func saveBookmark(_ url: URL) {
guard let defaults = CacheModel.shared.bookmarksDefaults else {
logger.error("could not open bookmarks defaults")
return
}
2022-11-10 17:11:28 +00:00
if let bookmarkData = try? url.bookmarkData(options: bookmarkCreationOptions, includingResourceValuesForKeys: nil, relativeTo: nil) {
defaults.set(bookmarkData, forKey: url.absoluteString)
2022-11-10 17:11:28 +00:00
logger.info("saved bookmark for \(url.absoluteString)")
} else {
logger.error("no bookmark data for \(url.absoluteString)")
2022-11-10 17:11:28 +00:00
}
}
func loadBookmark(_ url: URL) -> URL? {
logger.info("loading bookmark for \(url.absoluteString)")
guard let defaults = CacheModel.shared.bookmarksDefaults else {
logger.error("could not open bookmarks defaults")
2022-11-10 17:11:28 +00:00
return nil
}
if let data = defaults.data(forKey: url.absoluteString) {
do {
var isStale = false
let url = try URL(
resolvingBookmarkData: data,
options: bookmarkResolutionOptions,
relativeTo: nil,
bookmarkDataIsStale: &isStale
)
if isStale {
saveBookmark(url)
}
logger.info("loaded bookmark for \(url.absoluteString)")
return url
} catch {
print("Error resolving bookmark:", error)
return nil
}
} else {
logger.warning("could not find bookmark for \(url.absoluteString)")
2022-11-10 17:11:28 +00:00
return nil
}
}
var bookmarkCreationOptions: URL.BookmarkCreationOptions {
#if os(macOS)
return [.withSecurityScope, .securityScopeAllowOnlyReadAccess]
#else
return []
#endif
}
var bookmarkResolutionOptions: URL.BookmarkResolutionOptions {
#if os(macOS)
return [.withSecurityScope]
#else
return []
#endif
}
}