From d47e4696654a2a6d9de61c8dfd7114ce6cde5474 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 28 Sep 2015 15:02:46 +0200 Subject: [PATCH] Show chapter list --- .../eu/kanade/mangafeed/AppComponent.java | 4 +- .../data/helpers/DatabaseHelper.java | 1 + .../mangafeed/data/helpers/DbOpenHelper.java | 2 + .../data/managers/ChapterManager.java | 24 +++++++- .../mangafeed/data/managers/MangaManager.java | 2 +- .../mangafeed/data/tables/ChaptersTable.java | 13 +++++ .../mangafeed/presenter/LibraryPresenter.java | 15 +++-- .../presenter/MangaDetailPresenter.java | 44 ++++++++++++++ .../mangafeed/ui/activity/BaseActivity.java | 4 ++ .../ui/activity/MangaDetailActivity.java | 58 +++++++++++++------ .../ui/adapter/ChapterListHolder.java | 26 +++++++++ .../ui/fragment/LibraryFragment.java | 20 +++---- .../mangafeed/view/MangaDetailView.java | 12 ++++ app/src/main/res/layout/item_chapter.xml | 15 ++--- 14 files changed, 189 insertions(+), 51 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/view/MangaDetailView.java diff --git a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java index 157208d495..fec255375f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java @@ -7,6 +7,7 @@ import javax.inject.Singleton; import dagger.Component; import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.presenter.LibraryPresenter; +import eu.kanade.mangafeed.presenter.MangaDetailPresenter; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.fragment.LibraryFragment; @@ -20,9 +21,8 @@ import eu.kanade.mangafeed.ui.fragment.LibraryFragment; ) public interface AppComponent { - void inject(MainActivity mainActivity); void inject(LibraryPresenter libraryPresenter); - void inject(MangaDetailActivity mangaDetailActivity); + void inject(MangaDetailPresenter mangaDetailPresenter); Application application(); } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index afa07905ee..1c02f22ab8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -16,6 +16,7 @@ import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteGetResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.managers.MangaManager; +import eu.kanade.mangafeed.data.tables.ChaptersTable; public class DatabaseHelper { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DbOpenHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DbOpenHelper.java index 8354343c45..9eeb40b27e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DbOpenHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DbOpenHelper.java @@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.support.annotation.NonNull; +import eu.kanade.mangafeed.data.tables.ChaptersTable; import eu.kanade.mangafeed.data.tables.MangasTable; public class DbOpenHelper extends SQLiteOpenHelper { @@ -19,6 +20,7 @@ public class DbOpenHelper extends SQLiteOpenHelper { @Override public void onCreate(@NonNull SQLiteDatabase db) { db.execSQL(MangasTable.getCreateTableQuery()); + db.execSQL(ChaptersTable.getCreateTableQuery()); } @Override diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index daf9689a5f..162efa5d98 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -1,6 +1,7 @@ package eu.kanade.mangafeed.data.managers; import com.pushtorefresh.storio.sqlite.StorIOSQLite; +import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.queries.Query; import java.util.List; @@ -10,9 +11,6 @@ import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.tables.ChaptersTable; import rx.Observable; -/** - * Created by len on 26/09/2015. - */ public class ChapterManager extends BaseManager { public ChapterManager(StorIOSQLite db) { @@ -30,4 +28,24 @@ public class ChapterManager extends BaseManager { .prepare() .createObservable(); } + + public Observable insert(Chapter chapter) { + return db.put() + .object(chapter) + .prepare() + .createObservable(); + } + + public void createDummyChapters() { + Chapter c; + + for (int i = 1; i < 100; i++) { + c = new Chapter(); + c.manga_id = 1; + c.name = "Chapter " + i; + c.url = "http://example.com/1"; + insert(c).subscribe(); + } + + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java index 4e5db42b5e..8d9477ad94 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java @@ -44,7 +44,7 @@ public class MangaManager extends BaseManager { m.genre="Action, Drama"; m.status="Ongoing"; m.thumbnail_url="http://example.com/pic.png"; - m.title="Berserk"; + m.title="One Piece"; insert(m).subscribe(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java index 121afb00bf..88eb8c601c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java @@ -27,4 +27,17 @@ public class ChaptersTable { @NonNull public static final String COLUMN_DATE_FETCH = "date_fetch"; + + @NonNull + public static String getCreateTableQuery() { + return "CREATE TABLE " + TABLE + "(" + + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " + + COLUMN_MANGA_ID + " INTEGER NOT NULL, " + + COLUMN_URL + " TEXT NOT NULL, " + + COLUMN_NAME + " TEXT NOT NULL, " + + COLUMN_READ + " BOOLEAN NOT NULL, " + + COLUMN_DATE_FETCH + " LONG NOT NULL, " + + "FOREIGN KEY(" + COLUMN_MANGA_ID + ") REFERENCES " + MangasTable.TABLE + "(" + MangasTable.COLUMN_ID + ")" + + ");"; + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index ca39755897..27065254b9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -4,7 +4,6 @@ import android.content.Intent; 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.data.models.Manga; @@ -16,28 +15,28 @@ import static rx.android.schedulers.AndroidSchedulers.mainThread; public class LibraryPresenter { - private LibraryView mLibraryView; + private LibraryView view; @Inject public DatabaseHelper db; - public LibraryPresenter(LibraryView libraryView) { - mLibraryView = libraryView; - App.getComponent(libraryView.getActivity()).inject(this); + public LibraryPresenter(LibraryView view) { + this.view = view; + App.getComponent(view.getActivity()).inject(this); } public void onMangaClick(EasyAdapter adapter, int position) { Intent intent = MangaDetailActivity.newIntent( - mLibraryView.getActivity(), + view.getActivity(), adapter.getItem(position) ); - mLibraryView.getActivity().startActivity(intent); + view.getActivity().startActivity(intent); } public void initializeMangas() { db.manga.get() .observeOn(mainThread()) - .subscribe(mLibraryView::setMangas); + .subscribe(view::setMangas); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java new file mode 100644 index 0000000000..2d73cf0049 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java @@ -0,0 +1,44 @@ +package eu.kanade.mangafeed.presenter; + +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.data.models.Manga; +import eu.kanade.mangafeed.view.MangaDetailView; + +public class MangaDetailPresenter { + + private MangaDetailView view; + + @Inject + DatabaseHelper db; + + public MangaDetailPresenter(MangaDetailView view) { + this.view = view; + App.getComponent(view.getActivity()).inject(this); + } + + public void onStart() { + EventBus.getDefault().registerSticky(this); + } + + public void onStop() { + EventBus.getDefault().unregister(this); + } + + public void onEventMainThread(Manga manga) { + view.loadManga(manga); + initializeChapters(manga); + } + + public static void newIntent(Manga manga) { + EventBus.getDefault().postSticky(manga); + } + + public void initializeChapters(Manga manga) { + db.chapter.get(manga) + .subscribe(view::setChapters); + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java index 1b014121b0..f652151eab 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java @@ -1,6 +1,7 @@ package eu.kanade.mangafeed.ui.activity; import android.app.FragmentManager; +import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; @@ -39,4 +40,7 @@ public class BaseActivity extends AppCompatActivity { return App.get(this).getComponent(); } + public Context getActivity() { + return this; + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java index a1942556a2..519a9f7619 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java @@ -6,29 +6,40 @@ import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; +import android.widget.ListView; + +import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; -import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.presenter.MangaDetailPresenter; +import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; +import eu.kanade.mangafeed.view.MangaDetailView; +import uk.co.ribot.easyadapter.EasyAdapter; -public class MangaDetailActivity extends BaseActivity { +public class MangaDetailActivity extends BaseActivity implements MangaDetailView { Manga manga; + MangaDetailPresenter presenter; + EasyAdapter adapter; @Bind(R.id.toolbar) Toolbar toolbar; + @Bind(R.id.manga_chapters_list) + ListView list_chapters; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_manga_detail); ButterKnife.bind(this); + presenter = new MangaDetailPresenter(this); setupToolbar(toolbar); - - EventBus.getDefault().registerSticky(this); } @Override @@ -53,26 +64,39 @@ public class MangaDetailActivity extends BaseActivity { return super.onOptionsItemSelected(item); } - @Override - public void onDestroy() { - EventBus.getDefault().unregister(this); - - super.onDestroy(); - } - public static Intent newIntent(Context context, Manga manga) { Intent intent = new Intent(context, MangaDetailActivity.class); - EventBus.getDefault().postSticky(manga); + MangaDetailPresenter.newIntent(manga); return intent; } - public void onEventMainThread(Manga manga) { - this.manga = manga; - loadManga(); - //loadChapters(); + @Override + public void onStart() { + super.onStart(); + presenter.onStart(); } - private void loadManga() { + @Override + public void onStop() { + super.onStop(); + presenter.onStop(); + } + + public void loadManga(Manga manga) { setToolbarTitle(manga.title); } + + public void setChapters(List chapters) { + if (adapter == null) { + adapter = new EasyAdapter( + getActivity(), + ChapterListHolder.class, + chapters + ); + list_chapters.setAdapter(adapter); + } else { + adapter.setItems(chapters); + } + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java new file mode 100644 index 0000000000..ecd42bfd8b --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java @@ -0,0 +1,26 @@ +package eu.kanade.mangafeed.ui.adapter; + +import android.view.View; +import android.widget.TextView; + +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Chapter; +import uk.co.ribot.easyadapter.ItemViewHolder; +import uk.co.ribot.easyadapter.PositionInfo; +import uk.co.ribot.easyadapter.annotations.LayoutId; +import uk.co.ribot.easyadapter.annotations.ViewId; + +@LayoutId(R.layout.item_chapter) +public class ChapterListHolder extends ItemViewHolder { + + @ViewId(R.id.chapter_title) + TextView title; + + public ChapterListHolder(View view) { + super(view); + } + + public void onSetValues(Chapter chapter, PositionInfo positionInfo) { + title.setText(chapter.name); + } +} 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 60a5d7b4f4..a614fffbce 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 @@ -7,7 +7,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.GridView; -import java.util.ArrayList; import java.util.List; import butterknife.Bind; @@ -15,7 +14,6 @@ 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.BaseActivity; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder; import eu.kanade.mangafeed.view.LibraryView; @@ -25,8 +23,8 @@ import uk.co.ribot.easyadapter.EasyAdapter; public class LibraryFragment extends Fragment implements LibraryView { @Bind(R.id.gridView) GridView grid; - LibraryPresenter mLibraryPresenter; - EasyAdapter mEasyAdapter; + LibraryPresenter presenter; + EasyAdapter adapter; public static LibraryFragment newInstance() { LibraryFragment fragment = new LibraryFragment(); @@ -39,7 +37,7 @@ public class LibraryFragment extends Fragment implements LibraryView { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mLibraryPresenter = new LibraryPresenter(this); + presenter = new LibraryPresenter(this); } @Override @@ -50,22 +48,22 @@ public class LibraryFragment extends Fragment implements LibraryView { ((MainActivity)getActivity()).setToolbarTitle(getString(R.string.library_title)); ButterKnife.bind(this, view); - mLibraryPresenter.initializeMangas(); + presenter.initializeMangas(); setMangaClickListener(); return view; } public void setMangas(List mangas) { - if (mEasyAdapter == null) { - mEasyAdapter = new EasyAdapter( + if (adapter == null) { + adapter = new EasyAdapter( getActivity(), MangaLibraryHolder.class, mangas ); - grid.setAdapter(mEasyAdapter); + grid.setAdapter(adapter); } else { - mEasyAdapter.setItems(mangas); + adapter.setItems(mangas); } } @@ -73,7 +71,7 @@ public class LibraryFragment extends Fragment implements LibraryView { private void setMangaClickListener() { grid.setOnItemClickListener( (parent, view, position, id) -> - mLibraryPresenter.onMangaClick(mEasyAdapter, position) + presenter.onMangaClick(adapter, position) ); } diff --git a/app/src/main/java/eu/kanade/mangafeed/view/MangaDetailView.java b/app/src/main/java/eu/kanade/mangafeed/view/MangaDetailView.java new file mode 100644 index 0000000000..f016a03f4b --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/view/MangaDetailView.java @@ -0,0 +1,12 @@ +package eu.kanade.mangafeed.view; + +import java.util.List; + +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.Manga; + +public interface MangaDetailView extends BaseView { + + void loadManga(Manga manga); + void setChapters(List chapters); +} diff --git a/app/src/main/res/layout/item_chapter.xml b/app/src/main/res/layout/item_chapter.xml index 74e72cb488..9c33e3f703 100644 --- a/app/src/main/res/layout/item_chapter.xml +++ b/app/src/main/res/layout/item_chapter.xml @@ -3,21 +3,18 @@ xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" - android:layout_height="wrap_content" - > - + android:layout_height="40dp"> + android:id="@+id/chapter_selection"/> + tools:src="@mipmap/ic_launcher"/> + android:textSize="16sp"/> + android:textSize="12sp"/> @@ -56,5 +53,5 @@ android:layout_gravity="bottom" android:layout_marginBottom="2dp" android:textSize="12sp" - android:paddingRight="5dp" /> + android:paddingRight="5dp"/> \ No newline at end of file