Skip to content

Commit 8a8a4c0

Browse files
committed
feat(history): make toolbar morph on select.
1 parent fa27e2e commit 8a8a4c0

9 files changed

Lines changed: 252 additions & 70 deletions

File tree

‎Snapper/App/AppConfiguration.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import SwiftData
33
import SwiftUI
44

55
enum Application {
6+
static var isSimulator: Bool {
7+
#if targetEnvironment(simulator)
8+
true
9+
#else
10+
false
11+
#endif
12+
}
13+
614
static var isTest: Bool {
715
#if DEBUG
816
ProcessInfo.processInfo.arguments.contains(AppLaunchConfiguration.uiTest)

‎Snapper/App/AppDependencies.Test.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ struct TestDependencies: AppDependencies {
1515
let historyViewModel: AlbumHistoryViewModel
1616
let locationCoordinator: AlbumLocationCaptureCoordinator
1717

18-
init() {
18+
init(limit: Int = 10) {
1919
let searchContainer: ModelContainer
2020
let historyContainer: ModelContainer
2121

2222
do {
23-
searchContainer = try SampleDataStore.makeContainer(named: "search")
24-
historyContainer = try SampleDataStore.makeContainer(named: "albums")
23+
searchContainer = try SampleDataStore.makeContainer(named: "search", limit: limit)
24+
historyContainer = try SampleDataStore.makeContainer(named: "albums", limit: limit)
2525
} catch {
2626
fatalError("Unable to create schema.")
2727
}

‎Snapper/App/AppNavigation.swift‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SwiftUI
2+
3+
@MainActor
4+
@Observable
5+
final class AppNavigation {
6+
var screen: AppScreen = .search
7+
}
8+
9+
extension EnvironmentValues {
10+
@Entry var navigation: AppNavigation? = nil
11+
}

‎Snapper/Features/AlbumHistory/AlbumHistoryView.swift‎

Lines changed: 81 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import SwiftUI
44
struct AlbumHistoryView: View {
55
@Environment(AlbumHistoryViewModel.self) private var vm
66

7+
@Environment(\.navigation) private var navigation
8+
79
@Namespace private var namespace
810

911
@State private var path = NavigationPath()
@@ -57,7 +59,11 @@ struct AlbumHistoryView: View {
5759
ThemeContentUnavailableView.empty(
5860
.History.stateEmptyTitle,
5961
description: .History.stateEmptyDesc,
60-
)
62+
) {
63+
if let navigation {
64+
EmptyStateButton(navigation: navigation)
65+
}
66+
}
6167
} else {
6268
ThemeContentUnavailableView.search(text: vm.searchText)
6369
}
@@ -81,7 +87,7 @@ struct AlbumHistoryView: View {
8187
)
8288
)
8389
.toolbar {
84-
AlbumHistoryToolbar(vm: vm) {
90+
albumHistoryToolbar {
8591
let count = vm.selectedIds.count
8692
alert = AlertDestination(
8793
title: .alertsHistoryDeleteTitle(count: count),
@@ -108,41 +114,39 @@ struct AlbumHistoryView: View {
108114
}
109115
}
110116
}
111-
}
112117

113-
private struct AlbumSearchSuggestions: View {
114-
@Query(sort: \Album.title) private var albums: [Album]
115-
116-
let maxResults: Int
117-
118-
init(maxResults: Int = 3) {
119-
self.maxResults = maxResults
120-
}
121-
122-
var titles: [String] {
123-
Array(albums.map(\.title).prefix(min(albums.count, maxResults)))
124-
}
125-
126-
var body: some View {
127-
ForEach(titles, id: \.self) { title in
128-
Text(title)
129-
.searchCompletion(title)
130-
}
131-
}
132-
}
133-
134-
private struct AlbumHistoryToolbar: ToolbarContent {
135-
@Bindable var vm: AlbumHistoryViewModel
136-
137-
let deleteAction: () -> Void
138-
139-
var body: some ToolbarContent {
118+
@ToolbarContentBuilder
119+
private func albumHistoryToolbar(
120+
deleteAction: @escaping () -> Void
121+
) -> some ToolbarContent {
140122
// ToolbarTitle(.History.title)
141123

142124
if vm.isSelecting {
143-
ToolbarItemGroup {
125+
if vm.allSelected {
126+
ToolbarItem(placement: .topBarLeading) {
127+
Button(.toolsSelectAllUndo) {
128+
withAnimation {
129+
vm.deselectAll(vm.filteredIds)
130+
}
131+
}
132+
// TODO: logic for disabling button.
133+
}
134+
} else {
135+
ToolbarItem(placement: .topBarLeading) {
136+
Button(.toolsSelectAll) {
137+
withAnimation {
138+
vm.selectAll(vm.filteredIds)
139+
}
140+
}
141+
// TODO: logic for disabling button.
142+
}
143+
}
144+
145+
ToolbarItemGroup(placement: .topBarTrailing) {
144146
Button(role: .cancel) {
145-
vm.clearSelection()
147+
withAnimation {
148+
vm.clearSelection()
149+
}
146150
}
147151
.accessibilityIdentifier("history-cancel-selection")
148152

@@ -153,38 +157,76 @@ private struct AlbumHistoryToolbar: ToolbarContent {
153157
.accessibilityIdentifier("history-delete-selection")
154158
}
155159
} else {
156-
ToolbarItem {
160+
ToolbarItem(placement: .topBarTrailing) {
157161
Button(.toolsSelect) {
158-
vm.isSelecting = true
162+
withAnimation {
163+
vm.isSelecting = true
164+
}
159165
}
160166
.disabled(vm.filteredCount == 0)
161167
}
162168

163-
ToolbarSpacer(.fixed)
169+
ToolbarSpacer(.fixed, placement: .topBarTrailing)
170+
171+
ToolbarItem(placement: .topBarTrailing) {
172+
@Bindable var vm = vm
164173

165-
ToolbarItem {
166174
Menu {
167-
Picker("Criteria", selection: $vm.sort.criterion) {
175+
Picker(.toolsSortCriteria, selection: $vm.sort.criterion) {
168176
ForEach(SortCriterion.allCases) { criterion in
169177
Text(criterion.resource)
170178
.tag(criterion)
171179
}
172180
}
173181

174-
Picker("Order", selection: vm.currentOrder) {
182+
Picker(.toolsSortDirection, selection: vm.currentOrder) {
175183
ForEach(vm.sort.criterion.orders) { order in
176184
Text(order.resource)
177185
.tag(order)
178186
}
179187
}
180188
} label: {
181-
Image(systemName: "arrow.up.arrow.down")
189+
Label(.toolsSort, systemImage: "arrow.up.arrow.down")
182190
}
183191
}
184192
}
185193
}
186194
}
187195

196+
private struct EmptyStateButton: View {
197+
var navigation: AppNavigation
198+
199+
var body: some View {
200+
Button(.History.stateEmptyButtonLabel, systemImage: "magnifyingglass") {
201+
navigation.screen = .search
202+
}
203+
.labelStyle(.badge(appearance: .highlighted))
204+
.controlSize(.large)
205+
.imageScale(.small)
206+
}
207+
}
208+
209+
private struct AlbumSearchSuggestions: View {
210+
@Query(sort: \Album.title) private var albums: [Album]
211+
212+
let maxResults: Int
213+
214+
init(maxResults: Int = 3) {
215+
self.maxResults = maxResults
216+
}
217+
218+
var titles: [String] {
219+
Array(albums.map(\.title).prefix(min(albums.count, maxResults)))
220+
}
221+
222+
var body: some View {
223+
ForEach(titles, id: \.self) { title in
224+
Text(title)
225+
.searchCompletion(title)
226+
}
227+
}
228+
}
229+
188230
private struct AlbumHistoryFilterBar: View {
189231
@Environment(\.locale) private var locale
190232

‎Snapper/Features/AlbumHistory/AlbumHistoryViewModel.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ final class AlbumHistoryViewModel {
168168
)
169169
}
170170

171+
var filteredIds: Set<Album.ID> {
172+
let albums = (try? context.fetch(descriptor)) ?? []
173+
174+
return Set(albums.map(\.id))
175+
}
176+
177+
var allSelected: Bool {
178+
if selectedIds.isEmpty { return false }
179+
180+
let ids = filteredIds
181+
182+
return ids.isEmpty == false && ids.isSubset(of: selectedIds)
183+
}
184+
185+
func selectAll(_ ids: Set<Album.ID>) {
186+
selectedIds.formUnion(ids)
187+
}
188+
189+
func deselectAll(_ ids: Set<Album.ID>) {
190+
selectedIds.subtract(ids)
191+
}
192+
171193
func clearSelection() {
172194
selectedIds.removeAll()
173195
isSelecting = false

0 commit comments

Comments
 (0)