migrate to pigeon
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
// Autogenerated from Pigeon (v26.1.5), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(iOS)
|
||||
import Flutter
|
||||
#elseif os(macOS)
|
||||
import FlutterMacOS
|
||||
#else
|
||||
#error("Unsupported platform.")
|
||||
#endif
|
||||
|
||||
/// Error class for passing custom error details to Dart side.
|
||||
final class PigeonError: Error {
|
||||
let code: String
|
||||
let message: String?
|
||||
let details: Sendable?
|
||||
|
||||
init(code: String, message: String?, details: Sendable?) {
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.details = details
|
||||
}
|
||||
|
||||
var localizedDescription: String {
|
||||
return
|
||||
"PigeonError(code: \(code), message: \(message ?? "<nil>"), details: \(details ?? "<nil>")"
|
||||
}
|
||||
}
|
||||
|
||||
private func wrapResult(_ result: Any?) -> [Any?] {
|
||||
return [result]
|
||||
}
|
||||
|
||||
private func wrapError(_ error: Any) -> [Any?] {
|
||||
if let pigeonError = error as? PigeonError {
|
||||
return [
|
||||
pigeonError.code,
|
||||
pigeonError.message,
|
||||
pigeonError.details,
|
||||
]
|
||||
}
|
||||
if let flutterError = error as? FlutterError {
|
||||
return [
|
||||
flutterError.code,
|
||||
flutterError.message,
|
||||
flutterError.details,
|
||||
]
|
||||
}
|
||||
return [
|
||||
"\(error)",
|
||||
"\(type(of: error))",
|
||||
"Stacktrace: \(Thread.callStackSymbols)",
|
||||
]
|
||||
}
|
||||
|
||||
private func isNullish(_ value: Any?) -> Bool {
|
||||
return value is NSNull || value == nil
|
||||
}
|
||||
|
||||
private func nilOrValue<T>(_ value: Any?) -> T? {
|
||||
if value is NSNull { return nil }
|
||||
return value as! T?
|
||||
}
|
||||
|
||||
|
||||
private class MessagesPigeonCodecReader: FlutterStandardReader {
|
||||
}
|
||||
|
||||
private class MessagesPigeonCodecWriter: FlutterStandardWriter {
|
||||
}
|
||||
|
||||
private class MessagesPigeonCodecReaderWriter: FlutterStandardReaderWriter {
|
||||
override func reader(with data: Data) -> FlutterStandardReader {
|
||||
return MessagesPigeonCodecReader(data: data)
|
||||
}
|
||||
|
||||
override func writer(with data: NSMutableData) -> FlutterStandardWriter {
|
||||
return MessagesPigeonCodecWriter(data: data)
|
||||
}
|
||||
}
|
||||
|
||||
class MessagesPigeonCodec: FlutterStandardMessageCodec, @unchecked Sendable {
|
||||
static let shared = MessagesPigeonCodec(readerWriter: MessagesPigeonCodecReaderWriter())
|
||||
}
|
||||
|
||||
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
|
||||
protocol VibrateApi {
|
||||
func canVibrate() throws -> Bool
|
||||
func vibrate(duration: Int64) throws
|
||||
func impact() throws
|
||||
func selection() throws
|
||||
func success() throws
|
||||
func warning() throws
|
||||
func error() throws
|
||||
func heavy() throws
|
||||
func medium() throws
|
||||
func light() throws
|
||||
}
|
||||
|
||||
/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
|
||||
class VibrateApiSetup {
|
||||
static var codec: FlutterStandardMessageCodec { MessagesPigeonCodec.shared }
|
||||
/// Sets up an instance of `VibrateApi` to handle messages through the `binaryMessenger`.
|
||||
static func setUp(binaryMessenger: FlutterBinaryMessenger, api: VibrateApi?, messageChannelSuffix: String = "") {
|
||||
let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
|
||||
let canVibrateChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.canVibrate\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
canVibrateChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
let result = try api.canVibrate()
|
||||
reply(wrapResult(result))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
canVibrateChannel.setMessageHandler(nil)
|
||||
}
|
||||
let vibrateChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.vibrate\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
vibrateChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let durationArg = args[0] as! Int64
|
||||
do {
|
||||
try api.vibrate(duration: durationArg)
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vibrateChannel.setMessageHandler(nil)
|
||||
}
|
||||
let impactChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.impact\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
impactChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.impact()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
impactChannel.setMessageHandler(nil)
|
||||
}
|
||||
let selectionChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.selection\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
selectionChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.selection()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
selectionChannel.setMessageHandler(nil)
|
||||
}
|
||||
let successChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.success\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
successChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.success()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
successChannel.setMessageHandler(nil)
|
||||
}
|
||||
let warningChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.warning\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
warningChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.warning()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warningChannel.setMessageHandler(nil)
|
||||
}
|
||||
let errorChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.error\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
errorChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.error()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
errorChannel.setMessageHandler(nil)
|
||||
}
|
||||
let heavyChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.heavy\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
heavyChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.heavy()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
heavyChannel.setMessageHandler(nil)
|
||||
}
|
||||
let mediumChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.medium\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
mediumChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.medium()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mediumChannel.setMessageHandler(nil)
|
||||
}
|
||||
let lightChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.flutter_vibrate.VibrateApi.light\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
lightChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.light()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lightChannel.setMessageHandler(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,100 +2,84 @@ import Flutter
|
||||
import UIKit
|
||||
import AudioToolbox
|
||||
|
||||
private let isDevice = TARGET_OS_SIMULATOR == 0
|
||||
|
||||
public class SwiftVibratePlugin: NSObject, FlutterPlugin {
|
||||
public class SwiftVibratePlugin: NSObject, FlutterPlugin, VibrateApi {
|
||||
private let isDevice = TARGET_OS_SIMULATOR == 0
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let channel = FlutterMethodChannel(name: "vibrate", binaryMessenger: registrar.messenger())
|
||||
let instance = SwiftVibratePlugin()
|
||||
registrar.addMethodCallDelegate(instance, channel: channel)
|
||||
let messenger : FlutterBinaryMessenger = registrar.messenger()
|
||||
let api : VibrateApi & NSObjectProtocol = SwiftVibratePlugin()
|
||||
VibrateApiSetup.setUp(binaryMessenger: messenger, api: api)
|
||||
}
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
switch (call.method) {
|
||||
case "canVibrate":
|
||||
if isDevice {
|
||||
result(true)
|
||||
} else {
|
||||
result(false)
|
||||
}
|
||||
case "vibrate":
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
// Feedback
|
||||
case "impact":
|
||||
if #available(iOS 10.0, *) {
|
||||
let impact = UIImpactFeedbackGenerator()
|
||||
impact.prepare()
|
||||
impact.impactOccurred()
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "selection":
|
||||
if #available(iOS 10.0, *) {
|
||||
let selection = UISelectionFeedbackGenerator()
|
||||
selection.prepare()
|
||||
selection.selectionChanged()
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "success":
|
||||
if #available(iOS 10.0, *) {
|
||||
let notification = UINotificationFeedbackGenerator()
|
||||
notification.prepare()
|
||||
notification.notificationOccurred(.success)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "warning":
|
||||
if #available(iOS 10.0, *) {
|
||||
let notification = UINotificationFeedbackGenerator()
|
||||
notification.prepare()
|
||||
notification.notificationOccurred(.warning)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "error":
|
||||
if #available(iOS 10.0, *) {
|
||||
let notification = UINotificationFeedbackGenerator()
|
||||
notification.prepare()
|
||||
notification.notificationOccurred(.error)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "heavy":
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .heavy)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "medium":
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .medium)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
case "light":
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .light)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
default:
|
||||
result(FlutterMethodNotImplemented)
|
||||
func canVibrate() throws -> Bool {
|
||||
return isDevice
|
||||
}
|
||||
|
||||
func vibrate(duration: Int64) throws {
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
|
||||
}
|
||||
|
||||
func impact() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .medium)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selection() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UISelectionFeedbackGenerator()
|
||||
generator.prepare()
|
||||
generator.selectionChanged()
|
||||
}
|
||||
}
|
||||
|
||||
func success() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UINotificationFeedbackGenerator()
|
||||
generator.prepare()
|
||||
generator.notificationOccurred(.success)
|
||||
}
|
||||
}
|
||||
|
||||
func warning() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UINotificationFeedbackGenerator()
|
||||
generator.prepare()
|
||||
generator.notificationOccurred(.warning)
|
||||
}
|
||||
}
|
||||
|
||||
func error() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UINotificationFeedbackGenerator()
|
||||
generator.prepare()
|
||||
generator.notificationOccurred(.error)
|
||||
}
|
||||
}
|
||||
|
||||
func heavy() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .heavy)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
}
|
||||
}
|
||||
|
||||
func medium() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .medium)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
}
|
||||
}
|
||||
|
||||
func light() throws {
|
||||
if #available(iOS 10.0, *) {
|
||||
let generator = UIImpactFeedbackGenerator(style: .light)
|
||||
generator.prepare()
|
||||
generator.impactOccurred()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user