Show chapter list

This commit is contained in:
inorichi 2015-09-28 15:02:46 +02:00
parent d3b6b8721e
commit d47e469665
14 changed files with 189 additions and 51 deletions

View file

@ -7,6 +7,7 @@ import javax.inject.Singleton;
import dagger.Component; import dagger.Component;
import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.data.DataModule;
import eu.kanade.mangafeed.presenter.LibraryPresenter; 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.MainActivity;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.fragment.LibraryFragment; import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
@ -20,9 +21,8 @@ import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
) )
public interface AppComponent { public interface AppComponent {
void inject(MainActivity mainActivity);
void inject(LibraryPresenter libraryPresenter); void inject(LibraryPresenter libraryPresenter);
void inject(MangaDetailActivity mangaDetailActivity); void inject(MangaDetailPresenter mangaDetailPresenter);
Application application(); Application application();
} }

View file

@ -16,6 +16,7 @@ import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver;
import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteGetResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteGetResolver;
import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver;
import eu.kanade.mangafeed.data.managers.MangaManager; import eu.kanade.mangafeed.data.managers.MangaManager;
import eu.kanade.mangafeed.data.tables.ChaptersTable;
public class DatabaseHelper { public class DatabaseHelper {

View file

@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import eu.kanade.mangafeed.data.tables.ChaptersTable;
import eu.kanade.mangafeed.data.tables.MangasTable; import eu.kanade.mangafeed.data.tables.MangasTable;
public class DbOpenHelper extends SQLiteOpenHelper { public class DbOpenHelper extends SQLiteOpenHelper {
@ -19,6 +20,7 @@ public class DbOpenHelper extends SQLiteOpenHelper {
@Override @Override
public void onCreate(@NonNull SQLiteDatabase db) { public void onCreate(@NonNull SQLiteDatabase db) {
db.execSQL(MangasTable.getCreateTableQuery()); db.execSQL(MangasTable.getCreateTableQuery());
db.execSQL(ChaptersTable.getCreateTableQuery());
} }
@Override @Override

View file

@ -1,6 +1,7 @@
package eu.kanade.mangafeed.data.managers; package eu.kanade.mangafeed.data.managers;
import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.StorIOSQLite;
import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
import com.pushtorefresh.storio.sqlite.queries.Query; import com.pushtorefresh.storio.sqlite.queries.Query;
import java.util.List; import java.util.List;
@ -10,9 +11,6 @@ import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.data.tables.ChaptersTable; import eu.kanade.mangafeed.data.tables.ChaptersTable;
import rx.Observable; import rx.Observable;
/**
* Created by len on 26/09/2015.
*/
public class ChapterManager extends BaseManager { public class ChapterManager extends BaseManager {
public ChapterManager(StorIOSQLite db) { public ChapterManager(StorIOSQLite db) {
@ -30,4 +28,24 @@ public class ChapterManager extends BaseManager {
.prepare() .prepare()
.createObservable(); .createObservable();
} }
public Observable<PutResult> 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();
}
}
} }

View file

@ -44,7 +44,7 @@ public class MangaManager extends BaseManager {
m.genre="Action, Drama"; m.genre="Action, Drama";
m.status="Ongoing"; m.status="Ongoing";
m.thumbnail_url="http://example.com/pic.png"; m.thumbnail_url="http://example.com/pic.png";
m.title="Berserk"; m.title="One Piece";
insert(m).subscribe(); insert(m).subscribe();
} }

View file

@ -27,4 +27,17 @@ public class ChaptersTable {
@NonNull @NonNull
public static final String COLUMN_DATE_FETCH = "date_fetch"; 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 + ")"
+ ");";
}
} }

View file

@ -4,7 +4,6 @@ import android.content.Intent;
import javax.inject.Inject; import javax.inject.Inject;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
@ -16,28 +15,28 @@ import static rx.android.schedulers.AndroidSchedulers.mainThread;
public class LibraryPresenter { public class LibraryPresenter {
private LibraryView mLibraryView; private LibraryView view;
@Inject @Inject
public DatabaseHelper db; public DatabaseHelper db;
public LibraryPresenter(LibraryView libraryView) { public LibraryPresenter(LibraryView view) {
mLibraryView = libraryView; this.view = view;
App.getComponent(libraryView.getActivity()).inject(this); App.getComponent(view.getActivity()).inject(this);
} }
public void onMangaClick(EasyAdapter<Manga> adapter, int position) { public void onMangaClick(EasyAdapter<Manga> adapter, int position) {
Intent intent = MangaDetailActivity.newIntent( Intent intent = MangaDetailActivity.newIntent(
mLibraryView.getActivity(), view.getActivity(),
adapter.getItem(position) adapter.getItem(position)
); );
mLibraryView.getActivity().startActivity(intent); view.getActivity().startActivity(intent);
} }
public void initializeMangas() { public void initializeMangas() {
db.manga.get() db.manga.get()
.observeOn(mainThread()) .observeOn(mainThread())
.subscribe(mLibraryView::setMangas); .subscribe(view::setMangas);
} }
} }

View file

@ -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);
}
}

View file

@ -1,6 +1,7 @@
package eu.kanade.mangafeed.ui.activity; package eu.kanade.mangafeed.ui.activity;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.MenuItem; import android.view.MenuItem;
@ -39,4 +40,7 @@ public class BaseActivity extends AppCompatActivity {
return App.get(this).getComponent(); return App.get(this).getComponent();
} }
public Context getActivity() {
return this;
}
} }

View file

@ -6,29 +6,40 @@ import android.os.Bundle;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ListView;
import java.util.List;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Chapter;
import eu.kanade.mangafeed.data.models.Manga; 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; Manga manga;
MangaDetailPresenter presenter;
EasyAdapter<Chapter> adapter;
@Bind(R.id.toolbar) @Bind(R.id.toolbar)
Toolbar toolbar; Toolbar toolbar;
@Bind(R.id.manga_chapters_list)
ListView list_chapters;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manga_detail); setContentView(R.layout.activity_manga_detail);
ButterKnife.bind(this); ButterKnife.bind(this);
presenter = new MangaDetailPresenter(this);
setupToolbar(toolbar); setupToolbar(toolbar);
EventBus.getDefault().registerSticky(this);
} }
@Override @Override
@ -53,26 +64,39 @@ public class MangaDetailActivity extends BaseActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
public static Intent newIntent(Context context, Manga manga) { public static Intent newIntent(Context context, Manga manga) {
Intent intent = new Intent(context, MangaDetailActivity.class); Intent intent = new Intent(context, MangaDetailActivity.class);
EventBus.getDefault().postSticky(manga); MangaDetailPresenter.newIntent(manga);
return intent; return intent;
} }
public void onEventMainThread(Manga manga) { @Override
this.manga = manga; public void onStart() {
loadManga(); super.onStart();
//loadChapters(); presenter.onStart();
} }
private void loadManga() { @Override
public void onStop() {
super.onStop();
presenter.onStop();
}
public void loadManga(Manga manga) {
setToolbarTitle(manga.title); setToolbarTitle(manga.title);
} }
public void setChapters(List<Chapter> chapters) {
if (adapter == null) {
adapter = new EasyAdapter<Chapter>(
getActivity(),
ChapterListHolder.class,
chapters
);
list_chapters.setAdapter(adapter);
} else {
adapter.setItems(chapters);
}
}
} }

View file

@ -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<Chapter> {
@ViewId(R.id.chapter_title)
TextView title;
public ChapterListHolder(View view) {
super(view);
}
public void onSetValues(Chapter chapter, PositionInfo positionInfo) {
title.setText(chapter.name);
}
}

View file

@ -7,7 +7,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.GridView; import android.widget.GridView;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import butterknife.Bind; import butterknife.Bind;
@ -15,7 +14,6 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.LibraryPresenter; 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.activity.MainActivity;
import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder; import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder;
import eu.kanade.mangafeed.view.LibraryView; import eu.kanade.mangafeed.view.LibraryView;
@ -25,8 +23,8 @@ import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryFragment extends Fragment implements LibraryView { public class LibraryFragment extends Fragment implements LibraryView {
@Bind(R.id.gridView) GridView grid; @Bind(R.id.gridView) GridView grid;
LibraryPresenter mLibraryPresenter; LibraryPresenter presenter;
EasyAdapter<Manga> mEasyAdapter; EasyAdapter<Manga> adapter;
public static LibraryFragment newInstance() { public static LibraryFragment newInstance() {
LibraryFragment fragment = new LibraryFragment(); LibraryFragment fragment = new LibraryFragment();
@ -39,7 +37,7 @@ public class LibraryFragment extends Fragment implements LibraryView {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mLibraryPresenter = new LibraryPresenter(this); presenter = new LibraryPresenter(this);
} }
@Override @Override
@ -50,22 +48,22 @@ public class LibraryFragment extends Fragment implements LibraryView {
((MainActivity)getActivity()).setToolbarTitle(getString(R.string.library_title)); ((MainActivity)getActivity()).setToolbarTitle(getString(R.string.library_title));
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
mLibraryPresenter.initializeMangas(); presenter.initializeMangas();
setMangaClickListener(); setMangaClickListener();
return view; return view;
} }
public void setMangas(List<Manga> mangas) { public void setMangas(List<Manga> mangas) {
if (mEasyAdapter == null) { if (adapter == null) {
mEasyAdapter = new EasyAdapter<Manga>( adapter = new EasyAdapter<Manga>(
getActivity(), getActivity(),
MangaLibraryHolder.class, MangaLibraryHolder.class,
mangas mangas
); );
grid.setAdapter(mEasyAdapter); grid.setAdapter(adapter);
} else { } else {
mEasyAdapter.setItems(mangas); adapter.setItems(mangas);
} }
} }
@ -73,7 +71,7 @@ public class LibraryFragment extends Fragment implements LibraryView {
private void setMangaClickListener() { private void setMangaClickListener() {
grid.setOnItemClickListener( grid.setOnItemClickListener(
(parent, view, position, id) -> (parent, view, position, id) ->
mLibraryPresenter.onMangaClick(mEasyAdapter, position) presenter.onMangaClick(adapter, position)
); );
} }

View file

@ -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<Chapter> chapters);
}

View file

@ -3,9 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="40dp">
>
<CheckBox <CheckBox
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -16,8 +14,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/chapter_download_image" android:id="@+id/chapter_download_image"
tools:src="@mipmap/ic_launcher" tools:src="@mipmap/ic_launcher"/>
/>
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"