Fixed review
This commit is contained in:
parent
095e21da44
commit
03eeecfea3
|
|
@ -2,6 +2,9 @@ package ru.touchin.roboswag.components.navigation.keyboard_resizeable
|
|||
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.OnLifecycleEvent
|
||||
import ru.touchin.roboswag.components.navigation.activities.BaseActivity
|
||||
|
||||
/**
|
||||
|
|
@ -11,18 +14,18 @@ import ru.touchin.roboswag.components.navigation.activities.BaseActivity
|
|||
*/
|
||||
class KeyboardBehaviorDetector(
|
||||
activity: BaseActivity
|
||||
) {
|
||||
) : LifecycleObserver {
|
||||
|
||||
private val view = activity.window.decorView
|
||||
|
||||
var keyboardHideListener: (() -> Unit)? = null
|
||||
var keyboardShowListener: ((Int) -> Unit)? = null
|
||||
|
||||
// -1 when we never measure insets yet
|
||||
var startNavigationBarHeight = -1
|
||||
private set
|
||||
|
||||
private val listener = { isKeyboardOpen: Boolean,
|
||||
windowInsets: WindowInsetsCompat ->
|
||||
private val listener = { isKeyboardOpen: Boolean, windowInsets: WindowInsetsCompat ->
|
||||
if (isKeyboardOpen) {
|
||||
keyboardShowListener?.invoke(
|
||||
windowInsets.systemWindowInsetBottom - startNavigationBarHeight
|
||||
|
|
@ -32,7 +35,7 @@ class KeyboardBehaviorDetector(
|
|||
}
|
||||
}
|
||||
|
||||
// Call this in "onResume()" of a fragment
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_START)
|
||||
fun startDetection() {
|
||||
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsets ->
|
||||
val bottomInset = windowInsets.systemWindowInsetBottom
|
||||
|
|
@ -45,7 +48,7 @@ class KeyboardBehaviorDetector(
|
|||
ViewCompat.requestApplyInsets(view)
|
||||
}
|
||||
|
||||
// Call this in "onPause()" of a fragment
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
|
||||
fun stopDetection() {
|
||||
ViewCompat.setOnApplyWindowInsetsListener(view, null)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ru.touchin.roboswag.components.navigation.keyboard_resizeable
|
|||
import android.os.Build
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.LayoutRes
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import ru.touchin.roboswag.components.navigation.activities.BaseActivity
|
||||
import ru.touchin.roboswag.components.navigation.activities.OnBackPressedListener
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController
|
||||
|
|
@ -19,6 +20,7 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
|
||||
creationContext.container?.requestApplyInsets()
|
||||
}
|
||||
lifecycle.addObserver(activity.keyboardBehaviorDetector as LifecycleObserver)
|
||||
}
|
||||
|
||||
private var keyboardIsVisible: Boolean = false
|
||||
|
|
@ -66,7 +68,6 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
|
|||
onKeyboardShow(diff)
|
||||
keyboardIsVisible = true
|
||||
}
|
||||
startDetection()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +76,6 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
|
|||
activity.keyboardBehaviorDetector?.apply {
|
||||
keyboardHideListener = null
|
||||
keyboardShowListener = null
|
||||
stopDetection()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue