Skip to content

Commit

Permalink
doc(voice): add swift docs to all customisation options
Browse files Browse the repository at this point in the history
Also some refactoring (extracting methods)
  • Loading branch information
spinach committed Jul 16, 2018
1 parent 2be56cc commit 7fb2c89
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 32 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion VoiceOverlay-Demo/VoiceOverlay-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ViewController: UIViewController, VoiceOverlayDelegate {
// If you want to start recording as soon as modal view pops up, change to true
voiceOverlayController.settings.autoStart = true
voiceOverlayController.settings.autoStop = true
voiceOverlayController.settings.showResultScreen = false
voiceOverlayController.settings.showResultScreen = true
//voiceOverlayController.settings.layout.recordingScreen.titleListening = "custom title when listening"

}
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion VoiceOverlay/Controllers/NoPermissionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class NoPermissionViewController: UIViewController {

if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true

})
}
}
Expand Down
2 changes: 1 addition & 1 deletion VoiceOverlay/Controllers/PermissionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import AVFoundation

public class PermissionViewController: UIViewController {
class PermissionViewController: UIViewController {

var dismissHandler: (() -> ())? = nil
var speechController: SpeechController!
Expand Down
43 changes: 22 additions & 21 deletions VoiceOverlay/Controllers/RecordingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class RecordingViewController: UIViewController {
}
}

// handle notification
@objc func resultScreenTextReceived(_ notification: NSNotification) {

if let resultScreenText = notification.userInfo?["resultScreenText"] as? NSAttributedString {
guard let resultViewController = resultViewController else { return }
resultScreentimer?.invalidate()
Expand Down Expand Up @@ -100,7 +98,7 @@ class RecordingViewController: UIViewController {
self.dismissHandler?(false)
}
}

func toggleRecording(_ recordingButton: RecordingButton, dismiss: Bool = true) {
isRecording = !isRecording
recordingButton.animate(isRecording)
Expand All @@ -122,24 +120,7 @@ class RecordingViewController: UIViewController {
if dismiss {
if settings.showResultScreen {
resultViewController = ResultViewController()
guard let resultViewController = resultViewController else { return }
resultViewController.dismissHandler = { [unowned self] (retry) in
self.resultScreentimer?.invalidate()
self.resultViewController?.dismissMe(animated: false) {
self.dismissMe(animated: false) {
self.dismissHandler?(true)
}
}
}
resultViewController.constants = self.settings.layout.resultScreen
self.present(resultViewController, animated: false)
resultScreentimer = Timer.scheduledTimer(withTimeInterval: self.settings.showResultScreenTimeout, repeats: false, block: { (_) in
self.resultViewController?.dismissMe(animated: false) {
self.dismissMe(animated: false) {
self.dismissHandler?(false)
}
}
})
setup(resultViewController!)
} else {
dismissMe(animated: true) {
self.dismissHandler?(false)
Expand Down Expand Up @@ -189,6 +170,26 @@ class RecordingViewController: UIViewController {
})
}

fileprivate func setup(_ resultViewController: ResultViewController) {
resultViewController.dismissHandler = { [unowned self] (retry) in
self.resultScreentimer?.invalidate()
self.resultViewController?.dismissMe(animated: false) {
self.dismissMe(animated: false) {
self.dismissHandler?(true)
}
}
}
resultViewController.constants = self.settings.layout.resultScreen
self.present(resultViewController, animated: false)
resultScreentimer = Timer.scheduledTimer(withTimeInterval: self.settings.showResultScreenTimeout, repeats: false, block: { (_) in
self.resultViewController?.dismissMe(animated: false) {
self.dismissMe(animated: false) {
self.dismissHandler?(false)
}
}
})
}

func handleVoiceError(_ error: Error?) {
titleLabel.text = constants.titleError
subtitleLabel.text = constants.subtitleError
Expand Down
1 change: 1 addition & 0 deletions VoiceOverlay/Controllers/ResultViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ResultViewController: UIViewController {
let subtitleLabel = UILabel()
let startAgainButton = UIButton()

// The bool specifies whether we dismiss with retry or not
var dismissHandler: ((Bool) -> ())? = nil

var voiceOutputText: NSAttributedString? {
Expand Down
9 changes: 4 additions & 5 deletions VoiceOverlay/Controllers/VoiceOverlayController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import UIKit
import Speech

/// Controller that takes care of launching a voice overlay and providing handlers to listen to text and error events.
public class VoiceOverlayController {

let permissionViewController = PermissionViewController()
Expand All @@ -22,11 +23,9 @@ public class VoiceOverlayController {
public var settings: VoiceUISettings = VoiceUISettings()
var recordingViewController: RecordingViewController? = RecordingViewController()


public init() {}

// TODO: Define datasource that will be used to give back the text from the SpeechController
public var datasource: Any? = nil

public init() {}

public func start(on view: UIViewController, textHandler: @escaping SpeechTextHandler, errorHandler: @escaping SpeechErrorHandler) {
self.speechTextHandler = textHandler
Expand Down Expand Up @@ -59,7 +58,7 @@ public class VoiceOverlayController {
}
}

func checkSpeechAuthorizationStatusAndRedirectToCorrectScreen(_ view: UIViewController) {
fileprivate func checkSpeechAuthorizationStatusAndRedirectToCorrectScreen(_ view: UIViewController) {

// speech permissions
switch SFSpeechRecognizer.authorizationStatus() {
Expand Down
22 changes: 19 additions & 3 deletions VoiceOverlay/Helpers/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ import Foundation
import CoreGraphics
import UIKit

/// Customisation Settings for the Voice Overlay
public class VoiceUISettings {

public var autoStart = false
/// Specifies whether the overlay directly starts recording (true), or if it requires the user to click the mic (false).
public var autoStart = true

/// Specifies whether the overlay stops recording after the user stops talking for `autoStopTimeout` seconds (true), or if it requires the user to click the mic (false).
public var autoStop = true

/// when autoStop is set to true, autoStopTimeout determines the amount of silence time before the user stops talking.
public var autoStopTimeout: TimeInterval = 2

/// Whether or not to show a result screen after the recording is finished.
public var showResultScreen = false

/// Timeout for showing the result screen in case no resultScreenText is provided on time.
public var showResultScreenTimeout: TimeInterval = 2

/// Time for showing the result screen with the provided resultScreenText.
public var showResultScreenTime: TimeInterval = 4
public var autoStopTimeout: TimeInterval = 2
public var layout: Layout = Layout()

/// The processed result screen text that should be appear in the result screen.
public var resultScreenText: NSAttributedString? {
didSet {
if let resultScreenText = resultScreenText {
Expand All @@ -27,6 +40,9 @@ public class VoiceUISettings {
}
}

/// The layout and style of all screens of the voice overlay.
public var layout: Layout = Layout()

public class Layout {

public var permissionScreen = PermissionScreenConstants()
Expand Down

0 comments on commit 7fb2c89

Please sign in to comment.