Back to project page iwannawatch-android.
The source code is released under:
MIT License
If you think the Android project iwannawatch-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.meoyawn.iwannawatch.doers; /*w w w . j a v a2 s.c o m*/ import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import com.meoyawn.iwannawatch.models.Movie; import java.util.Date; import javax.inject.Inject; import javax.inject.Singleton; import rx.Observable; import rx.Subscriber; import rx.functions.Func1; import static nl.qbusict.cupboard.CupboardFactory.cupboard; /** * Created by adel on 3/1/14 */ public @Singleton class DatabaseHelper extends SQLiteOpenHelper { static final String NAME = "movies.sqlite3"; static final int VERSION = 1; static { cupboard().register(Movie.class); } @Inject public DatabaseHelper(Context context) { super(context, NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { cupboard().withDatabase(db).createTables(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { cupboard().withDatabase(db).upgradeTables(); } public Observable<Cursor> getMovies(boolean watched) { return Observable.create((Subscriber<? super Cursor> subscriber) -> { subscriber.onNext(cupboard() .withDatabase(getReadableDatabase()) .query(Movie.class) .withSelection("watched = ?", new String[]{Integer.toString(watched ? 1 : 0)}) .orderBy("createdAt DESC") .getCursor()); subscriber.onCompleted(); }); } public Func1<Movie, Observable<Cursor>> saveAndGetAll(boolean watched) { return movie -> { movie.createdAt = new Date(); cupboard().withDatabase(getWritableDatabase()).put(movie); return getMovies(watched); }; } public Func1<Movie, Observable<Cursor>> changeStatusAndGetAll(boolean watched) { return movie -> { movie.watched = !watched; cupboard().withDatabase(getWritableDatabase()).put(movie); return getMovies(watched); }; } public Func1<Movie, Observable<Cursor>> deleteAndGetAll(boolean watched) { return movie -> { cupboard().withDatabase(getWritableDatabase()).delete(movie); return getMovies(watched); }; } }