migrate to pigeon
This commit is contained in:
@@ -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