1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Views/FavoriteButton.swift

51 lines
1.4 KiB
Swift
Raw Normal View History

import Defaults
2021-11-02 03:26:18 +05:30
import Foundation
import SwiftUI
struct FavoriteButton: View {
2021-12-03 02:05:25 +05:30
let item: FavoriteItem!
2021-11-02 03:26:18 +05:30
let favorites = FavoritesModel.shared
2022-02-04 23:08:29 +05:30
let labelPadding: Bool
init(item: FavoriteItem?, labelPadding: Bool = false) {
self.item = item
self.labelPadding = labelPadding
}
2021-11-02 03:26:18 +05:30
@State private var isFavorite = false
var body: some View {
Group {
2021-12-03 02:05:25 +05:30
if favorites.isEnabled {
Button {
2021-12-03 02:05:25 +05:30
guard !item.isNil else {
return
}
favorites.toggle(item)
isFavorite.toggle()
} label: {
2022-02-04 23:08:29 +05:30
Group {
if isFavorite {
Label("Remove from Favorites", systemImage: "heart.fill")
} else {
Label("Add to Favorites", systemImage: "heart")
}
}
2022-02-04 23:08:29 +05:30
#if os(iOS)
.padding(labelPadding ? 10 : 0)
.contentShape(Rectangle())
#endif
}
2024-02-26 18:42:11 +05:30
.help(isFavorite ? "Remove from Favorites" : "Add to Favorites")
2021-12-03 02:05:25 +05:30
.disabled(item.isNil)
.onAppear {
2021-12-03 02:05:25 +05:30
isFavorite = item.isNil ? false : favorites.contains(item)
}
2021-11-02 03:26:18 +05:30
} else {
EmptyView()
2021-11-02 03:26:18 +05:30
}
}
}
}