Get data from database

This commit is contained in:
inorichi 2015-09-26 14:25:18 +02:00
parent 9302ecfb92
commit 8b6bdfdfdc
18 changed files with 378 additions and 59 deletions

View File

@ -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') {

View File

@ -12,7 +12,7 @@
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="eu.kanade.mangafeed.ui.activity.MainActivity"
android:name=".ui.activity.MainActivity"
android:label="@string/label_main"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
@ -22,14 +22,14 @@
</intent-filter>
</activity>
<receiver
android:name="eu.kanade.mangafeed.data.SyncService$SyncOnConnectionAvailable"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<activity
android:name=".ui.activity.MangaDetailActivity"
android:label="@string/title_activity_manga_detail"
android:parentActivityName=".ui.activity.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="eu.kanade.mangafeed.ui.activity.MainActivity" />
</activity>
</application>
</manifest>

View File

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

View File

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

View File

@ -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<List<Manga>> getMangas() {
return db.get()
.listOfObjects(Manga.class)
.withQuery(Query.builder()
.table(MangasTable.TABLE)
.addTypeMapping(Manga.class, SQLiteTypeMapping.<Manga>builder()
.putResolver(new MangaStorIOSQLitePutResolver())
.getResolver(new MangaStorIOSQLiteGetResolver())
.deleteResolver(new MangaStorIOSQLiteDeleteResolver())
.build())
.prepare()
.createObservable();
.addTypeMapping(Chapter.class, SQLiteTypeMapping.<Chapter>builder()
.putResolver(new ChapterStorIOSQLitePutResolver())
.getResolver(new ChapterStorIOSQLiteGetResolver())
.deleteResolver(new ChapterStorIOSQLiteDeleteResolver())
.build())
.build();
manga = new MangaManager(db);
chapter = new ChapterManager(db);
}
}

View File

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

View File

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

View File

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

View File

@ -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<Manga> mangass;
public MangaManager(StorIOSQLite db) {
super(db);
}
public Observable<List<Manga>> get() {
return db.get()
.listOfObjects(Manga.class)
.withQuery(Query.builder()
.table(MangasTable.TABLE)
.build())
.prepare()
.createObservable();
}
public Observable<PutResult> insert(Manga manga) {
return db.put()
.object(manga)
.prepare()
.createObservable();
}
}

View File

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

View File

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

View File

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

View File

@ -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<Manga> {
Context context;
int layoutResourceId;
ArrayList<Manga> data;
List<Manga> data;
public LibraryAdapter(Context context, int layoutResourceId, ArrayList<Manga> data) {
public LibraryAdapter(Context context, int layoutResourceId, List<Manga> data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;

View File

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

View File

@ -0,0 +1,18 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="eu.kanade.mangafeed.ui.activity.MangaDetailActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/manga_chapters_list"
tools:listitem="@layout/item_chapter"
android:height="?android:listPreferredItemHeight"/>
</RelativeLayout>

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
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"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter_title"
tools:text="Chapter 32"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter_pages"
tools:text="Pages: 45"
android:layout_weight="1"
android:gravity="center"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter_date"
tools:text="22/02/2016"
android:layout_gravity="bottom"
android:layout_marginBottom="2dp"
android:textSize="12sp"
android:paddingRight="5dp" />
</LinearLayout>

View File

@ -0,0 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="eu.kanade.mangafeed.ui.activity.MangaDetailActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

View File

@ -26,5 +26,9 @@
<string name="recent_updates_title">Recent updates</string>
<string name="catalogues_title">Catalogues</string>
<string name="settings_title">Settings</string>
<string name="title_activity_manga_detail">MangaDetailActivity</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>