~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
215 lines · 5.8 KB Raw History
import Foundation

/// Opaque protocol data. Keep unchanged, in memory only; never log it.
public struct PaginationToken: RawRepresentable, Hashable, Sendable, CustomStringConvertible {
  public let rawValue: String
  public init(rawValue: String) { self.rawValue = rawValue }
  public var description: String { "PaginationToken(<opaque>)" }
}

/// Optional display metadata from a browse response's own header, never an item or menu.
public struct CatalogPageHeader: Equatable, Sendable {
  public let title: String?
  public let subtitle: String?
  public let thumbnailURL: URL?
  public let artistReferences: [Artist]?

  public init(
    title: String? = nil, subtitle: String? = nil, thumbnailURL: URL? = nil,
    artistReferences: [Artist]? = nil
  ) {
    self.title = title
    self.subtitle = subtitle
    self.thumbnailURL = thumbnailURL
    self.artistReferences = artistReferences
  }

  public func fillingMissingValues(from previous: Self?) -> Self {
    Self(
      title: title ?? previous?.title, subtitle: subtitle ?? previous?.subtitle,
      thumbnailURL: thumbnailURL ?? previous?.thumbnailURL,
      artistReferences: artistReferences ?? previous?.artistReferences)
  }
}

public struct Page<Element: Sendable>: Sendable {
  public let items: [Element]
  public let next: PaginationToken?
  public let header: CatalogPageHeader?

  public init(
    items: [Element], next: PaginationToken? = nil, header: CatalogPageHeader? = nil
  ) {
    self.items = items
    self.next = next
    self.header = header
  }
}

extension Page: Equatable where Element: Equatable {}

public enum CatalogLoadPolicy: Sendable {
  case cacheFirst
  case reload
}

public enum HomeAudience: Hashable, Sendable {
  case anonymous
  case authenticated
}

public enum LibraryCategory: String, CaseIterable, Hashable, Sendable, Identifiable {
  case playlists
  case songs
  case albums
  case artists

  public var id: String { rawValue }

  public var title: String {
    switch self {
    case .playlists: "Playlists"
    case .songs: "Songs"
    case .albums: "Albums"
    case .artists: "Artists"
    }
  }
}

public struct Album: Codable, Identifiable, Hashable, Sendable {
  public var id: String { browseID }
  public let browseID: String
  public let playlistID: String?
  public let title: String
  public let subtitle: String?
  public let thumbnailURL: URL?
  public let artistReferences: [Artist]?
  /// Opaque browse parameters from the linked endpoint, when supplied.
  public let params: String?

  public init(
    browseID: String, playlistID: String? = nil, title: String,
    subtitle: String? = nil, thumbnailURL: URL? = nil, artistReferences: [Artist]? = nil,
    params: String? = nil
  ) {
    self.browseID = browseID
    self.playlistID = playlistID
    self.title = title
    self.subtitle = subtitle
    self.thumbnailURL = thumbnailURL
    self.artistReferences = artistReferences
    self.params = params
  }
}

public struct Artist: Codable, Identifiable, Hashable, Sendable {
  public var id: String { browseID }
  public let browseID: String
  public let title: String
  public let thumbnailURL: URL?
  /// Opaque browse parameters from the linked endpoint, when supplied.
  public let params: String?

  public init(browseID: String, title: String, thumbnailURL: URL? = nil, params: String? = nil) {
    self.browseID = browseID
    self.title = title
    self.thumbnailURL = thumbnailURL
    self.params = params
  }
}

public enum LibraryItem: Identifiable, Hashable, Sendable {
  case song(Song)
  case playlist(Playlist)
  case album(Album)
  case artist(Artist)

  public var id: String {
    switch self {
    case .song(let value): "song-\(value.id)"
    case .playlist(let value): "playlist-\(value.id)"
    case .album(let value): "album-\(value.id)"
    case .artist(let value): "artist-\(value.id)"
    }
  }
}

public enum CatalogItem: Identifiable, Hashable, Sendable {
  case song(Song)
  case playlist(Playlist)
  case album(Album)

  public var id: String {
    switch self {
    case .song(let value): return "song-\(value.id)"
    case .playlist(let value): return "playlist-\(value.id)"
    case .album(let value): return "album-\(value.id)"
    }
  }

  public var title: String {
    switch self {
    case .song(let value): return value.title
    case .playlist(let value): return value.title
    case .album(let value): return value.title
    }
  }

  public var subtitle: String? {
    switch self {
    case .song(let value): return value.artist
    case .playlist(let value): return value.subtitle
    case .album(let value): return value.subtitle
    }
  }

  public var thumbnailURL: URL? {
    switch self {
    case .song(let value): return value.thumbnailURL
    case .playlist(let value): return value.thumbnailURL
    case .album(let value): return value.thumbnailURL
    }
  }
}

public struct HomeFilter: Identifiable, Hashable, Sendable {
  public let title: String
  public let params: String?
  public var id: String { params ?? "home-all" }

  public init(title: String, params: String? = nil) {
    self.title = title
    self.params = params
  }
}

public struct HomeSection: Identifiable, Hashable, Sendable {
  public let id: String
  public let title: String
  public let subtitle: String?
  public let items: [CatalogItem]

  public init(id: String, title: String, subtitle: String? = nil, items: [CatalogItem]) {
    self.id = id
    self.title = title
    self.subtitle = subtitle
    self.items = items
  }
}

public struct HomePage: Equatable, Sendable {
  public let filters: [HomeFilter]
  public let sections: [HomeSection]
  public let next: PaginationToken?
  public let header: CatalogPageHeader?

  public init(
    filters: [HomeFilter] = [], sections: [HomeSection], next: PaginationToken? = nil,
    header: CatalogPageHeader? = nil
  ) {
    self.filters = filters
    self.sections = sections
    self.next = next
    self.header = header
  }
}