Skip to content

Commit aca664d

Browse files
committed
Updated to v1.2.0 of SwiftDux
1 parent a21cfe9 commit aca664d

9 files changed

+106
-168
lines changed

Todo Lists.xcodeproj/project.pbxproj

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
/* Begin PBXBuildFile section */
1010
A9307ECA23D91EF0005F5C96 /* SwiftDux in Frameworks */ = {isa = PBXBuildFile; productRef = A9307EC923D91EF0005F5C96 /* SwiftDux */; };
11+
A94FAF0F24608EF9005F9F4B /* MainSceneContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A94FAF0E24608EF9005F9F4B /* MainSceneContainer.swift */; };
1112
A96EA6AC23F44F7C0024DBAA /* TodoListsRoot.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96EA6AB23F44F7C0024DBAA /* TodoListsRoot.swift */; };
1213
A96EA6AE23F44FB50024DBAA /* TodosRoot.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96EA6AD23F44FB50024DBAA /* TodosRoot.swift */; };
1314
A96EA6B023F452250024DBAA /* configureStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96EA6AF23F452250024DBAA /* configureStore.swift */; };
1415
A976F54F23D3D88E00004B33 /* TodosAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A976F54E23D3D88E00004B33 /* TodosAction.swift */; };
15-
A991C7DF23D6519F00AD7AA3 /* TodoListNameContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A991C7DE23D6519F00AD7AA3 /* TodoListNameContainer.swift */; };
16-
A991C7E123D6568400AD7AA3 /* NewTodoContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A991C7E023D6568400AD7AA3 /* NewTodoContainer.swift */; };
1716
AA2F1A81231782D3003B088A /* CheckedToggleStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA2F1A80231782D3003B088A /* CheckedToggleStyle.swift */; };
1817
AA427ACA22C02E75005A4368 /* TodosReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA427AC922C02E75005A4368 /* TodosReducer.swift */; };
19-
AA427ACC22C143AE005A4368 /* AppContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA427ACB22C143AE005A4368 /* AppContainer.swift */; };
2018
AA427B0922C3D097005A4368 /* TodoListsReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA427B0822C3D097005A4368 /* TodoListsReducer.swift */; };
2119
AA696F8D2321ED7300BFCC0B /* NewTodoRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA696F8C2321ED7300BFCC0B /* NewTodoRow.swift */; };
2220
AA94FB9322F4D188008D0272 /* TodoListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA94FB9222F4D188008D0272 /* TodoListRow.swift */; };
@@ -53,15 +51,13 @@
5351
/* Begin PBXFileReference section */
5452
A904010A23D8BA63004D65F1 /* SwiftDux.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SwiftDux.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5553
A904010C23D8BA63004D65F1 /* SwiftDuxExtras.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SwiftDuxExtras.framework; sourceTree = BUILT_PRODUCTS_DIR; };
54+
A94FAF0E24608EF9005F9F4B /* MainSceneContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainSceneContainer.swift; sourceTree = "<group>"; };
5655
A96EA6AB23F44F7C0024DBAA /* TodoListsRoot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListsRoot.swift; sourceTree = "<group>"; };
5756
A96EA6AD23F44FB50024DBAA /* TodosRoot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodosRoot.swift; sourceTree = "<group>"; };
5857
A96EA6AF23F452250024DBAA /* configureStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = configureStore.swift; sourceTree = "<group>"; };
5958
A976F54E23D3D88E00004B33 /* TodosAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodosAction.swift; sourceTree = "<group>"; };
60-
A991C7DE23D6519F00AD7AA3 /* TodoListNameContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodoListNameContainer.swift; sourceTree = "<group>"; };
61-
A991C7E023D6568400AD7AA3 /* NewTodoContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewTodoContainer.swift; sourceTree = "<group>"; };
6259
AA2F1A80231782D3003B088A /* CheckedToggleStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckedToggleStyle.swift; sourceTree = "<group>"; };
6360
AA427AC922C02E75005A4368 /* TodosReducer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodosReducer.swift; sourceTree = "<group>"; };
64-
AA427ACB22C143AE005A4368 /* AppContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppContainer.swift; sourceTree = "<group>"; };
6561
AA427B0822C3D097005A4368 /* TodoListsReducer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodoListsReducer.swift; sourceTree = "<group>"; };
6662
AA696F8C2321ED7300BFCC0B /* NewTodoRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewTodoRow.swift; sourceTree = "<group>"; };
6763
AA94FB9222F4D188008D0272 /* TodoListRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodoListRow.swift; sourceTree = "<group>"; };
@@ -213,11 +209,9 @@
213209
AA9DA97622ADA74C00617E46 /* Containers */ = {
214210
isa = PBXGroup;
215211
children = (
216-
AA427ACB22C143AE005A4368 /* AppContainer.swift */,
212+
A94FAF0E24608EF9005F9F4B /* MainSceneContainer.swift */,
217213
AA9DA96C22AD8FC600617E46 /* TodoListBrowserContainer.swift */,
218214
AA9DA94822AD8A2100617E46 /* TodoListContainer.swift */,
219-
A991C7E023D6568400AD7AA3 /* NewTodoContainer.swift */,
220-
A991C7DE23D6519F00AD7AA3 /* TodoListNameContainer.swift */,
221215
AA9DA96822AD8E4C00617E46 /* TodoContainer.swift */,
222216
);
223217
path = Containers;
@@ -304,16 +298,15 @@
304298
files = (
305299
AA9DA96D22AD8FC600617E46 /* TodoListBrowserContainer.swift in Sources */,
306300
AA94FB9322F4D188008D0272 /* TodoListRow.swift in Sources */,
307-
A991C7DF23D6519F00AD7AA3 /* TodoListNameContainer.swift in Sources */,
308301
AA9DA94522AD8A2100617E46 /* AppDelegate.swift in Sources */,
302+
A94FAF0F24608EF9005F9F4B /* MainSceneContainer.swift in Sources */,
309303
AA696F8D2321ED7300BFCC0B /* NewTodoRow.swift in Sources */,
310304
AA9DA96522AD8BA100617E46 /* Todo.swift in Sources */,
311305
AA427B0922C3D097005A4368 /* TodoListsReducer.swift in Sources */,
312306
A976F54F23D3D88E00004B33 /* TodosAction.swift in Sources */,
313307
AABF238C22D2B45800F332DC /* TodoListsAction.swift in Sources */,
314308
AA9DA96922AD8E4C00617E46 /* TodoContainer.swift in Sources */,
315309
AA2F1A81231782D3003B088A /* CheckedToggleStyle.swift in Sources */,
316-
A991C7E123D6568400AD7AA3 /* NewTodoContainer.swift in Sources */,
317310
AA9DA94722AD8A2100617E46 /* SceneDelegate.swift in Sources */,
318311
AA94FB9822F4D869008D0272 /* TodoListNameField.swift in Sources */,
319312
A96EA6B023F452250024DBAA /* configureStore.swift in Sources */,
@@ -325,7 +318,6 @@
325318
AA94FB9C22F4D969008D0272 /* AddButton.swift in Sources */,
326319
AA9DA94922AD8A2100617E46 /* TodoListContainer.swift in Sources */,
327320
AA9DA96322AD8B5900617E46 /* TodoList.swift in Sources */,
328-
AA427ACC22C143AE005A4368 /* AppContainer.swift in Sources */,
329321
);
330322
runOnlyForDeploymentPostprocessing = 0;
331323
};
@@ -525,8 +517,8 @@
525517
isa = XCRemoteSwiftPackageReference;
526518
repositoryURL = "https://github.com/StevenLambion/SwiftDux.git";
527519
requirement = {
528-
branch = "swift-5.2";
529-
kind = branch;
520+
kind = upToNextMinorVersion;
521+
minimumVersion = 1.2.0;
530522
};
531523
};
532524
/* End XCRemoteSwiftPackageReference section */

Todo/Containers/AppContainer.swift renamed to Todo/Containers/MainSceneContainer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftUI
22
import SwiftDux
33

4-
struct AppContainer : View {
4+
struct MainSceneContainer : View {
55
var store: Store<AppState>
66

77
var body: some View {
@@ -13,13 +13,13 @@ struct AppContainer : View {
1313
}
1414

1515
#if DEBUG
16-
public enum AppContainer_Previews: PreviewProvider {
16+
public enum MainSceneContainer_Previews: PreviewProvider {
1717
static var store: Store<AppState> {
1818
configureStore()
1919
}
2020

2121
public static var previews: some View {
22-
AppContainer(store: store)
22+
MainSceneContainer(store: store)
2323
}
2424
}
2525
#endif

Todo/Containers/NewTodoContainer.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

Todo/Containers/TodoListBrowserContainer.swift

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,57 @@ import Combine
44

55
struct TodoListBrowserContainer : ConnectableView {
66
@Environment(\.horizontalSizeClass) private var sizeClass
7-
@MappedDispatch() private var dispatch
87

9-
struct Props: Equatable {
10-
var todoLists: OrderedState<TodoList>
11-
var selectedTodoListId: String?
12-
}
13-
14-
func map(state: AppState) -> Props? {
8+
func map(state: AppState, binder: ActionBinder) -> Props? {
159
Props(
1610
todoLists: state.todoLists,
17-
selectedTodoListId: state.selectedTodoListId
11+
selectedTodoListId: binder.bind(state.selectedTodoListId) {
12+
TodoListsAction.selectTodoList(id: $0)
13+
},
14+
addNewTodoList: binder.bind(TodoListsAction.addNewTodoList),
15+
moveTodoLists: binder.bind { TodoListsAction.moveTodoLists(from: $0, to: $1) },
16+
removeTodoLists: binder.bind { TodoListsAction.removeTodoLists(at: $0) },
17+
selectDefaultTodoList: binder.bind {
18+
guard state.selectedTodoListId == nil && self.sizeClass == .regular else { return nil }
19+
return TodoListsAction.selectTodoList(id: state.todoLists.first?.id)
20+
}
1821
)
1922
}
2023

2124
func body(props: Props) -> some View {
2225
List {
2326
ForEach(props.todoLists) { todoList in
24-
self.renderRow(props: props, todoList: todoList)
27+
self.row(todoList: todoList, selectedTodoListId: props.$selectedTodoListId)
2528
}
26-
.onMove(perform: moveTodoList)
27-
.onDelete(perform: removeTodoLists)
29+
.onMove(perform: props.moveTodoLists)
30+
.onDelete(perform: props.removeTodoLists)
2831
}
2932
.navigationBarTitle(Text("Todo Lists"))
3033
.navigationBarItems(
3134
leading: EditButton(),
32-
trailing: AddButton { self.dispatch(TodoListsAction.addNewTodoList()) }
35+
trailing: AddButton(onAdd: props.addNewTodoList)
3336
)
34-
.onAppear { self.selectDefaultTodoList(props: props) }
37+
.onAppear { props.selectDefaultTodoList() }
3538
}
3639

37-
func renderRow(props: Props, todoList: TodoList) -> some View {
40+
func row(todoList: TodoList, selectedTodoListId: Binding<String?>) -> some View {
3841
TodoListRow(
3942
todoList: todoList,
40-
selected: Binding(
41-
get: { props.selectedTodoListId == todoList.id },
42-
set: { if $0 == true { self.selectTodoList(id: todoList.id) } }
43-
),
44-
destination: self.renderTodoList
43+
selectedId: selectedTodoListId,
44+
destination: TodoListContainer(id: todoList.id)
4545
)
4646
}
47-
48-
func renderTodoList(id: String) -> some View {
49-
TodoListContainer(id: id)
50-
}
51-
52-
func addNewTodoList() {
53-
dispatch(TodoListsAction.addNewTodoList())
54-
}
55-
56-
func moveTodoList(from indexSet: IndexSet, to index: Int) {
57-
dispatch(TodoListsAction.moveTodoLists(from: indexSet, to: index))
58-
}
59-
60-
func removeTodoLists(at indexSet: IndexSet) {
61-
dispatch(TodoListsAction.removeTodoLists(at: indexSet))
62-
}
63-
64-
func selectTodoList(id: String) {
65-
dispatch(TodoListsAction.selectTodoList(id: id))
66-
}
67-
68-
func selectDefaultTodoList(props: Props) {
69-
guard props.selectedTodoListId == nil && sizeClass == .regular else { return }
70-
dispatch(TodoListsAction.selectTodoList(id: props.todoLists.first?.id))
71-
}
47+
}
7248

49+
extension TodoListBrowserContainer{
50+
struct Props: Equatable {
51+
var todoLists: OrderedState<TodoList>
52+
@ActionBinding var selectedTodoListId: String?
53+
@ActionBinding var addNewTodoList: ()->()
54+
@ActionBinding var moveTodoLists: (IndexSet, Int)->()
55+
@ActionBinding var removeTodoLists: (IndexSet)->()
56+
@ActionBinding var selectDefaultTodoList: ()->()
57+
}
7358
}
7459

7560
#if DEBUG

Todo/Containers/TodoListContainer.swift

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,57 @@ struct TodoListContainer : ConnectableView {
66
var id: String
77

88
@Environment(\.horizontalSizeClass) private var sizeClass
9-
@MappedDispatch() private var dispatch
109

11-
func map(state: AppState) -> [String]? {
12-
state.todoLists[id]?.todoIds
10+
func map(state: AppState, binder: ActionBinder) -> Props? {
11+
guard let todoList = state.todoLists[id] else { return nil }
12+
return Props(
13+
todoIds: todoList.todoIds,
14+
name: binder.bind(todoList.name) {
15+
TodoListsAction.setName(id: todoList.id, name: $0)
16+
},
17+
newTodoText: binder.bind(todoList.newTodoText) {
18+
TodoListsAction.setNewTodoText(id: todoList.id, text: $0)
19+
},
20+
onAddTodo: binder.bind { TodoListsAction.addTodo(id: self.id, text: $0) },
21+
moveTodoLists: binder.bind { TodoListsAction.moveTodos(id: self.id, from: $0, to: $1) },
22+
removeTodoLists: binder.bind { TodoListsAction.removeTodos(id: self.id, at: $0) },
23+
deselectTodoList: binder.bind { TodoListsAction.selectTodoList(id: nil) }
24+
)
1325
}
1426

1527
func body(props: Props) -> some View {
1628
VStack {
17-
TodoListNameContainer(id: id)
18-
NewTodoContainer(id: id)
29+
TodoListNameField(name: props.$name)
30+
NewTodoRow(text: props.$newTodoText, onAddTodo: props.onAddTodo).padding()
1931
renderList(props: props)
2032
}
21-
.onDisappear(perform: deselectTodoList)
33+
.onDisappear {
34+
if self.sizeClass == .compact {
35+
props.deselectTodoList()
36+
}
37+
}
2238
}
2339

2440
func renderList(props: Props) -> some View {
2541
List {
26-
ForEach(props, id: \.self) { todoId in
42+
ForEach(props.todoIds, id: \.self) { todoId in
2743
TodoContainer(todoListId: self.id, todoId: todoId)
2844
}
29-
.onMove(perform: moveTodoLists)
30-
.onDelete(perform: removeTodoLists)
45+
.onMove(perform: props.moveTodoLists)
46+
.onDelete(perform: props.removeTodoLists)
3147
}
3248
}
33-
34-
func addNewTodo(text: String) {
35-
dispatch(TodoListsAction.addTodo(id: id, text: text))
36-
}
37-
38-
func moveTodoLists(from indexSet: IndexSet, to index: Int) {
39-
dispatch(TodoListsAction.moveTodos(id: id, from: indexSet, to: index))
40-
}
41-
42-
func removeTodoLists(at indexSet: IndexSet) {
43-
dispatch(TodoListsAction.removeTodos(id: id, at: indexSet))
44-
}
45-
46-
func deselectTodoList() {
47-
if self.sizeClass == .compact {
48-
self.dispatch(TodoListsAction.selectTodoList(id: nil))
49-
}
49+
}
50+
51+
extension TodoListContainer {
52+
struct Props: Equatable {
53+
var todoIds: [String]
54+
@ActionBinding var name: String
55+
@ActionBinding var newTodoText: String
56+
@ActionBinding var onAddTodo: (String)->()
57+
@ActionBinding var moveTodoLists: (IndexSet, Int)->()
58+
@ActionBinding var removeTodoLists: (IndexSet)->()
59+
@ActionBinding var deselectTodoList: ()->()
5060
}
5161
}
5262

Todo/Containers/TodoListNameContainer.swift

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import SwiftUI
2+
import SwiftDux
3+
4+
struct TodoListSceneContainer : View {
5+
var todoListId: String
6+
var store: Store<AppState>
7+
8+
var body: some View {
9+
NavigationView {
10+
TodoListContainer(id: todoListId)
11+
AnyView(Text("Select a todo list."))
12+
}.provideStore(store)
13+
}
14+
}
15+
16+
#if DEBUG
17+
public enum TodoListSceneContainer_Previews: PreviewProvider {
18+
static var store: Store<AppState> {
19+
configureStore()
20+
}
21+
22+
public static var previews: some View {
23+
TodoListSceneContainer(todoListId: "123", store: store)
24+
}
25+
}
26+
#endif

0 commit comments

Comments
 (0)