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) {
|
LazyHStack(alignment: .top, spacing: 14) {
|
||||||
ForEach(viewModel.albums) { album in
|
ForEach(viewModel.albums) { album in
|
||||||
NavigationLink { AlbumDetailView(albumID: album.id) } label: {
|
NavigationLink { AlbumDetailView(albumID: album.id) } label: {
|
||||||
VStack(alignment: .leading, spacing: 6) {
|
AlbumCardView(
|
||||||
AlbumArtworkView(url: viewModel.artworkURL(for: album))
|
album: album,
|
||||||
Text(album.name).font(.subheadline.weight(.medium))
|
artworkURL: viewModel.artworkURL(for: album),
|
||||||
.lineLimit(2).foregroundStyle(.primary)
|
showsArtists: false
|
||||||
}
|
)
|
||||||
.containerRelativeFrame(.horizontal) { length, _ in (length - 40 - 28) / 2.5 }
|
.containerRelativeFrame(.horizontal) { length, _ in (length - 40 - 28) / 2.5 }
|
||||||
}
|
}
|
||||||
.buttonStyle(.plain)
|
.buttonStyle(.plain)
|
||||||
|
|||||||
@@ -85,7 +85,8 @@ private struct HomeAlbumCarousel: View {
|
|||||||
NavigationLink {
|
NavigationLink {
|
||||||
AlbumDetailView(albumID: album.id)
|
AlbumDetailView(albumID: album.id)
|
||||||
} label: {
|
} label: {
|
||||||
HomeAlbumCard(album: album, artworkURL: artworkURL(album))
|
AlbumCardView(album: album, artworkURL: artworkURL(album))
|
||||||
|
.frame(maxWidth: 150)
|
||||||
.containerRelativeFrame(.horizontal) { length, _ in
|
.containerRelativeFrame(.horizontal) { length, _ in
|
||||||
(length - 40 - 28) / 2.2
|
(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 {
|
struct HomeView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
HomeView(viewModel: .preview)
|
HomeView(viewModel: .preview)
|
||||||
|
|||||||
@@ -231,32 +231,14 @@ struct LibraryAlbumsView: View {
|
|||||||
albumCard(album)
|
albumCard(album)
|
||||||
}
|
}
|
||||||
.buttonStyle(.plain)
|
.buttonStyle(.plain)
|
||||||
|
.gridCellAnchor(.topLeading)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func albumCard(_ album: Album) -> some View {
|
private func albumCard(_ album: Album) -> some View {
|
||||||
VStack(alignment: .leading, spacing: 5) {
|
AlbumCardView(album: album, artworkURL: model.artworkURL(for: album))
|
||||||
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: ", ")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -162,28 +162,8 @@ struct LibraryView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func recentAlbumCard(_ album: Album) -> some View {
|
private func recentAlbumCard(_ album: Album) -> some View {
|
||||||
VStack(alignment: .leading, spacing: 5) {
|
AlbumCardView(album: album, artworkURL: model.artworkURL(for: album))
|
||||||
AlbumArtworkView(url: model.artworkURL(for: album))
|
.frame(width: 170, alignment: .topLeading)
|
||||||
.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: ", ")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user