From 2e59b8a92987175fcb3ba8e66033c0a85ba2035d Mon Sep 17 00:00:00 2001 From: ksza Date: Tue, 16 Feb 2016 15:51:02 +0200 Subject: [PATCH 1/2] Created DB module --- app/build.gradle | 6 +- app/src/main/java/com/tpg/movierx/db/Db.java | 43 ++++++ .../java/com/tpg/movierx/db/DbModule.java | 50 +++++++ .../java/com/tpg/movierx/db/DbOpenHelper.java | 55 ++++++++ .../java/com/tpg/movierx/db/MovieItem.java | 129 ++++++++++++++++++ .../main/java/com/tpg/movierx/db/Util.java | 13 ++ 6 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/tpg/movierx/db/Db.java create mode 100644 app/src/main/java/com/tpg/movierx/db/DbModule.java create mode 100644 app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java create mode 100644 app/src/main/java/com/tpg/movierx/db/MovieItem.java create mode 100644 app/src/main/java/com/tpg/movierx/db/Util.java diff --git a/app/build.gradle b/app/build.gradle index 4ba8c31..28f52d2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/tpg/movierx/db/Db.java b/app/src/main/java/com/tpg/movierx/db/Db.java new file mode 100644 index 0000000..6506ceb --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/Db.java @@ -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."); + } +} diff --git a/app/src/main/java/com/tpg/movierx/db/DbModule.java b/app/src/main/java/com/tpg/movierx/db/DbModule.java new file mode 100644 index 0000000..be805b8 --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/DbModule.java @@ -0,0 +1,50 @@ +/* + * 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 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); + } +} diff --git a/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java b/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java new file mode 100644 index 0000000..b913372 --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java @@ -0,0 +1,55 @@ +/* + * 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; + +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) { + } +} diff --git a/app/src/main/java/com/tpg/movierx/db/MovieItem.java b/app/src/main/java/com/tpg/movierx/db/MovieItem.java new file mode 100644 index 0000000..b57c62b --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/MovieItem.java @@ -0,0 +1,129 @@ +/* + * 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.ContentValues; +import android.database.Cursor; + +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 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; // TODO defensive copy? + } + + 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 createDummyMovieList(final int size) { + final List 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; + } +} diff --git a/app/src/main/java/com/tpg/movierx/db/Util.java b/app/src/main/java/com/tpg/movierx/db/Util.java new file mode 100644 index 0000000..80bf4f4 --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/Util.java @@ -0,0 +1,13 @@ +package com.tpg.movierx.db; + +/** + * 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"; +} From 747cdb531de7d24743875e515ba5534655d70c48 Mon Sep 17 00:00:00 2001 From: ksza Date: Wed, 17 Feb 2016 11:01:07 +0200 Subject: [PATCH 2/2] Created a DAO abstraction for the movie item --- .../java/com/tpg/movierx/db/DbModule.java | 7 ++++ .../java/com/tpg/movierx/db/DbOpenHelper.java | 2 + .../main/java/com/tpg/movierx/db/Util.java | 2 + .../com/tpg/movierx/db/dao/MovieItemDao.java | 39 +++++++++++++++++++ .../tpg/movierx/db/{ => model}/MovieItem.java | 6 ++- 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/tpg/movierx/db/dao/MovieItemDao.java rename app/src/main/java/com/tpg/movierx/db/{ => model}/MovieItem.java (97%) diff --git a/app/src/main/java/com/tpg/movierx/db/DbModule.java b/app/src/main/java/com/tpg/movierx/db/DbModule.java index be805b8..ac46315 100644 --- a/app/src/main/java/com/tpg/movierx/db/DbModule.java +++ b/app/src/main/java/com/tpg/movierx/db/DbModule.java @@ -20,6 +20,7 @@ import com.squareup.sqlbrite.BriteDatabase; import com.squareup.sqlbrite.SqlBrite; +import com.tpg.movierx.db.dao.MovieItemDao; import javax.inject.Singleton; @@ -47,4 +48,10 @@ SqlBrite provideSqlBrite() { BriteDatabase provideDatabase(SqlBrite sqlBrite, SQLiteOpenHelper helper) { return sqlBrite.wrapDatabaseHelper(helper); } + + @Provides + @Singleton + MovieItemDao provideMovieItemDao(BriteDatabase db) { + return new MovieItemDao(db); + } } diff --git a/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java b/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java index b913372..e33640f 100644 --- a/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java +++ b/app/src/main/java/com/tpg/movierx/db/DbOpenHelper.java @@ -19,6 +19,8 @@ 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; diff --git a/app/src/main/java/com/tpg/movierx/db/Util.java b/app/src/main/java/com/tpg/movierx/db/Util.java index 80bf4f4..1a42510 100644 --- a/app/src/main/java/com/tpg/movierx/db/Util.java +++ b/app/src/main/java/com/tpg/movierx/db/Util.java @@ -1,5 +1,7 @@ package com.tpg.movierx.db; +import com.tpg.movierx.db.model.MovieItem; + /** * Created by karoly.szanto on 11/11/15. */ diff --git a/app/src/main/java/com/tpg/movierx/db/dao/MovieItemDao.java b/app/src/main/java/com/tpg/movierx/db/dao/MovieItemDao.java new file mode 100644 index 0000000..b5d7246 --- /dev/null +++ b/app/src/main/java/com/tpg/movierx/db/dao/MovieItemDao.java @@ -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> 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); + } +} diff --git a/app/src/main/java/com/tpg/movierx/db/MovieItem.java b/app/src/main/java/com/tpg/movierx/db/model/MovieItem.java similarity index 97% rename from app/src/main/java/com/tpg/movierx/db/MovieItem.java rename to app/src/main/java/com/tpg/movierx/db/model/MovieItem.java index b57c62b..a3b4c70 100644 --- a/app/src/main/java/com/tpg/movierx/db/MovieItem.java +++ b/app/src/main/java/com/tpg/movierx/db/model/MovieItem.java @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.tpg.movierx.db; +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; @@ -92,7 +94,7 @@ public Builder poster(String poster) { } public ContentValues build() { - return values; // TODO defensive copy? + return values; } public MovieItem buildItem() {