- Added a global handling of timer and cell reuse. - Fixed the issue of song timer. Made the AvPlayer Optional to handle multiple instances - Made a logic to handle the player stop. matched the current time with the total time.
93 lines
2.9 KiB
Swift
93 lines
2.9 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!
|
|
|
|
// var timer: Timer?
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
|
|
}
|
|
|
|
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
|
|
currentTimePlayer = 0
|
|
// self.timer?.invalidate()
|
|
// self.timer = nil
|
|
}
|
|
|
|
func setData(data: SongBlogDM.PaintDatum, playerStatus: PlayerStatus, active: Bool, currentTime: Int) {
|
|
totalTime = data.songDuration?.timeStringToSeconds() ?? 0
|
|
ongoingTimeLabel.text = formatTime(currentTime)
|
|
playPauseImage.image = UIImage(systemName: "play.fill")
|
|
|
|
if active {
|
|
ongoingTimeLabel.text = formatTime(currentTime)
|
|
switch playerStatus {
|
|
case .stopped:
|
|
showHideLoading(show: false)
|
|
playPauseImage.image = UIImage(systemName: "play.fill")
|
|
case .pause:
|
|
showHideLoading(show: false)
|
|
playPauseImage.image = UIImage(systemName: "play.fill")
|
|
case .play, .resume:
|
|
showHideLoading(show: false)
|
|
playPauseImage.image = UIImage(systemName: "pause.fill")
|
|
case .loading:
|
|
showHideLoading(show: true)
|
|
break
|
|
}
|
|
} else {
|
|
showHideLoading(show: false)
|
|
}
|
|
|
|
if let totalDuration = data.songDuration {
|
|
let totalTimeFiltered = totalDuration.checkHourZero()
|
|
totalTimeLabel.text = "/" + totalTimeFiltered
|
|
} else {
|
|
totalTimeLabel.text = "/" + (data.songDuration ?? "00:00")
|
|
}
|
|
|
|
songTitle.text = data.title ?? "Unknown Title"
|
|
}
|
|
|
|
func showHideLoading(show : Bool){
|
|
show ? self.indicatorView.showLoading() : self.indicatorView.hideLoading()
|
|
self.indicatorView.isHidden = !show
|
|
self.playPauseImage.isHidden = show
|
|
}
|
|
|
|
func updateCurrentTime() {
|
|
let timeInSeconds = Int(currentTimePlayer)
|
|
currentTimePlayer = timeInSeconds
|
|
ongoingTimeLabel.text = formatTime(currentTimePlayer)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|