|
| 1 | +import { Utils, ViewBase } from '@nativescript/core'; |
1 | 2 | import { FlutterCommon } from './common';
|
2 | 3 |
|
3 |
| -let rootFlutterVc: FlutterViewController; |
| 4 | +let flutterEngineGroup: FlutterEngineGroup; |
4 | 5 |
|
5 | 6 | export class Flutter extends FlutterCommon {
|
6 | 7 | flutterViewController: FlutterViewController;
|
| 8 | + platformChannel: FlutterMethodChannel; |
7 | 9 |
|
8 | 10 | createNativeView() {
|
9 |
| - this.flutterViewController = FlutterViewController.alloc().initWithEngineNibNameBundle((<any>UIApplication.sharedApplication.delegate)._flutterEngine, null, null); |
10 |
| - rootFlutterVc = this.flutterViewController; |
| 11 | + if (!this.id) { |
| 12 | + throw new Error(`Flutter requires an 'id' property set to match your Dart entry point name.`); |
| 13 | + } |
| 14 | + const engine = flutterEngineGroup.makeEngineWithEntrypointLibraryURI(this.id, null); |
| 15 | + GeneratedPluginRegistrant.registerWithRegistry(engine); |
| 16 | + this.flutterViewController = FlutterViewController.alloc().initWithEngineNibNameBundle(engine, null, null); |
11 | 17 | return this.flutterViewController.view;
|
12 | 18 | }
|
13 |
| -} |
14 |
| - |
15 |
| -@NativeClass() |
16 |
| -class FlutterDelegate extends UIResponder implements UIApplicationDelegate, FlutterAppLifeCycleProvider { |
17 |
| - _flutterEngine: FlutterEngine; |
18 |
| - _lifeCycleDelegate: FlutterPluginAppLifeCycleDelegate; |
19 | 19 |
|
20 |
| - static ObjCProtocols = [UIApplicationDelegate, FlutterAppLifeCycleProvider]; |
21 |
| - |
22 |
| - init() { |
23 |
| - this._lifeCycleDelegate = FlutterPluginAppLifeCycleDelegate.alloc().init(); |
24 |
| - return this; |
| 20 | + initNativeView() { |
| 21 | + this.platformChannel = FlutterMethodChannel.methodChannelWithNameBinaryMessenger('nativescript', this.flutterViewController.binaryMessenger); |
| 22 | + this.platformChannel.setMethodCallHandler(this._methodCallHandler.bind(this)); |
25 | 23 | }
|
26 | 24 |
|
27 |
| - applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<string, any>): boolean { |
28 |
| - this._flutterEngine = FlutterEngine.alloc().initWithNameProject('io.flutter', null); |
29 |
| - this._flutterEngine.runWithEntrypoint(null); |
30 |
| - GeneratedPluginRegistrant.registerWithRegistry(this._flutterEngine); |
31 |
| - return this._lifeCycleDelegate.applicationDidFinishLaunchingWithOptions(application, launchOptions); |
| 25 | + disposeNativeView() { |
| 26 | + if (this.platformChannel) { |
| 27 | + this.platformChannel.setMethodCallHandler(null); |
| 28 | + this.platformChannel = null; |
| 29 | + } |
32 | 30 | }
|
33 | 31 |
|
34 |
| - touchesBeganWithEvent(touches: NSSet<UITouch>, event: _UIEvent): void { |
35 |
| - super.touchesBeganWithEvent(touches, event); |
| 32 | + invoke(name: string, args?: Array<any>, callback?: (value?: any) => void) { |
| 33 | + if (this.platformChannel) { |
| 34 | + if (callback) { |
| 35 | + this.platformChannel.invokeMethodArgumentsResult(name, args, (result) => { |
| 36 | + this.notify({ |
| 37 | + eventName: Flutter.invokeResultEvent, |
| 38 | + object: this, |
| 39 | + data: result, |
| 40 | + }); |
| 41 | + }); |
| 42 | + } else { |
| 43 | + this.platformChannel.invokeMethodArguments(name, args); |
| 44 | + } |
| 45 | + } |
36 | 46 | }
|
37 | 47 |
|
38 |
| - rootFlutterViewController() { |
39 |
| - return rootFlutterVc; |
| 48 | + private _methodCallHandler(call: FlutterMethodCall, result: any) { |
| 49 | + // console.log('Flutter called NativeScript with:', call.method); |
| 50 | + |
| 51 | + if (this.channel?.[call.method]) { |
| 52 | + const methodArgs = call.arguments ? Utils.dataDeserialize(call.arguments) : null; |
| 53 | + this.channel[call.method](methodArgs).then((value) => { |
| 54 | + // console.log('value:', value); |
| 55 | + result(value); |
| 56 | + }); |
| 57 | + } |
40 | 58 | }
|
41 | 59 |
|
42 |
| - addApplicationLifeCycleDelegate(delegate: NSObject): void { |
43 |
| - this._lifeCycleDelegate.addDelegate(delegate); |
| 60 | + onLoaded() { |
| 61 | + super.onLoaded(); |
| 62 | + // we do this on onLoaded as the viewControllers are not properly setup on createNativeView |
| 63 | + // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 64 | + let vcParent: ViewBase = this; |
| 65 | + while (vcParent && !vcParent.viewController) { |
| 66 | + vcParent = vcParent.parent; |
| 67 | + } |
| 68 | + const vc = vcParent?.viewController || Utils.ios.getRootViewController(); |
| 69 | + if (vc) { |
| 70 | + vc.addChildViewController(this.flutterViewController); |
| 71 | + this.flutterViewController.didMoveToParentViewController(vc); |
| 72 | + } |
44 | 73 | }
|
45 | 74 | }
|
46 | 75 |
|
47 |
| -export { FlutterDelegate }; |
| 76 | +@NativeClass() |
| 77 | +export class FlutterDelegate extends FlutterAppDelegate implements UIApplicationDelegate { |
| 78 | + static ObjCProtocols = [UIApplicationDelegate, FlutterAppLifeCycleProvider, FlutterPluginRegistry]; |
| 79 | + |
| 80 | + applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<string, any>): boolean { |
| 81 | + flutterEngineGroup = FlutterEngineGroup.alloc().initWithNameProject('ns_flutter_engine', null); |
| 82 | + return true; |
| 83 | + } |
| 84 | +} |
0 commit comments