1
- import { Application , Utils } from '@nativescript/core' ;
1
+ import { Application , Utils , ViewBase } from '@nativescript/core' ;
2
2
import { Video as VideoBase , VideoFill , videoSourceProperty , fillProperty , subtitleSourceProperty , VideoEventData } from './common' ;
3
3
4
4
export * from './common' ;
@@ -9,9 +9,25 @@ export * from './common';
9
9
10
10
declare const ASBPlayerSubtitling ;
11
11
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
+ }
15
31
} ;
16
32
17
33
export class Video extends VideoBase {
@@ -71,12 +87,24 @@ export class Video extends VideoBase {
71
87
}
72
88
73
89
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 ( ) ;
75
104
if ( vc ) {
76
105
vc . addChildViewController ( this . _playerController ) ;
77
106
this . _playerController . didMoveToParentViewController ( vc ) ;
78
107
}
79
- return this . _playerController . view ;
80
108
}
81
109
82
110
[ videoSourceProperty . setNative ] ( value : AVPlayerItem ) {
0 commit comments