ChadMusic.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Foundation
  2. // MARK: - Chad Music API Response Models
  3. /// A category entry from GET /api/cat/:category (e.g., album, artist, genre).
  4. struct ChadCategory: Codable, Identifiable, Hashable {
  5. let item: String
  6. let count: Int?
  7. var id: String { item }
  8. var name: String { item }
  9. }
  10. /// An album from the Chad Music API.
  11. struct ChadAlbum: Codable, Identifiable, Hashable {
  12. let id: String
  13. let album: String?
  14. let artist: String?
  15. let year: Int?
  16. let genre: String?
  17. let trackCount: Int?
  18. let cover: String?
  19. let publisher: String?
  20. let country: String?
  21. let type: String?
  22. let status: String?
  23. let totalDuration: Double?
  24. let originalDate: String?
  25. let mbId: String?
  26. var title: String { album ?? "Untitled" }
  27. enum CodingKeys: String, CodingKey {
  28. case id, album, artist, year, genre, cover, publisher, country
  29. case trackCount = "track_count"
  30. case totalDuration = "total_duration"
  31. case originalDate = "original_date"
  32. case mbId = "mb_id"
  33. case type, status
  34. }
  35. }
  36. /// A track from GET /api/album/:id/tracks.
  37. struct ChadTrack: Codable, Identifiable, Hashable {
  38. let id: String
  39. let title: String
  40. let artist: String?
  41. let albumArtist: String?
  42. let album: String?
  43. let duration: Double?
  44. let no: Int?
  45. let url: String
  46. let bitRate: Int?
  47. let year: Int?
  48. let cover: String?
  49. var trackNumber: Int? { no }
  50. enum CodingKeys: String, CodingKey {
  51. case id, title, artist, album, duration, no, url, year, cover
  52. case albumArtist = "album_artist"
  53. case bitRate = "bit_rate"
  54. }
  55. var formattedDuration: String {
  56. guard let duration else { return "—" }
  57. let total = Int(duration)
  58. let minutes = total / 60
  59. let seconds = total % 60
  60. return String(format: "%d:%02d", minutes, seconds)
  61. }
  62. }
  63. /// Library statistics from GET /api/stats.
  64. struct ChadStats: Codable {
  65. let tracks: Int?
  66. let albums: Int?
  67. let artists: Int?
  68. let duration: String?
  69. }
  70. /// The browsable category types exposed by the API.
  71. enum ChadCategoryType: String, CaseIterable, Identifiable {
  72. case album
  73. case artist
  74. case genre
  75. case year
  76. case publisher
  77. case country
  78. case type
  79. case status
  80. var id: String { rawValue }
  81. var displayName: String {
  82. switch self {
  83. case .album: "Albums"
  84. case .artist: "Artists"
  85. case .genre: "Genres"
  86. case .year: "Years"
  87. case .publisher: "Publishers"
  88. case .country: "Countries"
  89. case .type: "Types"
  90. case .status: "Status"
  91. }
  92. }
  93. var icon: String {
  94. switch self {
  95. case .album: "square.stack"
  96. case .artist: "person.2"
  97. case .genre: "guitars"
  98. case .year: "calendar"
  99. case .publisher: "building.2"
  100. case .country: "globe"
  101. case .type: "tag"
  102. case .status: "checkmark.circle"
  103. }
  104. }
  105. }