From b45794b0ca73c71fb5723cab087e100b2681142e Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 16 Jan 2023 15:11:58 +0100 Subject: [PATCH] remove withNullableControllerViewBinding it seems async calls are not cancelled reliable so it was decided to always check for null bindings to avoid NPEs Signed-off-by: Marcel Hibbe --- .../talk/controllers/ChatController.kt | 6 +-- .../talk/controllers/ContactsController.kt | 42 +++++++------------ .../ConversationsListController.kt | 36 +++++----------- .../talk/controllers/base/BaseController.kt | 22 ---------- 4 files changed, 27 insertions(+), 79 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 73a97fb90..8dd60445a 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -419,10 +419,8 @@ class ChatController(args: Bundle) : } ) - withNullableControllerViewBinding { - val itemTouchHelper = ItemTouchHelper(messageSwipeController) - itemTouchHelper.attachToRecyclerView(binding?.messagesListView) - } + val itemTouchHelper = ItemTouchHelper(messageSwipeController) + itemTouchHelper.attachToRecyclerView(binding?.messagesListView) } } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.kt index b1bcc05c2..a3ce1bbbe 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.kt @@ -452,22 +452,16 @@ class ContactsController(args: Bundle) : adapter?.filterItems() } - withNullableControllerViewBinding { - binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false - } + binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false } override fun onError(e: Throwable) { - withNullableControllerViewBinding { - binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false - } + binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false dispose(contactsQueryDisposable) } override fun onComplete() { - withNullableControllerViewBinding { - binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false - } + binding?.controllerGenericRv?.swipeRefreshLayout?.isRefreshing = false dispose(contactsQueryDisposable) alreadyFetching = false disengageProgressBar() @@ -699,9 +693,7 @@ class ContactsController(args: Bundle) : adapter?.updateDataSet(contactItems as List?) } - withNullableControllerViewBinding { - binding?.controllerGenericRv?.swipeRefreshLayout?.isEnabled = !adapter!!.hasFilter() - } + binding?.controllerGenericRv?.swipeRefreshLayout?.isEnabled = !adapter!!.hasFilter() return true } @@ -929,25 +921,21 @@ class ContactsController(args: Bundle) : } private fun toggleConversationPrivacyLayout(showInitialLayout: Boolean) { - withNullableControllerViewBinding { - if (showInitialLayout) { - binding?.conversationPrivacyToggle?.initialRelativeLayout?.visibility = View.VISIBLE - binding?.conversationPrivacyToggle?.secondaryRelativeLayout?.visibility = View.GONE - } else { - binding?.conversationPrivacyToggle?.initialRelativeLayout?.visibility = View.GONE - binding?.conversationPrivacyToggle?.secondaryRelativeLayout?.visibility = View.VISIBLE - } + if (showInitialLayout) { + binding?.conversationPrivacyToggle?.initialRelativeLayout?.visibility = View.VISIBLE + binding?.conversationPrivacyToggle?.secondaryRelativeLayout?.visibility = View.GONE + } else { + binding?.conversationPrivacyToggle?.initialRelativeLayout?.visibility = View.GONE + binding?.conversationPrivacyToggle?.secondaryRelativeLayout?.visibility = View.VISIBLE } } private fun toggleConversationViaLinkVisibility(isPublicCall: Boolean) { - withNullableControllerViewBinding { - if (isPublicCall) { - binding?.joinConversationViaLink?.joinConversationViaLinkRelativeLayout?.visibility = View.GONE - updateGroupParticipantSelection() - } else { - binding?.joinConversationViaLink?.joinConversationViaLinkRelativeLayout?.visibility = View.VISIBLE - } + if (isPublicCall) { + binding?.joinConversationViaLink?.joinConversationViaLinkRelativeLayout?.visibility = View.GONE + updateGroupParticipantSelection() + } else { + binding?.joinConversationViaLink?.joinConversationViaLinkRelativeLayout?.visibility = View.VISIBLE } } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt index fa3f132ba..9712c3fbc 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt @@ -409,9 +409,7 @@ class ConversationsListController(bundle: Bundle) : adapter!!.setHeadersShown(true) adapter!!.updateDataSet(searchableConversationItems, false) adapter!!.showAllHeaders() - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isEnabled = false - } + binding?.swipeRefreshLayoutView?.isEnabled = false return true } @@ -424,9 +422,7 @@ class ConversationsListController(bundle: Bundle) : searchHelper!!.cancelSearch() binding?.swipeRefreshLayoutView?.isRefreshing = false } - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isEnabled = true - } + binding?.swipeRefreshLayoutView?.isEnabled = true searchView!!.onActionViewCollapsed() val mainActivity = getActivity() as MainActivity? if (mainActivity != null) { @@ -514,21 +510,15 @@ class ConversationsListController(bundle: Bundle) : adapter!!.updateDataSet(conversationItems, false) Handler().postDelayed({ checkToShowUnreadBubble() }, UNREAD_BUBBLE_DELAY.toLong()) fetchOpenConversations(apiVersion) - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = false - } + binding?.swipeRefreshLayoutView?.isRefreshing = false }, { throwable: Throwable -> handleHttpExceptions(throwable) - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = false - showErrorDialog() - } + binding?.swipeRefreshLayoutView?.isRefreshing = false + showErrorDialog() dispose(roomsQueryDisposable) }) { dispose(roomsQueryDisposable) - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = false - } + binding?.swipeRefreshLayoutView?.isRefreshing = false isRefreshing = false } } @@ -854,9 +844,7 @@ class ConversationsListController(bundle: Bundle) : @SuppressLint("CheckResult") // handled by helper private fun startMessageSearch(search: String?) { - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = true - } + binding?.swipeRefreshLayoutView?.isRefreshing = true searchHelper?.startMessageSearch(search!!) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -1331,17 +1319,13 @@ class ConversationsListController(bundle: Bundle) : binding?.recyclerView?.scrollToPosition(0) } } - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = false - } + binding?.swipeRefreshLayoutView?.isRefreshing = false } private fun onMessageSearchError(throwable: Throwable) { handleHttpExceptions(throwable) - withNullableControllerViewBinding { - binding?.swipeRefreshLayoutView?.isRefreshing = false - showErrorDialog() - } + binding?.swipeRefreshLayoutView?.isRefreshing = false + showErrorDialog() } companion object { diff --git a/app/src/main/java/com/nextcloud/talk/controllers/base/BaseController.kt b/app/src/main/java/com/nextcloud/talk/controllers/base/BaseController.kt index 6ccb4fe7c..ce97fc3a9 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/base/BaseController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/base/BaseController.kt @@ -54,7 +54,6 @@ import com.nextcloud.talk.controllers.ServerSelectionController import com.nextcloud.talk.controllers.SwitchAccountController import com.nextcloud.talk.controllers.WebViewLoginController import com.nextcloud.talk.controllers.base.providers.ActionBarProvider -import com.nextcloud.talk.controllers.util.ControllerViewBindingDelegate import com.nextcloud.talk.databinding.ActivityMainBinding import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.DisplayUtils @@ -304,27 +303,6 @@ abstract class BaseController(@LayoutRes var layoutRes: Int, args: Bundle? = nul } } - /** - * Mainly intended to be used in async listeners that may be called after the controller has been destroyed. - * - * If you need to use this function to patch a NPE crash, something is wrong in the way that the async calls are - * handled, they should have been cancelled when the controller UI was destroyed (if their only purpose was - * updating UI). - */ - @Suppress("Detekt.TooGenericExceptionCaught") - inline fun withNullableControllerViewBinding(block: () -> Unit) { - try { - block() - } catch (e: NullPointerException) { - // Handle only the exceptions we know about, let everything else pass through - if (e.stackTrace.firstOrNull()?.className == ControllerViewBindingDelegate::class.qualifiedName) { - Log.w("ControllerViewBinding", "Trying to update UI on a null ViewBinding.", e) - } else { - throw e - } - } - } - open val appBarLayoutType: AppBarLayoutType get() = AppBarLayoutType.TOOLBAR val searchHint: String