Skip to content

Commit

Permalink
Broadcast station #63: Show connection status in ReaderTranslatorMac.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 13, 2019
1 parent 8413cb9 commit 55b5010
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 46 deletions.
8 changes: 8 additions & 0 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
F04C830F23617BE6003A25B4 /* NSObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04C830E23617BE6003A25B4 /* NSObject.swift */; };
F04C831023617BE6003A25B4 /* NSObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04C830E23617BE6003A25B4 /* NSObject.swift */; };
F04C831123617BE6003A25B4 /* NSObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04C830E23617BE6003A25B4 /* NSObject.swift */; };
F04D5D3F23A3677800424479 /* ConnectionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04D5D3E23A3677800424479 /* ConnectionStatus.swift */; };
F04D5D4023A3677800424479 /* ConnectionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04D5D3E23A3677800424479 /* ConnectionStatus.swift */; };
F04D5D4123A3677800424479 /* ConnectionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = F04D5D3E23A3677800424479 /* ConnectionStatus.swift */; };
F0505C542360D474004F2D50 /* LongmanRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0505C522360D37C004F2D50 /* LongmanRepresenter.swift */; };
F0505C552360D475004F2D50 /* LongmanRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0505C522360D37C004F2D50 /* LongmanRepresenter.swift */; };
F0505C562360D485004F2D50 /* LongmanView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0505C532360D3DA004F2D50 /* LongmanView.swift */; };
Expand Down Expand Up @@ -304,6 +307,7 @@
F04C830B23617653003A25B4 /* StatusBarView_ViewsEnabler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_ViewsEnabler.swift; sourceTree = "<group>"; };
F04C830E23617BE6003A25B4 /* NSObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSObject.swift; sourceTree = "<group>"; };
F04C8312236187D6003A25B4 /* MacmillanRepresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacmillanRepresenter.swift; sourceTree = "<group>"; };
F04D5D3E23A3677800424479 /* ConnectionStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionStatus.swift; sourceTree = "<group>"; };
F0505C522360D37C004F2D50 /* LongmanRepresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongmanRepresenter.swift; sourceTree = "<group>"; };
F0505C532360D3DA004F2D50 /* LongmanView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongmanView.swift; sourceTree = "<group>"; };
F0553727239BE3C700BA24BA /* LongmanStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongmanStore.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -622,6 +626,7 @@
F02B04BA23A2896900F93B84 /* PeerListener.swift */,
F02B04BE23A2898300F93B84 /* PeerConnection.swift */,
F02B04B223A2847700F93B84 /* GameProtocol.swift */,
F04D5D3E23A3677800424479 /* ConnectionStatus.swift */,
);
path = Networking;
sourceTree = "<group>";
Expand Down Expand Up @@ -1124,6 +1129,7 @@
files = (
F08EBB0D23956A20009025D3 /* NetworkManager.swift in Sources */,
F058C7F32397F968002C84F0 /* WKScriptsSetup.swift in Sources */,
F04D5D4123A3677800424479 /* ConnectionStatus.swift in Sources */,
F012038723928631008D0B47 /* UserDefault.swift in Sources */,
F058C7FB2397FABD002C84F0 /* String.swift in Sources */,
F0418B9A239A32FD00B59C66 /* Bookmark.swift in Sources */,
Expand Down Expand Up @@ -1238,6 +1244,7 @@
F075443A234479DA00E1D88E /* AppDelegate.swift in Sources */,
F0C36A972359AF76001E396C /* StatusBarView_Bookmarks.swift in Sources */,
F02989EC2360488800DF722E /* OpenPanel.swift in Sources */,
F04D5D4023A3677800424479 /* ConnectionStatus.swift in Sources */,
F0039EC323447E24002F3F95 /* SharedContainer.swift in Sources */,
F0C53179234B3EE1003174B5 /* WKCoordinator.swift in Sources */,
F0505C552360D475004F2D50 /* LongmanRepresenter.swift in Sources */,
Expand Down Expand Up @@ -1332,6 +1339,7 @@
F04C830C23617653003A25B4 /* StatusBarView_ViewsEnabler.swift in Sources */,
F023CEB2239A5674006DE5EA /* BookmarksView_List.swift in Sources */,
F02B04BF23A2898300F93B84 /* PeerConnection.swift in Sources */,
F04D5D3F23A3677800424479 /* ConnectionStatus.swift in Sources */,
F058C800239930F2002C84F0 /* BookmarksView.swift in Sources */,
F0FCDD40236189750016F23F /* MacmillanView.swift in Sources */,
F0C4EDA42349260000CCD97A /* ReversoView.swift in Sources */,
Expand Down
22 changes: 11 additions & 11 deletions ReaderTranslator/Model/AvailableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,27 @@ enum AvailableView: String, Codable, CaseIterable {
var view: some View {
switch self {
case .wikipedia:
return AnyView(WikipediaView())
return WikipediaView().any
case .reverso:
return AnyView(ReversoView())
return ReversoView().any
case .gTranslator:
return AnyView(GTranslatorView())
return GTranslatorView().any
case .yTranslator:
return AnyView(YTranslatorView())
return YTranslatorView().any
case .longman:
return AnyView(LongmanView())
return LongmanView().any
case .macmillan:
return AnyView(MacmillanView())
return MacmillanView().any
case .collins:
return AnyView(CollinsView())
return CollinsView().any
case .bookmarks:
return AnyView(BookmarksView())
return BookmarksView().any
case .pdf:
return AnyView(ReaderView_Pdf())
return ReaderView_Pdf().any
case .web:
return AnyView(ReaderView_Web())
return ReaderView_Web().any
case .safari:
return AnyView(SafariView())
return SafariView().any
}
}

Expand Down
46 changes: 46 additions & 0 deletions ReaderTranslator/Networking/ConnectionStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// ConnectionStatus.swift
// ReaderTranslator
//
// Created by Viktor Kushnerov on 13/12/19.
// Copyright © 2019 Viktor Kushnerov. All rights reserved.
//

import Network

enum ErrorConnection: Error {
case listener(error: NWError)
case connection(text: String)

var status: String {
switch self {
case .listener(let error):
return "\(error)"
case .connection(let text):
return text
}
}
}

enum ConnectionStatus {
case none
case started(name: String, passcode: String)
case ready
case connected
case failed(error: ErrorConnection)

var status: String {
switch self {
case .none:
return "Start the server"
case .ready:
return "The server is ready"
case .started(let name, let passcode):
return "Pass code for \(name): \(passcode)"
case .connected:
return "The server is connected"
case .failed(let error):
return "The server: \(error.status)"
}
}
}
17 changes: 1 addition & 16 deletions ReaderTranslator/Networking/PeerListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ class PeerListener {
self.delegate = delegate
self.name = name
self.passcode = passcode
startListening()
}

// Start listening and advertising.
func startListening() {
func startListening(stateUpdateHandler: ((NWListener.State) -> Void)?) {
do {
// Create the listener object.
let listener = try NWListener(using: NWParameters(passcode: passcode))
Expand All @@ -36,20 +35,6 @@ class PeerListener {
// Set the service to advertise.
listener.service = NWListener.Service(name: self.name, type: "_reader_translator._tcp")

listener.stateUpdateHandler = { newState in
switch newState {
case .ready:
print("Listener ready on \(String(describing: listener.port))")
case .failed(let error):
// If the listener fails, re-start.
print("Listener failed with \(error), restarting")
listener.cancel()
self.startListening()
default:
break
}
}

listener.newConnectionHandler = { newConnection in
if let delegate = self.delegate {
if sharedConnection == nil {
Expand Down
57 changes: 40 additions & 17 deletions ReaderTranslator/Views/StatusBarView/StatusBarView_Listener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,61 @@ import SwiftUI
import Network

struct StatusBarView_Listener: View {
class Coordinator {}
class Coordinator: ObservableObject {
var name: String { "ReaderTranslator_\(connectionCount)" }
var generatePasscode: String { String("\(randomInt)\(randomInt)\(randomInt)\(randomInt)") }
private var randomInt: Int { Int.random(in: 0...9) }

@State var passcode = ""
private var generatePasscode: String { String("\(randomInt)\(randomInt)\(randomInt)\(randomInt)") }
private let coordinator = Coordinator()
private var randomInt: Int { Int.random(in: 0...9) }
private let name = "ReaderTranslator"
@State var connectionCount = 0
@Published var connectionCount = 0
@Published var status = ConnectionStatus.none
@Published var passcode = ""
}

@ObservedObject var coordinator: Coordinator = Coordinator()

var body: some View {
Text("passcode: \(passcode)").onTapGesture {
self.passcode = self.generatePasscode
self.connectionCount += 1
sharedListener?.stopListening()
sharedListener = PeerListener(
name: "\(self.name)_\(self.connectionCount)",
passcode: self.passcode,
delegate: self.coordinator)
Text(coordinator.status.status).onTapGesture(perform: coordinator.start)
}
}

extension StatusBarView_Listener.Coordinator {
func stateUpdateHandler(newState: NWListener.State) {
switch newState {
case .ready:
status = .ready
case .failed(let error):
status = .failed(error: .listener(error: error))
// If the listener fails, re-start.
print("Listener failed with \(error), restarting")
sharedListener?.listener?.cancel()
sharedListener?.startListening(stateUpdateHandler: stateUpdateHandler)

default:
break
}
}

func start() {
self.passcode = self.generatePasscode
self.connectionCount += 1
sharedListener?.stopListening()

status = .started(name: name, passcode: passcode)
sharedListener = PeerListener(name: name, passcode: self.passcode, delegate: self)
sharedListener?.startListening(stateUpdateHandler: stateUpdateHandler)
}
}

extension StatusBarView_Listener.Coordinator: PeerConnectionDelegate {
// When a connection becomes ready, move into game mode.
func connectionReady() {
//navigationController?.performSegue(withIdentifier: "showGameSegue", sender: nil)
print("\(#function)")
status = .connected
}

// Ignore connection failures and messages prior to starting a game.
func connectionFailed() {
print("\(#function)")
status = .failed(error: .connection(text: "connection failed"))
}
func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
print(2)
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>1103</string>
<string>1133</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
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>1769</string>
<string>1825</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit 55b5010

Please sign in to comment.