Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 29, 2018 16:00
Show Gist options
  • Save SeongUgJung/9e505487ecb3345616577d379a5692eb to your computer and use it in GitHub Desktop.
Save SeongUgJung/9e505487ecb3345616577d379a5692eb to your computer and use it in GitHub Desktop.
class MainViewModel(private val layoutCenterViewUsecase : LayoutCenterViewUsecase) {
val centerY = ObservableInt()
init {
layoutCenterViewUsecase.observe()
.subscribe { y : Int ->
centerY.set(y)
}
}
}
class LayoutCenterViewUsecase(layouts : () -> View) {
private val layout by lazy(layouts)
fun observe() : Observable<Int> {
return Observable.create { emitter ->
emitter.onNext(layout.bottom - layout.top)
val callback = object : View.OnLayoutChangeListener {
override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
emitter.onNext(bottom - top)
}
}
layout.addOnLayoutChangeListener(callback)
emitter.setCancellable {
layout.removeLayoutChangeListener(callback)
}
}.map { it / 2}
}
}
class MainActivity : AppCompatActivity() {
fun onCreated(saved : Bundle?) {
val vm = MainViewModel(LayoutCenterViewUsecase { findViewById<ViewGroup>(R.id.layout) } )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment