Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created DB module #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'

// SQLBrite
compile 'com.squareup.sqlbrite:sqlbrite:0.5.1'
// helper for seamless parceling
compile 'com.github.frankiesardo:auto-parcel:0.3.1'
apt 'com.github.frankiesardo:auto-parcel-processor:0.3.1'
// Test
testCompile 'junit:junit:4.12'
}
43 changes: 43 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/Db.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tpg.movierx.db;

import android.database.Cursor;

public final class Db {
public static final int BOOLEAN_FALSE = 0;
public static final int BOOLEAN_TRUE = 1;

public static String getString(Cursor cursor, String columnName) {
return cursor.getString(cursor.getColumnIndexOrThrow(columnName));
}

public static boolean getBoolean(Cursor cursor, String columnName) {
return getInt(cursor, columnName) == BOOLEAN_TRUE;
}

public static long getLong(Cursor cursor, String columnName) {
return cursor.getLong(cursor.getColumnIndexOrThrow(columnName));
}

public static int getInt(Cursor cursor, String columnName) {
return cursor.getInt(cursor.getColumnIndexOrThrow(columnName));
}

private Db() {
throw new AssertionError("No instances.");
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/DbModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tpg.movierx.db;

import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;

import com.squareup.sqlbrite.BriteDatabase;
import com.squareup.sqlbrite.SqlBrite;
import com.tpg.movierx.db.dao.MovieItemDao;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public final class DbModule {

@Provides
@Singleton
SQLiteOpenHelper provideOpenHelper(Context context) {
return new DbOpenHelper(context);
}

@Provides
@Singleton
SqlBrite provideSqlBrite() {
// return SqlBrite.create(message -> logger.debug("Database {}", message));
return SqlBrite.create();
}

@Provides
@Singleton
BriteDatabase provideDatabase(SqlBrite sqlBrite, SQLiteOpenHelper helper) {
return sqlBrite.wrapDatabaseHelper(helper);
}

@Provides
@Singleton
MovieItemDao provideMovieItemDao(BriteDatabase db) {
return new MovieItemDao(db);
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tpg.movierx.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.tpg.movierx.db.model.MovieItem;

final class DbOpenHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;

private static final String CREATE_ITEM = ""
+ "CREATE TABLE " + MovieItem.TABLE + "("
+ MovieItem.ID + " INTEGER NOT NULL PRIMARY KEY,"
+ MovieItem.TITLE + " TEXT NOT NULL,"
+ MovieItem.PLOT + " TEXT NOT NULL,"
+ MovieItem.ACTORS + " TEXT NOT NULL,"
+ MovieItem.RATING + " TEXT NOT NULL,"
+ MovieItem.POSTER + " TEXT NOT NULL"
+ ")";

public DbOpenHelper(Context context) {
super(context, "favourites.db", null /* factory */, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ITEM);

db.insert(MovieItem.TABLE, null, new MovieItem.Builder()
.title("Kill Bill: Vol. 1")
.plot("The Bride wakens from a four-year coma. The child she carried in her womb is gone. Now she must wreak vengeance on the team of assassins who betrayed her - a team she was once part of.")
.actors("Uma Thurman, Lucy Liu, Vivica A. Fox, Daryl Hannah")
.rating("8.1")
.poster("http://ia.media-imdb.com/images/M/MV5BMTU1NDg1Mzg4M15BMl5BanBnXkFtZTYwMDExOTc3._V1_SX300.jpg")
.build());
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tpg.movierx.db;

import com.tpg.movierx.db.model.MovieItem;

/**
* Created by karoly.szanto on 11/11/15.
*/
public class Util {

public static final String ALL_MOVIES_QUERY = "SELECT * FROM "
+ MovieItem.TABLE
+ " = ? ORDER BY "
+ MovieItem.TITLE
+ " ASC";
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/dao/MovieItemDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tpg.movierx.db.dao;

import android.content.ContentValues;

import com.squareup.sqlbrite.BriteDatabase;
import com.tpg.movierx.db.Util;
import com.tpg.movierx.db.model.MovieItem;

import java.util.List;

import rx.Observable;

/**
* Created by karoly.szanto on 17/02/16.
*/
public class MovieItemDao {

private final BriteDatabase db;

public MovieItemDao(final BriteDatabase db) {
this.db = db;
}

public Observable<List<MovieItem>> allMovieItems() {
return db.createQuery(MovieItem.TABLE, Util.ALL_MOVIES_QUERY, "1").mapToList(MovieItem.MAPPER);
}

public long insert(final String title, final String actors, final String plot, final String imdRating, final String poster) {
final ContentValues movieItem = new MovieItem.Builder()
.title(title)
.actors(actors)
.plot(plot)
.rating(imdRating)
.poster(poster)
.build();

return db.insert(MovieItem.TABLE, movieItem);
}
}
131 changes: 131 additions & 0 deletions app/src/main/java/com/tpg/movierx/db/model/MovieItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tpg.movierx.db.model;

import android.content.ContentValues;
import android.database.Cursor;

import com.tpg.movierx.db.Db;

import java.util.ArrayList;
import java.util.List;

import auto.parcel.AutoParcel;
import rx.functions.Func1;

@AutoParcel
public abstract class MovieItem {
public static final String TABLE = "movie_item";

public static final String ID = "_id";
public static final String TITLE = "title";
public static final String PLOT = "plot";
public static final String ACTORS = "actors";
public static final String RATING = "rating";
public static final String POSTER = "poster";

public abstract long id();

public abstract String title();

public abstract String plot();

public abstract String actors();

public abstract String rating();

public abstract String poster();

public static final Func1<Cursor, MovieItem> MAPPER = cursor -> {
final long id = Db.getLong(cursor, ID);
final String title = Db.getString(cursor, TITLE);
final String plot = Db.getString(cursor, PLOT);
final String actors = Db.getString(cursor, ACTORS);
final String rating = Db.getString(cursor, RATING);
final String poster = Db.getString(cursor, POSTER);

return new AutoParcel_MovieItem(id, title, plot, actors, rating, poster);
};

public static final class Builder {
private final ContentValues values = new ContentValues();

public Builder id(long id) {
values.put(ID, id);
return this;
}

public Builder title(String title) {
values.put(TITLE, title);
return this;
}

public Builder plot(String plot) {
values.put(PLOT, plot);
return this;
}

public Builder actors(String actors) {
values.put(ACTORS, actors);
return this;
}

public Builder rating(String rating) {
values.put(RATING, rating);
return this;
}

public Builder poster(String poster) {
values.put(POSTER, poster);
return this;
}

public ContentValues build() {
return values;
}

public MovieItem buildItem() {

return new AutoParcel_MovieItem(
values.getAsLong(ID),
values.getAsString(TITLE),
values.getAsString(PLOT),
values.getAsString(ACTORS),
values.getAsString(RATING),
values.getAsString(POSTER)
);
}
}

public static List<MovieItem> createDummyMovieList(final int size) {
final List<MovieItem> movieItemList = new ArrayList<>();

for (int i = 0; i <= size; i++) {
movieItemList.add(
new MovieItem.Builder()
.id(i + 1)
.title("Movie Title " + i)
.plot("here goes a short description of the movie")
.actors("Actor 1, Actor 2, Actor 3")
.rating("8." + i)
.poster("http://ia.media-imdb.com/images/M/MV5BMTU1NDg1Mzg4M15BMl5BanBnXkFtZTYwMDExOTc3._V1_SX300.jpg")
.buildItem()
);
}

return movieItemList;
}
}