// // ThemeTwoVM.swift // WOKA // // Created by MacBook Pro on 27/05/24. // import UIKit import AVFoundation import JWPlayerKit struct Theme2Struct{ let imageName : String let text : String } class ThemeTwoVM{ weak var vc : ThemeTwoVC! var liveStreamURL = "https://d3volyx7jx7oal.cloudfront.net/master.m3u8" var avPlayer : AVPlayer! var playerLayer: AVPlayerLayer! /* Static cell data */ var cellData = [Theme2Struct(imageName: "WokaFMT2", text: "WOKA FM"), Theme2Struct(imageName: "LiveTVT2", text: "LIVE TV"), Theme2Struct(imageName: "WebSeriesT2", text: "WEB SERIES"), Theme2Struct(imageName: "GamesT2", text: "GAMES"), Theme2Struct(imageName: "AudioBooksT2", text: "AUDIO BOOKS"), Theme2Struct(imageName: "KaraokeT2", text: "KARAOKE"), Theme2Struct(imageName: "ShopT2", text: "SHOP")] func initView(){ setupCell() setupAvPlayer() setUserData() handleNotificationCenter() } private func handleNotificationCenter(){ NotificationCenter.default.addObserver(self, selector: #selector(self.reloadTheme), name: NSNotification.Name(rawValue: K.NotificationCenterReloads.reloadTheme), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil) } // MARK: - Notification Center Handlers @objc func reloadTheme(){ self.vc.delegate?.didPressSwitchButton(from: self.vc) } @objc func appDidEnterBackground() { // Code to execute when the app enters the background print("App entered background") self.avPlayer.pause() } @objc func appWillEnterForeground() { // Code to execute when the app enters the foreground print("App will enter foreground") self.avPlayer.play() } // MARK: - Live TV func playLiveTV(){ Utilities.startProgressHUD(msg: "Loading...") print("tapped") let vc = self.vc.storyboard?.instantiateViewController(identifier: "PlayerVC") as! PlayerVC DispatchQueue.main.async { do { // Create a JWMediaTrack with the thumbnails .vtt file // let thumbnailTrack = try JWThumbnailTrackBuilder() // .file(URL(string:"https://content.jwplatform.com/videos/Agy4RIje-Ysj2G4DQ.mp4")!) // .build() // Create a JWPlayerItem let item = try JWPlayerItemBuilder() .file(URL(string: self.liveStreamURL)!) // .posterImage(URL(string: "https://img.freepik.com/free-photo/painting-mountain-lake-with-mountain-background_188544-9126.jpg")!) // .mediaTracks([thumbnailTrack]) .build() // Create a config, and give it the item as a playlist. let config = try JWPlayerConfigurationBuilder() .playlist(items: [item]) .autostart(true) // .preload(.auto) // .repeatContent(true) .build() vc.config = config vc.dismissTapped = self.tapped vc.contentType = .liveStream vc.modalPresentationStyle = .overFullScreen vc.modalTransitionStyle = .crossDissolve Utilities.dismissProgressHUD() self.vc.present(vc, animated: false) { self.stopLiveStream() // vc.setDeviceOrientation(orientation: .landscapeRight) } // Utilities.dismissProgressHUD() // self.vc.navigationController?.pushViewController(vc, animated: true) // self.stopLiveStream() } catch { // Handle Error } } } func tapped(){ Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { _ in self.startLiveStream() self.vc.liveTvView.layoutIfNeeded() } print("Sadasd") } func startLiveStream(){ avPlayer.play() avPlayer.volume = 0 } func stopLiveStream(){ avPlayer.pause() } func setUserData(){ guard let data = AuthFunc.shareInstance.userData else{return} //set the first name as the name /* Check User Type, Dont show username if the user type is guest */ switch data.userType{ case "1": // child vc.nameLabel.text = data.fullname?.components(separatedBy: " ").first vc.notificationBtn.isHidden = false case "2" : // adult vc.nameLabel.text = data.fullname?.components(separatedBy: " ").first vc.notificationBtn.isHidden = false case "3": // Guest vc.nameLabel.text = "" vc.notificationBtn.isHidden = true break default: break } if let avatar = data.avtar{ //https://wokaland.com/admin/storage/app/public/uploads/avtar/avatar2.png?d=1716889852 vc.avatarImage.imageURL("https://wokaland.com/admin/storage/app/public/uploads/avtar/avatar6.png") } } func setupAvPlayer(){ /* Av Player Setup */ let streamURL = URL(string: liveStreamURL) // Create AVPlayer with the stream URL avPlayer = AVPlayer(url: streamURL!) // avPlayer.isMuted = true // Create AVPlayerLayer playerLayer = AVPlayerLayer(player: avPlayer) playerLayer.videoGravity = .resizeAspectFill // You can set different videoGravity as per your need playerLayer.frame = self.vc.liveTvView.bounds self.vc.liveTvView.layer.addSublayer(playerLayer) avPlayer.play() avPlayer.volume = 0 } func setData(){ playerLayer.frame = self.vc.liveTvView.bounds guard let data = AuthFunc.shareInstance.userData else{return} //set the first name as the name /* Check User Type, Dont show username if the user type is guest */ switch data.userType{ case "1": // child vc.nameLabel.text = data.fullname?.components(separatedBy: " ").first case "2" : // adult vc.nameLabel.text = data.fullname?.components(separatedBy: " ").first case "3": // Guest vc.nameLabel.text = "" break default: break } } // MARK: - SetupCell func setupCell(){ vc.collectionView.register(UINib(nibName: K.CellIdentifier.Theme.homeExploreCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Theme.homeExploreCell) vc.collectionView.delegate = vc.self vc.collectionView.dataSource = vc.self } }