Skip to content

Commit

Permalink
Broadcast station #63: Send bookmarks from macOS to mobile devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 13, 2019
1 parent 236ea06 commit df79f99
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
14 changes: 13 additions & 1 deletion ReaderTranslator/Networking/ConnectionServerStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ enum ErrorServerConnection: Error {
}
}

enum ConnectionServerStatus {
enum ConnectionServerStatus: Equatable {
static func == (lhs: ConnectionServerStatus, rhs: ConnectionServerStatus) -> Bool {
switch (lhs, rhs) {
case (.none, .none): return true
case (.ready, .ready): return true
case (let .started(lName, lPasscode), let .started(rName, rPasscode)):
return lName == rName && lPasscode == rPasscode
case (.connected, .connected): return true
case (let .failed(lError), let .failed(rError)): return lError.status == rError.status
default: return false
}
}

case none
case started(name: String, passcode: String)
case ready
Expand Down
22 changes: 17 additions & 5 deletions ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import SwiftUI
import Network

struct StatusBarView_Listener: View {
@ObservedObject var store = Store.shared

class Coordinator: ObservableObject {
var name: String { "ReaderTranslator_\(connectionCount)" }
var generatePasscode: String { String("\(randomInt)\(randomInt)\(randomInt)\(randomInt)") }
Expand All @@ -23,7 +25,21 @@ struct StatusBarView_Listener: View {
@ObservedObject var coordinator: Coordinator = Coordinator()

var body: some View {
Text(coordinator.status.status).onTapGesture(perform: coordinator.start)
HStack {
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)
}
}, label: { Text("Send bookmarks") })
}
}
}
}

Expand Down Expand Up @@ -68,10 +84,6 @@ extension StatusBarView_Listener.Coordinator: PeerConnectionDelegate {
}
func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
guard let content = content else { return }
print(content)
logMessage(content)
}
func logMessage(_ content: Data) {
if let text = String(data: content, encoding: .unicode) {
print(text)
}
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>1169</string>
<string>1184</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 2 additions & 0 deletions ReaderTranslatorPlayer/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ class Store: ObservableObject {

@Published(wrappedValue: nil, key: "lastAudio") var lastAudio: URL?
@Published(key: "audioRate") var audioRate: Float = 1

@Published(key: "bookmarks") var bookmarks: Bookmarks = []
}
42 changes: 29 additions & 13 deletions ReaderTranslatorPlayer/Views/HostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ struct HostsView: View {
class Coordinator: ObservableObject {
@Published var hosts = [NWBrowser.Result]()
@Published var status = ConnectionClientStatus.none
@ObservedObject var store = Store.shared
}

@ObservedObject var store = Store.shared
@ObservedObject var coordinator = Coordinator()
@State var passcode = ""

Expand All @@ -25,18 +27,25 @@ struct HostsView: View {
}

var body: some View {
List {
VStack {
TextField("Enter pass code", text: $passcode)
ForEach(coordinator.hosts, id: \.self) { host in
Button(
action: { self.join(host: host) },
label: {
HStack {
Text(self.hostInfo(host))
Spacer()
Text(self.coordinator.status.status)
}
})
List {
ForEach(coordinator.hosts, id: \.self) { host in
Button(
action: { self.join(host: host) },
label: {
HStack {
Text(self.hostInfo(host))
Spacer()
Text(self.coordinator.status.status)
}
})
}
}
List {
ForEach(store.bookmarks, id: \.self) { bookmark in
Text("\(bookmark.text)")
}
}
}
}
Expand Down Expand Up @@ -69,15 +78,22 @@ extension HostsView.Coordinator: PeerBrowserDelegate {
extension HostsView.Coordinator: PeerConnectionDelegate {
func connectionReady() {
status = .connected
sharedConnection?.sendMove("Let's go")
}

func connectionFailed() {
status = .failed(error: "failed")
}

func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
print("\(#function)")
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)
}
}
}
}

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>1870</string>
<string>1900</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit df79f99

Please sign in to comment.