Match album page reference layout
This commit is contained in:
1 parent
1e6bb30b50
commit
d6b35fe346
4 files changed
+132
-107
No files matched your search
@@ -0,0 +1,43 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct AlbumArtworkView: View {
|
||||||
|
let url: URL?
|
||||||
|
var cornerRadius: CGFloat = 12
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
AsyncImage(url: url) { phase in
|
||||||
|
switch phase {
|
||||||
|
case .success(let image):
|
||||||
|
image
|
||||||
|
.resizable()
|
||||||
|
.scaledToFill()
|
||||||
|
case .empty:
|
||||||
|
artworkPlaceholder
|
||||||
|
.overlay { ProgressView().tint(.red) }
|
||||||
|
case .failure:
|
||||||
|
artworkPlaceholder
|
||||||
|
@unknown default:
|
||||||
|
artworkPlaceholder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.aspectRatio(1, contentMode: .fit)
|
||||||
|
.clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
|
||||||
|
}
|
||||||
|
|
||||||
|
private var artworkPlaceholder: some View {
|
||||||
|
Image(systemName: "music.note")
|
||||||
|
.font(.system(size: 48, weight: .light))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||||
|
.background(.quaternary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AlbumArtworkView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
AlbumArtworkView(url: nil)
|
||||||
|
.frame(width: 240)
|
||||||
|
.padding()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -142,7 +142,7 @@ struct AlbumDetailView: View {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle(viewModel.album?.name ?? "Album")
|
.navigationTitle("")
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarTitleDisplayMode(.inline)
|
||||||
.tint(.red)
|
.tint(.red)
|
||||||
.task {
|
.task {
|
||||||
@@ -152,8 +152,9 @@ struct AlbumDetailView: View {
|
|||||||
|
|
||||||
private func albumContent(_ album: Album) -> some View {
|
private func albumContent(_ album: Album) -> some View {
|
||||||
ScrollView {
|
ScrollView {
|
||||||
VStack(alignment: .leading, spacing: 24) {
|
VStack(alignment: .leading, spacing: 20) {
|
||||||
albumHeader(album)
|
albumHeader(album)
|
||||||
|
Divider()
|
||||||
|
|
||||||
if let errorMessage = viewModel.errorMessage {
|
if let errorMessage = viewModel.errorMessage {
|
||||||
ContentUnavailableView {
|
ContentUnavailableView {
|
||||||
@@ -185,7 +186,7 @@ struct AlbumDetailView: View {
|
|||||||
LazyVStack(spacing: 0) {
|
LazyVStack(spacing: 0) {
|
||||||
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
||||||
let track = viewModel.tracks[index]
|
let track = viewModel.tracks[index]
|
||||||
AlbumTrackRow(track: track, index: index)
|
TrackRowView(track: track, index: index)
|
||||||
if index < viewModel.tracks.count - 1 {
|
if index < viewModel.tracks.count - 1 {
|
||||||
Divider()
|
Divider()
|
||||||
.padding(.leading, 42)
|
.padding(.leading, 42)
|
||||||
@@ -205,54 +206,69 @@ struct AlbumDetailView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func albumHeader(_ album: Album) -> some View {
|
private func albumHeader(_ album: Album) -> some View {
|
||||||
VStack(alignment: .leading, spacing: 12) {
|
VStack(alignment: .center, spacing: 12) {
|
||||||
AsyncImage(url: viewModel.artworkURL(for: album)) { phase in
|
AlbumArtworkView(url: viewModel.artworkURL(for: album), cornerRadius: 18)
|
||||||
switch phase {
|
|
||||||
case .success(let image):
|
|
||||||
image
|
|
||||||
.resizable()
|
|
||||||
.scaledToFill()
|
|
||||||
case .empty:
|
|
||||||
artworkPlaceholder
|
|
||||||
.overlay { ProgressView().tint(.red) }
|
|
||||||
case .failure:
|
|
||||||
artworkPlaceholder
|
|
||||||
@unknown default:
|
|
||||||
artworkPlaceholder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.frame(maxWidth: .infinity)
|
|
||||||
.containerRelativeFrame(.horizontal) { length, _ in
|
.containerRelativeFrame(.horizontal) { length, _ in
|
||||||
min(280, length - 40)
|
min(280, length - 40)
|
||||||
}
|
}
|
||||||
.aspectRatio(1, contentMode: .fit)
|
|
||||||
.clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
|
|
||||||
.frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
.accessibilityLabel("\(album.name) album artwork")
|
.accessibilityLabel("\(album.name) album artwork")
|
||||||
|
|
||||||
Text(album.name)
|
Text(album.name)
|
||||||
.font(.title2.weight(.bold))
|
.font(.title.weight(.bold))
|
||||||
.fixedSize(horizontal: false, vertical: true)
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
|
.multilineTextAlignment(.center)
|
||||||
|
|
||||||
if !artistNames(for: album.artists).isEmpty {
|
if !artistNames(for: album.artists).isEmpty {
|
||||||
Text(artistNames(for: album.artists))
|
Text(artistNames(for: album.artists))
|
||||||
.font(.subheadline)
|
.font(.title3)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
|
.multilineTextAlignment(.center)
|
||||||
}
|
}
|
||||||
|
|
||||||
Text("\(viewModel.tracks.count) \(viewModel.tracks.count == 1 ? "song" : "songs")")
|
Text(albumMetadata(for: album))
|
||||||
.font(.footnote)
|
.font(.subheadline.weight(.semibold))
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
|
|
||||||
|
albumActions
|
||||||
}
|
}
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var artworkPlaceholder: some View {
|
private var albumActions: some View {
|
||||||
Image(systemName: "music.note")
|
HStack(spacing: 16) {
|
||||||
.font(.system(size: 48, weight: .light))
|
Image(systemName: "shuffle")
|
||||||
.foregroundStyle(.secondary)
|
.font(.title3.weight(.semibold))
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
.frame(width: 54, height: 54)
|
||||||
.background(.quaternary)
|
.background(.thinMaterial, in: Circle())
|
||||||
|
.accessibilityLabel("Shuffle album")
|
||||||
|
|
||||||
|
Label("Play", systemImage: "play.fill")
|
||||||
|
.font(.title3.weight(.semibold))
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.frame(height: 54)
|
||||||
|
.foregroundStyle(.white)
|
||||||
|
.background(.red, in: Capsule())
|
||||||
|
.accessibilityLabel("Play album")
|
||||||
|
|
||||||
|
Image(systemName: "plus")
|
||||||
|
.font(.title3.weight(.semibold))
|
||||||
|
.frame(width: 54, height: 54)
|
||||||
|
.background(.thinMaterial, in: Circle())
|
||||||
|
.accessibilityLabel("Add album")
|
||||||
|
}
|
||||||
|
.padding(.top, 8)
|
||||||
|
.accessibilityElement(children: .combine)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func albumMetadata(for album: Album) -> String {
|
||||||
|
let genre = album.genre.flatMap { reference -> String? in
|
||||||
|
guard case .value(let genre) = reference else { return nil }
|
||||||
|
return genre.name
|
||||||
|
}
|
||||||
|
let songCount =
|
||||||
|
"\(viewModel.tracks.count) \(viewModel.tracks.count == 1 ? "song" : "songs")"
|
||||||
|
return [genre, songCount].compactMap { $0 }.joined(separator: " · ")
|
||||||
}
|
}
|
||||||
|
|
||||||
private func artistNames(for artists: [APIReference<Artist>]) -> String {
|
private func artistNames(for artists: [APIReference<Artist>]) -> String {
|
||||||
@@ -264,55 +280,6 @@ struct AlbumDetailView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct AlbumTrackRow: View {
|
|
||||||
let track: Track
|
|
||||||
let index: Int
|
|
||||||
|
|
||||||
private var artistNames: String {
|
|
||||||
track.artists.compactMap { reference in
|
|
||||||
guard case .value(let artist) = reference else { return nil }
|
|
||||||
return artist.name
|
|
||||||
}
|
|
||||||
.joined(separator: ", ")
|
|
||||||
}
|
|
||||||
|
|
||||||
private var duration: String {
|
|
||||||
let seconds = max(0, Int(track.durationInSeconds))
|
|
||||||
return String(format: "%d:%02d", seconds / 60, seconds % 60)
|
|
||||||
}
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
HStack(spacing: 14) {
|
|
||||||
Text("\(index + 1)")
|
|
||||||
.font(.footnote.monospacedDigit())
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.frame(width: 28, alignment: .trailing)
|
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 3) {
|
|
||||||
Text(track.name)
|
|
||||||
.font(.body)
|
|
||||||
.foregroundStyle(.primary)
|
|
||||||
.lineLimit(1)
|
|
||||||
|
|
||||||
if !artistNames.isEmpty {
|
|
||||||
Text(artistNames)
|
|
||||||
.font(.footnote)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.lineLimit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Spacer(minLength: 8)
|
|
||||||
|
|
||||||
Text(duration)
|
|
||||||
.font(.footnote.monospacedDigit())
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
}
|
|
||||||
.padding(.vertical, 12)
|
|
||||||
.accessibilityElement(children: .combine)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct AlbumDetailView_Previews: PreviewProvider {
|
struct AlbumDetailView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
|
|||||||
@@ -118,24 +118,7 @@ private struct HomeAlbumCard: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack(alignment: .leading, spacing: 5) {
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
AsyncImage(url: artworkURL) { phase in
|
AlbumArtworkView(url: artworkURL)
|
||||||
switch phase {
|
|
||||||
case .success(let image):
|
|
||||||
image
|
|
||||||
.resizable()
|
|
||||||
.scaledToFill()
|
|
||||||
case .empty:
|
|
||||||
ProgressView()
|
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
||||||
.background(.quaternary)
|
|
||||||
case .failure:
|
|
||||||
artworkPlaceholder
|
|
||||||
@unknown default:
|
|
||||||
artworkPlaceholder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.aspectRatio(1, contentMode: .fit)
|
|
||||||
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
|
|
||||||
|
|
||||||
Text(album.name)
|
Text(album.name)
|
||||||
.font(.subheadline.weight(.medium))
|
.font(.subheadline.weight(.medium))
|
||||||
@@ -152,13 +135,6 @@ private struct HomeAlbumCard: View {
|
|||||||
.accessibilityElement(children: .combine)
|
.accessibilityElement(children: .combine)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var artworkPlaceholder: some View {
|
|
||||||
Image(systemName: "music.note")
|
|
||||||
.font(.largeTitle)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
||||||
.background(.quaternary)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct HomeAlbumCard_Previews: PreviewProvider {
|
private struct HomeAlbumCard_Previews: PreviewProvider {
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct TrackRowView: View {
|
||||||
|
let track: Track
|
||||||
|
let index: Int
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
HStack(spacing: 14) {
|
||||||
|
Text("\(index + 1)")
|
||||||
|
.font(.body.monospacedDigit())
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.frame(width: 28, alignment: .trailing)
|
||||||
|
|
||||||
|
Text(track.name)
|
||||||
|
.font(.body)
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
.lineLimit(1)
|
||||||
|
|
||||||
|
Spacer(minLength: 8)
|
||||||
|
|
||||||
|
Image(systemName: "ellipsis")
|
||||||
|
.font(.body.weight(.semibold))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.frame(width: 32, height: 44)
|
||||||
|
.accessibilityHidden(true)
|
||||||
|
}
|
||||||
|
.padding(.vertical, 8)
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
.accessibilityElement(children: .combine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TrackRowView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
TrackRowView(track: AlbumDetailViewModel.preview.tracks[0], index: 0)
|
||||||
|
.padding(.horizontal)
|
||||||
|
.previewLayout(.sizeThatFits)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user