Add SwiftUI + Redux

This commit is contained in:
Ivan Vorobei
2019-06-07 08:58:01 +03:00
parent 5b758db8cd
commit f385cd4929
29 changed files with 1311 additions and 0 deletions

View 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 {
}

View 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
}

View 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")]

View 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
}

View 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
}
}

View 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

View 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 { }

View 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
}
}