Last active
August 29, 2018 16:00
-
-
Save SeongUgJung/9e505487ecb3345616577d379a5692eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class MainViewModel(private val layoutCenterViewUsecase : LayoutCenterViewUsecase) { | |
val centerY = ObservableInt() | |
init { | |
layoutCenterViewUsecase.observe() | |
.subscribe { y : Int -> | |
centerY.set(y) | |
} | |
} | |
} |
This file contains hidden or 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
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} | |
} | |
} |
This file contains hidden or 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
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