mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-20 00:24:17 +01:00
Add SwiftUI + Redux
This commit is contained in:
13
Examples/SwiftUI + Redux/SwiftUIDemo/flux/actions/Action.swift
Executable file
13
Examples/SwiftUI + Redux/SwiftUIDemo/flux/actions/Action.swift
Executable file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// Action.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Action {
|
||||
|
||||
}
|
||||
19
Examples/SwiftUI + Redux/SwiftUIDemo/flux/actions/UsersAction.swift
Executable file
19
Examples/SwiftUI + Redux/SwiftUIDemo/flux/actions/UsersAction.swift
Executable file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// UsersAction.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum UserActions: Action {
|
||||
case addUser
|
||||
case deleteUser(index: Int)
|
||||
case move(from: Int, to: Int)
|
||||
case editUser(id: Int, name: String, username: String)
|
||||
case testEditFirstUser
|
||||
case startEditUser
|
||||
case stopEditUser
|
||||
}
|
||||
20
Examples/SwiftUI + Redux/SwiftUIDemo/flux/models/User.swift
Executable file
20
Examples/SwiftUI + Redux/SwiftUIDemo/flux/models/User.swift
Executable file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// User.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 04/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct User: Identifiable {
|
||||
let id: Int
|
||||
var name: String
|
||||
var username: String
|
||||
let imageName = "person"
|
||||
}
|
||||
|
||||
let sampleData = [User(id: 0, name: "user 1", username: "@user1"),
|
||||
User(id: 1, name: "user 2", username: "@user2")]
|
||||
14
Examples/SwiftUI + Redux/SwiftUIDemo/flux/reducers/Reducer.swift
Executable file
14
Examples/SwiftUI + Redux/SwiftUIDemo/flux/reducers/Reducer.swift
Executable file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Reducer.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Reducer {
|
||||
associatedtype StateType: FluxState
|
||||
func reduce(state: StateType, action: Action) -> StateType
|
||||
}
|
||||
42
Examples/SwiftUI + Redux/SwiftUIDemo/flux/reducers/UsersStateReducer.swift
Executable file
42
Examples/SwiftUI + Redux/SwiftUIDemo/flux/reducers/UsersStateReducer.swift
Executable file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// UsersStateReducer.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct UserStateReducer: Reducer {
|
||||
func reduce(state: UsersState, action: Action) -> UsersState {
|
||||
var state = state
|
||||
switch action {
|
||||
case UserActions.addUser:
|
||||
state.users.append(User(id: state.users.count,
|
||||
name: "New user \(state.users.count + 1)",
|
||||
username: "@newuser\(state.users.count + 1)"))
|
||||
case let UserActions.deleteUser(index):
|
||||
state.users.remove(at: index)
|
||||
case let UserActions.move(from, to):
|
||||
let user = state.users.remove(at: from)
|
||||
state.users.insert(user, at: to)
|
||||
case let UserActions.editUser(id, name, username):
|
||||
var user = state.users[id]
|
||||
user.name = name
|
||||
user.username = username
|
||||
state.users[id] = user
|
||||
case UserActions.testEditFirstUser:
|
||||
if !state.users.isEmpty {
|
||||
state.users[0] = User(id: 0, name: "user1", username: "u\ns\ne\nr\nn\na\nm\ne")
|
||||
}
|
||||
case UserActions.startEditUser:
|
||||
state.isEditingUser = true
|
||||
case UserActions.stopEditUser:
|
||||
state.isEditingUser = false
|
||||
default:
|
||||
break
|
||||
}
|
||||
return state
|
||||
}
|
||||
}
|
||||
34
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/AppState.swift
Executable file
34
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/AppState.swift
Executable file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// AppStore.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
final class AppState: BindableObject {
|
||||
var didChange = PassthroughSubject<AppState, Never>()
|
||||
|
||||
var usersState: UsersState
|
||||
|
||||
init(usersState: UsersState = UsersState()) {
|
||||
self.usersState = usersState
|
||||
}
|
||||
|
||||
func dispatch(action: Action) {
|
||||
usersState = UserStateReducer().reduce(state: usersState, action: action)
|
||||
didChange.send(self)
|
||||
}
|
||||
}
|
||||
|
||||
let store = AppState()
|
||||
|
||||
#if DEBUG
|
||||
let sampleStore = AppState(usersState: UsersState(users: sampleData))
|
||||
#endif
|
||||
|
||||
|
||||
11
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/FluxState.swift
Executable file
11
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/FluxState.swift
Executable file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// FluxState.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 05/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol FluxState { }
|
||||
20
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/UsersState.swift
Executable file
20
Examples/SwiftUI + Redux/SwiftUIDemo/flux/states/UsersState.swift
Executable file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// UsersStore.swift
|
||||
// SwiftUIDemo
|
||||
//
|
||||
// Created by Thomas Ricouard on 04/06/2019.
|
||||
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
struct UsersState: FluxState {
|
||||
var users: [User]
|
||||
var isEditingUser = false
|
||||
|
||||
init(users: [User] = []) {
|
||||
self.users = users
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user