Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.0 kB
1
Indexable
Never
import Foundation
import Flutter
import UIKit
import Liveness

class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger

    init(messenger: FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }

    func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return FLNativeView(
            frame: frame,
            viewIdentifier: viewId,
            arguments: args,
            binaryMessenger: messenger)
    }

    public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
          return FlutterStandardMessageCodec.sharedInstance()
    }
}

class FLNativeView: NSObject, FlutterPlatformView {
    private var _view: FLEmbedView
    private var flutterResult:FlutterResult?

    init(
        frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?,
        binaryMessenger messenger: FlutterBinaryMessenger?
    ) {
        _view = FLEmbedView()

        super.init()
        self.createNativeView(Id: viewId,messenger: messenger,args: args as! Dictionary<String, Any>)
    }

    func view() -> UIView {
        return _view
    }

    func createNativeView(Id: Int64, messenger: FlutterBinaryMessenger?, args: Dictionary<String, Any>) {
    _view.updateArgs(args: args)
    
    let channel = FlutterMethodChannel(name: "LivenessView/\(Id)", binaryMessenger: messenger!)
    channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) in
        switch call.method {
        case "startWithToken":
            self?.flutterResult = result
            self?._view.superview?.translatesAutoresizingMaskIntoConstraints = true
            guard let args = call.arguments as? [String: Any] else {
                result(FlutterError(code: "-1", message: "Error", details: nil))
                return
            }
            
            let token = args["token"] as? String
            let urlConfig = args["urlConfig"] as? String
            let faceUp = args["faceUp"] as? String
            let faceDown = args["faceDown"] as? String
            let faceLeft = args["faceLeft"] as? String
            let faceRight = args["faceRight"] as? String
            self?._view.startWith(
              token: token, urlConfig: urlConfig, faceUp: faceUp, faceDown: faceDown, faceLeft: faceLeft,
              faceRight: faceRight)
        default:
            result(FlutterMethodNotImplemented)
        }
    }
}

//        func receiverResult(resultCode: Int, filePath: String) {
//        if resultCode == Int(RESULT_CODE_OK) {
//            flutterResult?(filePath)
//        }else{
//            flutterResult?(FlutterError(code: "\(resultCode)", message: "Error", details: filePath))
//        }
//        }

}

class FLEmbedView : UIView {
    let _ekycView = LivenessMas.shared
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let view = _ekycView.openSDKin()
        view.frame = bounds // Adjust the frame to match the bounds of the superview
        addSubview(view)

    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func removeFromSuperview() {
        super.removeFromSuperview()
    }
    
    func updateArgs(args: Dictionary<String, Any>) {
        if let faceUpValue: String = args["faceUp"] as? String,
           //let faceBlink: String = args["faceBlink"] as? String,
           let faceDownValue: String = args["faceDown"] as? String,
           let faceLeftValue: String = args["faceLeft"] as? String,
           let faceRightValue: String = args["faceRight"] as? String,
           let initStringValue: String = args["initString"] as? String,
           let normalValue: String = args["normal"] as? String {
            print("Received initStringValue: \(initStringValue)")
            print("Received normalValue: \(normalValue)")
            // All values are non-nil and of type String
            //_ekycView.faceBlink = faceBlink
            _ekycView.faceUpString = faceUpValue
            _ekycView.faceDownString = faceDownValue
            _ekycView.faceLeftString = faceLeftValue
            _ekycView.faceRightString = faceRightValue
            _ekycView.initString = initStringValue
            _ekycView.normalString = normalValue
        }
    }

    func startWith(
      token: String?, urlConfig: String?, faceUp: String?, faceDown: String?, faceLeft: String?,
      faceRight: String?
    ) {
      _ekycView.sessionToken = token
      _ekycView.urlConfig = urlConfig
      _ekycView.faceUpString = faceUp
      _ekycView.faceDownString = faceDown
      _ekycView.faceLeftString = faceLeft
      _ekycView.faceRightString = faceRight
    }
    
    override func layoutSubviews() {
            print("current frame: \(self.frame)")
            //_ekycView.frame = self.frame
            super.layoutSubviews()
    }

}