Last active
May 11, 2023 10:27
-
-
Save philipyoungg/631d49c9c2c5ca9e5b8230cc08d28ff4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// ExampleDailyMotion | |
// | |
// Created by Philip Young on 2023-05-11. | |
// | |
import SwiftUI | |
import DailymotionPlayerSDK | |
struct ContentView: View { | |
var body: some View { | |
DMPlayerViewContainer() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct DMPlayerViewContainer: View { | |
@State private var playerView: DMPlayerView? | |
@State private var showError: Bool = false | |
@State private var errorDescription: String = "" | |
var body: some View { | |
VStack { | |
Text(errorDescription) | |
if let playerView { | |
//3. masalah ketiga ada disini, view ini butuh dimensi (frame width height) biar si UIView bisa tau mau buat komponent nya sebesar apa | |
DMPlayerViewRepresentable(playerView: playerView).frame(width: 400, height: 400) | |
} else { | |
Text("Loading...") | |
} | |
}.onAppear { | |
setupPlayerView() | |
} | |
} | |
private func setupPlayerView() { | |
Dailymotion.createPlayer(playerId: "xe5zh", | |
videoId: "x8gi47u", | |
playerParameters: DMPlayerParameters(mute: false), | |
playerDelegate: nil) { [self] createdPlayerView, error in | |
// 1. masalah yang pertama itu disini. sebelumnya kamu let playerView = playerView (jadi kamu selalu coba unwrap variable view, bukan dari createdPlayerView. Harusnya assign playerView ke createdPlayerView | |
playerView = createdPlayerView | |
if let error = error { | |
print("dm: error while setup the player") | |
showError = true | |
errorDescription = error.localizedDescription | |
} | |
} | |
} | |
} | |
struct DMPlayerViewRepresentable: UIViewRepresentable { | |
var playerView: DMPlayerView | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func makeUIView(context: Context) -> UIView { | |
//2. masalah kedua ada disini, si playerview butuh parent view. jadi kita buat uiview, add si player view, terus kita bikin dia full screen sesuai dimensi view yang kita mau | |
let view = UIView() | |
view.addSubview(playerView) | |
let constraints = [ | |
playerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0), | |
playerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0), | |
playerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0), | |
playerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0) | |
] | |
// Activate created constraints | |
NSLayoutConstraint.activate(constraints) | |
return view | |
} | |
func updateUIView(_ view: UIView, context: Context) { | |
} | |
// 4. Kalau ada delegate play pause dll harusnya dari sini | |
class Coordinator: NSObject, DMPlayerDelegate { | |
var parent: DMPlayerViewRepresentable | |
init(_ parent: DMPlayerViewRepresentable) { | |
self.parent = parent | |
} | |
func player(_ player: DMPlayerView, openUrl url: URL) { | |
UIApplication.shared.open(url) | |
} | |
func playerWillPresentFullscreenViewController(_ player: DMPlayerView) -> UIViewController { | |
return getRootViewController() | |
} | |
func playerWillPresentAdInParentViewController(_ player: DMPlayerView) -> UIViewController { | |
return getRootViewController() | |
} | |
func getRootViewController() -> UIViewController { | |
guard let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene else { | |
fatalError("Unable to find an active window scene") | |
} | |
guard let rootViewController = scene.windows.first?.rootViewController else { | |
fatalError("Unable to find the root view controller") | |
} | |
return rootViewController | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kalau kamu mau, bisa juga taruh function
Dailymotion.createPlayer
di dalam si view representable. Jadi pass variablenya di pass ke component (bukan dari luar)