yattee/Shared/Views/FavoriteButton.swift
2022-08-15 16:33:54 +02:00

50 lines
1.4 KiB
Swift

import Defaults
import Foundation
import SwiftUI
struct FavoriteButton: View {
let item: FavoriteItem!
let favorites = FavoritesModel.shared
let labelPadding: Bool
init(item: FavoriteItem?, labelPadding: Bool = false) {
self.item = item
self.labelPadding = labelPadding
}
@State private var isFavorite = false
var body: some View {
Group {
if favorites.isEnabled {
Button {
guard !item.isNil else {
return
}
favorites.toggle(item)
isFavorite.toggle()
} label: {
Group {
if isFavorite {
Label("Remove from Favorites", systemImage: "heart.fill")
} else {
Label("Add to Favorites", systemImage: "heart")
}
}
#if os(iOS)
.padding(labelPadding ? 10 : 0)
.contentShape(Rectangle())
#endif
}
.disabled(item.isNil)
.onAppear {
isFavorite = item.isNil ? false : favorites.contains(item)
}
} else {
EmptyView()
}
}
}
}