SPB Git

Repository Public

MiniPlayer.swift 6.5 KB 127 lines swift

1// SP Music iOS2// File: Components/MiniPlayer.swift3// Description: Mini player (iPhone), player bar (iPad) and the seek bar.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import SPMusicCore9import SwiftUI1011/// Always within reach while browsing: cover, title, play/pause, next, and a thin progress line. A tap opens the12/// full player.13struct MiniPlayer: View {14    @Environment(AppEnvironment.self) private var env15    var body: some View {16        let player = env.player17        if let track = player.current {18            HStack(spacing: 10) {19                Button { env.showNowPlaying = true } label: {20                    HStack(spacing: 10) {21                        ArtworkView(id: track.artworkId, side: 34, corner: 6)22                        VStack(alignment: .leading, spacing: 1) {23                            Text(track.title).font(.subheadline.weight(.semibold)).lineLimit(1)24                            Text(player.error?.message ?? track.artistName).font(.caption).foregroundStyle(player.error == nil ? Color.spMuted : .yellow).lineLimit(1)25                        }26                        Spacer(minLength: 0)27                    }28                    .contentShape(.rect)29                }30                .buttonStyle(.plain)31                .accessibilityLabel("Ouvrir le lecteur : \(track.title), \(track.artistName)")32                .accessibilityIdentifier("mini-player")33                Button { player.toggle() } label: {34                    ZStack {35                        if player.isBuffering { ProgressView().controlSize(.small) }36                        else { Image(systemName: player.isPlaying ? "pause.fill" : "play.fill").font(.title3).contentTransition(.symbolEffect(.replace)) }37                    }38                    .frame(width: 36, height: 36).contentShape(.rect)39                }40                .buttonStyle(.plain)41                .accessibilityLabel(player.isPlaying ? "Pause" : "Lecture")42                .accessibilityIdentifier("mini-play-pause")43                Button { player.next() } label: { Image(systemName: "forward.fill").font(.body).frame(width: 34, height: 36).contentShape(.rect) }44                    .buttonStyle(.plain).accessibilityLabel("Titre suivant")45            }46            .padding(.horizontal, 12)47            .overlay(alignment: .bottom) {48                GeometryReader { g in49                    Capsule().fill(Color.spEmber).frame(width: g.size.width * (player.duration > 0 ? min(1, player.position / player.duration) : 0), height: 2)50                }51                .frame(height: 2).padding(.horizontal, 14).accessibilityHidden(true)52            }53        }54    }55}5657/// The wide player bar of the iPad layout: transport, scrubber and output on one line.58struct PlayerBar: View {59    @Environment(AppEnvironment.self) private var env60    var body: some View {61        let player = env.player62        if let track = player.current {63            HStack(spacing: 16) {64                Button { env.showNowPlaying = true } label: {65                    HStack(spacing: 12) {66                        ArtworkView(id: track.artworkId, side: 48, corner: 8)67                        VStack(alignment: .leading, spacing: 2) {68                            Text(track.title).font(.subheadline.weight(.semibold)).lineLimit(1)69                            Text(player.error?.message ?? "\(track.artistName) — \(track.albumTitle)").font(.caption).foregroundStyle(Color.spMuted).lineLimit(1)70                        }71                    }72                    .frame(minWidth: 150, maxWidth: 260, alignment: .leading).contentShape(.rect)73                }74                .buttonStyle(.plain).accessibilityIdentifier("mini-player")75                HStack(spacing: 22) {76                    Button { player.previous() } label: { Image(systemName: "backward.fill") }.accessibilityLabel("Titre précédent")77                    Button { player.toggle() } label: {78                        Image(systemName: player.isPlaying ? "pause.fill" : "play.fill").font(.title2).frame(width: 30).contentTransition(.symbolEffect(.replace))79                    }80                    .accessibilityLabel(player.isPlaying ? "Pause" : "Lecture").accessibilityIdentifier("mini-play-pause")81                    Button { player.next() } label: { Image(systemName: "forward.fill") }.accessibilityLabel("Titre suivant")82                }83                .buttonStyle(.plain).font(.title3)84                Text(Format.duration(player.position)).font(.caption.monospacedDigit()).foregroundStyle(Color.spMuted)85                Scrubber(compact: true).frame(minWidth: 120)86                Text(Format.duration(player.duration)).font(.caption.monospacedDigit()).foregroundStyle(Color.spMuted)87                Button { player.setShuffle(!player.queue.shuffle) } label: { Image(systemName: "shuffle").foregroundStyle(player.queue.shuffle ? Color.spEmber : .primary) }88                    .buttonStyle(.plain).accessibilityLabel("Lecture aléatoire").accessibilityValue(player.queue.shuffle ? "activée" : "désactivée")89                RoutePickerButton().frame(width: 28, height: 28).accessibilityLabel("Sortie audio")90            }91            .padding(.horizontal, 18).padding(.vertical, 10)92            .background(.regularMaterial)93            .overlay(alignment: .top) { Divider() }94        }95    }96}9798/// Seek bar. While the finger is down the thumb follows it and the audio keeps playing; the seek happens on release.99struct Scrubber: View {100    @Environment(AppEnvironment.self) private var env101    var compact = false102    @State private var dragging: Double?103104    var body: some View {105        let player = env.player106        let duration = max(player.duration, 0.1)107        let value = dragging ?? min(player.position, duration)108        VStack(spacing: 6) {109            Slider(value: Binding(get: { value }, set: { dragging = $0 }), in: 0...duration) { editing in110                if !editing, let t = dragging { player.seek(to: t); dragging = nil }111            }112            .tint(.white)113            .accessibilityLabel("Position de lecture")114            .accessibilityValue("\(Format.duration(value)) sur \(Format.duration(duration))")115            .accessibilityIdentifier("scrubber")116            if !compact {117                HStack {118                    Text(Format.duration(value)).accessibilityIdentifier("elapsed")119                    Spacer()120                    Text("-" + Format.duration(max(0, duration - value)))121                }122                .font(.caption.monospacedDigit()).foregroundStyle(.white.opacity(0.65))123            }124        }125    }126}127
Files main
Menu

Theme