UNPKG

@nativescript/flutter

Version:
114 lines 4.6 kB
import { Application, Utils } from '@nativescript/core'; import { FlutterCommon } from './common'; let flutterEngineGroup; var FlutterDelegate = /** @class */ (function (_super) { __extends(FlutterDelegate, _super); function FlutterDelegate() { return _super !== null && _super.apply(this, arguments) || this; } FlutterDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) { configureFlutterEngineGroup(); return true; }; FlutterDelegate.ObjCProtocols = [UIApplicationDelegate, FlutterAppLifeCycleProvider, FlutterPluginRegistry]; return FlutterDelegate; }(FlutterAppDelegate)); export { FlutterDelegate }; export function configureFlutterEngineGroup() { flutterEngineGroup = FlutterEngineGroup.alloc().initWithNameProject('ns_flutter_engine', null); } export function init() { if (global.isIOS) { Application.ios.delegate = FlutterDelegate; } } export class Flutter extends FlutterCommon { createNativeView() { if (!this.id) { throw new Error(`Flutter requires an 'id' property set to match your Dart entry point name.`); } if (!flutterEngineGroup) { throw new Error(`Ensure you have called @nativescript/flutter 'init' from you main bootstrap file.`); } this._engine = flutterEngineGroup.makeEngineWithEntrypointLibraryURI(this.id, null); GeneratedPluginRegistrant.registerWithRegistry(this._engine); this._flutterViewController = FlutterViewController.alloc().initWithEngineNibNameBundle(this._engine, null, null); return this._flutterViewController.view; } initNativeView() { this._platformChannel = FlutterBasicMessageChannel.messageChannelWithNameBinaryMessengerCodec('nativescript', this._flutterViewController.binaryMessenger, FlutterStringCodec.sharedInstance()); this._platformChannel.setMessageHandler(this._methodCallHandler.bind(this)); } disposeNativeView() { if (this._platformChannel) { this._platformChannel.setMessageHandler(null); this._platformChannel = null; } } sendMessage(type, data, callback) { if (this._platformChannel) { const message = { type, }; if (data) { message.data = data; } try { const json = JSON.stringify(message, null, 0); if (callback) { this._platformChannel.sendMessageReply(json, (result) => { this.notify({ eventName: Flutter.sendMessageReplyEvent, object: this, data: result, }); if (callback) { callback(result); } }); } else { this._platformChannel.sendMessage(json); } if (Flutter.DEBUG) { console.log('NativeScript sent message to Flutter:', json); } } catch (err) { console.log('Flutter message error:', err); } } } _methodCallHandler(message, result) { try { const flutterMessage = JSON.parse(message); if (flutterMessage) { if (Flutter.DEBUG) { console.log('Flutter called NativeScript with type:', flutterMessage.type); console.log('data:', flutterMessage.data); } if (this.channel?.[flutterMessage.type]) { this.channel[flutterMessage.type](flutterMessage.data); } } } catch (err) { console.log('Flutter message error:', err); } } onLoaded() { super.onLoaded(); // we do this on onLoaded as the viewControllers are not properly setup on createNativeView // eslint-disable-next-line @typescript-eslint/no-this-alias let vcParent = this; while (vcParent && !vcParent.viewController) { vcParent = vcParent.parent; } const vc = vcParent?.viewController || Utils.ios.getRootViewController(); if (vc) { vc.addChildViewController(this._flutterViewController); this._flutterViewController.didMoveToParentViewController(vc); } } } //# sourceMappingURL=index.ios.js.map