Files
music-app/Music/Utils/API/Models.swift
T
2026-10-16 03:18:49 +02:00

166 lines
5.1 KiB
Swift

import Foundation
/// MongoDB references are populated on some API routes and bare ObjectIds on others.
nonisolated public enum APIReference<Value: Decodable & Sendable>: Decodable, Sendable {
case id(String)
case value(Value)
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let id = try? container.decode(String.self) {
self = .id(id)
} else {
self = .value(try container.decode(Value.self))
}
}
}
nonisolated public struct Artist: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
public let file: String?
enum CodingKeys: String, CodingKey {
case id = "_id"
case name, file
}
}
nonisolated public struct Genre: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
enum CodingKeys: String, CodingKey {
case id = "_id"
case name
}
}
nonisolated public struct Album: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
public let artists: [APIReference<Artist>]
public let file: String?
public let genre: APIReference<Genre>?
enum CodingKeys: String, CodingKey {
case id = "_id"
case name, artists, file, genre
}
}
nonisolated public struct TrackAlbum: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
public let file: String
enum CodingKeys: String, CodingKey {
case id = "_id"
case name, file
}
}
nonisolated public struct Lyrics: Decodable, Sendable {
public let synced: Bool
public let text: String
}
nonisolated public struct Track: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
public let album: APIReference<TrackAlbum>
public let artists: [APIReference<Artist>]
public let fileDir: String
public let durationInSeconds: Double
public let lyrics: Lyrics?
enum CodingKeys: String, CodingKey {
case id = "_id"
case name, album, artists, fileDir, durationInSeconds, lyrics
}
}
nonisolated public struct User: Decodable, Identifiable, Sendable {
public let id: String
public let name: String
public let email: String
public let role: String
public let savedTracks: [String]?
enum CodingKeys: String, CodingKey {
case id = "_id"
case name, email, role, savedTracks
}
}
nonisolated public struct SessionStatus: Decodable, Sendable { public let authenticated: Bool }
nonisolated public struct Session: Decodable, Sendable {
public let expireAt: Date
/// Returned for administrative bearer auth; ordinary user auth uses the HttpOnly cookie.
public let sessionToken: String
}
nonisolated public struct SearchResults: Decodable, Sendable {
public let tracks: [Track]
public let albums: [Album]
public let artists: [Artist]
}
nonisolated public struct TrackMembership: Decodable, Sendable { public let contains: Bool }
nonisolated public struct PlaybackHandoff: Decodable, Sendable {
public let currentTrackId: String
public let position: Double
public let queueTrackIds: [String]
public let remainingTrackIds: [String]
public let historyTrackIds: [String]
public let repeatEnabled: Bool
public let repeatOnce: Bool
public let wasPlaying: Bool
enum CodingKeys: String, CodingKey {
case currentTrackId, position, queueTrackIds, remainingTrackIds, historyTrackIds
case repeatEnabled = "repeat"
case repeatOnce, wasPlaying
}
}
nonisolated public struct PlaybackHandoffBody: Encodable, Sendable {
public let currentTrackId: String
public let position: Double
public let queueTrackIds: [String]
public let remainingTrackIds: [String]
public let historyTrackIds: [String]
public let repeatEnabled: Bool
public let repeatOnce: Bool
public let wasPlaying: Bool
enum CodingKeys: String, CodingKey {
case currentTrackId, position, queueTrackIds, remainingTrackIds, historyTrackIds
case repeatEnabled = "repeat"
case repeatOnce, wasPlaying
}
public init(
currentTrackId: String,
position: Double,
queueTrackIds: [String],
remainingTrackIds: [String],
historyTrackIds: [String],
repeatEnabled: Bool,
repeatOnce: Bool,
wasPlaying: Bool
) {
self.currentTrackId = currentTrackId
self.position = position
self.queueTrackIds = queueTrackIds
self.remainingTrackIds = remainingTrackIds
self.historyTrackIds = historyTrackIds
self.repeatEnabled = repeatEnabled
self.repeatOnce = repeatOnce
self.wasPlaying = wasPlaying
}
}
nonisolated public struct PlaybackHandoffSaveResponse: Decodable, Sendable {
public let saved: Bool
public let expiresAt: String
}
nonisolated public struct PlaybackHandoffDeleteResponse: Decodable, Sendable {
public let deleted: Bool
}
nonisolated public enum SearchType: String, Sendable { case track, album, artist, `default` }