From e72708a7ad52a88e28fcce7ea9c77111be6da8d2 Mon Sep 17 00:00:00 2001 From: LZRS <12814349+LZRS@users.noreply.github.com> Date: Wed, 13 Oct 2021 11:34:30 +0300 Subject: [PATCH] LMH1-104: switch navigation adapters to pick current activity to avoid nullpointer exceptions for an already destroyed activity --- .../chw/core/adapter/NavigationAdapter.java | 10 +++--- .../chw/core/custom_views/NavigationMenu.java | 14 +++++--- .../chw/core/listener/NavigationListener.java | 35 +++++++++++++++---- .../chw/core/model/NavigationOption.java | 4 +++ .../core/presenter/NavigationPresenter.java | 33 +++++++++-------- .../chw/core/utils/CoreConstants.java | 1 - 6 files changed, 67 insertions(+), 30 deletions(-) diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/adapter/NavigationAdapter.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/adapter/NavigationAdapter.java index 080c7bde39..4e48f3bc2b 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/adapter/NavigationAdapter.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/adapter/NavigationAdapter.java @@ -66,9 +66,9 @@ public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { NavigationOption model = navigationOptionList.get(position); holder.tvName.setText(context.getResources().getText(model.getTitleID())); - if (model.getRegisterCount() >= 0) { + if (model.hasRegisterCount()) { holder.tvCount.setText(String.format(Locale.getDefault(), "%d", model.getRegisterCount())); - }else{ + } else { holder.tvCount.setText(null); } holder.ivIcon.setImageResource(model.getResourceID()); @@ -81,7 +81,7 @@ public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.tvCount.setTextColor(context.getResources().getColor(R.color.navigation_item_unselected)); holder.tvName.setTextColor(context.getResources().getColor(R.color.navigation_item_unselected)); holder.ivIcon.setImageResource(model.getResourceID()); - }else if (selectedView != null && selectedView.equals(model.getMenuTitle())){ + } else if (selectedView != null && selectedView.equals(model.getMenuTitle())) { holder.itemView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent)); holder.tvCount.setTextColor(context.getResources().getColor(R.color.navigation_item_selected)); holder.tvName.setTextColor(context.getResources().getColor(R.color.navigation_item_selected)); @@ -117,7 +117,9 @@ private MyViewHolder(View view) { if (onClickListener != null) { view.setOnClickListener(v -> { - if (drawerLayout != null) drawerLayout.closeDrawers(); + if (drawerLayout != null) { + drawerLayout.closeDrawers(); + } onClickListener.onClick(v); }); } diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/custom_views/NavigationMenu.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/custom_views/NavigationMenu.java index a15e41bfd1..c25de0c662 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/custom_views/NavigationMenu.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/custom_views/NavigationMenu.java @@ -289,16 +289,20 @@ private void registerDrawer(Activity parentActivity) { private void registerNavigation(Activity parentActivity) { if (recyclerView != null) { + RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(parentActivity); + recyclerView.setLayoutManager(mLayoutManager); + recyclerView.setItemAnimator(new DefaultItemAnimator()); List navigationOptions = mPresenter.getOptions(); if (navigationAdapter == null) { navigationAdapter = new NavigationAdapter(navigationOptions, parentActivity, registeredActivities, drawer); + recyclerView.setAdapter(navigationAdapter); + }else { + NavigationAdapter previous = navigationAdapter; + navigationAdapter = new NavigationAdapter(navigationOptions, parentActivity, registeredActivities, drawer); + recyclerView.swapAdapter(navigationAdapter, true); + navigationAdapter.setSelectedView(previous.getSelectedView()); } - - RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(parentActivity); - recyclerView.setLayoutManager(mLayoutManager); - recyclerView.setItemAnimator(new DefaultItemAnimator()); - recyclerView.setAdapter(navigationAdapter); } } diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/listener/NavigationListener.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/listener/NavigationListener.java index d73530ba23..68fd1a8f01 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/listener/NavigationListener.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/listener/NavigationListener.java @@ -5,6 +5,9 @@ import android.view.View; import android.widget.Toast; +import androidx.annotation.NonNull; + +import org.jetbrains.annotations.NotNull; import org.smartregister.chw.core.R; import org.smartregister.chw.core.adapter.NavigationAdapter; import org.smartregister.chw.core.utils.CoreConstants; @@ -27,7 +30,6 @@ public void onClick(View v) { String tag = (String) v.getTag(); switch (tag) { case CoreConstants.DrawerMenu.CHILD_CLIENTS: - case CoreConstants.DrawerMenu.ALL_CHILDREN: startRegisterActivity(getActivity(CoreConstants.REGISTERED_ACTIVITIES.CHILD_REGISTER_ACTIVITY)); break; case CoreConstants.DrawerMenu.ALL_FAMILIES: @@ -69,9 +71,7 @@ public void onClick(View v) { startRegisterActivity(getActivity(CoreConstants.REGISTERED_ACTIVITIES.UPDATES_REGISTER_ACTIVITY)); break; case CoreConstants.DrawerMenu.REPORTS: - activity.startActivity(new Intent(activity, getActivity(CoreConstants.REGISTERED_ACTIVITIES.REPORTS_ACTIVITY))); - activity.overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); - activity.finish(); + startRegisterActivity(getActivity(CoreConstants.REGISTERED_ACTIVITIES.REPORTS_ACTIVITY)); break; case CoreConstants.DrawerMenu.ADD_NEW_FAMILY: Class newFamilyRegisterClass = getActivity(CoreConstants.REGISTERED_ACTIVITIES.ADD_NEW_FAMILY); @@ -81,7 +81,7 @@ public void onClick(View v) { } else { Intent intent = new Intent(activity, newFamilyRegisterClass); intent.putExtra(CoreConstants.ACTIVITY_PAYLOAD.ACTION, CoreConstants.ACTION.START_REGISTRATION); - activity.startActivity(intent); + startRegisterActivity(intent, false); } break; default: @@ -92,11 +92,34 @@ public void onClick(View v) { } } - public void startRegisterActivity(Class registerClass) { + private boolean isClassCurrentActivity(Class klass){ + return klass.getName().equals(activity.getClass().getName()); + } + + private boolean isIntentForCurrentActivity(Intent intent) { + return intent.getComponent().getClassName().equals(activity.getClass().getName()); + } + + public void startRegisterActivity(@NotNull @NonNull Class registerClass) { + startRegisterActivity(registerClass, true); + } + + public void startRegisterActivity(@NotNull @NonNull Class registerClass, boolean finish) { if (registerClass != null) { Intent intent = new Intent(activity, registerClass); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); activity.startActivity(intent); + if (!isClassCurrentActivity(registerClass) && finish) { + activity.overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); + activity.finish(); + } + } + } + + public void startRegisterActivity(@NonNull @NotNull Intent intent, boolean finish) { + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + activity.startActivity(intent); + if (!isIntentForCurrentActivity(intent) && finish) { activity.overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); activity.finish(); } diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/model/NavigationOption.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/model/NavigationOption.java index ea302701c2..add77ab3dd 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/model/NavigationOption.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/model/NavigationOption.java @@ -56,4 +56,8 @@ public void setRegisterCount(long registerCount) { RegisterCount = registerCount; } + public boolean hasRegisterCount() { + return this.RegisterCount >= 0; + } + } diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/presenter/NavigationPresenter.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/presenter/NavigationPresenter.java index 918cc9865a..cbae37357e 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/presenter/NavigationPresenter.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/presenter/NavigationPresenter.java @@ -82,20 +82,25 @@ public void refreshNavigationCount(final Activity activity) { int x = 0; while (x < mModel.getNavigationItems().size()) { - final int finalX = x; - mInteractor.getRegisterCount(tableMap.get(mModel.getNavigationItems().get(x).getMenuTitle()), new NavigationContract.InteractorCallback() { - @Override - public void onResult(Integer result) { - mModel.getNavigationItems().get(finalX).setRegisterCount(result); - getNavigationView().refreshCount(); - } - - @Override - public void onError(Exception e) { - // getNavigationView().displayToast(activity, "Error retrieving count for " + tableMap.get(mModel.getNavigationItems().get(finalX).getMenuTitle())); - Timber.e("Error retrieving count for %s", tableMap.get(mModel.getNavigationItems().get(finalX).getMenuTitle())); - } - }); + final NavigationOption navigationOption = mModel.getNavigationItems().get(x); + final String navTitle = navigationOption.getMenuTitle(); + if (tableMap.containsKey(navTitle)) { + mInteractor.getRegisterCount(tableMap.get(navTitle), new NavigationContract.InteractorCallback() { + @Override + public void onResult(Integer result) { + navigationOption.setRegisterCount(result); + getNavigationView().refreshCount(); + } + + @Override + public void onError(Exception e) { + // getNavigationView().displayToast(activity, "Error retrieving count for " + tableMap.get(mModel.getNavigationItems().get(finalX).getMenuTitle())); + Timber.e("Error retrieving count for %s", tableMap.get(navTitle)); + } + }); + }else if (navigationOption.hasRegisterCount()){ + Timber.e("Error retrieving count for %s, table not defined in 'tableMap'", navTitle); + } x++; } diff --git a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/utils/CoreConstants.java b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/utils/CoreConstants.java index 1703b0d39a..72a120bd1b 100644 --- a/opensrp-chw-core/src/main/java/org/smartregister/chw/core/utils/CoreConstants.java +++ b/opensrp-chw-core/src/main/java/org/smartregister/chw/core/utils/CoreConstants.java @@ -725,7 +725,6 @@ public static final class DrawerMenu { public static final String REFERRALS = "Referrals"; public static final String STOCK_USAGE_REPORT = "StockUsageItemModel usage report"; public static final String UPDATES = "Updates"; - public static final String ALL_CHILDREN = "All Children"; public static final String REPORTS = "Reports"; public static final String ADD_NEW_FAMILY = "Add New Family"; }