All Projects → sergdort → Combinefeedback

sergdort / Combinefeedback

Licence: mit
Unidirectional reactive architecture using new Apple Combine framework https://developer.apple.com/documentation/combine

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Combinefeedback

Moduliths
Building modular, monolithic applications using Spring Boot
Stars: ✭ 478 (-16.29%)
Mutual labels:  architecture
Awaker
article app for android
Stars: ✭ 526 (-7.88%)
Mutual labels:  architecture
Reference Architectures
templates and scripts for deploying Azure Reference Architectures
Stars: ✭ 554 (-2.98%)
Mutual labels:  architecture
Designpatternslibrary
A comprehensive design patterns library implemented in C#, which covers various design patterns from the most commonly used ones to the lesser-known ones. Get familiar with and learn design patterns through moderately realistic examples.
Stars: ✭ 485 (-15.06%)
Mutual labels:  architecture
Enterprise Scale
The Enterprise-Scale architecture provides prescriptive guidance coupled with Azure best practices, and it follows design principles across the critical design areas for organizations to define their Azure architecture
Stars: ✭ 511 (-10.51%)
Mutual labels:  architecture
Swift Viper Module
Xcode template for VIPER Architecture written in Swift 4
Stars: ✭ 527 (-7.71%)
Mutual labels:  architecture
Coordinator Mvvm Rx Example
Example of MVVM-C architecture implemented with RxSwift
Stars: ✭ 469 (-17.86%)
Mutual labels:  architecture
Ios Developer Roadmap
Roadmap to becoming an iOS developer in 2018.
Stars: ✭ 5,514 (+865.67%)
Mutual labels:  architecture
Android Starter
[Android Architecture] Android starter based on MVP/Dagger2/RxJava2/Robolectric/Espresso/Mockito. It provides a generator to fast create a Android template project.
Stars: ✭ 522 (-8.58%)
Mutual labels:  architecture
Pluggableapplicationdelegate
Smallest AppDelegate ever by using a decoupled-services based architecture. 🛠
Stars: ✭ 536 (-6.13%)
Mutual labels:  architecture
Android Showcase
💎 Android application following best practices: Kotlin, Coroutines, JetPack, Clean Architecture, Feature Modules, Tests, MVVM, DI, Static Analysis...
Stars: ✭ 5,214 (+813.13%)
Mutual labels:  architecture
Modular Monolith With Ddd
Full Modular Monolith application with Domain-Driven Design approach.
Stars: ✭ 6,210 (+987.57%)
Mutual labels:  architecture
Abixen Platform
Abixen Platform
Stars: ✭ 530 (-7.18%)
Mutual labels:  architecture
Viabus Architecture
让 Android 开发可以像流水线一样高效的,职责分离架构 ⚡ 不同于 MVP 的配置解耦,也不能和 似是而非 的 MVVM - Clean 同日而语。VIABUS 是世界范围内首个明确提出,通过职责分离,来真正实现 UI 和 业务并行开发的 Android 项目级开发架构和设计模式理念。
Stars: ✭ 485 (-15.06%)
Mutual labels:  architecture
Angular Architecture
Examples of Angular Architecture Concepts
Stars: ✭ 556 (-2.63%)
Mutual labels:  architecture
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-17.16%)
Mutual labels:  architecture
3rs Of Software Architecture
A guide on how to write readable, reusable, and refactorable software
Stars: ✭ 525 (-8.06%)
Mutual labels:  architecture
React Native Nw React Calculator
Mobile, desktop and website Apps with the same code
Stars: ✭ 5,116 (+795.97%)
Mutual labels:  architecture
React Best Practices
A comprehensive reference guide to kickstart your React architecting career!
Stars: ✭ 566 (-0.88%)
Mutual labels:  architecture
Awesome Fenix
讨论如何构筑一套可靠的分布式大型软件系统
Stars: ✭ 530 (-7.18%)
Mutual labels:  architecture

CombineFeedback

Unidirectional Reactive Architecture. This is a Combine implemetation of ReactiveFeedback and RxFeedback

Diagram

Motivation

Requirements for iOS apps have become huge. Our code has to manage a lot of state e.g. server responses, cached data, UI state, routing etc. Some may say that Reactive Programming can help us a lot but, in the wrong hands, it can do even more harm to your code base.

The goal of this library is to provide a simple and intuitive approach to designing reactive state machines.

Core Concepts

State

State is the single source of truth. It represents a state of your system and is usually a plain Swift type. Your state is immutable. The only way to transition from one State to another is to emit an Event.

Event

Represents all possible events that can happen in your system which can cause a transition to a new State.

Reducer

A Reducer is a pure function with a signature of ( inout State, Event) -> Void. While Event represents an action that results in a State change, it's actually not what causes the change. An Event is just that, a representation of the intention to transition from one state to another. What actually causes the State to change, the embodiment of the corresponding Event, is a Reducer. A Reducer is the only place where a State can be changed.

Feedback

While State represents where the system is at a given time, Event represents a state change, and a Reducer is the pure function that enacts the event causing the state to change, there is not as of yet any type to decide which event should take place given a particular current state. That's the job of the Feedback. It's essentially a "processing engine", listening to changes in the current State and emitting the corresponding next events to take place. Feedbacks don't directly mutate states. Instead, they only emit events which then cause states to change in reducers.

To some extent it's like reactive Middleware in Redux having a signature of (AnyPublisher<State, Never>) -> AnyPublisher<Event, Never> allows us to observe State changes and perform some side effects based on its changes e.g if a system is in loading state we can start fetching data from network.

CombineFeedbackUI

CombineFeedbackUI provides several convenience API to deal with state management and SwiftUI.

ViewModel

ViewModel - is a base class responsible for initializing a UI state machine. It provides two ways to interact with it.

  • We can start a state machine by observing var state: AnyPublisher<S, Never>.
  • We can send input events into it via public final func send(event: E).

This is useful if we want to mutate our state in response to user input. Let's consider a Counter example

struct State {
    var count = 0
}

enum Event {
    case increment
    case decrement
}

When we press + button we want the State of the system to be incremented by 1. To do that somewhere in our UI we can do:

Button(action: {
    viewModel.send(event: .increment)
}) {
    return Text("+").font(.largeTitle)
}

Also, we can use the send(event:) method to initiate side effects. For example, imagine that we are building an infinite list, and we want to trigger the next batch load when a user reaches the end of the list.

enum Event {
    case didLoad(Results)
    case didFail(Error)
    case fetchNext
}

struct State: Builder {
    var batch: Results
    var movies: [Movie]
    var status: Status
}
enum Status {
    case idle
    case loading
    case failed(Error)
}

struct MoviesView: View {
    typealias State = MoviesViewModel.State
    typealias Event = MoviesViewModel.Event
    let context: Context<State, Event>

    var body: some View {
        List {
            ForEach(context.movies.identified(by: \.id)) { movie in
                MovieCell(movie: movie).onAppear {
                // When we reach the end of the list
                // we send `fetchNext` event
                    if self.context.movies.last == movie {
                        self.context.send(event: .fetchNext)
                    }
                }
            }
        }
    }
}

When we send .fetchNext event, it goes to the reducer where we put our system into .loading state, which in response triggers effect in the whenLoading feedback, which is reacting to particular state changes

    static func reducer(state: inout State, event: Event) {
        switch event {
        case .didLoad(let batch):
            state.movies += batch.results
            state.status = .idle
            state.batch = batch
        case .didFail(let error):
            state.status = .failed(error)
        case .retry:
            state.status = .loading
        case .fetchNext:
            state.status = .loading
        }
    }

    static var feedback: Feedback<State, Event> {
        return Feedback(lensing: { $0.nextPage }) { page in
            URLSession.shared
                .fetchMovies(page: page)
                .map(Event.didLoad)
                .replaceError(replace: Event.didFail)
                .receive(on: DispatchQueue.main)
        }
    }

Sometimes tho we are only interested in the particular value of State property to be changed. E.g if we are building a signup form and we just want to change email property on the state. We can do something like this

struct State  {
    var email = ""
    var password = ""
}

viewModel.mutate(keyPath: \.email, "[email protected]")

Widget

Widget<State, Event> - is a convenience View that takes a ViewModel and render closure which renders new content every time the State changes.

Widget(viewModel: SignInViewModel()) { context in
    SignInView(context: context)
}

Context

Context<State, Event> - is a rendering context that we can use to interact with UI and render information. Via @dynamicMemberLookup it has all of the properties of the State and several conveniences methods for more seamless integration with SwiftUI. (Credits to @andersio)

struct State  {
    var email = ""
    var password = ""
}
enum Event {
	case signIn
}
struct SignInView: View {
    private let context: Context<State, Event>
    
    init(context: Context<State, Event>) {
        self.context = context
    }
    
    var body: some View {
        return Form {
            Section {
                TextField(context.binding(for: \.email))
                TextField(context.binding(for: \.password))
                Button(action: context.action(for: .signIn)) {
                    Text("Sign In")
                }
            }
        }
    }
}

Example

Counter Infinite List SignIn Form Traffic Light
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].