Files
Woka_Native_iOS/WOKA/Theme/ViewModel/ThemeTwoVM.swift
Bilal 210094e65c - finalised theme 2 collection clicks
- worked on homelive tv observers
- addd fav remove fav for audio books will now locally update my list instead reloading
- addd fav remove fav for karaoke will now locally update my list instead reloading
- addd fav remove fav for games will now locally update my list instead reloading
2024-08-02 23:22:06 +05:30

265 lines
10 KiB
Swift

//
// 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 playerItem: AVPlayerItem!
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)
NotificationCenter.default.addObserver(self, selector: #selector(self.viewPush(notification:)), name: NSNotification.Name(rawValue: K.NotificationCenterReloads.themeTwoPush), object: nil)
}
// MARK: - This will handle all clicks for modules
// This comes from Explore WOKA CLicks
@objc func viewPush(notification: Notification){
if let userInfo = notification.userInfo, let action = userInfo["action"] as? TopViewPush {
checkType(action: action)
}
}
// Made a common func to check which module to push
func checkType(action : TopViewPush){
switch action {
case .webseries:
let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesVC) as! WebSeriesVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .audioBooks:
let sb = UIStoryboard(name: K.StoryBoard.audioBooks, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.AudioBooks.audioBookHomeVC) as! AudioBookHomeVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .games:
let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesListVC) as! GamesListVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .karaoke:
let sb = UIStoryboard(name: K.StoryBoard.Karaoke, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Karaoke.karaokeListingVC) as! KaraokeListingVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .shop:
let sb = UIStoryboard(name: K.StoryBoard.shop, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Shop.shopListingVC) as! ShopListingVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .liveTV:
playLiveTV()
case .blogs:
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Theme.blogsVC) as! BlogsVC
vc.navigationController?.pushViewController(vcPush, animated: true)
case .radio:
let sb = UIStoryboard(name: K.StoryBoard.wokaFM, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WokaFM.wokaFMVC) as! WokaFMVC
vcPush.modalPresentationStyle = .overCurrentContext
vcPush.modalTransitionStyle = .crossDissolve
vc.present(vcPush, animated: true)
}
}
// 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.avtarURL{
// vc.avatarImage.imageURL("https://wokaland.com/secret-panel-10102023/hidden-admin-portal-20092023/storage/app/public/uploads/avtar/avatar2.png?d=1716889852")
vc.avatarImage.imageURL(avatar)
}
}
func setupAvPlayer(){
/*
Av Player Setup
*/
guard let streamURL = URL(string: liveStreamURL) else{return}
playerItem = AVPlayerItem(url: streamURL)
// Create AVPlayer with the stream URL
avPlayer = AVPlayer(playerItem: playerItem)
// 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
}
}