diff --git a/ReaderTranslator/Stores/Bookmark.swift b/ReaderTranslator/Stores/Bookmark.swift index 9eba375..cff200c 100644 --- a/ReaderTranslator/Stores/Bookmark.swift +++ b/ReaderTranslator/Stores/Bookmark.swift @@ -79,4 +79,32 @@ extension Array where Element == Bookmark { self[index].changed = Date() } } + + var json: String { + do { + let jsonData = try JSONEncoder().encode(self) + if let jsonString = String(data: jsonData, encoding: .utf8) { + return jsonString + } + } catch { + print("Bookmarks_\(#function)", error) + } + return "[]" + } + + mutating func save(data: Data?) { + guard let content = data else { return } + if let jsonString = String(data: content, encoding: .unicode) { + self.save(jsonString: jsonString) + } + } + + mutating func save(jsonString: String) { + guard let jsonData = jsonString.data(using: .utf8) else { return } + do { + self = try JSONDecoder().decode(Bookmarks.self, from: jsonData) + } catch { + print("Bookmarks_\(#function)", error) + } + } } diff --git a/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_Controls/BookmarksView_Controls_ActionMenu.swift b/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_Controls/BookmarksView_Controls_ActionMenu.swift index b471151..5b0550a 100644 --- a/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_Controls/BookmarksView_Controls_ActionMenu.swift +++ b/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_Controls/BookmarksView_Controls_ActionMenu.swift @@ -15,12 +15,11 @@ struct BookmarksView_Controls_ActionMenu: View { var body: some View { MenuButton("Actions") { Button(action: { - Clipboard.copy(self.store.bookmarks.joined(separator: "\n")) + Clipboard.copy(self.store.bookmarks.json) }, label: { Text("Copy bookmarks to Clipboard") }) Button(action: { - let items = Clipboard.string.split(separator: .BackslashN) - self.store.bookmarks.append(items: items) - }, label: { Text("Paste bookmarks separated by \\n from Clipboard") }) + self.store.bookmarks.save(jsonString: Clipboard.string) + }, label: { Text("Paste bookmarks from Clipboard") }) Button(action: { self.store.bookmarks.clearAllCounters() }, label: { Text("Clear all counters") }) diff --git a/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_List/BookmarksView_List.swift b/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_List/BookmarksView_List.swift index 384297b..0466fe6 100644 --- a/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_List/BookmarksView_List.swift +++ b/ReaderTranslator/Views/ReaderView/Modes/Bookmarks/BookmarksView_List/BookmarksView_List.swift @@ -30,7 +30,6 @@ struct BookmarksView_List: View { return bookmarks .filter(counter: store.bookmarksCounterFilter) - .sorted .chunked(into: columnts) } diff --git a/ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift b/ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift index 462afcf..3cc0b6c 100644 --- a/ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift +++ b/ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift @@ -29,14 +29,7 @@ struct StatusBarView_Listener: View { Text(coordinator.status.status).onTapGesture(perform: coordinator.start) if coordinator.status == .connected { Button(action: { - do { - let jsonData = try JSONEncoder().encode(self.store.bookmarks) - if let jsonString = String(data: jsonData, encoding: .utf8) { - sharedConnection?.sendMove(jsonString) - } - } catch { - print("\(self.theClassName)_\(#function)", error) - } + sharedConnection?.sendMove(self.store.bookmarks.json) }, label: { Text("Send bookmarks") }) } } diff --git a/ReaderTranslatorMac/Info.plist b/ReaderTranslatorMac/Info.plist index 6450ba3..a7b9087 100644 --- a/ReaderTranslatorMac/Info.plist +++ b/ReaderTranslatorMac/Info.plist @@ -21,7 +21,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 1220 + 1224 LSApplicationCategoryType public.app-category.education LSMinimumSystemVersion diff --git a/ReaderTranslatorPlayer/Views/ViewModes/HostsView.swift b/ReaderTranslatorPlayer/Views/ViewModes/HostsView.swift index 1517c57..610ad65 100644 --- a/ReaderTranslatorPlayer/Views/ViewModes/HostsView.swift +++ b/ReaderTranslatorPlayer/Views/ViewModes/HostsView.swift @@ -87,15 +87,7 @@ extension HostsView.Coordinator: PeerConnectionDelegate { } func receivedMessage(content: Data?, message _: NWProtocolFramer.Message) { - guard let content = content else { return } - if let data = String(data: content, encoding: .unicode) { - guard let jsonData = data.data(using: .utf8) else { return } - do { - store.bookmarks = try JSONDecoder().decode(Bookmarks.self, from: jsonData) - } catch { - print("HostsView.Coordinator_\(#function)", error) - } - } + store.bookmarks.save(data: content) } } diff --git a/ReaderTranslatorSafari/Info.plist b/ReaderTranslatorSafari/Info.plist index 3327e7e..b9641ab 100644 --- a/ReaderTranslatorSafari/Info.plist +++ b/ReaderTranslatorSafari/Info.plist @@ -21,7 +21,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 1938 + 1942 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSAppleEventsUsageDescription