diff --git a/ListenerGS/GSConnection.swift b/ListenerGS/GSConnection.swift index cd131df..535571d 100644 --- a/ListenerGS/GSConnection.swift +++ b/ListenerGS/GSConnection.swift @@ -68,8 +68,8 @@ class GSConnection : ObservableObject { private let writeQueue = OperationQueue() private var mainQueue = OperationQueue.main - private var condition = NSCondition() private var canSend = true + private var lastSent = "" private func changeState(newState : GSConnectionState) { @@ -154,10 +154,10 @@ class GSConnection : ObservableObject { do { let unpacked = try unpack(" Bool { @@ -236,6 +232,7 @@ class GSConnection : ObservableObject { func listen(speechForwarder: SpeechForwarderProtocol) { textHeard = "" + lastSent = "" writeQueue.addOperation { if (!self.sendListenMsg(isListening: true)) { self.errorOccurred(title: "Write Error", message: "Unable to send data to the GS") @@ -252,57 +249,39 @@ class GSConnection : ObservableObject { } self.speechForwarder = speechForwarder } - - self.send() - - _ = self.sendListenMsg(isListening: false) - - self.mainQueue.addOperation { - if (self.state == .stoplistening) { - self.changeState(newState: .connected) + } + } + + private func trySend() { + if (textHeard == lastSent) { + if (state == .stoplistening) { + writeQueue.addOperation { + _ = self.sendListenMsg(isListening: false) + } + changeState(newState: .connected) + } + return + } + + if (!canSend) { + return + } + + canSend = false + let stringToSend = textHeard + writeQueue.addOperation { + if self.send(latestText: stringToSend, lastSent: self.lastSent) { + self.mainQueue.addOperation { + self.lastSent = stringToSend + self.trySend() } } } } - func set(text:String) - { - condition.lock() + func set(text:String) { textHeard = text - condition.broadcast() - condition.unlock() - } - - private func send() { - var stringLastSent = "" - var stringToSend = "" - - while true { - condition.lock() - guard client != nil else { - condition.unlock() - return - } - if ((stringLastSent == textHeard) && (state == .stoplistening)) { - condition.unlock() - return - } - if ((!canSend) || - (stringLastSent == textHeard)) { - condition.wait() - condition.unlock() - continue - } - stringToSend = textHeard - condition.unlock() - - if send(latestText: stringToSend, lastSent: stringLastSent) { - stringLastSent = stringToSend - condition.lock() - canSend = false - condition.unlock() - } - } + trySend() } private func send(latestText : String, lastSent: String) -> Bool { diff --git a/ListenerGS/Info.plist b/ListenerGS/Info.plist index a7d1a70..21d4e75 100644 --- a/ListenerGS/Info.plist +++ b/ListenerGS/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.0 CFBundleVersion - 598 + 619 LSApplicationCategoryType public.app-category.utilities LSRequiresIPhoneOS diff --git a/ListenerGS/RichText/Webview.swift b/ListenerGS/RichText/Webview.swift index 9f87fc1..b548ab2 100644 --- a/ListenerGS/RichText/Webview.swift +++ b/ListenerGS/RichText/Webview.swift @@ -80,7 +80,6 @@ struct Webview: UIViewRepresentable { decisionHandler(WKNavigationActionPolicy.cancel) return } - print("no link") decisionHandler(WKNavigationActionPolicy.allow) } } diff --git a/ListenerGSTests/ListenerGSTests.swift b/ListenerGSTests/ListenerGSTests.swift index b2ec27a..197bf71 100644 --- a/ListenerGSTests/ListenerGSTests.swift +++ b/ListenerGSTests/ListenerGSTests.swift @@ -103,9 +103,10 @@ class ListenerGSTests: XCTestCase { XCTAssertEqual(connection.textHeard, "Hello, everyone!") XCTAssert(server.sendMore()) + connection.waitForMain() + connection.waitForWriteQueue() XCTAssertEqual(server.getText(), "\u{7f}\u{7f}\u{7f}\u{7f}\u{7f}\u{7f}everyone!") - connection.waitForWriteQueue() connection.waitForMain() XCTAssert(server.getListenState(isListening: false))