- 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
141 lines
4.4 KiB
Swift
141 lines
4.4 KiB
Swift
//
|
|
// SongListCell.swift
|
|
// WOKA
|
|
//
|
|
// Created by Bilal Khan on 10/06/24.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SongListCell: UITableViewCell {
|
|
|
|
@IBOutlet weak var ongoingTimeLabel: UILabel!
|
|
@IBOutlet weak var totalTimeLabel: UILabel!
|
|
@IBOutlet weak var songTitle: UILabel!
|
|
@IBOutlet weak var outerStack: UIStackView!
|
|
@IBOutlet weak var playPauseImage: UIImageView!
|
|
@IBOutlet weak var indicatorView: UIView!
|
|
|
|
// typealias btnTappedBlock = (String?) -> Void
|
|
// var btnTapped : btnTappedBlock!
|
|
|
|
var timer: Timer?
|
|
private var currentTime: Int = 0
|
|
var totalTime: Int = 0
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
|
|
// self.addTapGesture { [weak self] in
|
|
// guard let self else{return}
|
|
// UIView.animate(withDuration: 0.1, animations: {
|
|
// self.outerStack.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
|
|
// }) { _ in
|
|
// UIView.animate(withDuration: 0.1) {
|
|
// self.outerStack.transform = .identity
|
|
// self.btnTapped(self.ongoingTimeLabel.text)
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
super.setSelected(selected, animated: animated)
|
|
// Configure the view for the selected state
|
|
}
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
// Invalidate the timer when the cell is reused
|
|
timer?.invalidate()
|
|
timer = nil
|
|
ongoingTimeLabel.text = "0:00"
|
|
}
|
|
|
|
func setData(data: SongBlogDM.PaintDatum, playerStatus: PlayerStatus) {
|
|
ongoingTimeLabel.text = "00:00"
|
|
totalTime = 0
|
|
currentTime = 0
|
|
|
|
|
|
switch playerStatus{
|
|
case .stopped:
|
|
playPauseImage.image = UIImage(systemName: "play.fill")
|
|
case .pause:
|
|
playPauseImage.image = UIImage(systemName: "play.fill")
|
|
case .play:
|
|
playPauseImage.image = UIImage(systemName: "pause.fill")
|
|
totalTime = data.songDuration?.timeStringToSeconds() ?? 0
|
|
|
|
// if let onGoingTime{
|
|
// currentTime = onGoingTime.timeStringToSeconds() ?? 0
|
|
// ongoingTimeLabel.text = onGoingTime
|
|
// }else{
|
|
// currentTime = 0
|
|
// }
|
|
startCountdown()
|
|
|
|
case .resume:
|
|
playPauseImage.image = UIImage(systemName: "pause.fill")
|
|
totalTime = data.songDuration?.timeStringToSeconds() ?? 0
|
|
// if let onGoingTime{
|
|
// currentTime = onGoingTime.timeStringToSeconds() ?? 0
|
|
// ongoingTimeLabel.text = onGoingTime
|
|
// }else{
|
|
// currentTime = 0
|
|
// }
|
|
startCountdown()
|
|
case .loading:
|
|
break
|
|
}
|
|
|
|
// Set total time label and song title
|
|
if let totalDuration = data.songDuration{
|
|
//Check of the hour is zero removing it
|
|
let totalTimeFiltered = totalDuration.checkHourZero()
|
|
totalTimeLabel.text = "/" + totalTimeFiltered
|
|
}else{
|
|
totalTimeLabel.text = "/" + (data.songDuration ?? "00:00")
|
|
}
|
|
|
|
songTitle.text = data.title ?? "Unknown Title"
|
|
}
|
|
|
|
private func startCountdown() {
|
|
// Invalidate any existing timer
|
|
timer?.invalidate()
|
|
|
|
// Schedule a new timer
|
|
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
|
|
}
|
|
|
|
@objc private func updateCountdown() {
|
|
// Update the timer label
|
|
if currentTime < totalTime {
|
|
currentTime += 1
|
|
ongoingTimeLabel.text = formatTime(currentTime)
|
|
} else {
|
|
timer?.invalidate()
|
|
timer = nil
|
|
}
|
|
}
|
|
// @objc private func updateCountdown() {
|
|
// // Update countdown label
|
|
// if totalTime > 0 {
|
|
// totalTime -= 1
|
|
// ongoingTimeLabel.text = formatTime(totalTime)
|
|
// } else {
|
|
// timer?.invalidate()
|
|
// timer = nil
|
|
// ongoingTimeLabel.text = "0:00"
|
|
// }
|
|
// }
|
|
|
|
private func formatTime(_ seconds: Int) -> String {
|
|
// Format time as MM:SS
|
|
let minutes = seconds / 60
|
|
let seconds = seconds % 60
|
|
return String(format: "%02d:%02d", minutes, seconds)
|
|
}
|
|
}
|