Fix urlbookmarks

This commit is contained in:
Arkadiusz Fal 2022-11-14 23:13:29 +01:00
parent de7eff389f
commit ae24dea4d6
4 changed files with 50 additions and 13 deletions

View File

@ -2,9 +2,21 @@ import Foundation
import Logging import Logging
struct URLBookmarkModel { struct URLBookmarkModel {
static let bookmarkPrefix = "urlbookmark-"
static var shared = URLBookmarkModel() static var shared = URLBookmarkModel()
var logger = Logger(label: "stream.yattee.url-bookmark") var logger = Logger(label: "stream.yattee.url-bookmark")
var allBookmarksKeys: [String] {
guard let defaults = CacheModel.shared.bookmarksDefaults else { return [] }
return defaults.dictionaryRepresentation().keys.filter { $0.starts(with: Self.bookmarkPrefix) }
}
var allURLs: [URL] {
allBookmarksKeys.compactMap { urlFromBookmark($0) }
}
func saveBookmark(_ url: URL) { func saveBookmark(_ url: URL) {
guard let defaults = CacheModel.shared.bookmarksDefaults else { guard let defaults = CacheModel.shared.bookmarksDefaults else {
logger.error("could not open bookmarks defaults") logger.error("could not open bookmarks defaults")
@ -12,22 +24,22 @@ struct URLBookmarkModel {
} }
if let bookmarkData = try? url.bookmarkData(options: bookmarkCreationOptions, includingResourceValuesForKeys: nil, relativeTo: nil) { if let bookmarkData = try? url.bookmarkData(options: bookmarkCreationOptions, includingResourceValuesForKeys: nil, relativeTo: nil) {
defaults.set(bookmarkData, forKey: url.absoluteString) defaults.set(bookmarkData, forKey: bookmarkKey(url))
logger.info("saved bookmark for \(url.absoluteString)") logger.info("saved bookmark for \(bookmarkKey(url))")
} else { } else {
logger.error("no bookmark data for \(url.absoluteString)") logger.error("no bookmark data for \(bookmarkKey(url))")
} }
} }
func loadBookmark(_ url: URL) -> URL? { func loadBookmark(_ url: URL) -> URL? {
logger.info("loading bookmark for \(url.absoluteString)") logger.info("loading bookmark for \(bookmarkKey(url))")
guard let defaults = CacheModel.shared.bookmarksDefaults else { guard let defaults = CacheModel.shared.bookmarksDefaults else {
logger.error("could not open bookmarks defaults") logger.error("could not open bookmarks defaults")
return nil return nil
} }
if let data = defaults.data(forKey: url.absoluteString) { if let data = defaults.data(forKey: bookmarkKey(url)) {
do { do {
var isStale = false var isStale = false
let url = try URL( let url = try URL(
@ -39,7 +51,7 @@ struct URLBookmarkModel {
if isStale { if isStale {
saveBookmark(url) saveBookmark(url)
} }
logger.info("loaded bookmark for \(url.absoluteString)") logger.info("loaded bookmark for \(bookmarkKey(url))")
return url return url
} catch { } catch {
@ -47,23 +59,46 @@ struct URLBookmarkModel {
return nil return nil
} }
} else { } else {
logger.warning("could not find bookmark for \(url.absoluteString)") logger.warning("could not find bookmark for \(bookmarkKey(url))")
return nil return nil
} }
} }
func removeBookmark(_ url: URL) { func removeBookmark(_ url: URL) {
logger.info("removing bookmark for \(url.absoluteString)") logger.info("removing bookmark for \(bookmarkKey(url))")
guard let defaults = CacheModel.shared.bookmarksDefaults else { guard let defaults = CacheModel.shared.bookmarksDefaults else {
logger.error("could not open bookmarks defaults") logger.error("could not open bookmarks defaults")
return return
} }
defaults.removeObject(forKey: url.absoluteString) defaults.removeObject(forKey: bookmarkKey(url))
} }
var bookmarkCreationOptions: URL.BookmarkCreationOptions { func refreshAll() {
logger.info("refreshing all bookamrks")
allURLs.forEach { url in
if loadBookmark(url) != nil {
logger.info("bookmark for \(url) exists")
} else {
logger.info("bookmark does not exist")
}
}
}
private func bookmarkKey(_ url: URL) -> String {
"\(Self.bookmarkPrefix)\(url.absoluteString)"
}
private func urlFromBookmark(_ key: String) -> URL? {
if let urlString = key.components(separatedBy: Self.bookmarkPrefix).last {
return URL(string: urlString)
}
return nil
}
private var bookmarkCreationOptions: URL.BookmarkCreationOptions {
#if os(macOS) #if os(macOS)
return [.withSecurityScope, .securityScopeAllowOnlyReadAccess] return [.withSecurityScope, .securityScopeAllowOnlyReadAccess]
#else #else
@ -71,7 +106,7 @@ struct URLBookmarkModel {
#endif #endif
} }
var bookmarkResolutionOptions: URL.BookmarkResolutionOptions { private var bookmarkResolutionOptions: URL.BookmarkResolutionOptions {
#if os(macOS) #if os(macOS)
return [.withSecurityScope] return [.withSecurityScope]
#else #else

View File

@ -4,7 +4,7 @@
<dict> <dict>
<key>com.apple.security.application-groups</key> <key>com.apple.security.application-groups</key>
<array> <array>
<string>group.stream.yattee.app.bookmarks</string> <string>group.78Z5H3M6RJ.stream.yattee.app.urlbookmarks</string>
</array> </array>
</dict> </dict>
</plist> </plist>

View File

@ -241,6 +241,8 @@ struct YatteeApp: App {
} }
} }
#endif #endif
URLBookmarkModel.shared.refreshAll()
} }
func migrateAccounts() { func migrateAccounts() {

View File

@ -4,7 +4,7 @@
<dict> <dict>
<key>com.apple.security.application-groups</key> <key>com.apple.security.application-groups</key>
<array> <array>
<string>group.stream.yattee.app.bookmarks</string> <string>group.78Z5H3M6RJ.stream.yattee.app.urlbookmarks</string>
</array> </array>
</dict> </dict>
</plist> </plist>