From 1f244eae25312e9ee0e0300cc2a9f51db02bdc47 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 17 Mar 2020 09:11:36 -0500 Subject: [PATCH] Animate expand/collapse in nav drawer --- .../adapter/NavigationDrawerAdapter.java | 38 ++++++++++--------- .../tasks/ui/NavigationDrawerFragment.java | 21 ++++------ 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java index 7b02b9196..87cc5b316 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java +++ b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java @@ -7,7 +7,6 @@ package com.todoroo.astrid.adapter; import static com.google.common.base.Objects.equal; -import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread; import static com.todoroo.astrid.api.FilterListItem.Type.ITEM; import static com.todoroo.astrid.api.FilterListItem.Type.SUBHEADER; @@ -18,15 +17,15 @@ import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.recyclerview.widget.AsyncListDiffer; import androidx.recyclerview.widget.DiffUtil.ItemCallback; -import androidx.recyclerview.widget.ListAdapter; +import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.ViewHolder; import com.todoroo.astrid.adapter.FilterViewHolder.OnClick; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.FilterListItem; import com.todoroo.astrid.api.FilterListItem.Type; -import java.util.HashMap; -import java.util.Map; +import java.util.List; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.billing.Inventory; @@ -39,7 +38,7 @@ import org.tasks.themes.ColorProvider; import org.tasks.themes.Theme; import org.tasks.themes.ThemeAccent; -public class NavigationDrawerAdapter extends ListAdapter { +public class NavigationDrawerAdapter extends RecyclerView.Adapter { private static final String TOKEN_SELECTED = "token_selected"; private final Activity activity; @@ -54,7 +53,8 @@ public class NavigationDrawerAdapter extends ListAdapter counts = new HashMap<>(); + + private AsyncListDiffer differ; @Inject public NavigationDrawerAdapter( @@ -67,7 +67,6 @@ public class NavigationDrawerAdapter extends ListAdapter(this, new DiffCallback()); } public void setOnClick(OnClick onClick) { @@ -92,17 +93,16 @@ public class NavigationDrawerAdapter extends ListAdapter counts) { - assertMainThread(); - this.counts = counts; - notifyDataSetChanged(); - } - @Override public long getItemId(int position) { return position; } + @Override + public int getItemCount() { + return differ.getCurrentList().size(); + } + public Filter getSelected() { return selected; } @@ -137,8 +137,7 @@ public class NavigationDrawerAdapter extends ListAdapter= 0 ? item.count : counts.get(item)); + ((FilterViewHolder) holder).bind(item, item.equals(selected), Math.max(item.count, 0)); } else if (type == SUBHEADER) { ((SubheaderViewHolder) holder).bind((NavigationDrawerSubheader) item); } @@ -149,9 +148,12 @@ public class NavigationDrawerAdapter extends ListAdapter filterListItems) { + differ.submitList(filterListItems); } private static class DiffCallback extends ItemCallback { diff --git a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.java b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.java index 8f18c90eb..cf62c1529 100644 --- a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.java +++ b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.java @@ -1,6 +1,5 @@ package org.tasks.ui; -import static com.google.common.collect.Iterables.filter; import static com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastLollipop; import static com.todoroo.andlib.utility.AndroidUtilities.preLollipop; @@ -34,9 +33,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.R; @@ -215,23 +212,21 @@ public class NavigationDrawerFragment extends InjectingFragment { private Disposable updateFilters() { return Single.fromCallable(() -> filterProvider.getItems(true)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnSuccess(adapter::submitList) - .observeOn(Schedulers.io()) .map(this::refreshFilterCount) + .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(adapter::setCounts); + .subscribe(adapter::submitList); } - private Map refreshFilterCount(List items) { + private List refreshFilterCount(List items) { assertNotMainThread(); - Map result = new HashMap<>(); - for (FilterListItem item : filter(items, i -> i instanceof Filter && i.count == -1)) { - result.put((Filter) item, taskDao.count((Filter) item)); + for (FilterListItem item : items) { + if (item instanceof Filter && item.count == -1) { + item.count = taskDao.count((Filter) item); + } } - return result; + return items; } private class RefreshReceiver extends BroadcastReceiver {