Latest 0.1.1
Homepage https://github.com/konomae/TrackedValue
License MIT
Platforms ios 8.0, osx 10.11, tvos 9.0, watchos 2.0
Authors

Build Status codecov cocoapods

TrackedValue is a struct that determines whether a value has changed or not.

Original Idea: ReactorKit#74

Example

var v1 = TrackedValue(1)
let v2 = v1
v1 == v2 // true

v1.value = 1 // same value but new one assigned
v1 == v2 // false

Use case

final class LoginReactor: Reactor {
    enum Action {
        case login
    }

    struct State {
        var error: Error?
    }

    let initialState = State()
}

final class LoginViewController: UIViewController, View {
    func bind(reactor: LoginReactor) {
        reactor.state.map { $0.error }
            // For some reasons, we need to skip if the error is same instance.
            // We can not use `distinctUntilChanged` here. 😭
            // Becase `Error` protocol is not `Equatable`.
            .distinctUntilChanged()
            .bind { [weak self] in self?.showError($0) }
            .disposed(by: disposeBag)
    }
}

If we use the TrackedValue, we can determine a value is changed or not.
Even if the original value is not Equatable.

final class LoginReactor: Reactor {
    enum Action {
        case login
    }

    struct State {
        var error: TrackedValue<Error?> = .init(nil)
    }

    let initialState = State()
}

final class LoginViewController: UIViewController, View {
    func bind(reactor: LoginReactor) {
        reactor.state.map { $0.error }
            // We can use `distinctUntilChanged`! 😀
            .distinctUntilChanged()
            .bind { [weak self] in self?.showError($0.value) }
            .disposed(by: disposeBag)
    }
}

Latest podspec

{
    "name": "TrackedValue",
    "version": "0.1.1",
    "summary": "TrackedValue is a struct that determines whether a value has changed or not.",
    "homepage": "https://github.com/konomae/TrackedValue",
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    },
    "authors": {
        "konomae": "[email protected]"
    },
    "source": {
        "git": "https://github.com/konomae/TrackedValue.git",
        "tag": "0.1.1"
    },
    "source_files": "Sources/**/*.swift",
    "swift_version": "4.2",
    "platforms": {
        "ios": "8.0",
        "osx": "10.11",
        "tvos": "9.0",
        "watchos": "2.0"
    }
}

Pin It on Pinterest

Share This