LibraryFragment.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package eu.kanade.mangafeed.ui.library;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.design.widget.AppBarLayout;
  6. import android.support.design.widget.TabLayout;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v4.view.ViewPager;
  9. import android.support.v7.view.ActionMode;
  10. import android.util.Pair;
  11. import android.view.LayoutInflater;
  12. import android.view.Menu;
  13. import android.view.MenuInflater;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import com.afollestad.materialdialogs.MaterialDialog;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import butterknife.Bind;
  22. import butterknife.ButterKnife;
  23. import de.greenrobot.event.EventBus;
  24. import eu.davidea.flexibleadapter.FlexibleAdapter;
  25. import eu.kanade.mangafeed.R;
  26. import eu.kanade.mangafeed.data.database.models.Category;
  27. import eu.kanade.mangafeed.data.database.models.Manga;
  28. import eu.kanade.mangafeed.data.sync.LibraryUpdateService;
  29. import eu.kanade.mangafeed.event.LibraryMangasEvent;
  30. import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
  31. import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
  32. import eu.kanade.mangafeed.ui.library.category.CategoryFragment;
  33. import eu.kanade.mangafeed.ui.main.MainActivity;
  34. import nucleus.factory.RequiresPresenter;
  35. @RequiresPresenter(LibraryPresenter.class)
  36. public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
  37. implements ActionMode.Callback {
  38. TabLayout tabs;
  39. AppBarLayout appBar;
  40. @Bind(R.id.view_pager) ViewPager viewPager;
  41. protected LibraryAdapter adapter;
  42. private ActionMode actionMode;
  43. public static LibraryFragment newInstance() {
  44. return new LibraryFragment();
  45. }
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setHasOptionsMenu(true);
  50. }
  51. @Override
  52. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  53. Bundle savedInstanceState) {
  54. // Inflate the layout for this fragment
  55. View view = inflater.inflate(R.layout.fragment_library, container, false);
  56. setToolbarTitle(getString(R.string.label_library));
  57. ButterKnife.bind(this, view);
  58. appBar = ((MainActivity) getActivity()).getAppBar();
  59. tabs = (TabLayout) inflater.inflate(R.layout.tab_layout, appBar, false);
  60. appBar.addView(tabs);
  61. adapter = new LibraryAdapter(getChildFragmentManager());
  62. viewPager.setAdapter(adapter);
  63. return view;
  64. }
  65. @Override
  66. public void onDestroyView() {
  67. appBar.removeView(tabs);
  68. super.onDestroyView();
  69. }
  70. @Override
  71. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  72. inflater.inflate(R.menu.library, menu);
  73. }
  74. @Override
  75. public boolean onOptionsItemSelected(MenuItem item) {
  76. switch (item.getItemId()) {
  77. case R.id.action_refresh:
  78. if (!LibraryUpdateService.isRunning(getActivity())) {
  79. Intent intent = LibraryUpdateService.getStartIntent(getActivity());
  80. getActivity().startService(intent);
  81. }
  82. return true;
  83. case R.id.action_edit_categories:
  84. onEditCategories();
  85. return true;
  86. }
  87. return super.onOptionsItemSelected(item);
  88. }
  89. private void onEditCategories() {
  90. Fragment fragment = CategoryFragment.newInstance();
  91. ((MainActivity) getActivity()).pushFragment(fragment);
  92. }
  93. public void onNextLibraryUpdate(Pair<List<Category>, Map<Integer, List<Manga>>> pair) {
  94. boolean mangasInDefaultCategory = pair.second.get(0) != null;
  95. boolean initialized = adapter.categories != null;
  96. // If there are mangas in the default category and the adapter doesn't have it,
  97. // create the default category
  98. if (mangasInDefaultCategory && (!initialized || !adapter.hasDefaultCategory())) {
  99. setCategoriesWithDefault(pair.first);
  100. }
  101. // If there aren't mangas in the default category and the adapter have it,
  102. // remove the default category
  103. else if (!mangasInDefaultCategory && (!initialized || adapter.hasDefaultCategory())) {
  104. setCategories(pair.first);
  105. }
  106. // Send the mangas to child fragments after the adapter is updated
  107. EventBus.getDefault().postSticky(new LibraryMangasEvent(pair.second));
  108. }
  109. private void setCategoriesWithDefault(List<Category> categories) {
  110. List<Category> categoriesWithDefault = new ArrayList<>();
  111. categoriesWithDefault.add(Category.createDefault());
  112. categoriesWithDefault.addAll(categories);
  113. setCategories(categoriesWithDefault);
  114. }
  115. private void setCategories(List<Category> categories) {
  116. adapter.setCategories(categories);
  117. tabs.setupWithViewPager(viewPager);
  118. tabs.setVisibility(categories.size() == 1 ? View.GONE : View.VISIBLE);
  119. }
  120. public void setContextTitle(int count) {
  121. actionMode.setTitle(getString(R.string.label_selected, count));
  122. }
  123. @Override
  124. public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  125. mode.getMenuInflater().inflate(R.menu.library_selection, menu);
  126. adapter.setSelectionMode(FlexibleAdapter.MODE_MULTI);
  127. return true;
  128. }
  129. @Override
  130. public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  131. return false;
  132. }
  133. @Override
  134. public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  135. switch (item.getItemId()) {
  136. case R.id.action_move_to_category:
  137. moveMangasToCategories(getPresenter().selectedMangas);
  138. return true;
  139. case R.id.action_delete:
  140. getPresenter().deleteMangas();
  141. destroyActionModeIfNeeded();
  142. return true;
  143. }
  144. return false;
  145. }
  146. @Override
  147. public void onDestroyActionMode(ActionMode mode) {
  148. adapter.setSelectionMode(FlexibleAdapter.MODE_SINGLE);
  149. getPresenter().selectedMangas.clear();
  150. actionMode = null;
  151. }
  152. public void destroyActionModeIfNeeded() {
  153. if (actionMode != null) {
  154. actionMode.finish();
  155. }
  156. }
  157. private void moveMangasToCategories(List<Manga> mangas) {
  158. new MaterialDialog.Builder(getActivity())
  159. .title(R.string.action_move_category)
  160. .items(getPresenter().getCategoriesNames())
  161. .itemsCallbackMultiChoice(null, (dialog, which, text) -> {
  162. getPresenter().moveMangasToCategories(which, mangas);
  163. destroyActionModeIfNeeded();
  164. return true;
  165. })
  166. .positiveText(R.string.button_ok)
  167. .negativeText(R.string.button_cancel)
  168. .show();
  169. }
  170. @Nullable
  171. public ActionMode getActionMode() {
  172. return actionMode;
  173. }
  174. public LibraryAdapter getAdapter() {
  175. return adapter;
  176. }
  177. public void createActionModeIfNeeded() {
  178. if (actionMode == null) {
  179. actionMode = ((BaseActivity) getActivity()).startSupportActionMode(this);
  180. }
  181. }
  182. public void invalidateActionMode() {
  183. actionMode.invalidate();
  184. }
  185. }