Prevent crash that happened once

This commit is contained in:
Sylvain Berfini 2022-01-10 10:14:59 +01:00
parent 44a61a2be2
commit 2ed6fa3246
2 changed files with 13 additions and 5 deletions

View file

@ -42,6 +42,10 @@ abstract class GenericFragment<T : ViewDataBinding> : Fragment() {
protected val binding get() = _binding!! protected val binding get() = _binding!!
protected var useMaterialSharedAxisXForwardAnimation = true protected var useMaterialSharedAxisXForwardAnimation = true
protected fun isBindingAvailable(): Boolean {
return _binding != null
}
protected val onBackPressedCallback = object : OnBackPressedCallback(true) { protected val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() { override fun handleOnBackPressed() {
lifecycleScope.launch { lifecycleScope.launch {

View file

@ -212,11 +212,15 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
.addOnGlobalLayoutListener( .addOnGlobalLayoutListener(
object : OnGlobalLayoutListener { object : OnGlobalLayoutListener {
override fun onGlobalLayout() { override fun onGlobalLayout() {
binding.chatMessagesList if (isBindingAvailable()) {
.viewTreeObserver binding.chatMessagesList
.removeOnGlobalLayoutListener(this) .viewTreeObserver
Log.i("[Chat Room] Messages have been displayed, scrolling to first unread message if any") .removeOnGlobalLayoutListener(this)
scrollToFirstUnreadMessageOrBottom(false) Log.i("[Chat Room] Messages have been displayed, scrolling to first unread message if any")
scrollToFirstUnreadMessageOrBottom(false)
} else {
Log.e("[Chat Room] Binding not available in onGlobalLayout callback!")
}
} }
} }
) )