Skip to content

Commit

Permalink
LMH1-104: switch navigation adapters to pick current activity
Browse files Browse the repository at this point in the history
to avoid nullpointer exceptions for an already destroyed activity
  • Loading branch information
LZRS committed Oct 13, 2021
1 parent 25cd03a commit e72708a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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));
Expand Down Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NavigationOption> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand All @@ -92,11 +92,34 @@ public void onClick(View v) {
}
}

public void startRegisterActivity(Class registerClass) {
private boolean isClassCurrentActivity(Class<? extends Activity> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public void setRegisterCount(long registerCount) {
RegisterCount = registerCount;
}

public boolean hasRegisterCount() {
return this.RegisterCount >= 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer>() {
@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<Integer>() {
@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++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit e72708a

Please sign in to comment.