//
// H M G P e n g u i n I n P l a t f o r m B r i d g e . s w i f t
// R u n n e r
//
// C r e a t e d b y H a r o o n A m j a d o n 1 3 / 0 8 / 2 0 2 4 .
//
import Foundation
var flutterMethodChannelPenguinIn : FlutterMethodChannel ? = nil
fileprivate var mainViewController : MainFlutterVC !
class HMGPenguinInPlatformBridge {
private let channelName = " launch_penguin_ui "
private static var shared_ : HMGPenguinInPlatformBridge ?
class func initialize ( flutterViewController : MainFlutterVC ) {
shared_ = HMGPenguinInPlatformBridge ( )
mainViewController = flutterViewController
shared_ ? . openChannel ( )
}
func shared ( ) -> HMGPenguinInPlatformBridge {
assert ( ( HMGPenguinInPlatformBridge . shared_ != nil ) , " HMGPenguinInPlatformBridge is not initialized, call initialize(mainViewController:MainFlutterVC) function first. " )
return HMGPenguinInPlatformBridge . shared_ !
}
private func openChannel ( ) {
flutterMethodChannelPenguinIn = FlutterMethodChannel ( name : channelName , binaryMessenger : mainViewController . binaryMessenger )
flutterMethodChannelPenguinIn ? . setMethodCallHandler { ( methodCall , result ) in
print ( " Called function \( methodCall . method ) " )
if let arguments = methodCall . arguments as Any ? {
if methodCall . method = = " launchPenguin " {
self . launchPenguinView ( arguments : arguments , result : result )
}
} else {
result ( FlutterError ( code : " INVALID_ARGUMENT " , message : " Storyboard name is required " , details : nil ) )
}
}
}
private func launchPenguinView ( arguments : Any , result : @ escaping FlutterResult ) {
let penguinView = PenguinView (
frame : CGRect ( x : 0 , y : 0 , width : UIScreen . main . bounds . width , height : UIScreen . main . bounds . height ) ,
viewIdentifier : 0 ,
arguments : arguments ,
binaryMessenger : mainViewController . binaryMessenger
)
let penguinUIView = penguinView . view ( )
penguinUIView . frame = mainViewController . view . bounds
penguinUIView . autoresizingMask = [ . flexibleWidth , . flexibleHeight ]
mainViewController . view . addSubview ( penguinUIView )
result ( nil ) // C a l l r e s u l t t o i n d i c a t e t h e m e t h o d w a s s u c c e s s f u l l y h a n d l e d
}
}