Skip to content

Commit

Permalink
Fix 03-BindingsAndCancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
kalupas226 committed Feb 21, 2024
1 parent c4b9a7d commit 4206498
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import Foundation
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

public init() {}
}

public enum Action: Equatable {
public enum Action {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
}

public init() {}
Expand All @@ -29,7 +31,7 @@ public struct RepositoryList: Reducer {
return .run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
let query = "composable"
let url = URL(
string: "https://api.github.com/search/repositories?q=\(query)&sort=stars"
Expand Down Expand Up @@ -66,11 +68,11 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import Foundation
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 {
public enum Action {
case onAppear
case searchRepositoriesResponse(TaskResult<[Repository]>)
case repositoryRow(id: RepositoryRow.State.ID, action: RepositoryRow.Action)
case searchRepositoriesResponse(Result<[Repository]>)
case repositoryRows(IdentifiedActionOf<RepositoryRow>)
}

public init() {}
Expand All @@ -30,7 +32,7 @@ public struct RepositoryList: Reducer {
return .run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
let query = "composable"
let url = URL(
string: "https://api.github.com/search/repositories?q=\(query)&sort=stars"
Expand Down Expand Up @@ -67,11 +69,11 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import Foundation
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 repositoryRow(IdentifiedActionOf<RepositoryRow>)
case binding(BindingAction<State>)
}

Expand All @@ -31,7 +33,7 @@ public struct RepositoryList: Reducer {
return .run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
let query = "composable"
let url = URL(
string: "https://api.github.com/search/repositories?q=\(query)&sort=stars"
Expand Down Expand Up @@ -68,11 +70,11 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import Foundation
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 repositoryRow(IdentifiedActionOf<RepositoryRow>)
case binding(BindingAction<State>)
}

Expand All @@ -32,7 +34,7 @@ public struct RepositoryList: Reducer {
return .run { send in
await send(
.searchRepositoriesResponse(
TaskResult {
Result {
let query = "composable"
let url = URL(
string: "https://api.github.com/search/repositories?q=\(query)&sort=stars"
Expand Down Expand Up @@ -69,13 +71,13 @@ public struct RepositoryList: Reducer {
// TODO: Handling error
return .none
}
case .repositoryRow:
case .repositoryRows:
return .none
case .binding:
return .none
}
}
.forEach(\.repositoryRows, action: /Action.repositoryRow(id:action:)) {
.forEach(\.repositoryRows, action: \.repositoryRows) {
RepositoryRow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,23 @@ public struct RepositoryListView: View {
}

public var body: some View {
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)
}
}
.onAppear {
store.send(.onAppear)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,37 @@ import IdentifiedCollections
import SwiftUI

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 {
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 @@ -6,40 +6,38 @@ import IdentifiedCollections
import SwiftUI

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
Loading

0 comments on commit 4206498

Please sign in to comment.