yattee/Shared/Views/FavoriteButton.swift

38 lines
1010 B
Swift
Raw Normal View History

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
@State private var isFavorite = false
var body: some View {
Group {
2021-12-02 20:35:25 +00:00
if favorites.isEnabled {
Button {
2021-12-02 20:35:25 +00:00
guard !item.isNil else {
return
}
favorites.toggle(item)
isFavorite.toggle()
} label: {
if isFavorite {
Label("Remove from Favorites", systemImage: "heart.fill")
} else {
Label("Add to Favorites", systemImage: "heart")
}
}
2021-12-02 20:35:25 +00:00
.disabled(item.isNil)
.onAppear {
2021-12-02 20:35:25 +00:00
isFavorite = item.isNil ? false : favorites.contains(item)
}
2021-11-01 21:56:18 +00:00
} else {
EmptyView()
2021-11-01 21:56:18 +00:00
}
}
}
}