Luminate/Sources/LuminateUI/Components/HomePosterCell.swift

103 lines
3.2 KiB
Swift

//
// HomePosterCell.swift
//
// Copyright 2026 Brendan Szymanski <hello@bscubed.dev>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
import Adwaita
import Foundation
import LuminateCore
import LuminateDI
struct HomePosterCell: View {
var item: Components.Schemas.BaseItemDto
@Injected(\.client) var client
@Injected(\.pageAnimationTracker) var pageAnimationTracker
@State private var imageData: Data?
var view: Body {
VStack {
if let data = imageData {
Picture()
.contentFit(.cover)
.data(data)
.frame(minWidth: 200, minHeight: 300)
.frame(maxWidth: 200)
.frame(maxHeight: 300)
} else {
Box(spacing: 0) {}
.frame(minWidth: 200, minHeight: 300)
.frame(maxWidth: 200)
.frame(maxHeight: 300)
.card()
}
VStack(spacing: 0) {
if let title = item.name, !title.isEmpty {
Text(item.name ?? "")
.ellipsize()
.heading()
.halign(.center)
.frame(maxWidth: 200)
}
let subtitle = item.yearString
if !subtitle.isEmpty {
Text(subtitle)
.ellipsize()
.caption()
.dimLabel()
.halign(.center)
.frame(maxWidth: 200)
}
}
.padding(6, .vertical)
.padding(12, .horizontal)
}
.onAppear {
Idle {
loadImage()
}
}
.overflow(.hidden)
.card()
}
private func loadImage() {
guard let tag = item.primaryImageTag,
let itemId = item.seriesId ?? item.id
else { return }
Task {
guard
let url = await client.imageURL(
itemId: itemId,
imageType: .primary,
tag: tag,
maxWidth: 400
)
else { return }
let service = ImageService()
let data = try? await service.loadImage(url: url)
if pageAnimationTracker.isAnimating {
_imageData.rawValue = data
} else {
imageData = data
}
}
}
}