LibraryFragment.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package eu.kanade.mangafeed.ui.fragment;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.widget.SearchView;
  5. import android.view.ActionMode;
  6. import android.view.LayoutInflater;
  7. import android.view.Menu;
  8. import android.view.MenuInflater;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.GridView;
  13. import java.util.List;
  14. import butterknife.Bind;
  15. import butterknife.ButterKnife;
  16. import butterknife.OnItemClick;
  17. import eu.kanade.mangafeed.R;
  18. import eu.kanade.mangafeed.data.models.Manga;
  19. import eu.kanade.mangafeed.data.services.LibraryUpdateService;
  20. import eu.kanade.mangafeed.presenter.LibraryPresenter;
  21. import eu.kanade.mangafeed.ui.activity.MainActivity;
  22. import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
  23. import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
  24. import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
  25. import nucleus.factory.RequiresPresenter;
  26. @RequiresPresenter(LibraryPresenter.class)
  27. public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
  28. @Bind(R.id.gridView) GridView grid;
  29. private MainActivity activity;
  30. private LibraryAdapter adapter;
  31. public static LibraryFragment newInstance() {
  32. LibraryFragment fragment = new LibraryFragment();
  33. Bundle args = new Bundle();
  34. fragment.setArguments(args);
  35. return fragment;
  36. }
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setHasOptionsMenu(true);
  41. activity = (MainActivity)getActivity();
  42. }
  43. @Override
  44. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  45. Bundle savedInstanceState) {
  46. // Inflate the layout for this fragment
  47. View view = inflater.inflate(R.layout.fragment_library, container, false);
  48. activity.setToolbarTitle(getString(R.string.library_title));
  49. ButterKnife.bind(this, view);
  50. createAdapter();
  51. setMangaLongClickListener();
  52. return view;
  53. }
  54. @Override
  55. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  56. inflater.inflate(R.menu.library, menu);
  57. initializeSearch(menu);
  58. }
  59. @Override
  60. public boolean onOptionsItemSelected(MenuItem item) {
  61. switch (item.getItemId()) {
  62. case R.id.action_refresh:
  63. if (!LibraryUpdateService.isRunning(activity)) {
  64. Intent intent = LibraryUpdateService.getStartIntent(activity);
  65. activity.startService(intent);
  66. }
  67. return true;
  68. }
  69. return super.onOptionsItemSelected(item);
  70. }
  71. private void initializeSearch(Menu menu) {
  72. final SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
  73. sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  74. @Override
  75. public boolean onQueryTextSubmit(String query) {
  76. return true;
  77. }
  78. @Override
  79. public boolean onQueryTextChange(String newText) {
  80. adapter.getFilter().filter(newText);
  81. return true;
  82. }
  83. });
  84. }
  85. private void createAdapter() {
  86. adapter = new LibraryAdapter(getActivity());
  87. grid.setAdapter(adapter);
  88. }
  89. public void onNextMangas(List<Manga> mangas) {
  90. adapter.setNewItems(mangas);
  91. }
  92. @OnItemClick(R.id.gridView)
  93. protected void onMangaClick(int position) {
  94. Intent intent = MangaDetailActivity.newIntent(
  95. getActivity(),
  96. adapter.getItem(position)
  97. );
  98. getActivity().startActivity(intent);
  99. }
  100. private void setMangaLongClickListener() {
  101. grid.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() {
  102. @Override
  103. public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
  104. mode.setTitle(getResources().getString(R.string.library_selection_title)
  105. + ": " + grid.getCheckedItemCount());
  106. }
  107. @Override
  108. public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  109. MenuInflater inflater = mode.getMenuInflater();
  110. inflater.inflate(R.menu.library_selection, menu);
  111. return true;
  112. }
  113. @Override
  114. public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  115. return false;
  116. }
  117. @Override
  118. public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  119. switch (item.getItemId()) {
  120. case R.id.action_delete:
  121. getPresenter().onDelete(grid.getCheckedItemPositions(), adapter);
  122. mode.finish();
  123. return true;
  124. }
  125. return false;
  126. }
  127. @Override
  128. public void onDestroyActionMode(ActionMode mode) {
  129. }
  130. });
  131. }
  132. }