[infra_ui][keyboard] Change ios plugin to swift and impl ios-side impl

This commit is contained in:
Jaylen Bian 2021-07-17 10:05:18 +08:00
parent 1ee5511046
commit b257ca11d0
2 changed files with 113 additions and 8 deletions

View File

@ -0,0 +1,85 @@
//
// KeyboardEventHandler.swift
// flowy_infra_ui
//
// Created by Jaylen Bian on 7/17/21.
//
class KeyboardEventHandler: NSObject, FlutterStreamHandler {
var isKeyboardShow: Bool = false
var eventSink: FlutterEventSink?
override init() {
super.init()
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardWillShow),
name: UIApplication.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardDidShow),
name: UIApplication.keyboardDidShowNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardWillHide),
name: UIApplication.keyboardWillHideNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleKeyboardDidHide),
name: UIApplication.keyboardDidHideNotification,
object: nil)
}
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
eventSink = events
return nil
}
func onCancel(withArguments arguments: Any?) -> FlutterError? {
eventSink = nil
return nil
}
// MARK: Helper
@objc
private func handleKeyboardWillShow() {
guard !isKeyboardShow else {
return
}
isKeyboardShow = true
eventSink?(NSNumber(booleanLiteral: true))
}
@objc
private func handleKeyboardDidShow() {
guard !isKeyboardShow else {
return
}
isKeyboardShow = true
eventSink?(NSNumber(booleanLiteral: true))
}
@objc
private func handleKeyboardWillHide() {
guard isKeyboardShow else {
return
}
isKeyboardShow = false
eventSink?(NSNumber(booleanLiteral: false))
}
@objc
private func handleKeyboardDidHide() {
guard isKeyboardShow else {
return
}
isKeyboardShow = false
eventSink?(NSNumber(booleanLiteral: false))
}
}

View File

@ -2,13 +2,33 @@ import Flutter
import UIKit
public class SwiftFlowyInfraUiPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flowy_infra_ui", binaryMessenger: registrar.messenger())
let instance = SwiftFlowyInfraUiPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
enum Constant {
static let infraUIMethodChannelName = "flowy_infra_ui_method"
static let infraUIKeyboardEventChannelName = "flowy_infra_ui_event/keyboard"
}
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = SwiftFlowyInfraUiPlugin()
let methodChannel = FlutterMethodChannel(
name: Constant.infraUIMethodChannelName,
binaryMessenger: registrar.messenger())
registrar.addMethodCallDelegate(instance, channel: methodChannel)
let keyboardEventChannel = FlutterEventChannel(
name: Constant.infraUIKeyboardEventChannelName,
binaryMessenger: registrar.messenger())
keyboardEventChannel.setStreamHandler(KeyboardEventHandler())
}
// MARK: - Method Channel
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
default:
assertionFailure("Unsupported method \(call.method)")
}
}
}