mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Get data from database
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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(); | ||||
| } | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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"; | ||||
|   | ||||
| @@ -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; | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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; | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
| @@ -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(); | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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); | ||||
|                     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; | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user