Skip to content

Commit

Permalink
Broadcast between macOS and mobile apps #63: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 16, 2019
1 parent e824c21 commit b65910b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
28 changes: 28 additions & 0 deletions ReaderTranslator/Stores/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct BookmarksView_List: View {

return bookmarks
.filter(counter: store.bookmarksCounterFilter)
.sorted
.chunked(into: columnts)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") })
}
}
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1220</string>
<string>1224</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
10 changes: 1 addition & 9 deletions ReaderTranslatorPlayer/Views/ViewModes/HostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorSafari/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1938</string>
<string>1942</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit b65910b

Please sign in to comment.