Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
cipolleschi / AppState.swift
Created March 22, 2020 13:54
DiceRoller App State
import Katana
/// The definition state of the app
struct AppState: State {
/// Slice of the state for the Dice
var diceState = DiceState()
}
extension AppState {
/// Definition of the state of our dice rolls
@cipolleschi
cipolleschi / AppDependencies.swift
Created March 22, 2020 13:57
Dice Roller AppDependencies
import Katana
import Tempura
/// The Container for the dependencies of the App
class AppDependencies:
SideEffectDependencyContainer,
NavigationProvider
{
// The dispatch function that can be used to dispatch
@cipolleschi
cipolleschi / AppDelegate.swift
Created March 22, 2020 14:00
Dice Roller AppDelegate
import Katana
import Tempura
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RootInstaller {
var window: UIWindow?
var store: Store<AppState, AppDependencies>?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
@cipolleschi
cipolleschi / DiceRollerVM.swift
Last active March 29, 2020 10:50
DiceRoller View Model
import Tempura
struct DiceRollerVM: ViewModelWithState {
private let currentResults: [AppState.DiceState.Die: Int]
init(state: AppState) {
self.currentResults = state.diceState.currentResults
}
@cipolleschi
cipolleschi / DiceRollerView.swift
Created March 29, 2020 10:53
Dice Roller View
class DiceRollerView: UIView, ViewControllerModellableView {
typealias VM = DiceRollerVM
// MARK: - Helper Class
/// Small helper class that simplify the logic
private class DieButton: UIButton {
let die: AppState.DiceState.Die
required init(die: AppState.DiceState.Die) {
self.die = die
super.init(frame: .zero)
@cipolleschi
cipolleschi / DiceRollerVC.swift
Created March 29, 2020 11:03
Dice Roller VC
import Tempura
class DiceRollerVC: ViewController<DiceRollerView> {
override func viewDidLoad() {
super.viewDidLoad()
}
}
@cipolleschi
cipolleschi / DiceRollerLogic.swift
Created March 29, 2020 11:17
Logic for the Dice Roller
import Katana
enum DiceRollerLogic {
struct RollDice: AnySideEffect {
let die: AppState.DiceState.Die
func sideEffect(_ context: AnySideEffectContext) throws {
let extreme = die.rawValue
let result = Int.random(in: 1...extreme)
context.dispatch(UpdateCurrentResult(die: die, result: result))
@cipolleschi
cipolleschi / SetupInteractions.swift
Created March 29, 2020 11:26
Setup interactions for the dice roller
override func setupInteraction() {
self.rootView.rollDieInteraction = { [unowned self] die in
self.dispatch(DiceRollerLogic.RollDice(die: die))
}
}
@cipolleschi
cipolleschi / LoadContentVC.swift
Created April 11, 2020 09:39
Code to load some data from the disk
class MyViewController: UIViewController {
var myPath: String
var content: [MyModel]
// ...
func loadContent() {
let fileManager = FileManager.default
guard
@cipolleschi
cipolleschi / LoadContentVC2.swift
Last active April 18, 2020 09:51
Improved code with dependencies injection.
class MyViewController: UIViewController {
var myPath: String
var content: [MyModel]
// ...
func loadContent(
fileManager: FileManager,
path: String,
jsonDecoder: JSONDecoder = JSONDecoder()