瀏覽代碼

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

Jaylen Bian 3 年之前
父節點
當前提交
b257ca11d0

+ 85 - 0
app_flowy/packages/flowy_infra_ui/ios/Classes/Event/KeyboardEventHandler.swift

@@ -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))
+    }
+}

+ 29 - 9
app_flowy/packages/flowy_infra_ui/ios/Classes/SwiftFlowyInfraUiPlugin.swift

@@ -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)")
+        }
+    }
+
 }