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

geesawra.industries/hydraulophone

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

0
0
main
231 lines · 7.1 KB Raw History
import HydraulophoneCore
import SwiftUI

/// Only linked protocol references are navigable. Display names never become IDs.
struct ArtistNameLinks: View {
  @EnvironmentObject private var model: AppModel
  @Environment(\.openTrackDestination) private var localNavigate
  @Environment(\.catalogTrackDestination) private var catalogNavigate
  private var navigate: ((TrackDestination) -> Void)? { catalogNavigate ?? localNavigate }
  @State private var navigationFeedback = 0
  let artists: [Artist]
  let fallback: String
  var compact = false
  var labelAlignment: Alignment = .center

  var body: some View {
    if !artists.isEmpty, let navigate, !model.isOfflineOnly {
      if artists.count == 1, let artist = artists.first {
        Button {
          navigationFeedback += 1
          navigate(.artist(artist))
        } label: {
          Text(artist.title).frame(minHeight: compact ? 22 : 44, alignment: labelAlignment)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .foregroundStyle(HydraulophoneTheme.accent)
        .accessibilityLabel("Go to artist \(artist.title)")
        .accessibilityIdentifier("music.artist.link")
        .sensoryFeedback(.selection, trigger: navigationFeedback)
      } else {
        Menu {
          ForEach(artists) { artist in
            Button(artist.title) {
              navigationFeedback += 1
              navigate(.artist(artist))
            }
          }
        } label: {
          Text(fallback).frame(minHeight: compact ? 22 : 44, alignment: labelAlignment)
        }
        .foregroundStyle(HydraulophoneTheme.accent)
        .accessibilityLabel("Go to artist")
        .accessibilityIdentifier("music.artist.link")
        .sensoryFeedback(.selection, trigger: navigationFeedback)
      }
    } else {
      Text(fallback).foregroundStyle(.secondary)
    }
  }
}

struct AlbumNameLink: View {
  @EnvironmentObject private var model: AppModel
  @Environment(\.openTrackDestination) private var localNavigate
  @Environment(\.catalogTrackDestination) private var catalogNavigate
  private var navigate: ((TrackDestination) -> Void)? { catalogNavigate ?? localNavigate }
  @State private var navigationFeedback = 0
  let album: Album?
  let fallback: String
  var compact = false
  var labelAlignment: Alignment = .center

  var body: some View {
    if let album, let navigate,
      !model.isOfflineOnly || model.downloads.albums.contains(where: { $0.id == album.id })
    {
      Button {
        navigationFeedback += 1
        navigate(.album(album))
      } label: {
        Text(fallback).frame(minHeight: compact ? 22 : 44, alignment: labelAlignment)
          .contentShape(Rectangle())
      }
      .buttonStyle(.plain)
      .foregroundStyle(HydraulophoneTheme.accent)
      .accessibilityLabel("Go to album \(fallback)")
      .accessibilityIdentifier("music.album.link")
      .sensoryFeedback(.selection, trigger: navigationFeedback)
    } else {
      Text(fallback).foregroundStyle(.secondary)
    }
  }
}

struct MusicPlaybackActions: View {
  @State private var playbackFeedback = 0
  let isEnabled: Bool
  let play: () -> Void
  let shuffle: () -> Void

  var body: some View {
    Group {
      if #available(iOS 26.0, *) {
        GlassEffectContainer(spacing: 12) { actionLayout }
      } else {
        actionLayout
      }
    }
    .disabled(!isEnabled)
    .sensoryFeedback(.impact(weight: .light), trigger: playbackFeedback)
  }

  private var actionLayout: some View {
    ViewThatFits(in: .horizontal) {
      HStack(spacing: 12) { buttons }
      VStack(spacing: 12) { buttons }
    }
  }

  @ViewBuilder private var buttons: some View {
    if #available(iOS 26.0, *) {
      Button {
        playbackFeedback += 1
        play()
      } label: {
        actionLabel("Play", symbol: "play.fill")
      }
      .buttonStyle(.glassProminent)
      .accessibilityIdentifier("music.play")
      Button {
        playbackFeedback += 1
        shuffle()
      } label: {
        actionLabel("Shuffle", symbol: "shuffle")
      }
      .buttonStyle(.glass)
      .accessibilityIdentifier("music.shuffle")
    } else {
      Button {
        playbackFeedback += 1
        play()
      } label: {
        actionLabel("Play", symbol: "play.fill")
      }
      .buttonStyle(.borderedProminent)
      .accessibilityIdentifier("music.play")
      Button {
        playbackFeedback += 1
        shuffle()
      } label: {
        actionLabel("Shuffle", symbol: "shuffle")
      }
      .buttonStyle(.bordered)
      .accessibilityIdentifier("music.shuffle")
    }
  }

  private func actionLabel(_ title: String, symbol: String) -> some View {
    Label(title, systemImage: symbol)
      .font(.headline)
      .fixedSize(horizontal: true, vertical: false)
      .frame(maxWidth: .infinity, minHeight: 44)
  }
}

struct AlbumDetailHeader: View {
  @Environment(\.horizontalSizeClass) private var sizeClass
  @Environment(\.dynamicTypeSize) private var typeSize
  let album: Album

  var body: some View {
    Group {
      if sizeClass == .regular, !typeSize.isAccessibilitySize {
        ViewThatFits(in: .horizontal) {
          HStack(alignment: .center, spacing: 32) {
            cover(size: 260)
            identity(alignment: .leading).frame(
              minWidth: 220, maxWidth: .infinity, alignment: .leading)
          }
          compactHeader
        }
      } else {
        compactHeader
      }
    }
    .padding(.vertical, 20)
    .accessibilityIdentifier("album.header")
  }

  private var compactHeader: some View {
    VStack(spacing: 20) {
      if typeSize.isAccessibilitySize {
        cover(size: 160)
      } else {
        ViewThatFits(in: .horizontal) {
          cover(size: 248)
          cover(size: 200)
          cover(size: 160)
        }
      }
      identity(alignment: .center).multilineTextAlignment(.center)
    }
    .frame(maxWidth: .infinity)
  }

  private func cover(size: CGFloat) -> some View {
    ArtworkView(url: album.thumbnailURL, size: size, cornerRadius: 12)
      .shadow(color: .black.opacity(0.16), radius: 16, y: 8)
  }

  private func identity(alignment: HorizontalAlignment) -> some View {
    VStack(alignment: alignment, spacing: 4) {
      Text(album.title).font(.title.bold()).accessibilityAddTraits(.isHeader)
      if let artists = album.artistReferences, !artists.isEmpty {
        ArtistNameLinks(artists: artists, fallback: artists.map(\.title).joined(separator: ", "))
          .font(.title3)
      }
      if let subtitle = album.subtitle, !subtitle.isEmpty,
        subtitle != album.artistReferences?.map(\.title).joined(separator: ", ")
      {
        Text(subtitle).font(.subheadline).foregroundStyle(.secondary)
      }
    }
  }
}

extension View {
  /// Glass belongs to the floating control layer; content keeps its own surface.
  @ViewBuilder func floatingMusicSurface() -> some View {
    if #available(iOS 26.0, *) {
      glassEffect(.regular, in: RoundedRectangle(cornerRadius: 28))
    } else {
      background(.regularMaterial, in: RoundedRectangle(cornerRadius: 28))
        .overlay {
          RoundedRectangle(cornerRadius: 28)
            .strokeBorder(.primary.opacity(0.1), lineWidth: 0.5)
        }
    }
  }
}