Reuse shared album cards across screens
This commit is contained in:
1 parent
85e3b3148e
commit
dd733e8e03
5 files changed
+67
-93
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AlbumCardView: View {
|
||||
let album: Album
|
||||
let artworkURL: URL?
|
||||
var showsArtists = true
|
||||
|
||||
private var artistNames: String {
|
||||
album.artists.compactMap { reference in
|
||||
guard case .value(let artist) = reference else { return nil }
|
||||
return artist.name
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
AlbumArtworkView(url: artworkURL)
|
||||
.frame(maxWidth: .infinity)
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
|
||||
Text(album.name)
|
||||
.font(.subheadline.weight(.medium))
|
||||
.lineLimit(1)
|
||||
.truncationMode(.tail)
|
||||
.foregroundStyle(.primary)
|
||||
|
||||
if showsArtists && !artistNames.isEmpty {
|
||||
Text(artistNames)
|
||||
.font(.footnote)
|
||||
.lineLimit(1)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .topLeading)
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
}
|
||||
|
||||
struct AlbumCardView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AlbumCardView(
|
||||
album: Album(
|
||||
id: "album-preview",
|
||||
name: "An Album with a Long Title",
|
||||
artists: [.value(Artist(id: "artist-preview", name: "Example Artist", file: nil))],
|
||||
file: nil,
|
||||
genre: nil
|
||||
),
|
||||
artworkURL: nil
|
||||
)
|
||||
.frame(width: 170)
|
||||
.padding()
|
||||
.preferredColorScheme(.dark)
|
||||
}
|
||||
}
|
||||
@@ -177,11 +177,11 @@ struct ArtistDetailView: View {
|
||||
LazyHStack(alignment: .top, spacing: 14) {
|
||||
ForEach(viewModel.albums) { album in
|
||||
NavigationLink { AlbumDetailView(albumID: album.id) } label: {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
AlbumArtworkView(url: viewModel.artworkURL(for: album))
|
||||
Text(album.name).font(.subheadline.weight(.medium))
|
||||
.lineLimit(2).foregroundStyle(.primary)
|
||||
}
|
||||
AlbumCardView(
|
||||
album: album,
|
||||
artworkURL: viewModel.artworkURL(for: album),
|
||||
showsArtists: false
|
||||
)
|
||||
.containerRelativeFrame(.horizontal) { length, _ in (length - 40 - 28) / 2.5 }
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
@@ -85,7 +85,8 @@ private struct HomeAlbumCarousel: View {
|
||||
NavigationLink {
|
||||
AlbumDetailView(albumID: album.id)
|
||||
} label: {
|
||||
HomeAlbumCard(album: album, artworkURL: artworkURL(album))
|
||||
AlbumCardView(album: album, artworkURL: artworkURL(album))
|
||||
.frame(maxWidth: 150)
|
||||
.containerRelativeFrame(.horizontal) { length, _ in
|
||||
(length - 40 - 28) / 2.2
|
||||
}
|
||||
@@ -112,51 +113,6 @@ private struct HomeAlbumCarousel_Previews: PreviewProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private struct HomeAlbumCard: View {
|
||||
let album: Album
|
||||
let artworkURL: URL?
|
||||
|
||||
private var artistNames: String {
|
||||
album.artists.compactMap { reference in
|
||||
guard case .value(let artist) = reference else { return nil }
|
||||
return artist.name
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
AlbumArtworkView(url: artworkURL)
|
||||
.frame(maxWidth: 150)
|
||||
|
||||
Text(album.name)
|
||||
.font(.subheadline.weight(.medium))
|
||||
.lineLimit(2)
|
||||
.foregroundStyle(.primary)
|
||||
|
||||
if !artistNames.isEmpty {
|
||||
Text(artistNames)
|
||||
.font(.footnote)
|
||||
.lineLimit(1)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct HomeAlbumCard_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
HomeAlbumCard(
|
||||
album: HomeViewModel.preview.sections[0].albums[0],
|
||||
artworkURL: nil
|
||||
)
|
||||
.padding()
|
||||
.frame(maxWidth: 240)
|
||||
}
|
||||
}
|
||||
|
||||
struct HomeView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
HomeView(viewModel: .preview)
|
||||
|
||||
@@ -231,32 +231,14 @@ struct LibraryAlbumsView: View {
|
||||
albumCard(album)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.gridCellAnchor(.topLeading)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func albumCard(_ album: Album) -> some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
AlbumArtworkView(url: model.artworkURL(for: album))
|
||||
Text(album.name)
|
||||
.font(.subheadline.weight(.medium))
|
||||
.lineLimit(2)
|
||||
.foregroundStyle(.primary)
|
||||
Text(artistNames(album.artists))
|
||||
.font(.footnote)
|
||||
.lineLimit(1)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
|
||||
private func artistNames(_ references: [APIReference<Artist>]) -> String {
|
||||
let names = references.compactMap { reference -> String? in
|
||||
guard case .value(let artist) = reference else { return nil }
|
||||
return artist.name
|
||||
}
|
||||
return names.isEmpty ? "Unknown Artist" : names.joined(separator: ", ")
|
||||
AlbumCardView(album: album, artworkURL: model.artworkURL(for: album))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -162,28 +162,8 @@ struct LibraryView: View {
|
||||
}
|
||||
|
||||
private func recentAlbumCard(_ album: Album) -> some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
AlbumArtworkView(url: model.artworkURL(for: album))
|
||||
.frame(width: 170)
|
||||
Text(album.name)
|
||||
.font(.subheadline.weight(.medium))
|
||||
.lineLimit(2)
|
||||
.foregroundStyle(.primary)
|
||||
Text(artistNames(album.artists))
|
||||
.font(.footnote)
|
||||
.lineLimit(1)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.frame(width: 170, alignment: .leading)
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
|
||||
private func artistNames(_ references: [APIReference<Artist>]) -> String {
|
||||
let names = references.compactMap { reference -> String? in
|
||||
guard case .value(let artist) = reference else { return nil }
|
||||
return artist.name
|
||||
}
|
||||
return names.isEmpty ? "Unknown Artist" : names.joined(separator: ", ")
|
||||
AlbumCardView(album: album, artworkURL: model.artworkURL(for: album))
|
||||
.frame(width: 170, alignment: .topLeading)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user