|
| 1 | +import SwiftUI |
| 2 | +import AuralKit |
| 3 | +import Speech |
| 4 | + |
| 5 | +// MARK: - Sub-views |
| 6 | + |
| 7 | +struct TranscriptContentView: View { |
| 8 | + let finalizedText: AttributedString |
| 9 | + let volatileText: AttributedString |
| 10 | + let currentTimeRange: String |
| 11 | + let isTranscribing: Bool |
| 12 | + |
| 13 | + var body: some View { |
| 14 | + VStack(alignment: .leading, spacing: 16) { |
| 15 | + if !finalizedText.characters.isEmpty { |
| 16 | + Text(finalizedText) |
| 17 | + .font(.body) |
| 18 | + .foregroundColor(.primary) |
| 19 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 20 | + .padding() |
| 21 | + .background(Color.gray.opacity(0.1)) |
| 22 | + .cornerRadius(12) |
| 23 | + } |
| 24 | + |
| 25 | + if !volatileText.characters.isEmpty { |
| 26 | + VolatileTextView( |
| 27 | + volatileText: volatileText, |
| 28 | + currentTimeRange: currentTimeRange |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + if finalizedText.characters.isEmpty && volatileText.characters.isEmpty && !isTranscribing { |
| 33 | + EmptyStateView() |
| 34 | + } |
| 35 | + } |
| 36 | + .padding() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +struct VolatileTextView: View { |
| 41 | + let volatileText: AttributedString |
| 42 | + let currentTimeRange: String |
| 43 | + |
| 44 | + var body: some View { |
| 45 | + VStack(alignment: .leading, spacing: 8) { |
| 46 | + HStack(spacing: 8) { |
| 47 | + ProgressView() |
| 48 | + .scaleEffect(0.8) |
| 49 | + Text(volatileText) |
| 50 | + .font(.body) |
| 51 | + .italic() |
| 52 | + } |
| 53 | + |
| 54 | + if !currentTimeRange.isEmpty { |
| 55 | + Label(currentTimeRange, systemImage: "clock") |
| 56 | + .font(.caption) |
| 57 | + .foregroundColor(.secondary) |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 62 | + .padding() |
| 63 | + .background(Color.blue.opacity(0.1)) |
| 64 | + .cornerRadius(12) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +struct EmptyStateView: View { |
| 69 | + var body: some View { |
| 70 | + VStack(spacing: 12) { |
| 71 | + Image(systemName: "mic.circle") |
| 72 | + .font(.system(size: 60)) |
| 73 | + .foregroundColor(.secondary) |
| 74 | + Text("Tap the button below to start transcribing") |
| 75 | + .font(.headline) |
| 76 | + .foregroundColor(.secondary) |
| 77 | + .multilineTextAlignment(.center) |
| 78 | + } |
| 79 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 80 | + .padding(.top, 100) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +struct LanguageSelectorView: View { |
| 85 | + @Bindable var manager: TranscriptionManager |
| 86 | + let commonLocales: [Locale] |
| 87 | + |
| 88 | + var body: some View { |
| 89 | + HStack { |
| 90 | + Image(systemName: "globe") |
| 91 | + .foregroundColor(.secondary) |
| 92 | + Text("Language:") |
| 93 | + .foregroundColor(.secondary) |
| 94 | + Spacer() |
| 95 | + Menu { |
| 96 | + ForEach(commonLocales, id: \.identifier) { locale in |
| 97 | + Button { |
| 98 | + manager.selectedLocale = locale |
| 99 | + } label: { |
| 100 | + HStack { |
| 101 | + Text(locale.localizedString(forIdentifier: locale.identifier) ?? locale.identifier) |
| 102 | + if locale == manager.selectedLocale { |
| 103 | + Image(systemName: "checkmark") |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } label: { |
| 109 | + HStack { |
| 110 | + Text(manager.selectedLocale.localizedString(forIdentifier: manager.selectedLocale.identifier) ?? manager.selectedLocale.identifier) |
| 111 | + .foregroundColor(.primary) |
| 112 | + Image(systemName: "chevron.down") |
| 113 | + .font(.caption) |
| 114 | + .foregroundColor(.secondary) |
| 115 | + } |
| 116 | + .padding(.horizontal, 12) |
| 117 | + .padding(.vertical, 6) |
| 118 | + .background(Color.gray.opacity(0.1)) |
| 119 | + .cornerRadius(8) |
| 120 | + } |
| 121 | + } |
| 122 | + .padding(.horizontal) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +struct RecordButtonView: View { |
| 127 | + let isTranscribing: Bool |
| 128 | + let isDisabled: Bool |
| 129 | + let action: () -> Void |
| 130 | + @Binding var animationScale: CGFloat |
| 131 | + |
| 132 | + var body: some View { |
| 133 | + Button(action: action) { |
| 134 | + ZStack { |
| 135 | + Circle() |
| 136 | + .fill(isTranscribing ? Color.red : Color.blue) |
| 137 | + .frame(width: 80, height: 80) |
| 138 | + .scaleEffect(animationScale) |
| 139 | + |
| 140 | + Image(systemName: isTranscribing ? "stop.fill" : "mic.fill") |
| 141 | + .font(.system(size: 30)) |
| 142 | + .foregroundColor(.white) |
| 143 | + } |
| 144 | + } |
| 145 | + .disabled(isDisabled) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +struct ErrorView: View { |
| 150 | + let error: String |
| 151 | + |
| 152 | + var body: some View { |
| 153 | + Text(error) |
| 154 | + .font(.caption) |
| 155 | + .foregroundColor(.red) |
| 156 | + .padding(.horizontal) |
| 157 | + .padding(.vertical, 8) |
| 158 | + .background(Color.red.opacity(0.1)) |
| 159 | + .cornerRadius(8) |
| 160 | + .padding(.horizontal) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +struct ControlsView: View { |
| 165 | + @Bindable var manager: TranscriptionManager |
| 166 | + @Binding var animationScale: CGFloat |
| 167 | + let commonLocales: [Locale] |
| 168 | + |
| 169 | + var body: some View { |
| 170 | + VStack(spacing: 20) { |
| 171 | + LanguageSelectorView( |
| 172 | + manager: manager, |
| 173 | + commonLocales: commonLocales |
| 174 | + ) |
| 175 | + |
| 176 | + RecordButtonView( |
| 177 | + isTranscribing: manager.isTranscribing, |
| 178 | + isDisabled: manager.error != nil, |
| 179 | + action: manager.toggleTranscription, |
| 180 | + animationScale: $animationScale |
| 181 | + ) |
| 182 | + |
| 183 | + Text(manager.isTranscribing ? "Listening..." : "Tap to start") |
| 184 | + .font(.caption) |
| 185 | + .foregroundColor(.secondary) |
| 186 | + .padding(.bottom) |
| 187 | + } |
| 188 | + .padding(.vertical) |
| 189 | + #if os(iOS) |
| 190 | + .background(Color(UIColor.systemBackground)) |
| 191 | + #else |
| 192 | + .background(Color(NSColor.windowBackgroundColor)) |
| 193 | + #endif |
| 194 | + .shadow(color: .black.opacity(0.1), radius: 10, y: -5) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +// MARK: - Main View |
| 199 | + |
| 200 | +struct AdvancedTranscriptionView: View { |
| 201 | + @Bindable var manager: TranscriptionManager |
| 202 | + @State private var animationScale: CGFloat = 1.0 |
| 203 | + @State private var showPermissionsAlert = false |
| 204 | + |
| 205 | + var body: some View { |
| 206 | + NavigationStack { |
| 207 | + VStack(spacing: 0) { |
| 208 | + // Transcript Display |
| 209 | + ScrollView { |
| 210 | + TranscriptContentView( |
| 211 | + finalizedText: manager.finalizedText, |
| 212 | + volatileText: manager.volatileText, |
| 213 | + currentTimeRange: manager.currentTimeRange, |
| 214 | + isTranscribing: manager.isTranscribing |
| 215 | + ) |
| 216 | + } |
| 217 | + .frame(maxHeight: .infinity) |
| 218 | + |
| 219 | + // Error Display |
| 220 | + if let error = manager.error { |
| 221 | + ErrorView(error: error) |
| 222 | + } |
| 223 | + |
| 224 | + // Controls |
| 225 | + ControlsView( |
| 226 | + manager: manager, |
| 227 | + animationScale: $animationScale, |
| 228 | + commonLocales: commonLocales |
| 229 | + ) |
| 230 | + } |
| 231 | + .navigationTitle("AuralKit Demo") |
| 232 | +#if os(iOS) |
| 233 | + .navigationBarTitleDisplayMode(.large) |
| 234 | +#endif |
| 235 | + .toolbar { |
| 236 | + ToolbarItemGroup(placement: .automatic) { |
| 237 | + if !manager.currentTranscript.isEmpty { |
| 238 | + ShareLink(item: manager.currentTranscript) { |
| 239 | + Image(systemName: "square.and.arrow.up") |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + Menu { |
| 244 | + Label("iOS 26+ Features Active", systemImage: "checkmark.circle.fill") |
| 245 | + .foregroundColor(.green) |
| 246 | + } label: { |
| 247 | + Image(systemName: "ellipsis.circle") |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + .onAppear { |
| 253 | + withAnimation(.easeInOut(duration: 1.0).repeatForever(autoreverses: true)) { |
| 254 | + animationScale = manager.isTranscribing ? 1.2 : 1.0 |
| 255 | + } |
| 256 | + } |
| 257 | + .onChange(of: manager.isTranscribing) { _, isTranscribing in |
| 258 | + withAnimation(.easeInOut(duration: 1.0).repeatForever(autoreverses: true)) { |
| 259 | + animationScale = isTranscribing ? 1.2 : 1.0 |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + var commonLocales: [Locale] { |
| 265 | + [ |
| 266 | + Locale(identifier: "en-US"), |
| 267 | + Locale(identifier: "es-ES"), |
| 268 | + Locale(identifier: "fr-FR"), |
| 269 | + Locale(identifier: "de-DE"), |
| 270 | + Locale(identifier: "it-IT"), |
| 271 | + Locale(identifier: "pt-BR"), |
| 272 | + Locale(identifier: "zh-CN"), |
| 273 | + Locale(identifier: "ja-JP"), |
| 274 | + Locale(identifier: "ko-KR") |
| 275 | + ] |
| 276 | + } |
| 277 | +} |
0 commit comments