Improve favorite button

This commit is contained in:
Arkadiusz Fal 2021-12-02 21:35:25 +01:00
parent 19b146c6ad
commit bd59b8e2c3
3 changed files with 25 additions and 21 deletions

View File

@ -5,6 +5,11 @@ struct FavoritesModel {
static let shared = FavoritesModel() static let shared = FavoritesModel()
@Default(.favorites) var all @Default(.favorites) var all
@Default(.visibleSections) var visibleSections
var isEnabled: Bool {
visibleSections.contains(.favorites)
}
func contains(_ item: FavoriteItem) -> Bool { func contains(_ item: FavoriteItem) -> Bool {
all.contains { $0 == item } all.contains { $0 == item }

View File

@ -23,6 +23,7 @@ struct SearchView: View {
@EnvironmentObject<AccountsModel> private var accounts @EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<RecentsModel> private var recents @EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state @EnvironmentObject<SearchModel> private var state
private var favorites = FavoritesModel.shared
@Default(.saveRecents) private var saveRecents @Default(.saveRecents) private var saveRecents
@ -70,10 +71,8 @@ struct SearchView: View {
#if !os(tvOS) #if !os(tvOS)
ToolbarItemGroup(placement: toolbarPlacement) { ToolbarItemGroup(placement: toolbarPlacement) {
#if os(macOS) #if os(macOS)
if let favoriteItem = favoriteItem { FavoriteButton(item: favoriteItem)
FavoriteButton(item: favoriteItem) .id(favoriteItem?.id)
.id(favoriteItem.id)
}
#endif #endif
if accounts.app.supportsSearchFilters { if accounts.app.supportsSearchFilters {
@ -97,10 +96,8 @@ struct SearchView: View {
#if os(iOS) #if os(iOS)
Spacer() Spacer()
if let favoriteItem = favoriteItem { FavoriteButton(item: favoriteItem)
FavoriteButton(item: favoriteItem) .id(favoriteItem?.id)
.id(favoriteItem.id)
}
Spacer() Spacer()
#endif #endif
@ -188,6 +185,7 @@ struct SearchView: View {
#endif #endif
#if os(iOS) #if os(iOS)
.navigationBarHidden(!Defaults[.visibleSections].isEmpty || navigationStyle == .sidebar) .navigationBarHidden(!Defaults[.visibleSections].isEmpty || navigationStyle == .sidebar)
.navigationBarTitleDisplayMode(.inline)
#endif #endif
} }
@ -203,12 +201,10 @@ struct SearchView: View {
filtersHorizontalStack filtersHorizontalStack
} }
if let favoriteItem = favoriteItem { FavoriteButton(item: favoriteItem)
FavoriteButton(item: favoriteItem) .id(favoriteItem?.id)
.id(favoriteItem.id) .labelStyle(.iconOnly)
.labelStyle(.iconOnly) .font(.system(size: 25))
.font(.system(size: 25))
}
} }
HorizontalCells(items: items) HorizontalCells(items: items)
@ -233,9 +229,9 @@ struct SearchView: View {
private var toolbarPlacement: ToolbarItemPlacement { private var toolbarPlacement: ToolbarItemPlacement {
#if os(iOS) #if os(iOS)
.bottomBar accounts.app.supportsSearchFilters || favorites.isEnabled ? .bottomBar : .automatic
#else #else
.automatic .automatic
#endif #endif
} }

View File

@ -3,17 +3,19 @@ import Foundation
import SwiftUI import SwiftUI
struct FavoriteButton: View { struct FavoriteButton: View {
let item: FavoriteItem let item: FavoriteItem!
let favorites = FavoritesModel.shared let favorites = FavoritesModel.shared
@State private var isFavorite = false @State private var isFavorite = false
@Default(.visibleSections) private var visibleSections
var body: some View { var body: some View {
Group { Group {
if visibleSections.contains(.favorites) { if favorites.isEnabled {
Button { Button {
guard !item.isNil else {
return
}
favorites.toggle(item) favorites.toggle(item)
isFavorite.toggle() isFavorite.toggle()
} label: { } label: {
@ -23,8 +25,9 @@ struct FavoriteButton: View {
Label("Add to Favorites", systemImage: "heart") Label("Add to Favorites", systemImage: "heart")
} }
} }
.disabled(item.isNil)
.onAppear { .onAppear {
isFavorite = favorites.contains(item) isFavorite = item.isNil ? false : favorites.contains(item)
} }
} else { } else {
EmptyView() EmptyView()