Skip to content

Commit

Permalink
Edit 04-Navigation-Lists-NavigateAndLoad file in SwiftUI CaseStudies (
Browse files Browse the repository at this point in the history
#3327)

* Edit 04-Navigation-Lists-NavigateAndLoad file in SwiftUI CaseStudies

* Update Examples/CaseStudies/SwiftUICaseStudies/04-Navigation-Lists-NavigateAndLoad.swift

Co-authored-by: Stephen Celis <stephen.celis@gmail.com>

* change selection Type to `Identified<Row.ID, Counter.State?>?`

---------

Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
Co-authored-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
3 people committed Sep 5, 2024
1 parent d5c2d76 commit 80aac23
Showing 1 changed file with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ private let readMe = """

@Reducer
struct NavigateAndLoadList {
@ObservableState
struct State: Equatable {
var rows: IdentifiedArrayOf<Row> = [
Row(count: 1, id: UUID()),
Expand Down Expand Up @@ -56,7 +57,8 @@ struct NavigateAndLoadList {

case .setNavigationSelectionDelayCompleted:
guard let id = state.selection?.id else { return .none }
state.selection?.value = Counter.State(count: state.rows[id: id]?.count ?? 0)
let count = state.rows[id: id]?.count ?? 0
state.selection?.value = Counter.State(count: count)
return .none
}
}
Expand All @@ -73,25 +75,23 @@ struct NavigateAndLoadListView: View {
@Bindable var store: StoreOf<NavigateAndLoadList>

var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
Form {
Section {
AboutView(readMe: readMe)
}
ForEach(viewStore.rows) { row in
NavigationLink(
"Load optional counter that starts from \(row.count)",
tag: row.id,
selection: viewStore.binding(
get: \.selection?.id,
send: { .setNavigation(selection: $0) }
)
) {
IfLetStore(self.store.scope(state: \.selection?.value, action: \.counter)) {
CounterView(store: $0)
} else: {
ProgressView()
}
Form {
Section {
AboutView(readMe: readMe)
}
ForEach(store.rows) { row in
NavigationLink(
"Load optional counter that starts from \(row.count)",
tag: row.id,
selection: .init(
get: { store.selection?.id },
set: { id in store.send(.setNavigation(selection: id)) }
)
) {
if let store = store.scope(state: \.selection?.value, action: \.counter) {
CounterView(store: store)
}else {
ProgressView()
}
}
}
Expand Down

0 comments on commit 80aac23

Please sign in to comment.