2021-12-01 11:22:19 +00:00
|
|
|
import Defaults
|
2021-11-01 21:56:18 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct FavoriteButton: View {
|
2021-12-02 20:35:25 +00:00
|
|
|
let item: FavoriteItem!
|
2021-11-01 21:56:18 +00:00
|
|
|
let favorites = FavoritesModel.shared
|
2022-02-04 17:38:29 +00:00
|
|
|
let labelPadding: Bool
|
|
|
|
|
|
|
|
init(item: FavoriteItem?, labelPadding: Bool = false) {
|
|
|
|
self.item = item
|
|
|
|
self.labelPadding = labelPadding
|
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
|
|
|
|
@State private var isFavorite = false
|
|
|
|
|
|
|
|
var body: some View {
|
2021-12-01 11:22:19 +00:00
|
|
|
Group {
|
2021-12-02 20:35:25 +00:00
|
|
|
if favorites.isEnabled {
|
2021-12-01 11:22:19 +00:00
|
|
|
Button {
|
2021-12-02 20:35:25 +00:00
|
|
|
guard !item.isNil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:22:19 +00:00
|
|
|
favorites.toggle(item)
|
|
|
|
isFavorite.toggle()
|
|
|
|
} label: {
|
2022-02-04 17:38:29 +00:00
|
|
|
Group {
|
|
|
|
if isFavorite {
|
|
|
|
Label("Remove from Favorites", systemImage: "heart.fill")
|
|
|
|
} else {
|
|
|
|
Label("Add to Favorites", systemImage: "heart")
|
|
|
|
}
|
2021-12-01 11:22:19 +00:00
|
|
|
}
|
2022-02-04 17:38:29 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.padding(labelPadding ? 10 : 0)
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
#endif
|
2021-12-01 11:22:19 +00:00
|
|
|
}
|
2021-12-02 20:35:25 +00:00
|
|
|
.disabled(item.isNil)
|
2021-12-01 11:22:19 +00:00
|
|
|
.onAppear {
|
2021-12-02 20:35:25 +00:00
|
|
|
isFavorite = item.isNil ? false : favorites.contains(item)
|
2021-12-01 11:22:19 +00:00
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
} else {
|
2021-12-01 11:22:19 +00:00
|
|
|
EmptyView()
|
2021-11-01 21:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|