Files
MobileMusicAssistant/Mobile Music Assistant/ViewsComponentsFavoriteButton.swift
T

49 lines
1.6 KiB
Swift

//
// FavoriteButton.swift
// Mobile Music Assistant
//
// Created by Sven Hanold on 05.04.26.
//
import SwiftUI
/// Reusable heart button for toggling favorites on artists, albums, and tracks.
struct FavoriteButton: View {
@Environment(MAService.self) private var service
@Environment(MAToastManager.self) private var toastManager
let uri: String
var size: CGFloat = 22
var showInLight: Bool = false
/// Display name shown in the toast when the item is liked. Pass nil to suppress the toast.
var itemName: String? = nil
private var isFavorite: Bool {
service.libraryManager.isFavorite(uri: uri)
}
var body: some View {
Button {
let wasAlreadyFavorite = isFavorite
Task {
await service.libraryManager.toggleFavorite(
uri: uri,
currentlyFavorite: wasAlreadyFavorite
)
if let name = itemName {
if wasAlreadyFavorite {
toastManager.show(name, icon: "heart.slash", iconColor: .secondary)
} else {
toastManager.show(name, icon: "heart.fill", iconColor: .red)
}
}
}
} label: {
Image(systemName: isFavorite ? "heart.fill" : "heart")
.font(.system(size: size))
.foregroundStyle(isFavorite ? .red : (showInLight ? .white.opacity(0.7) : .secondary))
.contentTransition(.symbolEffect(.replace))
}
.buttonStyle(.plain)
}
}