Create adapter in presenter

This commit is contained in:
inorichi 2015-10-01 00:11:19 +02:00
parent d7bb4a6a2a
commit db49c3ee88
4 changed files with 18 additions and 52 deletions

View file

@ -9,14 +9,8 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import eu.kanade.mangafeed.view.LibraryView;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import timber.log.Timber;
import uk.co.ribot.easyadapter.EasyAdapter;
import static rx.android.schedulers.AndroidSchedulers.mainThread;
@ -24,11 +18,10 @@ public class LibraryPresenter extends BasePresenter {
private LibraryView view;
@Inject
DatabaseHelper db;
@Inject DatabaseHelper db;
@Inject PreferencesHelper prefs;
@Inject
PreferencesHelper prefs;
LibraryAdapter<Manga> adapter;
public LibraryPresenter(LibraryView view) {
this.view = view;
@ -43,7 +36,7 @@ public class LibraryPresenter extends BasePresenter {
}
public void onMangaClick(EasyAdapter<Manga> adapter, int position) {
public void onMangaClick(int position) {
Intent intent = MangaDetailActivity.newIntent(
view.getActivity(),
adapter.getItem(position)
@ -52,13 +45,17 @@ public class LibraryPresenter extends BasePresenter {
}
public void initializeMangas() {
db.manga.get()
.observeOn(mainThread())
.subscribe(view::setMangas);
.subscribe(mangas -> {
adapter = new LibraryAdapter<>(view.getActivity(), mangas);
view.setAdapter(adapter);
});
}
public void onQueryTextChange(String query) {
view.getAdapter().getFilter().filter(query);
adapter.getFilter().filter(query);
}
}

View file

@ -32,7 +32,7 @@ public class LibraryAdapter<T> extends EasyAdapter<T> implements Filterable {
FilterResults results = new FilterResults();
String query = charSequence.toString().toLowerCase();
if (query == null || query.length() == 0) {
if (query.length() == 0) {
results.values = mangas;
results.count = mangas.size();
} else {

View file

@ -9,18 +9,12 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter;
import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder;
import eu.kanade.mangafeed.view.LibraryView;
import timber.log.Timber;
import uk.co.ribot.easyadapter.EasyAdapter;
@ -28,7 +22,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView {
@Bind(R.id.gridView) GridView grid;
LibraryPresenter presenter;
CatalogueArrayAdapter<Manga> adapter;
MainActivity activity;
public static LibraryFragment newInstance() {
@ -62,7 +55,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView {
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupToolbar();
setMangaClickListener();
presenter.initializeMangas();
}
@ -89,35 +81,17 @@ public class LibraryFragment extends BaseFragment implements LibraryView {
});
}
// LibraryView
public void setMangas(List<Manga> mangas) {
if (adapter == null) {
adapter = new CatalogueArrayAdapter<>(
getActivity(),
MangaLibraryHolder.class,
mangas
);
grid.setAdapter(adapter);
} else {
adapter.setItems(mangas);
}
}
private void setMangaClickListener() {
grid.setOnItemClickListener(
(parent, view, position, id) ->
presenter.onMangaClick(adapter, position)
presenter.onMangaClick(position)
);
}
private void setupToolbar() {
//activity.getSupportActionBar().
}
// LibraryView
public CatalogueArrayAdapter getAdapter() {
return adapter;
public void setAdapter(EasyAdapter adapter) {
grid.setAdapter(adapter);
}
}

View file

@ -1,13 +1,8 @@
package eu.kanade.mangafeed.view;
import java.util.ArrayList;
import java.util.List;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter;
import uk.co.ribot.easyadapter.EasyAdapter;
public interface LibraryView extends BaseView {
void setMangas(List<Manga> mangas);
CatalogueArrayAdapter getAdapter();
void setAdapter(EasyAdapter mangas);
}