1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-12 21:30:32 +05:30

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
struct URLBookmarkModel {
static let bookmarkPrefix = "urlbookmark-"
static var shared = URLBookmarkModel()
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) {
guard let defaults = CacheModel.shared.bookmarksDefaults else {
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) {
defaults.set(bookmarkData, forKey: url.absoluteString)
logger.info("saved bookmark for \(url.absoluteString)")
defaults.set(bookmarkData, forKey: bookmarkKey(url))
logger.info("saved bookmark for \(bookmarkKey(url))")
} else {
logger.error("no bookmark data for \(url.absoluteString)")
logger.error("no bookmark data for \(bookmarkKey(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 {
logger.error("could not open bookmarks defaults")
return nil
}
if let data = defaults.data(forKey: url.absoluteString) {
if let data = defaults.data(forKey: bookmarkKey(url)) {
do {
var isStale = false
let url = try URL(
@ -39,7 +51,7 @@ struct URLBookmarkModel {
if isStale {
saveBookmark(url)
}
logger.info("loaded bookmark for \(url.absoluteString)")
logger.info("loaded bookmark for \(bookmarkKey(url))")
return url
} catch {
@ -47,23 +59,46 @@ struct URLBookmarkModel {
return nil
}
} else {
logger.warning("could not find bookmark for \(url.absoluteString)")
logger.warning("could not find bookmark for \(bookmarkKey(url))")
return nil
}
}
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 {
logger.error("could not open bookmarks defaults")
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)
return [.withSecurityScope, .securityScopeAllowOnlyReadAccess]
#else
@ -71,7 +106,7 @@ struct URLBookmarkModel {
#endif
}
var bookmarkResolutionOptions: URL.BookmarkResolutionOptions {
private var bookmarkResolutionOptions: URL.BookmarkResolutionOptions {
#if os(macOS)
return [.withSecurityScope]
#else

View File

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

View File

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

View File

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