-
Notifications
You must be signed in to change notification settings - Fork 53
/
Demos.swift
144 lines (118 loc) · 5.03 KB
/
Demos.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Demos.swift
//
//
// Created by Cay Zhang on 2020/2/23.
//
import SwiftUI
import Combine
import Speech
public extension SwiftSpeech.Demos {
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()
.swiftSpeechToggleRecordingOnTap(sessionConfiguration: sessionConfiguration, animation: .spring(response: 0.3, dampingFraction: 0.5, blendDuration: 0))
.onRecognizeLatest(update: $text)
}.onAppear {
SwiftSpeech.requestSpeechRecognitionAuthorization()
}
}
}
struct Colors : View {
@State private var text = "Hold and say a color!"
static let colorDictionary: [String : Color] = [
"black": .black,
"white": .white,
"blue": .blue,
"gray": .gray,
"green": .green,
"orange": .orange,
"pink": .pink,
"purple": .purple,
"red": .red,
"yellow": .yellow
]
var color: Color? {
Colors.colorDictionary
.first { pair in
text.lowercased().contains(pair.key)
}?
.value
}
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()
.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 {
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))
}
public var body: some View {
NavigationView {
SwiftUI.List {
ForEach(list, id: \.session.id) { pair in
Text(pair.text)
}
}.overlay(
SwiftSpeech.RecordButton()
.swiftSpeechRecordOnHold(
sessionConfiguration: sessionConfiguration,
animation: .spring(response: 0.3, dampingFraction: 0.5, blendDuration: 0),
distanceToCancel: 100.0
).onStartRecording { session in
list.append((session, ""))
}.onCancelRecording { session in
_ = list.firstIndex { $0.session.id == session.id }
.map { list.remove(at: $0) }
}.onRecognize(includePartialResults: true) { session, result in
list.firstIndex { $0.session.id == session.id }
.map { index in
list[index].text = result.bestTranscription.formattedString + (result.isFinal ? "" : "...")
}
} handleError: { session, error in
list.firstIndex { $0.session.id == session.id }
.map { index in
list[index].text = "Error \((error as NSError).code)"
}
}.padding(20),
alignment: .bottom
).navigationBarTitle(Text("SwiftSpeech"))
}.onAppear {
SwiftSpeech.requestSpeechRecognitionAuthorization()
}
}
}
}