Skip to content

Commit 1d8e292

Browse files
committed
Conditionally hide favorites category.
1 parent 9024cf0 commit 1d8e292

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Shared/Supporting Files/Views/CategoriesView.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ struct CategoriesView: View {
2424
/// A Boolean value indicating whether the navigation destination is showing.
2525
@State private var destinationIsPresented = false
2626

27-
/// The list of categories.
28-
private let categories: [String] = {
29-
var categories = ["All", "Favorites"]
30-
let sampleCategories = Set(SamplesApp.samples.map(\.category))
31-
categories.append(contentsOf: sampleCategories.sorted())
32-
return categories
33-
}()
27+
/// The names of the favorite samples loaded from user defaults.
28+
@AppStorage(.favoriteSampleNames) private var favoriteNamesString = ""
29+
30+
/// The list of categories generated from the samples.
31+
private let sampleCategories = Set(SamplesApp.samples.map(\.category)).sorted()
32+
33+
/// The full list of categories to show.
34+
private var categories: [String] {
35+
let shouldShowFavorites = Array(jsonString: favoriteNamesString).isEmpty
36+
return (shouldShowFavorites ? ["All"] : ["All", "Favorites"]) + sampleCategories
37+
}
3438

3539
var body: some View {
3640
ScrollView {
@@ -54,6 +58,7 @@ struct CategoriesView: View {
5458
)
5559
)
5660
}
61+
.animation(.default, value: categories)
5762
}
5863
.padding()
5964
}

0 commit comments

Comments
 (0)