🚀Comprehensive Redux library for SwiftUI, ensures State consistency across Stores with type-safe pub/sub pattern.

Overview

SwiftUIRedux

Carthage compatible Swift Version License

Comprehensive Redux library for SwiftUI.

  • Keep State consistent across Stores by type safe pub/sub pattern with Reducers of RootStore.
  • Waterfall Action propagation flow from root to State subtree.

Unidirectional Data Flow

  • RootStore/Dispatcher: Propagates domained actions

  • Reducer: Subscribes to RootStore and reduces Action to generate new state

  • Action: Action driven - more loosely coupled pattern than Delegation pattern

  • State:

    • Reduces Action and outputs new State
    • Waterfall reduce flow: propagates Action to children nodes via State tree

Declarative/Stateful/Immutable/Predictable

  • Efficient tree diff algorithm, no more imperative manual update code.

Usage Example

FeedListView

struct FeedListView: View {
  @ObservedObject var state = FeedListState()
  
  var body: some View {
    List {
      ForEach(state.feeds, id: \.diffId) { feed in
        FeedCell(feed: feed)
      }
    }
  }
}

FeedListState

public class FeedListState: ReduxReducer, ObservableObject {  
  @Published var feeds: [Feed] = []
  
  public override func reduce(action: ReduxActionProtocol) {
    // Propagates `action` to the substate tree.
    // Setting `self.feeds` with new feeds triggers list UI reloading 
    // and SwiftUI will diff efficiently based on list identifier.
    feeds = feeds.map { $0.reduce(action: action) }
}

Dispatch FeedLikeAction

dispatch(action: FeedLikeAction(feed: feed))

Reduce FeedLikeAction

public struct Feed {
  public var diffId = UUID()
  public let feedId: Int
  public let title: String
  public var isLiked: Bool { didSet { diffId = UUID() } }     
}

extension Feed: ReduxStateProtocol {
  @discardableResult
  public func reduce(action: ReduxActionProtocol) -> Self {
    // Makes the deep copy of self.
    var feedCopy = self    
    switch action {
    case let action as FeedLikeAction:
      // Reduces the `FeedLikeAction`.
      if feedId == action.feed.feedId {
        feedCopy.isLiked = !action.feed.isLiked
      }
    default: break
    }
    return feedCopy
  }
}
You might also like...
A SARS-CoV-2 Mutation Pattern Query Tool

vdb A SARS-CoV-2 Mutation Pattern Query Tool 1. Purpose The vdb program is designed to query the SARS-CoV-2 mutational landscape. It runs as a command

Swordinator is a simple way of integrating an iOS Coordinator pattern.
Swordinator is a simple way of integrating an iOS Coordinator pattern.

Swordinator is a minimal, lightweight and easy customizable navigation framework for iOS applications. Requirements iOS 14.0+, Swift 5.0+ Installation

A declarative, thread safe, and reentrant way to define code that should only execute at most once over the lifetime of an object.

SwiftRunOnce SwiftRunOnce allows a developer to mark a block of logic as "one-time" code – code that will execute at most once over the lifetime of an

Extensions giving Swift's Codable API type inference super powers 🦸‍♂️🦹‍♀️
Extensions giving Swift's Codable API type inference super powers 🦸‍♂️🦹‍♀️

Welcome to Codextended — a suite of extensions that aims to make Swift’s Codable API easier to use by giving it type inference-powered capabilities an

Swift type modelling the success/failure of arbitrary operations.

Result This is a Swift µframework providing ResultValue, Error. ResultValue, Error values are either successful (wrapping Value) or failed (wrappi

UTIKit is an UTI (Uniform Type Identifier) wrapper for Swift.

UTIKit UTIKit is an UTI (Uniform Type Identifier) wrapper for Swift. Features UTIKit is a full featured library including entire UTI functions. Conver

DeviceGuru is a simple lib (Swift) to know the exact type of the device

DeviceGuru DeviceGuru is a simple lib (Swift) to know the exact type of the device, e.g. iPhone 6 or iPhone 6s Easy to use Light weight From version 5

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

A Codable Undefinable type for handling JSON undefined values.

Undefinable Overview The purpose of this type is represent the JSON undefined state in Swift structs and classes. The Undefinable enum is a generic wi

Comments
  • Reduce(action:) mutates the same model instance twice, if model is Reference Type

    Reduce(action:) mutates the same model instance twice, if model is Reference Type

    Example

    If FeedList/FeedDetails shares the same Feed reference, LikeFeed action in FeedDetails will possibly mutate Feed instance twice by both reduce(action:) in FeedDetailsState and FeedListState.

    Solution

    Recommendation is to define conformance as struct and return deep copy of self, e.g. var newSelf = self, to avoid duplicate mutations on the same model if with reference type.

    Workaround is to return complete deep copy of the instance if you have to use Reference type.

    bug 
    opened by geekaurora 1
Owner
Cheng Zhang
Software Engineer at Google
Cheng Zhang
A Swift property wrapper which stores the previous value

swift-with-previous A Swift property wrapper which stores the previous value. The previous value can be get by the projected value $propertyName. impo

IKEDA Sho 3 Feb 22, 2022
MediaType is a library that can be used to create Media Types in a type-safe manner.

This is a general purpose Swift library for a concept of typed treatment for Media Types. We use this library on clients and servers to speak the same dialect and to enjoy all the comfort strong types provide over raw strings.

21Gram Consulting 79 Jul 19, 2022
Type-Safe Associated Objects in Swift

Type-Safe Associated Objects in Swift TSAO is an implementation of type-safe associated objects in Swift. Objective-C associated objects are useful, b

Lily Ballard 135 Dec 21, 2022
Swift package for accessing SF Symbols in a type safe manner.

Swift Package Information Code Coverage Swift package for accessing SF Symbols in a type safe manner. Features ?? Contains all SF Symbols - 1.0, 2.0,

null 6 Dec 7, 2021
🟣 Verge is a very tunable state-management engine on iOS App (UIKit / SwiftUI) and built-in ORM.

Verge is giving the power of state-management in muukii/Brightroom v2 development! Verge.swift ?? An effective state management architecture for iOS -

VergeGroup 478 Dec 29, 2022
A meta library to provide a better `Delegate` pattern.

Delegate A meta library to provide a better Delegate pattern described here and here. Usage Instead of a regular Apple's protocol-delegate pattern, us

Wei Wang 67 Dec 23, 2022
OTAtomics - Multi-platform Swift thread-safe atomics library

OTAtomics Multi-platform Swift thread-safe atomics library. The library has full

Steffan Andrews 2 Jul 8, 2022
Sovran-Swift: Small, efficient, easy. State Management for Swift

Sovran-Swift: Small, efficient, easy. State Management for Swift

Segment 5 Jan 3, 2023
Safe and fast access to SwiftUI PreviewDevice

SafePreviewDevice Motivation At WWDC 2019, Apple announced SwiftUI a new library for building UI in a simple and fast way. Xcode’s SwiftUI preview let

Antonino Francesco Musolino 11 Jun 28, 2022
FluxCapacitor makes implementing Flux design pattern easily with protocols and typealias.

FluxCapacitor makes implementing Flux design pattern easily with protocols and typealias. Storable protocol Actionable protocol Dispatch

Taiki Suzuki 123 Aug 23, 2022