From 384bc3c690e20517b1d17400144bff52bead5dff Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 30 Dec 2015 14:10:31 +0100 Subject: [PATCH] Category improvements --- app/src/main/AndroidManifest.xml | 5 ++ .../ui/base/fab/ScrollAwareFABBehavior.java | 51 +++++++++++++++++++ .../ui/base/fragment/BaseFragment.java | 14 +++++ .../ui/catalogue/CatalogueFragment.java | 8 --- .../ui/library/LibraryCategoryFragment.java | 3 -- .../mangafeed/ui/library/LibraryFragment.java | 38 +++++++++----- .../ui/library/LibraryPresenter.java | 2 +- ...oryFragment.java => CategoryActivity.java} | 35 +++++++------ .../ui/library/category/CategoryAdapter.java | 12 ++--- .../library/category/CategoryPresenter.java | 4 +- .../ui/manga/chapter/ChaptersFragment.java | 3 +- ...ories.xml => activity_edit_categories.xml} | 16 +++--- app/src/main/res/layout/activity_main.xml | 8 +-- .../main/res/layout/library_tab_layout.xml | 14 +++++ app/src/main/res/layout/toolbar.xml | 1 + app/src/main/res/values/strings.xml | 1 + 16 files changed, 153 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/base/fab/ScrollAwareFABBehavior.java rename app/src/main/java/eu/kanade/mangafeed/ui/library/category/{CategoryFragment.java => CategoryActivity.java} (84%) rename app/src/main/res/layout/{fragment_edit_categories.xml => activity_edit_categories.xml} (60%) create mode 100644 app/src/main/res/layout/library_tab_layout.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 65b2a9f11..39d33885e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,6 +38,11 @@ android:label="@string/label_settings" android:parentActivityName=".ui.main.MainActivity" > + + 0 && child.getVisibility() == View.VISIBLE) { + // User scrolled down and the FAB is currently visible -> hide the FAB + child.hide(); + } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { + // User scrolled up and the FAB is currently not visible -> show the FAB + child.show(); + } + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java index c6f070cb9..352682dcf 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/base/fragment/BaseFragment.java @@ -1,12 +1,26 @@ package eu.kanade.mangafeed.ui.base.fragment; +import android.os.Bundle; import android.support.v4.app.Fragment; import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.ui.base.activity.BaseActivity; +import icepick.Icepick; public class BaseFragment extends Fragment { + @Override + public void onCreate(Bundle savedState) { + super.onCreate(savedState); + Icepick.restoreInstanceState(this, savedState); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + Icepick.saveInstanceState(this, outState); + } + public void setToolbarTitle(String title) { getBaseActivity().setToolbarTitle(title); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java index b07359b23..f9c56be26 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java @@ -33,7 +33,6 @@ import eu.kanade.mangafeed.ui.main.MainActivity; import eu.kanade.mangafeed.ui.manga.MangaActivity; import eu.kanade.mangafeed.util.ToastUtil; import eu.kanade.mangafeed.widget.EndlessScrollListener; -import icepick.Icepick; import icepick.State; import nucleus.factory.RequiresPresenter; import rx.Subscription; @@ -66,7 +65,6 @@ public class CatalogueFragment extends BaseRxFragment { @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); - Icepick.restoreInstanceState(this, savedState); setHasOptionsMenu(true); } @@ -168,12 +166,6 @@ public class CatalogueFragment extends BaseRxFragment { super.onDestroyView(); } - @Override - public void onSaveInstanceState(Bundle outState) { - Icepick.saveInstanceState(this, outState); - super.onSaveInstanceState(outState); - } - private void initializeSearchSubscription() { queryDebouncerSubject = PublishSubject.create(); queryDebouncerSubscription = queryDebouncerSubject diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java index e5cd2c2f4..6aa0b8968 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java @@ -24,7 +24,6 @@ import eu.kanade.mangafeed.ui.base.fragment.BaseFragment; import eu.kanade.mangafeed.ui.manga.MangaActivity; import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.widget.AutofitRecyclerView; -import icepick.Icepick; import icepick.State; import rx.Subscription; @@ -49,7 +48,6 @@ public class LibraryCategoryFragment extends BaseFragment // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_library_category, container, false); ButterKnife.bind(this, view); - Icepick.restoreInstanceState(this, savedState); adapter = new LibraryCategoryAdapter(this); recycler.setHasFixedSize(true); @@ -98,7 +96,6 @@ public class LibraryCategoryFragment extends BaseFragment @Override public void onSaveInstanceState(Bundle outState) { - Icepick.saveInstanceState(this, outState); adapter.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java index c64f7d386..97cb2ddd0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java @@ -5,7 +5,6 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.AppBarLayout; import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.view.ActionMode; import android.util.Pair; @@ -31,24 +30,26 @@ import eu.kanade.mangafeed.data.database.models.Category; import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.sync.LibraryUpdateService; import eu.kanade.mangafeed.event.LibraryMangasEvent; -import eu.kanade.mangafeed.ui.base.activity.BaseActivity; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; -import eu.kanade.mangafeed.ui.library.category.CategoryFragment; +import eu.kanade.mangafeed.ui.library.category.CategoryActivity; import eu.kanade.mangafeed.ui.main.MainActivity; +import icepick.State; import nucleus.factory.RequiresPresenter; @RequiresPresenter(LibraryPresenter.class) public class LibraryFragment extends BaseRxFragment implements ActionMode.Callback { - TabLayout tabs; - AppBarLayout appBar; - @Bind(R.id.view_pager) ViewPager viewPager; + private TabLayout tabs; + private AppBarLayout appBar; + protected LibraryAdapter adapter; private ActionMode actionMode; + @State int activeCategory; + public static LibraryFragment newInstance() { return new LibraryFragment(); } @@ -68,12 +69,12 @@ public class LibraryFragment extends BaseRxFragment ButterKnife.bind(this, view); appBar = ((MainActivity) getActivity()).getAppBar(); - tabs = (TabLayout) inflater.inflate(R.layout.tab_layout, appBar, false); + tabs = (TabLayout) inflater.inflate(R.layout.library_tab_layout, appBar, false); appBar.addView(tabs); - adapter = new LibraryAdapter(getChildFragmentManager()); viewPager.setAdapter(adapter); + tabs.setupWithViewPager(viewPager); return view; } @@ -81,9 +82,16 @@ public class LibraryFragment extends BaseRxFragment @Override public void onDestroyView() { appBar.removeView(tabs); + EventBus.getDefault().removeStickyEvent(LibraryMangasEvent.class); super.onDestroyView(); } + @Override + public void onSaveInstanceState(Bundle bundle) { + activeCategory = viewPager.getCurrentItem(); + super.onSaveInstanceState(bundle); + } + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.library, menu); @@ -107,8 +115,8 @@ public class LibraryFragment extends BaseRxFragment } private void onEditCategories() { - Fragment fragment = CategoryFragment.newInstance(); - ((MainActivity) getActivity()).pushFragment(fragment); + Intent intent = CategoryActivity.newIntent(getActivity()); + startActivity(intent); } public void onNextLibraryUpdate(Pair, Map>> pair) { @@ -125,6 +133,10 @@ public class LibraryFragment extends BaseRxFragment else if (!mangasInDefaultCategory && (!initialized || adapter.hasDefaultCategory())) { setCategories(pair.first); } + // Restore active category + if (!initialized) { + viewPager.setCurrentItem(activeCategory, false); + } // Send the mangas to child fragments after the adapter is updated EventBus.getDefault().postSticky(new LibraryMangasEvent(pair.second)); } @@ -139,8 +151,8 @@ public class LibraryFragment extends BaseRxFragment private void setCategories(List categories) { adapter.setCategories(categories); - tabs.setupWithViewPager(viewPager); - tabs.setVisibility(categories.size() == 1 ? View.GONE : View.VISIBLE); + tabs.setTabsFromPagerAdapter(adapter); + tabs.setVisibility(categories.size() <= 1 ? View.GONE : View.VISIBLE); } public void setContextTitle(int count) { @@ -211,7 +223,7 @@ public class LibraryFragment extends BaseRxFragment public void createActionModeIfNeeded() { if (actionMode == null) { - actionMode = ((BaseActivity) getActivity()).startSupportActionMode(this); + actionMode = getBaseActivity().startSupportActionMode(this); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java index cb67d8cda..423a12ac4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java @@ -59,7 +59,7 @@ public class LibraryPresenter extends BasePresenter { .observeOn(AndroidSchedulers.mainThread()); } - public Observable> getCategoriesObservable() { + private Observable> getCategoriesObservable() { return db.getCategories().createObservable() .doOnNext(categories -> this.categories = categories); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryActivity.java similarity index 84% rename from app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryFragment.java rename to app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryActivity.java index d46aa7966..92146d524 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryActivity.java @@ -1,17 +1,17 @@ package eu.kanade.mangafeed.ui.library.category; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.view.ActionMode; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; import android.support.v7.widget.helper.ItemTouchHelper; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; import com.afollestad.materialdialogs.MaterialDialog; @@ -21,19 +21,19 @@ import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.database.models.Category; -import eu.kanade.mangafeed.ui.base.activity.BaseActivity; +import eu.kanade.mangafeed.ui.base.activity.BaseRxActivity; import eu.kanade.mangafeed.ui.base.adapter.FlexibleViewHolder; import eu.kanade.mangafeed.ui.base.adapter.OnStartDragListener; -import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration; import eu.kanade.mangafeed.ui.library.LibraryCategoryAdapter; import nucleus.factory.RequiresPresenter; import rx.Observable; @RequiresPresenter(CategoryPresenter.class) -public class CategoryFragment extends BaseRxFragment implements +public class CategoryActivity extends BaseRxActivity implements ActionMode.Callback, FlexibleViewHolder.OnListItemClickListener, OnStartDragListener { + @Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.categories_list) RecyclerView recycler; @Bind(R.id.fab) FloatingActionButton fab; @@ -41,19 +41,20 @@ public class CategoryFragment extends BaseRxFragment implemen private ActionMode actionMode; private ItemTouchHelper touchHelper; - public static CategoryFragment newInstance() { - return new CategoryFragment(); + public static Intent newIntent(Context context) { + return new Intent(context, CategoryActivity.class); } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { - View view = inflater.inflate(R.layout.fragment_edit_categories, container, false); - ButterKnife.bind(this, view); + protected void onCreate(Bundle savedState) { + super.onCreate(savedState); + setContentView(R.layout.activity_edit_categories); + ButterKnife.bind(this); - setToolbarTitle(R.string.action_edit_categories); + setupToolbar(toolbar); adapter = new CategoryAdapter(this); - recycler.setLayoutManager(new LinearLayoutManager(getActivity())); + recycler.setLayoutManager(new LinearLayoutManager(this)); recycler.setHasFixedSize(true); recycler.setAdapter(adapter); recycler.addItemDecoration(new DividerItemDecoration( @@ -64,15 +65,13 @@ public class CategoryFragment extends BaseRxFragment implemen touchHelper.attachToRecyclerView(recycler); fab.setOnClickListener(v -> { - new MaterialDialog.Builder(getActivity()) + new MaterialDialog.Builder(this) .title(R.string.action_add_category) .input(R.string.name, 0, false, (dialog, input) -> { getPresenter().createCategory(input.toString()); }) .show(); }); - - return view; } public void setCategories(List categories) { @@ -99,7 +98,7 @@ public class CategoryFragment extends BaseRxFragment implemen @Override public void onListItemLongClick(int position) { if (actionMode == null) - actionMode = ((BaseActivity) getActivity()).startSupportActionMode(this); + actionMode = startSupportActionMode(this); toggleSelection(position); } @@ -165,7 +164,7 @@ public class CategoryFragment extends BaseRxFragment implemen } private void editCategory(Category category) { - new MaterialDialog.Builder(getActivity()) + new MaterialDialog.Builder(this) .title(R.string.action_rename_category) .input(getString(R.string.name), category.name, false, (dialog, input) -> { getPresenter().renameCategory(category, input.toString()); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryAdapter.java index e7daa28de..04c43136c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryAdapter.java @@ -18,11 +18,11 @@ import eu.kanade.mangafeed.ui.base.adapter.ItemTouchHelperAdapter; public class CategoryAdapter extends FlexibleAdapter implements ItemTouchHelperAdapter { - private final CategoryFragment fragment; + private final CategoryActivity activity; private final ColorGenerator generator; - public CategoryAdapter(CategoryFragment fragment) { - this.fragment = fragment; + public CategoryAdapter(CategoryActivity activity) { + this.activity = activity; generator = ColorGenerator.DEFAULT; setHasStableIds(true); } @@ -44,9 +44,9 @@ public class CategoryAdapter extends FlexibleAdapter i @Override public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) { - LayoutInflater inflater = LayoutInflater.from(fragment.getActivity()); + LayoutInflater inflater = activity.getLayoutInflater(); View v = inflater.inflate(R.layout.item_edit_categories, parent, false); - return new CategoryHolder(v, this, fragment, fragment); + return new CategoryHolder(v, this, activity, activity); } @Override @@ -70,7 +70,7 @@ public class CategoryAdapter extends FlexibleAdapter i } } - fragment.getPresenter().reorderCategories(mItems); + activity.getPresenter().reorderCategories(mItems); } @Override diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryPresenter.java index 930f530a1..5e4b1add6 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/category/CategoryPresenter.java @@ -11,7 +11,7 @@ import eu.kanade.mangafeed.data.database.models.Category; import eu.kanade.mangafeed.ui.base.presenter.BasePresenter; import rx.android.schedulers.AndroidSchedulers; -public class CategoryPresenter extends BasePresenter { +public class CategoryPresenter extends BasePresenter { @Inject DatabaseHelper db; @@ -27,7 +27,7 @@ public class CategoryPresenter extends BasePresenter { () -> db.getCategories().createObservable() .doOnNext(categories -> this.categories = categories) .observeOn(AndroidSchedulers.mainThread()), - CategoryFragment::setCategories); + CategoryActivity::setCategories); start(GET_CATEGORIES); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java index 4c09e7cf0..4abbfc2bc 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java @@ -27,7 +27,6 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.download.DownloadService; import eu.kanade.mangafeed.data.download.model.Download; -import eu.kanade.mangafeed.ui.base.activity.BaseActivity; import eu.kanade.mangafeed.ui.base.adapter.FlexibleViewHolder; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration; @@ -329,7 +328,7 @@ public class ChaptersFragment extends BaseRxFragment implemen @Override public void onListItemLongClick(int position) { if (actionMode == null) - actionMode = ((BaseActivity) getActivity()).startSupportActionMode(this); + actionMode = getBaseActivity().startSupportActionMode(this); toggleSelection(position); } diff --git a/app/src/main/res/layout/fragment_edit_categories.xml b/app/src/main/res/layout/activity_edit_categories.xml similarity index 60% rename from app/src/main/res/layout/fragment_edit_categories.xml rename to app/src/main/res/layout/activity_edit_categories.xml index 1314b9c08..126a86e08 100644 --- a/app/src/main/res/layout/fragment_edit_categories.xml +++ b/app/src/main/res/layout/activity_edit_categories.xml @@ -2,13 +2,16 @@ + android:layout_height="match_parent" + android:gravity="center"> + + @@ -17,10 +20,11 @@ android:id="@+id/fab" android:layout_height="wrap_content" android:layout_width="wrap_content" - android:layout_gravity="end|bottom" + android:layout_gravity="bottom|right" android:layout_margin="@dimen/fab_margin" - android:clickable="true" android:src="@drawable/ic_action_add_18dp" - app:borderWidth="0dp"/> + app:layout_anchor="@id/categories_list" + app:layout_anchorGravity="bottom|right|end" + app:layout_behavior="eu.kanade.mangafeed.ui.base.fab.ScrollAwareFABBehavior"/> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c0cddd69b..e5b468cde 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,4 +1,6 @@ - @@ -18,7 +20,7 @@ android:id="@+id/drawer_container" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_below="@id/appbar"> + app:layout_behavior="@string/appbar_scrolling_view_behavior"> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/library_tab_layout.xml b/app/src/main/res/layout/library_tab_layout.xml new file mode 100644 index 000000000..58fe32889 --- /dev/null +++ b/app/src/main/res/layout/library_tab_layout.xml @@ -0,0 +1,14 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/toolbar.xml b/app/src/main/res/layout/toolbar.xml index b440214ad..abc900b71 100644 --- a/app/src/main/res/layout/toolbar.xml +++ b/app/src/main/res/layout/toolbar.xml @@ -7,4 +7,5 @@ android:background="@color/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppTheme.Dark" + app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.Popup" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c87e24bd2..fd94cfe23 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ My library Recent updates Catalogues + Categories Selected: %1$d