From bd59b8e2c3ebe79ba2db39b8f9a6afe9fc4e2d29 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Thu, 2 Dec 2021 21:35:25 +0100 Subject: [PATCH] Improve favorite button --- Model/FavoritesModel.swift | 5 +++++ Shared/Search/SearchView.swift | 28 ++++++++++++---------------- Shared/Views/FavoriteButton.swift | 13 ++++++++----- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Model/FavoritesModel.swift b/Model/FavoritesModel.swift index 7a78f2f5..b95dc874 100644 --- a/Model/FavoritesModel.swift +++ b/Model/FavoritesModel.swift @@ -5,6 +5,11 @@ struct FavoritesModel { static let shared = FavoritesModel() @Default(.favorites) var all + @Default(.visibleSections) var visibleSections + + var isEnabled: Bool { + visibleSections.contains(.favorites) + } func contains(_ item: FavoriteItem) -> Bool { all.contains { $0 == item } diff --git a/Shared/Search/SearchView.swift b/Shared/Search/SearchView.swift index 22a40811..4312a5c5 100644 --- a/Shared/Search/SearchView.swift +++ b/Shared/Search/SearchView.swift @@ -23,6 +23,7 @@ struct SearchView: View { @EnvironmentObject private var accounts @EnvironmentObject private var recents @EnvironmentObject private var state + private var favorites = FavoritesModel.shared @Default(.saveRecents) private var saveRecents @@ -70,10 +71,8 @@ struct SearchView: View { #if !os(tvOS) ToolbarItemGroup(placement: toolbarPlacement) { #if os(macOS) - if let favoriteItem = favoriteItem { - FavoriteButton(item: favoriteItem) - .id(favoriteItem.id) - } + FavoriteButton(item: favoriteItem) + .id(favoriteItem?.id) #endif if accounts.app.supportsSearchFilters { @@ -97,10 +96,8 @@ struct SearchView: View { #if os(iOS) Spacer() - if let favoriteItem = favoriteItem { - FavoriteButton(item: favoriteItem) - .id(favoriteItem.id) - } + FavoriteButton(item: favoriteItem) + .id(favoriteItem?.id) Spacer() #endif @@ -188,6 +185,7 @@ struct SearchView: View { #endif #if os(iOS) .navigationBarHidden(!Defaults[.visibleSections].isEmpty || navigationStyle == .sidebar) + .navigationBarTitleDisplayMode(.inline) #endif } @@ -203,12 +201,10 @@ struct SearchView: View { filtersHorizontalStack } - if let favoriteItem = favoriteItem { - FavoriteButton(item: favoriteItem) - .id(favoriteItem.id) - .labelStyle(.iconOnly) - .font(.system(size: 25)) - } + FavoriteButton(item: favoriteItem) + .id(favoriteItem?.id) + .labelStyle(.iconOnly) + .font(.system(size: 25)) } HorizontalCells(items: items) @@ -233,9 +229,9 @@ struct SearchView: View { private var toolbarPlacement: ToolbarItemPlacement { #if os(iOS) - .bottomBar + accounts.app.supportsSearchFilters || favorites.isEnabled ? .bottomBar : .automatic #else - .automatic + .automatic #endif } diff --git a/Shared/Views/FavoriteButton.swift b/Shared/Views/FavoriteButton.swift index bd1c10ae..8ce088be 100644 --- a/Shared/Views/FavoriteButton.swift +++ b/Shared/Views/FavoriteButton.swift @@ -3,17 +3,19 @@ import Foundation import SwiftUI struct FavoriteButton: View { - let item: FavoriteItem + let item: FavoriteItem! let favorites = FavoritesModel.shared @State private var isFavorite = false - @Default(.visibleSections) private var visibleSections - var body: some View { Group { - if visibleSections.contains(.favorites) { + if favorites.isEnabled { Button { + guard !item.isNil else { + return + } + favorites.toggle(item) isFavorite.toggle() } label: { @@ -23,8 +25,9 @@ struct FavoriteButton: View { Label("Add to Favorites", systemImage: "heart") } } + .disabled(item.isNil) .onAppear { - isFavorite = favorites.contains(item) + isFavorite = item.isNil ? false : favorites.contains(item) } } else { EmptyView()