Last active
September 9, 2022 10:07
-
-
Save sgr-ksmt/2cc92d8c7d517e08767fbe296b6da720 to your computer and use it in GitHub Desktop.
MVVM + RxSwift + Property Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AnyObserver { | |
static func create<E>(_ relay: PublishRelay<E>) -> AnyObserver<E> { | |
return .init { event in | |
guard case let .next(value) = event else { return } | |
relay.accept(value) | |
} | |
} | |
static func create<E>(_ relay: BehaviorRelay<E>) -> AnyObserver<E> { | |
return .init { event in | |
guard case let .next(value) = event else { return } | |
relay.accept(value) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://github.com/inamiy/RxProperty/blob/master/Sources/RxProperty/RxProperty.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import RxRelay | |
import RxSwift | |
@propertyWrapper | |
struct RxTrigger<Value> { | |
private let relay = PublishRelay<Value>() | |
private let trigger: AnyObserver<Value> | |
private let observable: Observable<Value> | |
init() { | |
trigger = .create(relay) | |
observable = relay.asObservable() | |
} | |
var wrappedValue: AnyObserver<Value> { | |
trigger | |
} | |
var projectedValue: Observable<Value> { | |
observable | |
} | |
} | |
@propertyWrapper | |
struct RxProperty<Value> { | |
private let relay: BehaviorRelay<Value> | |
private let property: Property<Value> | |
init(value: Value) { | |
relay = .init(value: value) | |
property = .init(relay) | |
} | |
var wrappedValue: Property<Value> { | |
property | |
} | |
var projectedValue: BehaviorRelay<Value> { | |
relay | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class SampleViewController: UIViewController { | |
private let viewModel: SampleViewModelType | |
private let disposeBag = DisposeBag() | |
init(viewModel: SampleViewModelType = SampleViewModel()) { | |
self.viewModel = viewModel | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
viewModel.outputs.messages | |
.changed | |
.bind(onNext: { print($0) }) | |
.disposed(by: disposeBag) | |
rx.sentMessage(#selector(SampleViewController.viewWillAppear(_:))) | |
.map { _ in } | |
.take(1) | |
.bind(to: viewModel.inputs.refresh) | |
.disposed(by: disposeBag) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol SampleViewModelInputs { | |
var refresh: AnyObserver<Void> { get } | |
} | |
protocol SampleViewModelOutputs { | |
var messages: Property<[String]> { get } | |
} | |
protocol SampleViewModelType { | |
var inputs: SampleViewModelInputs { get } | |
var outputs: SampleViewModelOutputs { get } | |
} | |
final class SampleViewModel: SampleViewModelType, SampleViewModelInputs, SampleViewModelOutputs { | |
var inputs: SampleViewModelInputs { return self } | |
var outputs: SampleViewModelOutputs { return self } | |
@RxTrigger var refresh: AnyObserver<Void> | |
@RxProperty(value: []) var messages: Property<[String]> | |
private let disposeBag = DisposeBag() | |
init() { | |
$refresh | |
.map { ["Hello", "How about you?"] } | |
.bind(to: $messages) | |
.disposed(by: disposeBag) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol SampleViewModelInputs { | |
var refresh: AnyObserver<Void> { get } | |
} | |
protocol SampleViewModelOutputs { | |
var messages: Property<[String]> { get } | |
} | |
protocol SampleViewModelType { | |
var inputs: SampleViewModelInputs { get } | |
var outputs: SampleViewModelOutputs { get } | |
} | |
final class SampleViewModel: SampleViewModelType, SampleViewModelInputs, SampleViewModelOutputs { | |
var inputs: SampleViewModelInputs { return self } | |
var outputs: SampleViewModelOutputs { return self } | |
let refresh: AnyObserver<Void> | |
let messages: Property<[String]> | |
private let disposeBag = DisposeBag() | |
init() { | |
let _refresh = PublishRelay<Void>() | |
self.refresh = .create(_refresh) | |
let _messages = BehaviorRelay<[String]>(value: []) | |
self.messages = .init(_messages) | |
_refresh | |
.map { ["Hello", "How about you?"] } | |
.bind(to: _messages) | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how about this?