Android Open Source - iwannawatch-android Database Helper






From Project

Back to project page iwannawatch-android.

License

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.

Java Source Code

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




Java Source Code List

com.meoyawn.iwannawatch.AppModule.java
com.meoyawn.iwannawatch.App.java
com.meoyawn.iwannawatch.CrashReportingTree.java
com.meoyawn.iwannawatch.DynamicHeightImageView.java
com.meoyawn.iwannawatch.MainActivity.java
com.meoyawn.iwannawatch.MainFragment.java
com.meoyawn.iwannawatch.MovieView.java
com.meoyawn.iwannawatch.adapters.BaseMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.CursorMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.DynamicMoviesAdapter.java
com.meoyawn.iwannawatch.doers.DatabaseHelper.java
com.meoyawn.iwannawatch.doers.TheMovieDbApi.java
com.meoyawn.iwannawatch.models.Movie.java
com.meoyawn.iwannawatch.models.Search.java
com.meoyawn.iwannawatch.qualifiers.Delete.java
com.meoyawn.iwannawatch.qualifiers.Done.java