Skip to content

Commit

Permalink
Fix 06-TreeBasedNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
kalupas226 committed Feb 25, 2024
1 parent 5bc4404 commit 65ede25
Show file tree
Hide file tree
Showing 22 changed files with 607 additions and 786 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import GitHubAPIClient
import IdentifiedCollections
import SwiftUI

public struct RepositoryList: Reducer {
@Reducer
public struct RepositoryList {
@ObservableState
public struct State: Equatable {
var repositoryRows: IdentifiedArrayOf<RepositoryRow.State> = []
var isLoading: Bool = false
@BindingState var query: String = ""
var query: String = ""

public init() {}
}

public enum Action: Equatable, BindableAction {
public enum Action: BindableAction {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
case queryChangeDebounced
case binding(BindingAction<State>)
}
Expand Down Expand Up @@ -55,9 +57,9 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
case .binding(\.$query):
case .binding(\.query):
return .run { send in
await send(.queryChangeDebounced)
}
Expand All @@ -78,7 +80,7 @@ public struct RepositoryList: Reducer {
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand All @@ -87,7 +89,7 @@ public struct RepositoryList: Reducer {
.run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
try await gitHubAPIClient.searchRepositories(query)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import IdentifiedCollections
import SwiftUI
import SwiftUINavigationCore

public struct RepositoryList: Reducer {
@Reducer
public struct RepositoryList {
@ObservableState
public struct State: Equatable {
var repositoryRows: IdentifiedArrayOf<RepositoryRow.State> = []
var isLoading: Bool = false
@BindingState var query: String = ""
@PresentationState var alert: AlertState<Action.Alert>?
var query: String = ""
@Presents var alert: AlertState<Action.Alert>?

public init() {}
}

public enum Action: Equatable, BindableAction {
public enum Action: BindableAction {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
case queryChangeDebounced
case binding(BindingAction<State>)
case alert(PresentationAction<Alert>)
Expand Down Expand Up @@ -60,9 +62,9 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
case .binding(\.$query):
case .binding(\.query):
return .run { send in
await send(.queryChangeDebounced)
}
Expand All @@ -83,7 +85,7 @@ public struct RepositoryList: Reducer {
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand All @@ -92,7 +94,7 @@ public struct RepositoryList: Reducer {
.run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
try await gitHubAPIClient.searchRepositories(query)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import IdentifiedCollections
import SwiftUI
import SwiftUINavigationCore

public struct RepositoryList: Reducer {
@Reducer
public struct RepositoryList {
@ObservableState
public struct State: Equatable {
var repositoryRows: IdentifiedArrayOf<RepositoryRow.State> = []
var isLoading: Bool = false
@BindingState var query: String = ""
@PresentationState var alert: AlertState<Action.Alert>?
var query: String = ""
@Presents var alert: AlertState<Action.Alert>?

public init() {}
}

public enum Action: Equatable, BindableAction {
public enum Action: BindableAction {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
case queryChangeDebounced
case binding(BindingAction<State>)
case alert(PresentationAction<Alert>)
Expand Down Expand Up @@ -64,9 +66,9 @@ public struct RepositoryList: Reducer {
}
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
case .binding(\.$query):
case .binding(\.query):
return .run { send in
await send(.queryChangeDebounced)
}
Expand All @@ -89,7 +91,7 @@ public struct RepositoryList: Reducer {
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand All @@ -98,7 +100,7 @@ public struct RepositoryList: Reducer {
.run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
try await gitHubAPIClient.searchRepositories(query)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import IdentifiedCollections
import SwiftUI
import SwiftUINavigationCore

public struct RepositoryList: Reducer {
@Reducer
public struct RepositoryList {
@ObservableState
public struct State: Equatable {
var repositoryRows: IdentifiedArrayOf<RepositoryRow.State> = []
var isLoading: Bool = false
@BindingState var query: String = ""
@PresentationState var alert: AlertState<Action.Alert>?
var query: String = ""
@Presents var alert: AlertState<Action.Alert>?

public init() {}
}

public enum Action: Equatable, BindableAction {
public enum Action: BindableAction {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
case queryChangeDebounced
case binding(BindingAction<State>)
case alert(PresentationAction<Alert>)
Expand Down Expand Up @@ -60,9 +62,9 @@ public struct RepositoryList: Reducer {
state.alert = .networkError
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
case .binding(\.$query):
case .binding(\.query):
return .run { send in
await send(.queryChangeDebounced)
}
Expand All @@ -85,7 +87,7 @@ public struct RepositoryList: Reducer {
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand All @@ -94,7 +96,7 @@ public struct RepositoryList: Reducer {
.run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
try await gitHubAPIClient.searchRepositories(query)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,38 @@ import SwiftUI
import SwiftUINavigationCore

public struct RepositoryListView: View {
let store: StoreOf<RepositoryList>
@Bindable var store: StoreOf<RepositoryList>

public init(store: StoreOf<RepositoryList>) {
self.store = store
}

public var body: some View {
NavigationStack {
WithViewStore(store, observe: { $0 }) { viewStore in
Group {
if viewStore.isLoading {
ProgressView()
} else {
List {
ForEachStore(
store.scope(
state: \.repositoryRows,
action: { .repositoryRow(id: $0, action: $1) }
),
content: RepositoryRowView.init(store:)
)
}
Group {
if store.isLoading {
ProgressView()
} else {
List {
ForEach(
store.scope(
state: \.repositoryRows,
action: \.repositoryRows
),
content: RepositoryRowView.init(store:)
)
}
}
.onAppear {
viewStore.send(.onAppear)
}
.navigationTitle("Repositories")
.searchable(
text: viewStore.$query,
placement: .navigationBarDrawer,
prompt: "Input query"
)
}
.onAppear {
store.send(.onAppear)
}
.navigationTitle("Repositories")
.searchable(
text: $store.query,
placement: .navigationBarDrawer,
prompt: "Input query"
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,44 @@ import SwiftUI
import SwiftUINavigationCore

public struct RepositoryListView: View {
let store: StoreOf<RepositoryList>
@Bindable var store: StoreOf<RepositoryList>

public init(store: StoreOf<RepositoryList>) {
self.store = store
}

public var body: some View {
NavigationStack {
WithViewStore(store, observe: { $0 }) { viewStore in
Group {
if viewStore.isLoading {
ProgressView()
} else {
List {
ForEachStore(
store.scope(
state: \.repositoryRows,
action: { .repositoryRow(id: $0, action: $1) }
),
content: RepositoryRowView.init(store:)
)
}
Group {
if store.isLoading {
ProgressView()
} else {
List {
ForEach(
store.scope(
state: \.repositoryRows,
action: \.repositoryRows
),
content: RepositoryRowView.init(store:)
)
}
}
.onAppear {
viewStore.send(.onAppear)
}
.navigationTitle("Repositories")
.searchable(
text: viewStore.$query,
placement: .navigationBarDrawer,
prompt: "Input query"
)
.alert(
store: store.scope(
state: \.$alert,
action: { .alert($0) }
)
)
}
.onAppear {
store.send(.onAppear)
}
.navigationTitle("Repositories")
.searchable(
text: $store.query,
placement: .navigationBarDrawer,
prompt: "Input query"
)
.alert(
$store.scope(
state: \.alert,
action: \.alert
)
)
}
}
}
Expand Down

0 comments on commit 65ede25

Please sign in to comment.