Skip to content

Commit

Permalink
Sync bookmarks between macOS and iOS #63: Add log. Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 22, 2019
1 parent 5ee0686 commit f6c253c
Show file tree
Hide file tree
Showing 24 changed files with 414 additions and 207 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Explore features, limitations and bugs *SwiftUI, Combine and Catalyst*.
- Watch videos with subtitles [#55](https://github.com/filimo/ReaderTranslator/issues/55)
![](files/Release_2.0.0_1.png)

## Comming features
## Coming features
- [Voicing selected words and phrases by Longman speakers](https://github.com/filimo/ReaderTranslator/issues/62)
- [New features for ReaderTranslatorPlayer](https://github.com/filimo/ReaderTranslator/issues/58)
- [Broadcast between macOS and mobile apps](https://github.com/filimo/ReaderTranslator/issues/63)
- [Sync bookmarks between macOS and mobile apps](https://github.com/filimo/ReaderTranslator/issues/63)
- [Bookmarks for mobile version](https://github.com/filimo/ReaderTranslator/issues/66)

![](files/Release_1.9_1.gif)
Expand Down
87 changes: 75 additions & 12 deletions ReaderTranslator.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
}
},
{
"package": "SwiftSoup",
"repositoryURL": "https://github.com/scinfu/SwiftSoup.git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForTesting = "NO"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F0A2179723A9355E00968133"
BuildableName = "ReaderTranslatorMacTests.xctest"
BlueprintName = "ReaderTranslatorMacTests"
BlueprintIdentifier = "F0120372239182DA008D0B47"
BuildableName = "ReaderTranslatorPlayer.app"
BlueprintName = "ReaderTranslatorPlayer"
ReferencedContainer = "container:ReaderTranslator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F0120372239182DA008D0B47"
BuildableName = "ReaderTranslatorPlayer.app"
BlueprintName = "ReaderTranslatorPlayer"
BlueprintIdentifier = "F0A2179723A9355E00968133"
BuildableName = "ReaderTranslatorMacTests.xctest"
BlueprintName = "ReaderTranslatorMacTests"
ReferencedContainer = "container:ReaderTranslator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F0754436234479DA00E1D88E"
BuildableName = "ReaderTranslatorMac.app"
BlueprintName = "ReaderTranslatorMac"
ReferencedContainer = "container:ReaderTranslator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F0A2179723A9355E00968133"
Expand All @@ -42,6 +56,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F0A2179723A9355E00968133"
BuildableName = "ReaderTranslatorMacTests.xctest"
BlueprintName = "ReaderTranslatorMacTests"
ReferencedContainer = "container:ReaderTranslator.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ import SwiftUI
@Binding var url: URL?
static let pdfView = PDFView()

class Coordinator: NSObject, PDFViewDelegate {
class Server: NSObject, PDFViewDelegate {
var parent: PDFKitViewRepresentable

init(_ parent: PDFKitViewRepresentable) {
self.parent = parent
}
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
func makeCoordinator() -> Server {
Server(self)
}

func makeUIView(context _: Context) -> PDFView {
Expand All @@ -75,7 +75,7 @@ import SwiftUI
Self.pdfView.autoresizingMask = [.flexibleWidth]
Self.pdfView.autoScales = true
Self.pdfView.document = PDFDocument(url: url)
Self.pdfView.delegate = context.coordinator
Self.pdfView.delegate = context.server
}
}

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

import Foundation

final class Debug {
static var appName: String {
guard let dictionary = Bundle.main.infoDictionary else {
return ""
}
if let version: String = dictionary["CFBundleName"] as? String {
return version
} else {
return ""
}
}

static func log(_ prefix: String, _ text: String, _ text2: String, _ text3: String = "", _ text4: String = "") {
let format = DateFormatter()
format.dateFormat = "HH:mm:ss.SSSS"
let date = format.string(from: Date())

print("\(date) \(prefix)[\(Debug.appName)][\(text)][\(text2)][\(text3)]: \(text4)")
}
}
62 changes: 62 additions & 0 deletions ReaderTranslator/Networking/NetBrowser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Browser.swift
// ReaderTranslatorPlayer
//
// Created by Viktor Kushnerov on 22/12/19.
// Copyright © 2019 Viktor Kushnerov. All rights reserved.
//
import Network
import SwiftUI

class NetBrowser: ObservableObject {
@Published var servers = [NWBrowser.Result]()
@Published var status = ConnectionClientStatus.none
@ObservedObject var store = Store.shared

func hostInfo(_ server: NWBrowser.Result) -> String {
if case let NWEndpoint.service(name: name, type: _, domain: _, interface: _) = server.endpoint {
return name
} else {
return "empty"
}
}

func connectToServer(server: NWBrowser.Result, passcode: String) {
Debug.log("P2P", "\(Self.self)", "\(#function)")
sharedConnection = PeerConnection(
endpoint: server.endpoint,
interface: server.interfaces.first,
passcode: passcode
)
sharedConnection?.delegate = self
sharedConnection?.startConnection()
}
}

extension NetBrowser: PeerBrowserDelegate {
func refreshResults(results: Set<NWBrowser.Result>) {
Debug.log("P2P", "\(Self.self)", "PeerBrowserDelegate", "\(#function)")
status = .none
servers = results.map { $0 }
}
}

extension NetBrowser: PeerConnectionDelegate {
func connectionReady() {
Debug.log("P2P", "\(Self.self)", "PeerBrowserDelegate", "\(#function)")
status = .connected
}

func connectionFailed() {
Debug.log("P2P", "\(Self.self)", "PeerBrowserDelegate", "\(#function)")
status = .failed(error: "failed")
}

func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
Debug.log("P2P", "\(Self.self)", "PeerBrowserDelegate", "\(#function)")
// switch message.readerTranslatorMessageType {
// case .invalid: print("Received invalid message")
// case .send: store.bookmarks.merge(data: content)
// }
}
}
9 changes: 8 additions & 1 deletion ReaderTranslator/Networking/PeerBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PeerBrowser {

// Start browsing for services.
func startBrowsing() {
Debug.log("P2P", "\(Self.self)", "\(#function)")
// Create parameters, and allow browsing over peer-to-peer link.
let parameters = NWParameters()
parameters.includePeerToPeer = true
Expand All @@ -36,10 +37,15 @@ class PeerBrowser {
let browser = NWBrowser(for: .bonjour(type: "_reader_translator._tcp", domain: nil), using: parameters)
self.browser = browser
browser.stateUpdateHandler = { newState in
Debug.log("P2P", "\(Self.self)", "NWBrowser", "stateUpdateHandler", "\(newState)")
switch newState {
case let .failed(error):
// Restart the browser if it fails.
print("Browser failed with \(error), restarting")
Debug.log(
"P2P",
"\(Self.self)",
"\(#function)",
"Browser failed with \(error), restarting")
browser.cancel()
self.startBrowsing()
default:
Expand All @@ -49,6 +55,7 @@ class PeerBrowser {

// When the list of discovered endpoints changes, refresh the delegate.
browser.browseResultsChangedHandler = { results, _ in
Debug.log("P2P", "\(Self.self)", "NWBrowser", "browseResultsChangedHandler")
self.delegate?.refreshResults(results: results)
}

Expand Down
5 changes: 5 additions & 0 deletions ReaderTranslator/Networking/PeerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class PeerConnection {

// Handle starting the peer-to-peer connection for both inbound and outbound connections.
func startConnection() {
Debug.log("P2P", "\(Self.self)", "\(#function)")
guard let connection = connection else {
return
}

connection.stateUpdateHandler = { newState in
Debug.log("P2P", "\(Self.self)", "NWConnection", "stateUpdateHandler", "\(newState)")
switch newState {
case .ready:
print("\(connection) established")
Expand Down Expand Up @@ -89,6 +91,7 @@ class PeerConnection {

// Handle sending a "send" message.
func sendBookmarks(_ bookmarks: String) {
Debug.log("P2P", "\(Self.self)", "\(#function)")
guard let connection = connection else {
return
}
Expand All @@ -109,11 +112,13 @@ class PeerConnection {

// Receive a message, deliver it to your delegate, and continue receiving more messages.
func receiveNextMessage() {
Debug.log("P2P", "\(Self.self)", "\(#function)")
guard let connection = connection else {
return
}

connection.receiveMessage { content, context, _, error in
Debug.log("P2P", "\(Self.self)", "NWConnection", "receiveMessage")
// Extract your message type from the received context.
let message = context?.protocolMetadata(definition: ReaderTranslatorProtocol.definition)
if let message = message as? NWProtocolFramer.Message {
Expand Down
4 changes: 4 additions & 0 deletions ReaderTranslator/Networking/PeerListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PeerListener {

// Start listening and advertising.
func startListening(stateUpdateHandler _: ((NWListener.State) -> Void)?) {
Debug.log("P2P", "\(Self.self)", "\(#function)")
do {
// Create the listener object.
let listener = try NWListener(using: NWParameters(passcode: passcode))
Expand All @@ -35,6 +36,7 @@ class PeerListener {
listener.service = NWListener.Service(name: name, type: "_reader_translator._tcp")

listener.newConnectionHandler = { newConnection in
Debug.log("P2P", "\(Self.self)", "NWListener", "newConnectionHandler", "\(newConnection)")
if let delegate = self.delegate {
if sharedConnection == nil {
// Accept a new connection.
Expand All @@ -55,12 +57,14 @@ class PeerListener {
}

func stopListening() {
Debug.log("P2P", "\(Self.self)", "\(#function)")
listener?.cancel()
sharedConnection = nil
}

// If the user changes their name, update the advertised name.
func resetName(_ name: String) {
Debug.log("P2P", "\(Self.self)", "\(#function)", name)
self.name = name
if let listener = listener {
// Reset the service to advertise.
Expand Down
2 changes: 2 additions & 0 deletions ReaderTranslator/Networking/ReaderTranslatorProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ReaderTranslatorProtocol: NWProtocolFramerImplementation {
messageLength: Int,
isComplete _: Bool
) {
Debug.log("P2P", "\(Self.self)", "\(#function)", "\(message)")
// Extract the type of message.
let type = message.readerTranslatorMessageType

Expand Down Expand Up @@ -83,6 +84,7 @@ class ReaderTranslatorProtocol: NWProtocolFramerImplementation {
messageType = parsedMessageType
}
let message = NWProtocolFramer.Message(readerTranslatorMessageType: messageType)
Debug.log("P2P", "\(Self.self)", "\(#function)", "\(message)")

// Deliver the body of the message, along with the message object.
if !framer.deliverInputNoCopy(length: Int(header.length), message: message, isComplete: true) {
Expand Down
Loading

0 comments on commit f6c253c

Please sign in to comment.