diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-05-16 20:57:05 -0700 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-05-16 20:57:05 -0700 |
| commit | 277c4cb65218adb8485bb28560a9bb4cca42da82 (patch) | |
| tree | bbd368a8388122f486ccfd109b5e906390aba5ac /Hotline/Models | |
| parent | 1be30131690e32080da64324a70133e11281eb27 (diff) | |
Enable CloudKit. Fix saving server bookmarks. Remove old bookmarks code.
Diffstat (limited to 'Hotline/Models')
| -rw-r--r-- | Hotline/Models/Bookmark.swift | 16 | ||||
| -rw-r--r-- | Hotline/Models/BookmarksOld.swift | 126 |
2 files changed, 8 insertions, 134 deletions
diff --git a/Hotline/Models/Bookmark.swift b/Hotline/Models/Bookmark.swift index 160de30..3530b19 100644 --- a/Hotline/Models/Bookmark.swift +++ b/Hotline/Models/Bookmark.swift @@ -1,20 +1,20 @@ import Foundation import SwiftData -enum BookmarkType: Codable { - case tracker - case server - case temporary +enum BookmarkType: String, Codable { + case tracker = "tracker" + case server = "server" + case temporary = "temporary" } @Model final class Bookmark { - var type: BookmarkType + var type: BookmarkType = BookmarkType.server var order: Int = 0 - var name: String - var address: String - var port: Int + var name: String = "" + var address: String = "" + var port: Int = HotlinePorts.DefaultServerPort @Attribute(.allowsCloudEncryption) var login: String? diff --git a/Hotline/Models/BookmarksOld.swift b/Hotline/Models/BookmarksOld.swift deleted file mode 100644 index 0a7bb16..0000000 --- a/Hotline/Models/BookmarksOld.swift +++ /dev/null @@ -1,126 +0,0 @@ -import SwiftUI - -extension NSNotification { - static let BookmarkAdded = Notification.Name("BookmarkAdded") - static let BookmarkRemoved = Notification.Name("BookmarkRemoved") -} - -enum BookmarkOldType: String, Codable { - case tracker = "tracker" - case server = "server" -} - -struct BookmarkOld: Codable, Equatable { - let type: BookmarkOldType - let name: String - let address: String - let port: Int - let login: String? - let password: String? - - init(type: BookmarkOldType, name: String, address: String, port: Int = HotlinePorts.DefaultServerPort, login: String? = nil, password: String? = nil) { - self.type = type - self.name = name - self.address = address - self.port = port - self.login = login - self.password = password - } -} - -@Observable final class BookmarksOld { - var bookmarks: [BookmarkOld]? = nil - - static let DefaultBookmarks: [BookmarkOld] = [ - BookmarkOld(type: .server, name: "System 7 Today", address: "hotline.system7today.com"), - BookmarkOld(type: .server, name: "The Mobius Strip", address: "67.174.208.111"), - BookmarkOld(type: .tracker, name: "Featured Servers", address: "hltracker.com"), - ] - - init() { - self.load() - } - - func apply(_ newBookmarks: [BookmarkOld], save shouldSave: Bool = true) { - self.bookmarks = newBookmarks - if shouldSave { - self.save() - } - } - - func load() { - let jsonString: String? = DAKeychain.shared["Hotline Bookmarks"] - let jsonData: Data? = jsonString?.data(using: .utf8, allowLossyConversion: false) - - let decoder = JSONDecoder() - var decodedBookmarks = try? decoder.decode([BookmarkOld].self, from: jsonData ?? Data()) - if decodedBookmarks == nil || decodedBookmarks?.isEmpty == true { - print("Bookmarks: using default bookmarks") - decodedBookmarks = BookmarksOld.DefaultBookmarks - } - else { - print("Bookmarks: using saved bookmarks") - } - - self.bookmarks = [BookmarkOld](decodedBookmarks!) - } - - func save() { - var bookmarksToSave = self.bookmarks - if bookmarksToSave == BookmarksOld.DefaultBookmarks { - print("Bookmarks: skipping saving default bookmarks") - bookmarksToSave = [] - } - - let encoder = JSONEncoder() - encoder.outputFormatting = .prettyPrinted - if let jsonData = try? encoder.encode(bookmarksToSave) { - if let jsonString = String(data: jsonData, encoding: .utf8) { - DAKeychain.shared["Hotline Bookmarks"] = jsonString - } - } - } - - // MARK: - - - func add(_ bookmark: BookmarkOld, save shouldSave: Bool = true) { - self.bookmarks?.insert(bookmark, at: 0) - - if shouldSave { - self.save() - } - - DispatchQueue.main.async { - NotificationCenter.default.post(name: NSNotification.BookmarkAdded, object: nil, userInfo: ["index": 0]) - } - } - - func delete(_ bookmark: BookmarkOld, save shouldSave: Bool = true) -> Bool { - if let i = self.bookmarks?.firstIndex(where: { b in b.address.lowercased() == bookmark.address.lowercased() && b.port == bookmark.port }) { - self.bookmarks?.remove(at: i) - - if shouldSave { - self.save() - } - - DispatchQueue.main.async { - NotificationCenter.default.post(name: NSNotification.BookmarkRemoved, object: nil, userInfo: ["index": i]) - } - return true - } - - return false - } - - func update(_ bookmark: BookmarkOld, save shouldSave: Bool = true) -> Bool { - if let i = self.bookmarks?.firstIndex(where: { b in b.address.lowercased() == bookmark.address.lowercased() && b.port == bookmark.port }) { - self.bookmarks?[i] = bookmark - if shouldSave { - self.save() - } - return true - } - - return false - } -} |