Skip to content

Instantly share code, notes, and snippets.

@ermik
Forked from JUSTINMKAUFMAN/Shared.kt
Last active April 28, 2019 00:20
Show Gist options
  • Save ermik/708f5d299a5981d057e09971155371e3 to your computer and use it in GitHub Desktop.
Save ermik/708f5d299a5981d057e09971155371e3 to your computer and use it in GitHub Desktop.
Shared RIB Interface ('ChangePassword' Example)
// --------------------------------------------------------------------
// ChangePasswordViewController.swift
// --------------------------------------------------------------------
import ChangePasswordShared
import RxCocoa
import RxSwift
import UIKit
final class ChangePasswordViewController: UIViewController, View {
lazy var passwordFieldObservable: Observable<String> = passwordField.rx.text.asObservable()
lazy var didSubmitObservable: Observable<Void> = submitButton.rx.tap
var isSubmitEnabled: Bool {
get { return submitButton.isEnabled }
set { submitButton.isEnabled = newValue }
}
private let submitButton = UIButton()
private let passwordField = UITextField()
// etc.
}
// --------------------------------------------------------------------
// *** Shared ***
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// View.kt
// --------------------------------------------------------------------
/**
Shared interactors can reference properties and methods
defined in their RIB's View interface. RIBs can specialize
their own View interfaces to enable specific interaction
on the view layer.
*/
interface View: Presenter {
val isUserInteractionEnabled: Bool
val isHidden: Bool
val alpha: Double
}
// --------------------------------------------------------------------
// Result.kt
// --------------------------------------------------------------------
enum class Result {
SUCCESS,
ERROR
}
interface Resultable<Result> {
fun getResult(): Result
}
// --------------------------------------------------------------------
// ChangePasswordView.kt
// --------------------------------------------------------------------
/**
Both Android and iOS views must implement the protocol defined below
in order to attach the shared ChangePassword router (possible to have
compile-time guardrails around this?).
*/
interface ChangePasswordPresenter: View {
val passwordFieldObservable: Observable<String>
val didSubmitObservable: Observable<Void>
val isSubmitEnabled: Bool
}
// --------------------------------------------------------------------
// ChangePasswordInteractor.kt
// --------------------------------------------------------------------
@RibInteractor
class ChangePasswordInteractor : Interactor<ChangePasswordPresenter, ChangePasswordRouter>() {
lateinit var presenter: ChangePasswordPresenter
lateinit var listener: Listener
var result: ChangePasswordResultable<Result>
override fun didBecomeActive(savedInstanceState: Bundle?) {
super.didBecomeActive(savedInstanceState)
presenter.passwordFieldObservable
.observeOn(mainThreadScheduler)
.autoDisposable(this)
.subscribe { password ->
presenter.isSubmitEnabled = !password.isEmpty
}
presenter.didSubmitObservable
.observeOn(mainThreadScheduler)
.autoDisposable(this)
.subscribe { _ ->
presenter.isUserInteractionEnabled = false
val res = result.getResult() { res ->
if (res == SUCCESS) {
listener.changePasswordDidFinish()
} else if (res == ERROR) {
// Handle the failure
presenter.isUserInteractionEnabled = true
}
}
}
}
interface Listener {
fun changePasswordDidFinish()
}
}
// --------------------------------------------------------------------
// *** iOS ***
// --------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment