Skip to content

Commit

Permalink
Updated implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Moraleda committed Apr 28, 2023
1 parent 6e41f28 commit 6915274
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 335 deletions.
11 changes: 7 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import PackageDescription

let package = Package(
name: "SwiftSpeech",
platforms: [.iOS(.v13), .macOS(.v10_15)],
platforms: [.iOS(.v13)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SwiftSpeech",
targets: ["SwiftSpeech"]),
targets: ["SwiftSpeech"]
),
// .executable(name: "SwiftSpeechExample", targets: ["SwiftSpeechExample"]),
],
dependencies: [
Expand All @@ -22,9 +23,11 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftSpeech",
dependencies: []),
dependencies: []
),
.testTarget(
name: "SwiftSpeechTests",
dependencies: ["SwiftSpeech"]),
dependencies: ["SwiftSpeech"]
),
]
)
21 changes: 10 additions & 11 deletions Sources/SwiftSpeech/Authorization.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
//
// Authorization.swift
//
//
//
// Created by Cay Zhang on 2020/7/22.
//

import SwiftUI
import Combine
import Speech
import SwiftUI

extension SwiftSpeech {

public static func requestSpeechRecognitionAuthorization() {
AuthorizationCenter.shared.requestSpeechRecognitionAuthorization()
}

class AuthorizationCenter: ObservableObject {
@Published var speechRecognitionAuthorizationStatus: SFSpeechRecognizerAuthorizationStatus = SFSpeechRecognizer.authorizationStatus()

func requestSpeechRecognitionAuthorization() {
// Asynchronously make the authorization request.
SFSpeechRecognizer.requestAuthorization { authStatus in
Expand All @@ -28,26 +27,26 @@ extension SwiftSpeech {
}
}
}

static let shared = AuthorizationCenter()
}
}

@propertyWrapper public struct SpeechRecognitionAuthStatus: DynamicProperty {
@ObservedObject var authCenter = SwiftSpeech.AuthorizationCenter.shared

let trueValues: Set<SFSpeechRecognizerAuthorizationStatus>

public var wrappedValue: SFSpeechRecognizerAuthorizationStatus {
SwiftSpeech.AuthorizationCenter.shared.speechRecognitionAuthorizationStatus
}

public init(trueValues: Set<SFSpeechRecognizerAuthorizationStatus> = [.authorized]) {
self.trueValues = trueValues
}

public var projectedValue: Bool {
self.trueValues.contains(SwiftSpeech.AuthorizationCenter.shared.speechRecognitionAuthorizationStatus)
trueValues.contains(SwiftSpeech.AuthorizationCenter.shared.speechRecognitionAuthorizationStatus)
}
}

Expand Down
46 changes: 20 additions & 26 deletions Sources/SwiftSpeech/Demos.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
//
// Demos.swift
//
//
//
// Created by Cay Zhang on 2020/2/23.
//

import SwiftUI
import Combine
import Speech
import SwiftUI

public extension SwiftSpeech.Demos {

struct Basic : View {

struct Basic: View {
var sessionConfiguration: SwiftSpeech.Session.Configuration

@State private var text = "Tap to Speak"

public init(sessionConfiguration: SwiftSpeech.Session.Configuration) {
self.sessionConfiguration = sessionConfiguration
}

public init(locale: Locale = .current) {
self.init(sessionConfiguration: SwiftSpeech.Session.Configuration(locale: locale))
}

public init(localeIdentifier: String) {
self.init(locale: Locale(identifier: localeIdentifier))
}

public var body: some View {
VStack(spacing: 35.0) {
Text(text)
.font(.system(size: 25, weight: .bold, design: .default))
SwiftSpeech.RecordButton()
RecordButton()
.swiftSpeechToggleRecordingOnTap(sessionConfiguration: sessionConfiguration, animation: .spring(response: 0.3, dampingFraction: 0.5, blendDuration: 0))
.onRecognizeLatest(update: $text)

}.onAppear {
SwiftSpeech.requestSpeechRecognitionAuthorization()
}
}

}

struct Colors : View {

struct Colors: View {
@State private var text = "Hold and say a color!"

static let colorDictionary: [String : Color] = [
static let colorDictionary: [String: Color] = [
"black": .black,
"white": .white,
"blue": .blue,
Expand All @@ -58,7 +54,7 @@ public extension SwiftSpeech.Demos {
"pink": .pink,
"purple": .purple,
"red": .red,
"yellow": .yellow
"yellow": .yellow,
]

var color: Color? {
Expand All @@ -69,38 +65,36 @@ public extension SwiftSpeech.Demos {
.value
}

public init() { }
public init() {}

public var body: some View {
VStack(spacing: 35.0) {
Text(text)
.font(.system(size: 25, weight: .bold, design: .default))
.foregroundColor(color)
SwiftSpeech.RecordButton()
RecordButton()
.accentColor(color)
.swiftSpeechRecordOnHold(locale: Locale(identifier: "en_US"), animation: .spring(response: 0.3, dampingFraction: 0.5, blendDuration: 0))
.onRecognizeLatest(update: $text)
}.onAppear {
SwiftSpeech.requestSpeechRecognitionAuthorization()
}
}

}

struct List : View {

struct List: View {
var sessionConfiguration: SwiftSpeech.Session.Configuration

@State var list: [(session: SwiftSpeech.Session, text: String)] = []

public init(sessionConfiguration: SwiftSpeech.Session.Configuration) {
self.sessionConfiguration = sessionConfiguration
}

public init(locale: Locale = .current) {
self.init(sessionConfiguration: SwiftSpeech.Session.Configuration(locale: locale))
}

public init(localeIdentifier: String) {
self.init(locale: Locale(identifier: localeIdentifier))
}
Expand All @@ -112,7 +106,7 @@ public extension SwiftSpeech.Demos {
Text(pair.text)
}
}.overlay(
SwiftSpeech.RecordButton()
RecordButton()
.swiftSpeechRecordOnHold(
sessionConfiguration: sessionConfiguration,
animation: .spring(response: 0.3, dampingFraction: 0.5, blendDuration: 0),
Expand Down
15 changes: 7 additions & 8 deletions Sources/SwiftSpeech/Environments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,44 @@
// Created by Cay Zhang on 2020/2/16.
//

import SwiftUI
import Combine
import Speech
import SwiftUI

extension SwiftSpeech.EnvironmentKeys {
struct SwiftSpeechState: EnvironmentKey {
static let defaultValue: SwiftSpeech.State = .pending
}

struct ActionsOnStartRecording: EnvironmentKey {
static let defaultValue: [(_ session: SwiftSpeech.Session) -> Void] = []
}

struct ActionsOnStopRecording: EnvironmentKey {
static let defaultValue: [(_ session: SwiftSpeech.Session) -> Void] = []
}

struct ActionsOnCancelRecording: EnvironmentKey {
static let defaultValue: [(_ session: SwiftSpeech.Session) -> Void] = []
}
}

public extension EnvironmentValues {

var swiftSpeechState: SwiftSpeech.State {
get { self[SwiftSpeech.EnvironmentKeys.SwiftSpeechState.self] }
set { self[SwiftSpeech.EnvironmentKeys.SwiftSpeechState.self] = newValue }
}

var actionsOnStartRecording: [(_ session: SwiftSpeech.Session) -> Void] {
get { self[SwiftSpeech.EnvironmentKeys.ActionsOnStartRecording.self] }
set { self[SwiftSpeech.EnvironmentKeys.ActionsOnStartRecording.self] = newValue }
}

var actionsOnStopRecording: [(_ session: SwiftSpeech.Session) -> Void] {
get { self[SwiftSpeech.EnvironmentKeys.ActionsOnStopRecording.self] }
set { self[SwiftSpeech.EnvironmentKeys.ActionsOnStopRecording.self] = newValue }
}

var actionsOnCancelRecording: [(_ session: SwiftSpeech.Session) -> Void] {
get { self[SwiftSpeech.EnvironmentKeys.ActionsOnCancelRecording.self] }
set { self[SwiftSpeech.EnvironmentKeys.ActionsOnCancelRecording.self] = newValue }
Expand Down
Loading

0 comments on commit 6915274

Please sign in to comment.