Skip to content

Commit

Permalink
Merge branch 'improve_redux_action'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Sep 29, 2019
2 parents e8e46bc + 1c9c0f7 commit 310be4e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 75 deletions.
2 changes: 1 addition & 1 deletion FireTodo/Firebase/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FireSnapshot
import SwiftUI

extension Model {
struct Task: Codable, HasTimestamps, Equatable {
struct Task: HasTimestamps, Codable, Equatable{
var title: String = ""
var desc: String = ""
var completed: Bool = false
Expand Down
9 changes: 3 additions & 6 deletions FireTodo/Redux/Auth/AuthAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import ReSwiftThunk

enum AuthAction: Action {
case finishInitialLoad
case registerAuthChangeHandle(listenerHandle: AuthStateDidChangeListenerHandle)
case removeAuthChangeHandle
case subscribeAuthChange(cancellable: AnyCancellable)
case unsubscribeAuthChange
case updateAuthChangeListener(listener: AnyCancellable?)
case updateUser(user: Snapshot<Model.User>?)

static func subscribe() -> AppThunkAction {
Expand All @@ -39,14 +36,14 @@ enum AuthAction: Action {
}
.sink(receiveValue: dispatch)

dispatch(AuthAction.subscribeAuthChange(cancellable: cancellable))
dispatch(AuthAction.updateAuthChangeListener(listener: cancellable))
}
}

static func unsubscribe() -> AppThunkAction {
AppThunkAction { dispatch, getState in
getState()?.authState.listenerCancellable?.cancel()
dispatch(AuthAction.unsubscribeAuthChange)
dispatch(AuthAction.updateAuthChangeListener(listener: nil))
}
}

Expand Down
36 changes: 12 additions & 24 deletions FireTodo/Redux/Auth/AuthReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,20 @@ enum AuthReducer {
static var reduce: Reducer<AuthState> {
return { action, state in
var state = state ?? AuthState()
guard let action = action as? AuthAction else {
return state
}
switch action {
case let action as AuthAction:
switch action {
case .finishInitialLoad:
state.loadingState = .loaded
return state
case let .registerAuthChangeHandle(listenerHandle):
state.listenerHandle = listenerHandle
return state
case .removeAuthChangeHandle:
state.listenerHandle = nil
return state
case let .subscribeAuthChange(cancellable):
state.listenerCancellable = cancellable
return state
case .unsubscribeAuthChange:
state.listenerCancellable = nil
return state
case let .updateUser(user):
state.user = user
return state
}
default:
break
case .finishInitialLoad:
state.loadingState = .loaded
return state
case let .updateAuthChangeListener(cancellable):
state.listenerCancellable = cancellable
return state
case let .updateUser(user):
state.user = user
return state
}
return state
}
}
}
1 change: 0 additions & 1 deletion FireTodo/Redux/Auth/AuthState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ struct AuthState: StateType {

var loadingState: LoadingState = .initial
var user: Snapshot<Model.User>?
var listenerHandle: AuthStateDidChangeListenerHandle?
var listenerCancellable: AnyCancellable?
}
31 changes: 14 additions & 17 deletions FireTodo/Redux/EditTask/EditTaskReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ enum EditTaskReducer {
static var reduce: Reducer<EditTaskState> {
return { action, state in
var state = state ?? EditTaskState()
guard let action = action as? EditTaskAction else {
return state
}
switch action {
case let action as EditTaskAction:
switch action {
case .startRequest:
state.requesting = true
return state
case .endRequest:
state.requesting = false
return state
case .closeView:
state.saved = true
return state
case .reset:
return EditTaskState()
}
default:
break
case .startRequest:
state.requesting = true
return state
case .endRequest:
state.requesting = false
return state
case .closeView:
state.saved = true
return state
case .reset:
return EditTaskState()
}
return state
}
}
}
7 changes: 2 additions & 5 deletions FireTodo/Redux/Main/AppStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ import ReSwift
final class AppStore: StoreSubscriber, DispatchingStoreType, ObservableObject {
private let store: Store<AppState>
var state: AppState { store.state }
private let stateChange = ObservableObjectPublisher()
var objectWillChange: ObservableObjectPublisher {
stateChange
}
private(set) var objectWillChange: ObservableObjectPublisher = .init()

init(_ store: Store<AppState>) {
self.store = store
store.subscribe(self)
}

func newState(state: AppState) {
stateChange.send()
objectWillChange.send()
}

func dispatch(_ action: Action) {
Expand Down
2 changes: 1 addition & 1 deletion FireTodo/Redux/Middleware/Logging/LoggingMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func createLoggingMiddleware() -> Middleware<AppState> {
return { _, _ in
{ next in
{ action in
print("> \(action)")
print("[dispatch]: \(action)")
next(action)
}
}
Expand Down
11 changes: 6 additions & 5 deletions FireTodo/Redux/Tasks/TasksAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import ReSwift

enum TasksAction: Action {
case updateTasks(tasks: [Snapshot<Model.Task>])
case registerListener(listener: ListenerRegistration)
case removeListener
case updateListener(listener: ListenerRegistration?)

static func subscribe(userID: String) -> AppThunkAction {
AppThunkAction { dispatch, _ in
let listener = Snapshot<Model.Task>.listen(Model.Path.tasks(userID: userID)) { result in
let listener = Snapshot<Model.Task>.listen(Model.Path.tasks(userID: userID), queryBuilder: {
$0.order(by: "updateTime", descending: true).limit(to: 30)
}) { result in
switch result {
case let .success(tasks):
dispatch(TasksAction.updateTasks(tasks: tasks))
Expand All @@ -24,14 +25,14 @@ enum TasksAction: Action {
dispatch(TasksAction.updateTasks(tasks: []))
}
}
dispatch(TasksAction.registerListener(listener: listener))
dispatch(TasksAction.updateListener(listener: listener))
}
}

static func unsubscribe() -> AppThunkAction {
AppThunkAction { dispatch, getState in
getState()?.tasksState.tasksListener?.remove()
dispatch(TasksAction.removeListener)
dispatch(TasksAction.updateListener(listener: nil))
}
}

Expand Down
24 changes: 9 additions & 15 deletions FireTodo/Redux/Tasks/TasksReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ enum TasksReducer {
static var reduce: Reducer<TasksState> {
return { action, state in
var state = state ?? TasksState()
guard let action = action as? TasksAction else {
return state
}
switch action {
case let action as TasksAction:
switch action {
case let .updateTasks(tasks):
state.tasks = tasks
return state
case let .registerListener(listener):
state.tasksListener = listener
return state
case .removeListener:
state.tasksListener = nil
return state
}
default:
break
case let .updateTasks(tasks):
state.tasks = tasks
return state
case let .updateListener(listener):
state.tasksListener = listener
return state
}
return state
}
}
}

0 comments on commit 310be4e

Please sign in to comment.