From e567bc753f78ebb872702fa8b4f0f1eaf78838ff Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 26 Sep 2015 18:58:01 +0200 Subject: [PATCH] Update library when changes to the DB are made and open manga detail activity --- .../eu/kanade/mangafeed/AppComponent.java | 3 +- .../mangafeed/presenter/LibraryPresenter.java | 47 +++++++++++++ .../mangafeed/ui/adapter/LibraryAdapter.java | 13 +++- .../ui/fragment/LibraryFragment.java | 69 ++++++++++--------- .../eu/kanade/mangafeed/view/BaseView.java | 7 ++ .../eu/kanade/mangafeed/view/LibraryView.java | 10 +++ app/src/main/res/layout/item_library.xml | 2 +- 7 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/view/BaseView.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java diff --git a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java index 093ad1e993..157208d495 100644 --- a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java @@ -6,6 +6,7 @@ import javax.inject.Singleton; import dagger.Component; import eu.kanade.mangafeed.data.DataModule; +import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.fragment.LibraryFragment; @@ -20,7 +21,7 @@ import eu.kanade.mangafeed.ui.fragment.LibraryFragment; public interface AppComponent { void inject(MainActivity mainActivity); - void inject(LibraryFragment libraryFragment); + void inject(LibraryPresenter libraryPresenter); void inject(MangaDetailActivity mangaDetailActivity); Application application(); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java new file mode 100644 index 0000000000..e7b9ef3f4a --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -0,0 +1,47 @@ +package eu.kanade.mangafeed.presenter; + +import android.content.Intent; +import android.widget.GridView; + +import java.util.ArrayList; + +import javax.inject.Inject; + +import de.greenrobot.event.EventBus; +import eu.kanade.mangafeed.App; +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; +import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; +import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; +import eu.kanade.mangafeed.view.LibraryView; + +import static rx.android.schedulers.AndroidSchedulers.mainThread; + +public class LibraryPresenter { + + private LibraryView mLibraryView; + + @Inject + public DatabaseHelper db; + + public LibraryPresenter(LibraryView libraryView) { + mLibraryView = libraryView; + App.getComponent(libraryView.getActivity()).inject(this); + } + + public void onMangaClick(LibraryAdapter adapter, int position) { + Intent intent = new Intent(mLibraryView.getActivity(), MangaDetailActivity.class); + EventBus.getDefault().postSticky(adapter.getItem(position)); + mLibraryView.getActivity().startActivity(intent); + } + + public void initializeMangas() { + db.manga.get() + .observeOn(mainThread()) + .subscribe( + mangas -> { + mLibraryView.setMangas(new ArrayList<>(mangas)); + } + ); + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java index ef1f53a4a3..edb2b21fa2 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java @@ -11,6 +11,7 @@ import android.widget.TextView; import com.bumptech.glide.Glide; +import java.util.ArrayList; import java.util.List; import butterknife.Bind; @@ -28,9 +29,9 @@ public class LibraryAdapter extends ArrayAdapter { Context context; int layoutResourceId; - List data; + ArrayList data; - public LibraryAdapter(Context context, int layoutResourceId, List data) { + public LibraryAdapter(Context context, int layoutResourceId, ArrayList data) { super(context, layoutResourceId, data); this.context = context; this.layoutResourceId = layoutResourceId; @@ -63,6 +64,12 @@ public class LibraryAdapter extends ArrayAdapter { return row; } + public void setData(ArrayList mangas) { + // Avoid calling dataSetChanged twice + data.clear(); + addAll(mangas); + } + private String getImageUrl() { return "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg"; } @@ -78,4 +85,6 @@ public class LibraryAdapter extends ArrayAdapter { ButterKnife.bind(this, view); } } + + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java index 22579f659a..21b2752cec 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java @@ -1,37 +1,29 @@ package eu.kanade.mangafeed.ui.fragment; import android.app.Fragment; -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.GridView; -import java.util.List; - -import javax.inject.Inject; +import java.util.ArrayList; import butterknife.Bind; import butterknife.ButterKnife; -import de.greenrobot.event.EventBus; -import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.ui.activity.BaseActivity; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; -import rx.functions.Action1; +import eu.kanade.mangafeed.view.LibraryView; -public class LibraryFragment extends Fragment { - @Bind(R.id.gridView) - GridView grid; +public class LibraryFragment extends Fragment implements LibraryView { - @Inject - DatabaseHelper db; - - List mangas; + @Bind(R.id.gridView) GridView grid; + LibraryPresenter mLibraryPresenter; + LibraryAdapter mAdapter; public static LibraryFragment newInstance() { LibraryFragment fragment = new LibraryFragment(); @@ -40,34 +32,45 @@ public class LibraryFragment extends Fragment { return fragment; } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mLibraryPresenter = new LibraryPresenter(this); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_library, container, false); - App.getComponent(getActivity()).inject(this); ((BaseActivity) getActivity()).getSupportActionBar().setTitle(R.string.library_title); ButterKnife.bind(this, view); - db.manga.get().subscribe( - result -> { - mangas = result; - - LibraryAdapter adapter = new LibraryAdapter(getActivity(), - R.layout.item_library, mangas); - - grid.setAdapter(adapter); - grid.setOnItemClickListener( - (parent, v, position, id) -> { - Intent intent = new Intent(".ui.activity.MangaDetailActivity"); - EventBus.getDefault().postSticky(adapter.getItem(position)); - startActivity(intent); - } - ); - } - ); + mLibraryPresenter.initializeMangas(); + setMangaClickListener(); return view; } + public void setMangas(ArrayList mangas) { + if (mAdapter == null) { + mAdapter = new LibraryAdapter( + getActivity(), + R.layout.item_library, + mangas); + grid.setAdapter(mAdapter); + } else { + mAdapter.setData(mangas); + } + + } + + private void setMangaClickListener() { + grid.setOnItemClickListener( + (parent, view, position, id) -> + mLibraryPresenter.onMangaClick(mAdapter, position) + ); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java b/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java new file mode 100644 index 0000000000..019d18786c --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java @@ -0,0 +1,7 @@ +package eu.kanade.mangafeed.view; + +import android.content.Context; + +public interface BaseView { + Context getActivity(); +} diff --git a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java new file mode 100644 index 0000000000..ef83cecfa5 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java @@ -0,0 +1,10 @@ +package eu.kanade.mangafeed.view; + +import java.util.ArrayList; + +import eu.kanade.mangafeed.data.models.Manga; + +public interface LibraryView extends BaseView { + + void setMangas(ArrayList mangas); +} diff --git a/app/src/main/res/layout/item_library.xml b/app/src/main/res/layout/item_library.xml index fb952a45ee..e8535d72f7 100644 --- a/app/src/main/res/layout/item_library.xml +++ b/app/src/main/res/layout/item_library.xml @@ -17,7 +17,7 @@