-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add iOS language setting, resolves #17
- Loading branch information
1 parent
54be65e
commit f9db852
Showing
11 changed files
with
222 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,84 @@ | ||
import UIKit | ||
import Flutter | ||
import Speech | ||
import UIKit | ||
|
||
@UIApplicationMain | ||
@objc class AppDelegate: FlutterAppDelegate { | ||
|
||
private func receiveTxRequest(call: FlutterMethodCall, result: @escaping FlutterResult) { | ||
let uri = (call.arguments as! [String:Any])["path"] as! String | ||
let url = URL(fileURLWithPath: uri) | ||
guard let myRecognizer = SFSpeechRecognizer() else { | ||
// A recognizer is not supported for the current locale | ||
result(FlutterError(code: "FAILED_REC", message: "unsupported locale", details: nil)) | ||
return | ||
} | ||
|
||
if !myRecognizer.isAvailable { | ||
result(FlutterError(code: "FAILED_REC", message: "unavailable recognizer", details: nil)) | ||
return | ||
} | ||
let uri = (call.arguments as! [String: Any])["path"] as! String | ||
let localeId = (call.arguments as! [String: Any])["locale"] as! String | ||
let url = URL(fileURLWithPath: uri) | ||
guard let myRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: localeId)) else { | ||
// A recognizer is not supported for the current locale | ||
result(FlutterError(code: "FAILED_REC", message: "unsupported locale", details: nil)) | ||
return | ||
} | ||
|
||
if !myRecognizer.isAvailable { | ||
result(FlutterError(code: "FAILED_REC", message: "unavailable recognizer", details: nil)) | ||
return | ||
} | ||
|
||
let request = SFSpeechURLRecognitionRequest(url: url) | ||
myRecognizer.recognitionTask(with: request) { (res, error) in | ||
guard let res = res else { | ||
// Recognition failed, so check error for details and handle it | ||
result(FlutterError(code: "FAILED_REC", message: error.debugDescription, details: nil)) | ||
return | ||
} | ||
let request = SFSpeechURLRecognitionRequest(url: url) | ||
myRecognizer.recognitionTask(with: request) { (res, error) in | ||
guard let res = res else { | ||
// Recognition failed, so check error for details and handle it | ||
result(FlutterError(code: "FAILED_REC", message: error.debugDescription, details: nil)) | ||
return | ||
} | ||
|
||
// Print the speech that has been recognized so far | ||
if res.isFinal { | ||
result(String(res.bestTranscription.formattedString)) | ||
} | ||
} | ||
// Print the speech that has been recognized so far | ||
if res.isFinal { | ||
result(String(res.bestTranscription.formattedString)) | ||
} | ||
} | ||
} | ||
private func requestTxPermission(result: @escaping FlutterResult) { | ||
let status = SFSpeechRecognizer.authorizationStatus() | ||
switch status { | ||
case .notDetermined: | ||
SFSpeechRecognizer.requestAuthorization({ (status) -> Void in | ||
result(Bool(status == SFSpeechRecognizerAuthorizationStatus.authorized)) | ||
}) | ||
case .denied: | ||
result(Bool(false)) | ||
case .restricted: | ||
result(Bool(false)) | ||
case .authorized: | ||
result(Bool(true)) | ||
default: | ||
result(Bool(true)) | ||
} | ||
} | ||
private func requestTxPermission(result: @escaping FlutterResult) { | ||
let status = SFSpeechRecognizer.authorizationStatus() | ||
switch status { | ||
case .notDetermined: | ||
SFSpeechRecognizer.requestAuthorization({(status)->Void in | ||
result(Bool(status == SFSpeechRecognizerAuthorizationStatus.authorized)) | ||
}) | ||
case .denied: | ||
result(Bool(false)) | ||
case .restricted: | ||
result(Bool(false)) | ||
case .authorized: | ||
result(Bool(true)) | ||
default: | ||
result(Bool(true)) | ||
} | ||
private func getLocaleOptions(result: @escaping FlutterResult) { | ||
let locales = SFSpeechRecognizer.supportedLocales() | ||
var localeOptions: [String: String] = [:] | ||
for locale in locales { | ||
localeOptions[locale.identifier] = locale.localizedString(forIdentifier: locale.identifier)! | ||
} | ||
result(localeOptions) | ||
} | ||
override func application( | ||
_ application: UIApplication, | ||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | ||
) -> Bool { | ||
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController | ||
let transcribeChannel = FlutterMethodChannel(name: "voiceoutliner.saga.chat/iostx", binaryMessenger: controller.binaryMessenger) | ||
transcribeChannel.setMethodCallHandler({(call: FlutterMethodCall, result: @escaping FlutterResult)-> Void in | ||
switch call.method { | ||
case "transcribe": | ||
self.receiveTxRequest(call: call, result: result) | ||
case "requestPermission": | ||
self.requestTxPermission(result: result) | ||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
}) | ||
GeneratedPluginRegistrant.register(with: self) | ||
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | ||
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController | ||
let transcribeChannel = FlutterMethodChannel( | ||
name: "voiceoutliner.saga.chat/iostx", binaryMessenger: controller.binaryMessenger) | ||
transcribeChannel.setMethodCallHandler({ | ||
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in | ||
switch call.method { | ||
case "transcribe": | ||
self.receiveTxRequest(call: call, result: result) | ||
case "requestPermission": | ||
self.requestTxPermission(result: result) | ||
case "getLocaleOptions": | ||
self.getLocaleOptions(result: result) | ||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
}) | ||
GeneratedPluginRegistrant.register(with: self) | ||
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:voice_outliner/widgets/ios_locale_selector.dart'; | ||
|
||
class IOSTranscriptionSetupView extends StatelessWidget { | ||
const IOSTranscriptionSetupView({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Setup"), | ||
automaticallyImplyLeading: false, | ||
), | ||
body: Padding( | ||
padding: const EdgeInsets.all(20.0), | ||
child: Center( | ||
child: Column(children: [ | ||
const Text( | ||
"Select transcription language", | ||
style: TextStyle(fontSize: 18.0), | ||
), | ||
const SizedBox(height: 20), | ||
const IOSLocaleSelector(), | ||
const SizedBox(height: 20), | ||
ElevatedButton( | ||
onPressed: () => Navigator.pushNamedAndRemoveUntil( | ||
context, "/", (route) => false), | ||
child: const Text("continue")) | ||
]))), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.