- Created a enum to handle the play pause resume functionality - Added tap handler with animation for cells. - Made logic if one audio is playing and then other audio plays , it ill deinit the first audio and play second. - Added deinit to the view controller, if the view dismiss the play will stop and denitialize
144 lines
4.6 KiB
Swift
144 lines
4.6 KiB
Swift
//
|
|
// MoreVM.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 10/06/24.
|
|
//
|
|
|
|
import UIKit
|
|
import AVFoundation
|
|
|
|
enum PlayerStatus{
|
|
case play
|
|
case loading
|
|
case pause
|
|
case resume
|
|
case stopped
|
|
}
|
|
class MoreVM{
|
|
|
|
weak var vc : MoreVC!
|
|
|
|
var blogData = [BlogDM.Blog]()
|
|
|
|
var songData = [SongBlogDM.PaintDatum]()
|
|
|
|
var player = AVPlayer()
|
|
var playerObserver: NSKeyValueObservation?
|
|
var playerStatus = PlayerStatus.loading
|
|
|
|
var currentIndexPlayingSong : Int?
|
|
|
|
func initView(){
|
|
getBLogs()
|
|
getSong()
|
|
setupCell()
|
|
vc.songTableView.showsVerticalScrollIndicator = false
|
|
vc.songTableView.showsHorizontalScrollIndicator = false
|
|
vc.homeBtn.addTapGesture {
|
|
self.vc.dismiss(animated: true)
|
|
}
|
|
|
|
|
|
}
|
|
|
|
func observePlayer() {
|
|
playerObserver = self.player.observe(\.timeControlStatus, options: [.new, .old], changeHandler: { [weak self] player, change in
|
|
guard let self = self else { return }
|
|
switch player.timeControlStatus {
|
|
case .playing:
|
|
print("Player is playing")
|
|
self.playerStatus = .play
|
|
case .paused:
|
|
print("Player is paused")
|
|
self.playerStatus = .pause
|
|
case .waitingToPlayAtSpecifiedRate:
|
|
print("Player is waiting to play at specified rate")
|
|
self.playerStatus = .loading
|
|
@unknown default:
|
|
print("Unknown player status")
|
|
}
|
|
})
|
|
}
|
|
//
|
|
// func playSong(urlString: String) {
|
|
// guard let url = URL(string: urlString) else {
|
|
// print("Invalid URL string.")
|
|
// return
|
|
// }
|
|
//
|
|
// player = AVPlayer(url: url)
|
|
// player?.play()
|
|
// }
|
|
|
|
func setupCell(){
|
|
vc.blogsCollectionView.register(UINib(nibName: K.CellIdentifier.Home.blogsCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.blogsCell)
|
|
vc.blogsCollectionView.delegate = vc.self
|
|
vc.blogsCollectionView.dataSource = vc.self
|
|
|
|
vc.songTableView.register(UINib(nibName: K.CellIdentifier.Home.songListCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Home.songListCell)
|
|
vc.songTableView.delegate = vc.self
|
|
vc.songTableView.dataSource = vc.self
|
|
}
|
|
|
|
|
|
// MARK: - Get BLogs Data
|
|
|
|
func getBLogs(){
|
|
Utilities.startProgressHUD()
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.blogs, method: .get) {(result : Result<BaseResponseModel<BlogDM>, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
switch data.success{
|
|
case 0:
|
|
/*
|
|
Error
|
|
*/
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
guard let data = data.data?.blogs else{return}
|
|
self.blogData = data
|
|
self.blogData.append(contentsOf: data)
|
|
self.vc.blogsCollectionView.reloadData()
|
|
//Fetched Blogs
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
case .failure(let error):
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getSong(){
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.song_listing, method: .post) {(result : Result<BaseResponseModel<SongBlogDM>, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
switch data.success{
|
|
case 0:
|
|
/*
|
|
Error
|
|
*/
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
guard let data = data.data?.paintData else{return}
|
|
self.songData = data
|
|
self.vc.songTableView.reloadData()
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
case .failure(let error):
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
}
|
|
}
|
|
}
|
|
}
|