Skip to content

Commit 8240668

Browse files
authored
fix(exoplayer): find the correct VC for showing the video controllers (#123)
1 parent edf01d3 commit 8240668

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

packages/nativescript-exoplayer/index.ios.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Utils } from '@nativescript/core';
1+
import { Application, Utils, ViewBase } from '@nativescript/core';
22
import { Video as VideoBase, VideoFill, videoSourceProperty, fillProperty, subtitleSourceProperty, VideoEventData } from './common';
33

44
export * from './common';
@@ -9,9 +9,25 @@ export * from './common';
99

1010
declare const ASBPlayerSubtitling;
1111

12-
const rootVC = function () {
13-
const appWindow = UIApplication.sharedApplication.keyWindow;
14-
return Utils.ios.getVisibleViewController(appWindow.rootViewController);
12+
function getWindow() {
13+
const app = UIApplication.sharedApplication;
14+
if (!app) {
15+
return;
16+
}
17+
return app.keyWindow || (app.windows && app.windows.count > 0 && app.windows.objectAtIndex(0));
18+
}
19+
function rootVC() {
20+
if (Utils.ios.getRootViewController) {
21+
return Utils.ios.getRootViewController();
22+
} else {
23+
// fallback for older versions of NativeScript
24+
const win = getWindow();
25+
let vc = win && win.rootViewController;
26+
while (vc && vc.presentedViewController) {
27+
vc = vc.presentedViewController;
28+
}
29+
return vc;
30+
}
1531
};
1632

1733
export class Video extends VideoBase {
@@ -71,12 +87,24 @@ export class Video extends VideoBase {
7187
}
7288

7389
createNativeView() {
74-
const vc = rootVC();
90+
return this._playerController.view;
91+
}
92+
93+
onLoaded() {
94+
super.onLoaded();
95+
// we do this on onLoaded as the viewControllers are not properly setup on createNativeView
96+
// eslint-disable-next-line @typescript-eslint/no-this-alias
97+
let vcParent: ViewBase = this;
98+
// start iterating over viewControllers
99+
// we also iterate over this as if it has a viewController, it's most likely a modal
100+
while (vcParent && !vcParent.viewController) {
101+
vcParent = vcParent.parent;
102+
}
103+
const vc = vcParent?.viewController || rootVC();
75104
if (vc) {
76105
vc.addChildViewController(this._playerController);
77106
this._playerController.didMoveToParentViewController(vc);
78107
}
79-
return this._playerController.view;
80108
}
81109

82110
[videoSourceProperty.setNative](value: AVPlayerItem) {

0 commit comments

Comments
 (0)