1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 import ExpoModulesCore
4 import AVFoundation
5 
6 public final class VideoViewModule: Module {
7   public func definition() -> ModuleDefinition {
8     Name("ExpoVideoView")
9 
10     Constants([
11       "ScaleNone": AVLayerVideoGravity.resizeAspect,
12       "ScaleToFill": AVLayerVideoGravity.resize,
13       "ScaleAspectFit": AVLayerVideoGravity.resizeAspect,
14       "ScaleAspectFill": AVLayerVideoGravity.resizeAspectFill
15     ])
16 
17     AsyncFunction("setFullscreen") { (viewTag: Int, value: Bool, promise: Promise) in
18       self.runBlockForView(viewTag) { view in
19         view.setFullscreen(value, resolver: promise.resolver, rejecter: promise.legacyRejecter)
20       }
21     }
22 
23     ViewManager {
24       View {
25         let avModule: EXAVInterface? = self.appContext?.legacyModule(implementing: EXAVInterface.self)
26         return EXVideoView(avModule: avModule)
27       }
28 
29       Events(
30         "onStatusUpdate",
31         "onLoadStart",
32         "onLoad",
33         "onError",
34         "onReadyForDisplay",
35         "onFullscreenUpdate"
36       )
37 
38       Prop("status") { (view: EXVideoView, status: [String: Any]) in
39         view.status = status
40       }
41 
42       Prop("useNativeControls") { (view: EXVideoView, useNativeControls: Bool) in
43         view.useNativeControls = useNativeControls
44       }
45 
46       Prop("source") { (view: EXVideoView, source: [String: Any]) in
47         view.source = source
48       }
49 
50       Prop("resizeMode") { (view: EXVideoView, resizeMode: String) in
51         view.nativeResizeMode = resizeMode
52       }
53     }
54   }
55 
56   private func runBlockForView(_ tag: Int, _ block: @escaping (EXVideoView) -> Void) {
57     let uiManager: EXUIManager? = appContext?.legacyModule(implementing: EXUIManager.self)
58 
59     uiManager?.executeUIBlock({ (view: Any) in
60       if let view = view as? EXVideoView {
61         block(view)
62       }
63     }, forView: tag, of: EXVideoView.self)
64   }
65 }
66