KMBookmarkManager.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // KMBookmarkManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/7.
  6. //
  7. import Cocoa
  8. let bookmarksIdentifier = (Bundle.main.bundleIdentifier ?? "") + ".bookmarks"
  9. let kBookmarks = "bookmarks"
  10. let kRecentDocuments = "recentDocuments"
  11. let kProperties = "properties"
  12. class KMBookmarkManager {
  13. static let manager = KMBookmarkManager()
  14. var recentDocuments: [[String: Any]] = []
  15. var rootBookmark: KMRootBookmark = KMRootBookmark()
  16. private init() {
  17. self.reloadData()
  18. }
  19. func reloadData() {
  20. let bookmarkDictionary: NSDictionary = (UserDefaults.standard.persistentDomain(forName: bookmarksIdentifier) ?? NSDictionary() as! [String : Any]) as NSDictionary
  21. let documents: NSArray = (bookmarkDictionary.object(forKey: kRecentDocuments) ?? NSArray()) as! NSArray
  22. for info in documents {
  23. recentDocuments.append(info as! [String : Any])
  24. }
  25. let childrenProperties = (bookmarkDictionary[kBookmarks] ?? NSArray()) as! NSArray
  26. rootBookmark = KMRootBookmark.bookmarkRoot(childrenProperties: childrenProperties)
  27. }
  28. func saveData() {
  29. let bookmarksDictionary: [String: Any] = [
  30. "bookmarks": self.rootBookmark.children.map { $0.properties },
  31. "recentDocuments": recentDocuments as Any
  32. ]
  33. UserDefaults.standard.setPersistentDomain(bookmarksDictionary, forName: bookmarksIdentifier)
  34. // NSDictionary *bookmarksDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[[bookmarkRoot children] valueForKey:@"properties"], BOOKMARKS_KEY, recentDocuments, RECENTDOCUMENTS_KEY, nil];
  35. // [[NSUserDefaults standardUserDefaults] setPersistentDomain:bookmarksDictionary forName:SKBookmarksIdentifier];
  36. }
  37. }