- iOS 9.0+
- osX 10.10+
- Xcode 9+
- Swift 4
- RxCocoa 4.0+
ReusableView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ReusableView'
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)
prepareForUsage() - called only once before first assignment, can be used to initialize view. (check out default implementations)
viewModelWillUpdate() - called before each assignment.
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)
}
}
Artem Antihevich, [email protected]
ReusableView is available under the MIT license. See the LICENSE file for more info.