#import "FLNativeView.h" #import "PlayerView.h" @implementation FLNativeViewFactory { NSObject *_messenger; } - (instancetype)initWithMessenger: (NSObject *)messenger { self = [super init]; if (self) { _messenger = messenger; } return self; } - (NSObject *)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args { return [[FLNativeView alloc] initWithFrame:frame viewIdentifier:viewId arguments:args binaryMessenger:_messenger]; } /// Implementing this method is only necessary when the `arguments` in /// `createWithFrame` is not `nil`. - (NSObject *)createArgsCodec { return [FlutterStandardMessageCodec sharedInstance]; } @end @implementation FLNativeView { PlayerView *_view; FlutterMethodChannel *_methodChannel; } - (instancetype)initWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args binaryMessenger:(NSObject *)messenger { if (self = [super init]) { _view = [[PlayerView alloc] init]; _methodChannel = [FlutterMethodChannel methodChannelWithName: [NSString stringWithFormat:@"native_plugin/native_view_%lld", viewId] binaryMessenger:messenger]; __weak typeof(self) weakSelf = self; [_methodChannel setMethodCallHandler:^(FlutterMethodCall *_Nonnull call, FlutterResult _Nonnull result) { typeof(self) strongSelf = weakSelf; if (strongSelf != nil) { [strongSelf onMethodCall:call result:result]; } }]; } return self; } - (UIView *)view { return _view; } - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { if ([@"getInternalView" isEqualToString:call.method]) { result(@((uint64_t)(_view))); } else if ([@"updateContent" isEqualToString:call.method]) { NSDictionary *data = call.arguments; NSString *lang = data[@"lang"]; NSString *cn = data[@"cn"]; NSString *en = data[@"en"]; dispatch_async(dispatch_get_main_queue(), ^{ [_view updateWithLang:lang cn:cn en:en]; }); result(nil); } else { result(@(0)); } } @end