Files
Woka_Native_iOS/WOKA/Theme/ViewModel/ThemeTwoVM.swift
Bilal f3a62cee63 - Added api for getting live stream and fm url
- modified the data for live stream url in theme 1 and theme 2 with speicifed languages also with titles
- modified FM url
- worked on coredata crud. made functions for crud with queries
- added default avatar images for guest
- Finalised the Data Model and tried to save and update the counts in local
2024-08-06 21:17:00 +05:30

304 lines
11 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: K.StoryBoardID.Theme.playerVC) as! PlayerVC
guard let data = AuthFunc.shareInstance.staticURLs , let liveStreamData = data.liveData?.first else{
self.vc.toast(msg: "Issue with live streaming", time: 2)
return
}
var url = String()
var title = String()
if AuthFunc.shareInstance.languageSelected == .english{
url = liveStreamData.liveURL?.hdURLEn ?? ""
title = liveStreamData.name?.titleEn ?? ""
}else{
url = liveStreamData.liveURL?.hdURLHin ?? ""
title = liveStreamData.name?.titleHin ?? ""
}
do {
// Ensure the liveStreamURL is valid
guard let liveStreamURL = URL(string: url) else {
print("Invalid live stream URL")
Utilities.dismissProgressHUD()
return
}
let videoSourceBuilder = try JWVideoSourceBuilder()
// .defaultVideo(true)
.file(liveStreamURL)
.label(title)
.build()
// Create a JWPlayerItem
let item = try JWPlayerItemBuilder()
// .file(liveStreamURL)
.videoSources([videoSourceBuilder])
.title(title)
.build()
// Create a JWPlayerConfiguration
let config = try JWPlayerConfigurationBuilder()
.playlist(items: [item])
.preload(JWPreload(rawValue: 20) ?? .none)
.autostart(true)
.build()
vc.config = config
vc.dismissTapped = self.tapped
vc.contentType = .liveStream
vc.modalPresentationStyle = .fullScreen
vc.modalTransitionStyle = .crossDissolve
DispatchQueue.main.async { [weak self] in
guard let self else{return}
// Present the PlayerVC
self.vc.present(vc, animated: false) { [weak self] in
guard let self else{return}
stopLiveStream()
}
}
} catch {
print("Error creating JWPlayer configuration: \(error)")
Utilities.dismissProgressHUD()
}
// Dismiss the progress HUD after the view controller presentation
Utilities.dismissProgressHUD()
}
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)
}else{
vc.avatarImage.image = UIImage(named: "DefaultAvatar")
}
}
func setupAvPlayer(){
/*
Av Player Setup
*/
guard let data = AuthFunc.shareInstance.staticURLs , let liveStreamData = data.liveData?.first else{
self.vc.toast(msg: "Issue with live streaming", time: 2)
return
}
var url = String()
if AuthFunc.shareInstance.languageSelected == .english{
url = liveStreamData.liveURL?.hdURLEn ?? ""
}else{
url = liveStreamData.liveURL?.hdURLHin ?? ""
}
guard let streamURL = URL(string: url) 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
}
}