Add persistent mini player
This commit is contained in:
1 parent
f7e204f2d8
commit
f6f370d5ca
3 files changed
+134
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct MainTabView: View {
|
struct MainTabView: View {
|
||||||
|
@EnvironmentObject private var player: PlayerManager
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
TabView {
|
TabView {
|
||||||
Tab("Home", systemImage: "house") {
|
Tab("Home", systemImage: "house") {
|
||||||
@@ -16,6 +18,26 @@ struct MainTabView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.tint(.red)
|
.tint(.red)
|
||||||
|
.safeAreaInset(edge: .bottom, spacing: 0) {
|
||||||
|
if let track = player.currentTrack {
|
||||||
|
MiniPlayerView(
|
||||||
|
track: track,
|
||||||
|
isPlaying: player.isPlaying,
|
||||||
|
artworkURL: player.artworkURL(for: track),
|
||||||
|
onTogglePlayback: {
|
||||||
|
if player.isPlaying { player.pause() }
|
||||||
|
else { player.play() }
|
||||||
|
},
|
||||||
|
onNext: player.next
|
||||||
|
)
|
||||||
|
.padding(.horizontal, 12)
|
||||||
|
// The iOS 26 tab bar floats over the bottom safe area, so lift
|
||||||
|
// the mini-player above it instead of letting the glass layers collide.
|
||||||
|
.padding(.bottom, 54)
|
||||||
|
.transition(.move(edge: .bottom).combined(with: .opacity))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.animation(.snappy(duration: 0.25), value: player.currentTrack?.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct MiniPlayerView: View {
|
||||||
|
let track: Track
|
||||||
|
let isPlaying: Bool
|
||||||
|
let artworkURL: URL?
|
||||||
|
let onTogglePlayback: () -> Void
|
||||||
|
let onNext: () -> Void
|
||||||
|
|
||||||
|
private var artistNames: String {
|
||||||
|
track.artists.compactMap { reference in
|
||||||
|
guard case .value(let artist) = reference else { return nil }
|
||||||
|
return artist.name
|
||||||
|
}
|
||||||
|
.joined(separator: ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
HStack(spacing: 10) {
|
||||||
|
artwork
|
||||||
|
.frame(width: 46, height: 46)
|
||||||
|
.clipShape(RoundedRectangle(cornerRadius: 11, style: .continuous))
|
||||||
|
.accessibilityHidden(true)
|
||||||
|
|
||||||
|
VStack(alignment: .leading, spacing: 2) {
|
||||||
|
Text(track.name)
|
||||||
|
.font(.system(size: 16, weight: .semibold))
|
||||||
|
.lineLimit(1)
|
||||||
|
.truncationMode(.tail)
|
||||||
|
|
||||||
|
Text(artistNames.isEmpty ? "Unknown Artist" : artistNames)
|
||||||
|
.font(.system(size: 14))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.lineLimit(1)
|
||||||
|
.truncationMode(.tail)
|
||||||
|
}
|
||||||
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
|
|
||||||
|
Button(action: onTogglePlayback) {
|
||||||
|
Image(systemName: isPlaying ? "pause.fill" : "play.fill")
|
||||||
|
.font(.system(size: 21, weight: .medium))
|
||||||
|
.frame(width: 42, height: 46)
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.accessibilityLabel(isPlaying ? "Pause" : "Play")
|
||||||
|
|
||||||
|
Button(action: onNext) {
|
||||||
|
Image(systemName: "forward.end.fill")
|
||||||
|
.font(.system(size: 21, weight: .medium))
|
||||||
|
.frame(width: 42, height: 46)
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.accessibilityLabel("Next track")
|
||||||
|
}
|
||||||
|
.padding(.leading, 11)
|
||||||
|
.padding(.trailing, 9)
|
||||||
|
.frame(height: 62)
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
.background(.ultraThinMaterial, in: Capsule())
|
||||||
|
.overlay {
|
||||||
|
Capsule()
|
||||||
|
.strokeBorder(.primary.opacity(0.12), lineWidth: 1)
|
||||||
|
}
|
||||||
|
.shadow(color: .black.opacity(0.12), radius: 14, y: 5)
|
||||||
|
.accessibilityElement(children: .contain)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private var artwork: some View {
|
||||||
|
if let artworkURL {
|
||||||
|
AsyncImage(url: artworkURL) { phase in
|
||||||
|
if let image = phase.image {
|
||||||
|
image.resizable().scaledToFill()
|
||||||
|
} else {
|
||||||
|
artworkPlaceholder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
artworkPlaceholder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var artworkPlaceholder: some View {
|
||||||
|
Image(systemName: "music.note")
|
||||||
|
.font(.title2.weight(.light))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||||
|
.background(.quaternary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MiniPlayerView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
MiniPlayerView(
|
||||||
|
track: AlbumDetailViewModel.preview.tracks[0],
|
||||||
|
isPlaying: true,
|
||||||
|
artworkURL: nil,
|
||||||
|
onTogglePlayback: {},
|
||||||
|
onNext: {}
|
||||||
|
)
|
||||||
|
.padding()
|
||||||
|
.preferredColorScheme(.dark)
|
||||||
|
.previewLayout(.sizeThatFits)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -191,6 +191,11 @@ final class PlayerManager: ObservableObject {
|
|||||||
errorMessage = nil
|
errorMessage = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func artworkURL(for track: Track) -> URL? {
|
||||||
|
guard case .value(let album) = track.album else { return nil }
|
||||||
|
return apiConfiguration.mediaURL(for: album.file)
|
||||||
|
}
|
||||||
|
|
||||||
func shuffleUpcoming() {
|
func shuffleUpcoming() {
|
||||||
let currentIndex = player.currentIndex
|
let currentIndex = player.currentIndex
|
||||||
guard currentIndex >= 0, player.items.count - currentIndex > 2 else { return }
|
guard currentIndex >= 0, player.items.count - currentIndex > 2 else { return }
|
||||||
|
|||||||
Reference in new issue
Block a user