Skip to content

Commit a23140c

Browse files
luizmbnpvisual
authored andcommitted
Sharing some ideas
1 parent bb9f968 commit a23140c

File tree

11 files changed

+324
-118
lines changed

11 files changed

+324
-118
lines changed

ToDoList-Redux.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
8EE3040224D8702A00DE03BC /* Indexing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EE3040124D8702A00DE03BC /* Indexing.swift */; };
2323
8EF3BB6124DDFC180091C114 /* CheckmarkCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EF3BB6024DDFC180091C114 /* CheckmarkCellViewModel.swift */; };
2424
8EFB8B4D24DC9A2C00E5FDCE /* CombineRextensions in Frameworks */ = {isa = PBXBuildFile; productRef = 8EFB8B4C24DC9A2C00E5FDCE /* CombineRextensions */; };
25+
D6F1129124E30FA1000AD72E /* ActionPrism.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6F1129024E30FA1000AD72E /* ActionPrism.swift */; };
26+
D6F1129324E3105B000AD72E /* AppLifecycle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6F1129224E3105B000AD72E /* AppLifecycle.swift */; };
2527
/* End PBXBuildFile section */
2628

2729
/* Begin PBXContainerItemProxy section */
@@ -60,6 +62,8 @@
6062
8E5B2B8E24DA4D9700B3C3A8 /* TaskListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskListViewModel.swift; sourceTree = "<group>"; };
6163
8EE3040124D8702A00DE03BC /* Indexing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Indexing.swift; sourceTree = "<group>"; };
6264
8EF3BB6024DDFC180091C114 /* CheckmarkCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckmarkCellViewModel.swift; sourceTree = "<group>"; };
65+
D6F1129024E30FA1000AD72E /* ActionPrism.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionPrism.swift; sourceTree = "<group>"; };
66+
D6F1129224E3105B000AD72E /* AppLifecycle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppLifecycle.swift; sourceTree = "<group>"; };
6367
/* End PBXFileReference section */
6468

6569
/* Begin PBXFrameworksBuildPhase section */
@@ -122,6 +126,7 @@
122126
8E2F82BE24D38960007A6932 /* Assets.xcassets */,
123127
8E2F82C324D38961007A6932 /* Info.plist */,
124128
8E2F82C024D38961007A6932 /* Preview Content */,
129+
D6F1129224E3105B000AD72E /* AppLifecycle.swift */,
125130
);
126131
path = "ToDoList-Redux";
127132
sourceTree = "<group>";
@@ -175,6 +180,7 @@
175180
8E5B2B8924DA194400B3C3A8 /* ReduxFramework.swift */,
176181
8E5B2B8E24DA4D9700B3C3A8 /* TaskListViewModel.swift */,
177182
8EF3BB6024DDFC180091C114 /* CheckmarkCellViewModel.swift */,
183+
D6F1129024E30FA1000AD72E /* ActionPrism.swift */,
178184
);
179185
path = Binders;
180186
sourceTree = "<group>";
@@ -336,12 +342,14 @@
336342
buildActionMask = 2147483647;
337343
files = (
338344
8E5B2B8F24DA4D9700B3C3A8 /* TaskListViewModel.swift in Sources */,
345+
D6F1129124E30FA1000AD72E /* ActionPrism.swift in Sources */,
339346
8E2F82EB24D38CAF007A6932 /* TaskList.swift in Sources */,
340347
8E2F82ED24D38CBD007A6932 /* CheckmarkCellView.swift in Sources */,
341348
8EE3040224D8702A00DE03BC /* Indexing.swift in Sources */,
342349
8EF3BB6124DDFC180091C114 /* CheckmarkCellViewModel.swift in Sources */,
343350
8E5B2B8A24DA194400B3C3A8 /* ReduxFramework.swift in Sources */,
344351
8E2F82E924D389A4007A6932 /* Task.swift in Sources */,
352+
D6F1129324E3105B000AD72E /* AppLifecycle.swift in Sources */,
345353
8E2F82BB24D3895F007A6932 /* ToDoList_ReduxApp.swift in Sources */,
346354
);
347355
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>ReactiveSwift (Playground) 1.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>5</integer>
11+
</dict>
12+
<key>ReactiveSwift (Playground) 2.xcscheme</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>6</integer>
16+
</dict>
17+
<key>ReactiveSwift (Playground).xcscheme</key>
18+
<dict>
19+
<key>orderHint</key>
20+
<integer>4</integer>
21+
</dict>
22+
<key>ReactiveSwift-UIExamples (Playground) 1.xcscheme</key>
23+
<dict>
24+
<key>orderHint</key>
25+
<integer>2</integer>
26+
</dict>
27+
<key>ReactiveSwift-UIExamples (Playground) 2.xcscheme</key>
28+
<dict>
29+
<key>orderHint</key>
30+
<integer>3</integer>
31+
</dict>
32+
<key>ReactiveSwift-UIExamples (Playground).xcscheme</key>
33+
<dict>
34+
<key>orderHint</key>
35+
<integer>1</integer>
36+
</dict>
37+
<key>Rx (Playground) 1.xcscheme</key>
38+
<dict>
39+
<key>isShown</key>
40+
<false/>
41+
<key>orderHint</key>
42+
<integer>8</integer>
43+
</dict>
44+
<key>Rx (Playground) 2.xcscheme</key>
45+
<dict>
46+
<key>isShown</key>
47+
<false/>
48+
<key>orderHint</key>
49+
<integer>9</integer>
50+
</dict>
51+
<key>Rx (Playground).xcscheme</key>
52+
<dict>
53+
<key>isShown</key>
54+
<false/>
55+
<key>orderHint</key>
56+
<integer>7</integer>
57+
</dict>
58+
<key>ToDoList-Redux.xcscheme_^#shared#^_</key>
59+
<dict>
60+
<key>orderHint</key>
61+
<integer>0</integer>
62+
</dict>
63+
</dict>
64+
</dict>
65+
</plist>

ToDoList-Redux/AppLifecycle.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// AppLifecycleMiddleware.swift
3+
// ToDoList-Redux
4+
//
5+
// Created by Luiz Rodrigo Martins Barbosa on 11.08.20.
6+
//
7+
8+
import Combine
9+
import Foundation
10+
import SwiftRex
11+
import UIKit
12+
13+
enum AppLifecycleAction {
14+
case didEnterBackground
15+
case willEnterForeground
16+
case didBecomeActive
17+
case willBecomeInactive
18+
}
19+
20+
enum AppLifecycle: Equatable {
21+
case backgroundActive
22+
case backgroundInactive
23+
case foregroundActive
24+
case foregroundInactive
25+
}
26+
27+
extension Reducer where ActionType == AppLifecycleAction, StateType == AppLifecycle {
28+
static let lifecycle = Reducer { action, state in
29+
switch (state, action) {
30+
case (.backgroundActive, .didEnterBackground): return state
31+
case (.backgroundInactive, .didEnterBackground): return state
32+
case (.foregroundActive, .didEnterBackground): return .backgroundActive
33+
case (.foregroundInactive, .didEnterBackground): return .backgroundInactive
34+
35+
case (.backgroundActive, .willEnterForeground): return .foregroundActive
36+
case (.backgroundInactive, .willEnterForeground): return .foregroundInactive
37+
case (.foregroundActive, .willEnterForeground): return state
38+
case (.foregroundInactive, .willEnterForeground): return state
39+
40+
case (.backgroundActive, .didBecomeActive): return state
41+
case (.backgroundInactive, .didBecomeActive): return .backgroundActive
42+
case (.foregroundActive, .didBecomeActive): return state
43+
case (.foregroundInactive, .didBecomeActive): return .foregroundActive
44+
45+
case (.backgroundActive, .willBecomeInactive): return .backgroundInactive
46+
case (.backgroundInactive, .willBecomeInactive): return state
47+
case (.foregroundActive, .willBecomeInactive): return .foregroundInactive
48+
case (.foregroundInactive, .willBecomeInactive): return state
49+
}
50+
}
51+
}
52+
53+
// TODO: Later change to upcoming (0.8) EffectMiddleware
54+
class AppLifecycleMiddleware: Middleware {
55+
typealias InputActionType = Never
56+
typealias OutputActionType = AppLifecycleAction
57+
typealias StateType = Void
58+
59+
private var cancellable: AnyCancellable?
60+
61+
func receiveContext(getState: @escaping GetState<StateType>, output: AnyActionHandler<OutputActionType>) {
62+
// TODO: Dependency Injection of publishers, so it's easier to mock this Middleware
63+
let notificationCenter = NotificationCenter.default
64+
cancellable = Publishers.Merge4(
65+
notificationCenter
66+
.publisher(for: UIApplication.didBecomeActiveNotification)
67+
.map { _ in AppLifecycleAction.didBecomeActive },
68+
notificationCenter
69+
.publisher(for: UIApplication.willResignActiveNotification)
70+
.map { _ in AppLifecycleAction.willBecomeInactive },
71+
notificationCenter
72+
.publisher(for: UIApplication.didEnterBackgroundNotification)
73+
.map { _ in AppLifecycleAction.didEnterBackground },
74+
notificationCenter
75+
.publisher(for: UIApplication.willEnterForegroundNotification)
76+
.map { _ in AppLifecycleAction.willEnterForeground }
77+
).sink { action in
78+
output.dispatch(action)
79+
}
80+
}
81+
82+
func handle(action: InputActionType, from dispatcher: ActionSource, afterReducer: inout AfterReducer) {
83+
}
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// ActionPrism.swift
3+
// ToDoList-Redux
4+
//
5+
// Created by Luiz Rodrigo Martins Barbosa on 11.08.20.
6+
//
7+
8+
import Foundation
9+
10+
// TODO: Use code-generation
11+
12+
// MARK: - PRISM
13+
14+
extension AppAction {
15+
public var task: TaskAction? {
16+
get {
17+
guard case let .task(value) = self else { return nil }
18+
return value
19+
}
20+
set {
21+
guard case .task = self, let newValue = newValue else { return }
22+
self = .task(newValue)
23+
}
24+
}
25+
26+
public var isTask: Bool {
27+
self.task != nil
28+
}
29+
}
30+
31+
extension AppAction {
32+
public var appLifecycle: AppLifecycleAction? {
33+
get {
34+
guard case let .appLifecycle(value) = self else { return nil }
35+
return value
36+
}
37+
set {
38+
guard case .appLifecycle = self, let newValue = newValue else { return }
39+
self = .appLifecycle(newValue)
40+
}
41+
}
42+
43+
public var isAppLifecycle: Bool {
44+
self.appLifecycle != nil
45+
}
46+
}

ToDoList-Redux/Binders/CheckmarkCellViewModel.swift

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,17 @@ import Foundation
1010
import SwiftRex
1111

1212
extension CheckmarkCellView {
13-
14-
1513
// MARK: - STATE
1614
struct State: Equatable {
17-
let id: String
1815
let title: String
1916
let imageName: String
2017

21-
static var empty = State(id: UUID().uuidString,
22-
title: "Type to create a new task...",
18+
static var empty = State(title: "Type to create a new task...",
2319
imageName: "circle")
24-
25-
static var mock = State(id: UUID().uuidString,
26-
title: "An incomplete task",
27-
imageName: "circle")
2820
}
2921

3022
// MARK: - ACTIONS
3123
enum Action {
32-
case toggle(String)
33-
}
34-
}
35-
36-
// MARK: - REDUCERS
37-
38-
extension Reducer where ActionType == CheckmarkCellView.Action, StateType == CheckmarkCellView.State {
39-
static let cell = Reducer { action, state in
40-
var state = state
41-
switch action {
42-
case .toggle: break
43-
}
44-
return state
24+
case toggle
4525
}
4626
}

0 commit comments

Comments
 (0)