GithubHelp home page GithubHelp logo

lumiasaki / scenebox Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 114 KB

Dealing with states, navigations and even more in elegant way.

License: MIT License

Swift 100.00%
decoupling extensible injection navigation scalable

scenebox's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ”ญ Iโ€™m working on building app in Swift
  • ๐ŸŒฑ Iโ€™m learning RxSwift and SwiftUI ๐Ÿค”
  • ๐Ÿ“ซ How to reach me: [email protected]

scenebox's People

Contributors

lumiasaki avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

scenebox's Issues

Shared state extension supports using key path on state

Current situation

Shared state extension supports a simple dictionary driven store to save every single state and allows scenes to manipulate the state through the capabilities provided by shared state extension.

Developer needs to define a set of keys in any type which conform to Hashable protocol, then use traditional key-value way to get or put state onto the store, but literal keys might cause bugs due to typos, based on that, we decided to introduce key path into shared state extension.

How we work on shared state extension with key path

Key path is a feature provided by Swift, which give us a safer way to work on variables because of static type.

To improve the experience about using shared state extension, this time we introduced key path to this extension to free developers from typo caused bugs.

There are two types to make it runs, one is SharedStateKey, another one is SharedStateValues.

SharedStateKey represents a type for you to define your custom keys, here is a case:

struct TimestampKey: SharedStateKey {

    // this will give compiler a hint about concrete type.
    static var currentValue: TimeInterval?
}

SharedStateValues is a struct acts as entry point for you to extend variables as many as you like, which will also generate key path for your custom values, below is a case:

extension SharedStateValues {

    // this will generate a key path for `timestamp` variable.
    var timestamp: TimeInterval? {
        get { Self[TimestampKey.self] }
        set { Self[TimestampKey.self] = newValue }
    }
}

Above are all you need to do before making your variables can be shared between different scenes.

After that, you can get or put your variables by as same as before, the only one difference is you pass key path to method as arguments.

scene.sbx.getSharedState(by: \.timestamp)
// or
scene.sbx.putSharedState(by: \.timestamp, sharedState: value)

We also know the power of property wrapper, especially we have seen tons of usage of them in SwiftUI, so here we bring a new property wrapper for key path driven shared state extension, just like how @Environment works in SwiftUI.

class ViewController: UIViewController, Scene {

    // ...
    @SharedStateInjected(\.timestamp)
    var timestamp: TimeInterval?
}

Hope this newer, safer way to share state between scenes can reduce risks and improve experience to a higher level.

Implemented in #11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.