From 8b6bdfdfdc58435c4347d08ba30ba0cc66055129 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 26 Sep 2015 14:25:18 +0200 Subject: [PATCH] Get data from database --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 20 +++--- .../main/java/eu/kanade/mangafeed/App.java | 4 ++ .../eu/kanade/mangafeed/AppComponent.java | 4 ++ .../data/helpers/DatabaseHelper.java | 48 ++++++------- .../mangafeed/data/helpers/DbOpenHelper.java | 3 - .../mangafeed/data/managers/BaseManager.java | 12 ++++ .../data/managers/ChapterManager.java | 33 +++++++++ .../mangafeed/data/managers/MangaManager.java | 37 ++++++++++ .../kanade/mangafeed/data/models/Chapter.java | 67 +++++++++++++++++++ .../data/{entities => models}/Manga.java | 8 +-- .../ui/activity/MangaDetailActivity.java | 56 ++++++++++++++++ .../mangafeed/ui/adapter/LibraryAdapter.java | 8 +-- .../ui/fragment/LibraryFragment.java | 42 +++++++++--- .../main/res/layout/activity_manga_detail.xml | 18 +++++ app/src/main/res/layout/item_chapter.xml | 60 +++++++++++++++++ app/src/main/res/menu/menu_manga_detail.xml | 7 ++ app/src/main/res/values/strings.xml | 4 ++ 18 files changed, 378 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java rename app/src/main/java/eu/kanade/mangafeed/data/{entities => models}/Manga.java (97%) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java create mode 100644 app/src/main/res/layout/activity_manga_detail.xml create mode 100644 app/src/main/res/layout/item_chapter.xml create mode 100644 app/src/main/res/menu/menu_manga_detail.xml diff --git a/app/build.gradle b/app/build.gradle index b0ebe9e2ba..83b4036c05 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,6 +42,7 @@ dependencies { final DAGGER_VERSION = '2.0.1' final HAMCREST_VERSION = '1.3' final MOCKITO_VERSION = '1.10.19' + final STORIO_VERSION = '1.4.0' compile fileTree(dir: 'libs', include: ['*.jar']) @@ -54,8 +55,8 @@ dependencies { compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1' compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0' compile 'com.squareup.okhttp:okhttp:2.4.0' - compile 'com.pushtorefresh.storio:sqlite:1.4.0' - compile 'com.pushtorefresh.storio:sqlite-annotations:1.4.0' + compile "com.pushtorefresh.storio:sqlite:$STORIO_VERSION" + compile "com.pushtorefresh.storio:sqlite-annotations:$STORIO_VERSION" compile 'de.greenrobot:eventbus:2.4.0' compile 'com.github.bumptech.glide:glide:3.6.1' compile 'de.hdodenhof:circleimageview:1.3.0' @@ -66,6 +67,7 @@ dependencies { compile "com.google.dagger:dagger:$DAGGER_VERSION" apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION" + apt "com.pushtorefresh.storio:sqlite-annotations-processor:$STORIO_VERSION" provided 'org.glassfish:javax.annotation:10.0-b28' compile('com.mikepenz:materialdrawer:4.3.0@aar') { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 445d7a99be..c6dbe092db 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,9 +12,9 @@ android:label="@string/app_name" android:theme="@style/AppTheme" > + android:theme="@style/AppTheme.NoActionBar" > @@ -22,14 +22,14 @@ - - - - - - + + + diff --git a/app/src/main/java/eu/kanade/mangafeed/App.java b/app/src/main/java/eu/kanade/mangafeed/App.java index 3e0039dcc9..60fcbd1860 100644 --- a/app/src/main/java/eu/kanade/mangafeed/App.java +++ b/app/src/main/java/eu/kanade/mangafeed/App.java @@ -27,6 +27,10 @@ public class App extends Application { return mApplicationComponent; } + public static AppComponent getComponent(Context context) { + return get(context).getComponent(); + } + // Needed to replace the component with a test specific one public void setComponent(AppComponent applicationComponent) { mApplicationComponent = applicationComponent; diff --git a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java index 765f1bf948..093ad1e993 100644 --- a/app/src/main/java/eu/kanade/mangafeed/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/AppComponent.java @@ -7,6 +7,8 @@ import javax.inject.Singleton; import dagger.Component; import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.ui.activity.MainActivity; +import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; +import eu.kanade.mangafeed.ui.fragment.LibraryFragment; @Singleton @Component( @@ -18,6 +20,8 @@ import eu.kanade.mangafeed.ui.activity.MainActivity; public interface AppComponent { void inject(MainActivity mainActivity); + void inject(LibraryFragment libraryFragment); + void inject(MangaDetailActivity mangaDetailActivity); 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 2b767e0f56..afa07905ee 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 @@ -2,42 +2,44 @@ package eu.kanade.mangafeed.data.helpers; import android.content.Context; +import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; -import com.pushtorefresh.storio.sqlite.queries.Query; -import java.util.List; +import eu.kanade.mangafeed.data.managers.ChapterManager; +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteDeleteResolver; +import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteGetResolver; +import eu.kanade.mangafeed.data.models.ChapterStorIOSQLitePutResolver; +import eu.kanade.mangafeed.data.models.Manga; +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.entities.Manga; -import eu.kanade.mangafeed.data.tables.MangasTable; -import rx.Observable; - -/** - * Created by len on 23/09/2015. - */ public class DatabaseHelper { private StorIOSQLite db; + public MangaManager manga; + public ChapterManager chapter; public DatabaseHelper(Context context) { db = DefaultStorIOSQLite.builder() .sqliteOpenHelper(new DbOpenHelper(context)) - .build(); - } - - public StorIOSQLite getStorIODb() { - return db; - } - - public Observable> getMangas() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) + .addTypeMapping(Manga.class, SQLiteTypeMapping.builder() + .putResolver(new MangaStorIOSQLitePutResolver()) + .getResolver(new MangaStorIOSQLiteGetResolver()) + .deleteResolver(new MangaStorIOSQLiteDeleteResolver()) .build()) - .prepare() - .createObservable(); + .addTypeMapping(Chapter.class, SQLiteTypeMapping.builder() + .putResolver(new ChapterStorIOSQLitePutResolver()) + .getResolver(new ChapterStorIOSQLiteGetResolver()) + .deleteResolver(new ChapterStorIOSQLiteDeleteResolver()) + .build()) + .build(); + manga = new MangaManager(db); + chapter = new ChapterManager(db); } } 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 d3e0fcd4d6..8354343c45 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 @@ -7,9 +7,6 @@ import android.support.annotation.NonNull; import eu.kanade.mangafeed.data.tables.MangasTable; -/** - * Created by len on 23/09/2015. - */ public class DbOpenHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "mangafeed.db"; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java new file mode 100644 index 0000000000..bab0ca8817 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java @@ -0,0 +1,12 @@ +package eu.kanade.mangafeed.data.managers; + +import com.pushtorefresh.storio.sqlite.StorIOSQLite; + +public abstract class BaseManager { + + protected StorIOSQLite db; + + public BaseManager(StorIOSQLite db) { + this.db = db; + } +} 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 new file mode 100644 index 0000000000..daf9689a5f --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -0,0 +1,33 @@ +package eu.kanade.mangafeed.data.managers; + +import com.pushtorefresh.storio.sqlite.StorIOSQLite; +import com.pushtorefresh.storio.sqlite.queries.Query; + +import java.util.List; + +import eu.kanade.mangafeed.data.models.Chapter; +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) { + super(db); + } + + public Observable> get(Manga manga) { + return db.get() + .listOfObjects(Chapter.class) + .withQuery(Query.builder() + .table(ChaptersTable.TABLE) + .where(ChaptersTable.COLUMN_MANGA_ID + "=?") + .whereArgs(manga.id) + .build()) + .prepare() + .createObservable(); + } +} 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 new file mode 100644 index 0000000000..6a6519b0a5 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java @@ -0,0 +1,37 @@ +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; + +import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.data.tables.MangasTable; +import rx.Observable; + +public class MangaManager extends BaseManager { + List mangass; + + public MangaManager(StorIOSQLite db) { + super(db); + } + + public Observable> get() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .build()) + .prepare() + .createObservable(); + } + + public Observable insert(Manga manga) { + return db.put() + .object(manga) + .prepare() + .createObservable(); + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java new file mode 100644 index 0000000000..030f7e8f4d --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java @@ -0,0 +1,67 @@ +package eu.kanade.mangafeed.data.models; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; +import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; + +import eu.kanade.mangafeed.data.tables.ChaptersTable; + +@StorIOSQLiteType(table = ChaptersTable.TABLE) +public class Chapter { + + @Nullable + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_ID, key = true) + public Long id; + + @NonNull + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_MANGA_ID) + public int manga_id; + + @NonNull + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_URL) + public String url; + + @NonNull + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_NAME) + public String name; + + @NonNull + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_READ) + public int read; + + @NonNull + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_DATE_FETCH) + public long date_fetch; + + + public Chapter() {} + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Chapter chapter = (Chapter) o; + + if (manga_id != chapter.manga_id) return false; + if (read != chapter.read) return false; + if (date_fetch != chapter.date_fetch) return false; + if (id != null ? !id.equals(chapter.id) : chapter.id != null) return false; + if (!url.equals(chapter.url)) return false; + return name.equals(chapter.name); + + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + manga_id; + result = 31 * result + url.hashCode(); + result = 31 * result + name.hashCode(); + result = 31 * result + read; + result = 31 * result + (int) (date_fetch ^ (date_fetch >>> 32)); + return result; + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/entities/Manga.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java similarity index 97% rename from app/src/main/java/eu/kanade/mangafeed/data/entities/Manga.java rename to app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java index cf22206a01..4fb5483ad0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/entities/Manga.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Manga.java @@ -1,8 +1,4 @@ -package eu.kanade.mangafeed.data.entities; - -/** - * Created by len on 23/09/2015. - */ +package eu.kanade.mangafeed.data.models; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -74,7 +70,7 @@ public class Manga { @StorIOSQLiteColumn(name = MangasTable.COLUMN_CHAPTER_ORDER) public int chapter_order; - Manga() {} + public Manga() {} public Manga(String title) { this.title = title; 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 new file mode 100644 index 0000000000..183e18cbf5 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java @@ -0,0 +1,56 @@ +package eu.kanade.mangafeed.ui.activity; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; + +import de.greenrobot.event.EventBus; +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Manga; + +public class MangaDetailActivity extends AppCompatActivity { + + Manga manga; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manga_detail); + EventBus.getDefault().registerSticky(this); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_manga_detail, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } + + @Override + public void onDestroy() { + EventBus.getDefault().unregister(this); + + super.onDestroy(); + } + + public void onEvent(Manga manga) { + this.manga = manga; + //loadChapters(); + } +} 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 d0aec712ed..ef1f53a4a3 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,12 +11,12 @@ import android.widget.TextView; import com.bumptech.glide.Glide; -import java.util.ArrayList; +import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.data.entities.Manga; +import eu.kanade.mangafeed.data.models.Manga; import uk.co.ribot.easyadapter.annotations.LayoutId; /** @@ -28,9 +28,9 @@ public class LibraryAdapter extends ArrayAdapter { Context context; int layoutResourceId; - ArrayList data; + List data; - public LibraryAdapter(Context context, int layoutResourceId, ArrayList data) { + public LibraryAdapter(Context context, int layoutResourceId, List data) { super(context, layoutResourceId, data); this.context = context; this.layoutResourceId = layoutResourceId; 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 4ed4dc3d33..22579f659a 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,26 +1,38 @@ package eu.kanade.mangafeed.ui.fragment; -import android.os.Bundle; 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.ArrayList; +import java.util.List; + +import javax.inject.Inject; 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.entities.Manga; +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; +import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.activity.BaseActivity; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; +import rx.functions.Action1; public class LibraryFragment extends Fragment { @Bind(R.id.gridView) GridView grid; + @Inject + DatabaseHelper db; + + List mangas; + public static LibraryFragment newInstance() { LibraryFragment fragment = new LibraryFragment(); Bundle args = new Bundle(); @@ -33,19 +45,27 @@ public class LibraryFragment extends Fragment { 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); - ArrayList mangas = new ArrayList<>(); - mangas.add(new Manga("One Piece")); - mangas.add(new Manga("Berserk")); - mangas.add(new Manga("Fate/stay night: Unlimited Blade Works")); + db.manga.get().subscribe( + result -> { + mangas = result; - LibraryAdapter adapter = new LibraryAdapter(getActivity(), - R.layout.item_library, mangas); + LibraryAdapter adapter = new LibraryAdapter(getActivity(), + R.layout.item_library, mangas); - grid.setAdapter(adapter); + grid.setAdapter(adapter); + grid.setOnItemClickListener( + (parent, v, position, id) -> { + Intent intent = new Intent(".ui.activity.MangaDetailActivity"); + EventBus.getDefault().postSticky(adapter.getItem(position)); + startActivity(intent); + } + ); + } + ); return view; } diff --git a/app/src/main/res/layout/activity_manga_detail.xml b/app/src/main/res/layout/activity_manga_detail.xml new file mode 100644 index 0000000000..469662e589 --- /dev/null +++ b/app/src/main/res/layout/activity_manga_detail.xml @@ -0,0 +1,18 @@ + + + + + diff --git a/app/src/main/res/layout/item_chapter.xml b/app/src/main/res/layout/item_chapter.xml new file mode 100644 index 0000000000..74e72cb488 --- /dev/null +++ b/app/src/main/res/layout/item_chapter.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_manga_detail.xml b/app/src/main/res/menu/menu_manga_detail.xml new file mode 100644 index 0000000000..4f27727c03 --- /dev/null +++ b/app/src/main/res/menu/menu_manga_detail.xml @@ -0,0 +1,7 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5cc5ab3b99..400af76be1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -26,5 +26,9 @@ Recent updates Catalogues Settings + MangaDetailActivity + + Hello world! + Settings