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

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.MangaStorIOSQLitePutResolver;
import eu.kanade.mangafeed.data.managers.MangaManager;
import eu.kanade.mangafeed.data.tables.ChaptersTable;
public class DatabaseHelper {

View File

@ -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

View File

@ -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<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.status="Ongoing";
m.thumbnail_url="http://example.com/pic.png";
m.title="Berserk";
m.title="One Piece";
insert(m).subscribe();
}

View File

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

View File

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

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

View File

@ -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<Chapter> 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<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.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<Manga> mEasyAdapter;
LibraryPresenter presenter;
EasyAdapter<Manga> 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<Manga> mangas) {
if (mEasyAdapter == null) {
mEasyAdapter = new EasyAdapter<Manga>(
if (adapter == null) {
adapter = new EasyAdapter<Manga>(
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)
);
}

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,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">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/chapter_selection" />
android:id="@+id/chapter_selection"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter_download_image"
tools:src="@mipmap/ic_launcher"
/>
tools:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="0dp"
@ -34,7 +31,7 @@
android:layout_gravity="center_vertical"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp" />
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
@ -43,7 +40,7 @@
tools:text="Pages: 45"
android:layout_weight="1"
android:gravity="center"
android:textSize="12sp" />
android:textSize="12sp"/>
</LinearLayout>
@ -56,5 +53,5 @@
android:layout_gravity="bottom"
android:layout_marginBottom="2dp"
android:textSize="12sp"
android:paddingRight="5dp" />
android:paddingRight="5dp"/>
</LinearLayout>