Latest 2.0.2
Homepage https://github.com/sinarionn/ReusableView
License MIT
Platforms ios 9.0, osx 10.10, requires ARC
Dependencies RxCocoa
Authors

CI Status
codecov.io
Version
License
Platform

Requirements

  • iOS 9.0+
  • osX 10.10+
  • Xcode 9+
  • Swift 4
  • RxCocoa 4.0+

Installation

ReusableView is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'ReusableView'

Usage

Extend your class with one of the following protocols and get .viewModel property for free. )
Each viewModel change releases previous subscriptions (by releasing previous reuseBag) and calls onUpdate method again.

NonReusableType – if your view should not be reused. All next attempts to set viewModel will only call onAttemptToReuse method. (Allows you to ensure vm will be only one. Usually used with UIViewControllers.)

ReusableType – if your view supports reuse. viewModelWillUpdate will be called before each assignment. (can be used with cells, views in stackview and so on)

Methods

prepareForUsage() – called only once before first assignment, can be used to initialize view. (check out default implementations)

viewModelWillUpdate() – called before each assignment.

Examples

protocol MainViewModelType {
    var child: Driver<ChildViewModelType> { get }
}

protocol ChildViewModelType {
    var title: Driver<String> { get }
}

class MainViewController: UIViewController, NonReusableType {
    @IBOutlet weak var childView: ChildView!

    func onUpdate(with viewModel: MainViewModelType, reuseBag: DisposeBag) {
        viewModel.child.drive(childView.rx.viewModel).disposed(by: reuseBag)
    }
}

class ChildView: UIView, ReusableType {
    @IBOutlet weak var label: UILabel!

    // parameter reuseBag will be new for each new viewModel.
    func onUpdate(with viewModel: ChildViewModelType, reuseBag: DisposeBag) {
        viewModel.title.drive(label.rx.text).disposed(by: reuseBag)
    }
}

Author

Artem Antihevich, [email protected]

License

ReusableView is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "ReusableView",
    "version": "2.0.2",
    "summary": "Reusable and NonReusable viewModel containers",
    "homepage": "https://github.com/sinarionn/ReusableView",
    "license": "MIT",
    "authors": {
        "Artem Antihevich": "[email protected]"
    },
    "social_media_url": "https://twitter.com/sinarionn",
    "platforms": {
        "ios": "9.0",
        "osx": "10.10"
    },
    "source": {
        "git": "https://github.com/sinarionn/ReusableView.git",
        "tag": "2.0.2"
    },
    "requires_arc": true,
    "dependencies": {
        "RxCocoa": [
            "~> 4.0"
        ]
    },
    "source_files": "Sources/*.swift"
}

Pin It on Pinterest

Share This