mirror of
https://github.com/yattee/yattee.git
synced 2024-12-25 06:53:42 +00:00
50 lines
1.4 KiB
Swift
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()
|
|
}
|
|
}
|
|
}
|
|
}
|