QueueEntry.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Foundation
  2. struct QueueEntry: Identifiable, Equatable, Codable {
  3. let id: UUID
  4. let source: QueueSource
  5. let title: String
  6. let artist: String
  7. let duration: Double
  8. init(id: UUID = UUID(), source: QueueSource, title: String, artist: String, duration: Double) {
  9. self.id = id
  10. self.source = source
  11. self.title = title
  12. self.artist = artist
  13. self.duration = duration
  14. }
  15. static func == (lhs: Self, rhs: Self) -> Bool { lhs.id == rhs.id }
  16. enum QueueSource: Equatable, Codable {
  17. case swiftDataTrack(trackPersistentID: String, isCloud: Bool, cloudStreamPath: String?)
  18. case cloudDirect(chadTrackId: String, streamPath: String)
  19. }
  20. var formattedDuration: String {
  21. let total = Int(duration)
  22. let minutes = total / 60
  23. let seconds = total % 60
  24. return String(format: "%d:%02d", minutes, seconds)
  25. }
  26. static func from(track: Track) -> QueueEntry {
  27. QueueEntry(
  28. source: .swiftDataTrack(
  29. trackPersistentID: track.id.uuidString,
  30. isCloud: track.isCloud,
  31. cloudStreamPath: track.cloudStreamPath
  32. ),
  33. title: track.title,
  34. artist: track.artist,
  35. duration: track.duration
  36. )
  37. }
  38. static func from(cloudTrack: ChadTrack) -> QueueEntry {
  39. QueueEntry(
  40. source: .cloudDirect(
  41. chadTrackId: cloudTrack.id,
  42. streamPath: cloudTrack.url
  43. ),
  44. title: cloudTrack.title,
  45. artist: cloudTrack.artist ?? "",
  46. duration: cloudTrack.duration ?? 0
  47. )
  48. }
  49. }