Skip to content

Commit

Permalink
create database context with minimizing model
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Apr 23, 2019
1 parent ad50a6d commit a036b8b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dependencies {
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"

implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"

// Unit testing dependencies.
testImplementation 'junit:junit:4.12'
testImplementation 'com.google.truth:truth:0.44'
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/ratanparai/moviedog/db/AppDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ratanparai.moviedog.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ratanparai.moviedog.db.dao.MovieDao
import com.ratanparai.moviedog.db.entity.Movie
import com.ratanparai.moviedog.utilities.DATABASE_NAME


@Database(entities = [Movie::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun movieDao(): MovieDao

companion object {

// For Singleton instantiation
@Volatile private var instance: AppDatabase? = null

fun getInstance(context: Context): AppDatabase {
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
}

// Create and pre-populate the database. See this article for more details:
// https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1#4785
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.build()
}
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/ratanparai/moviedog/db/dao/MovieDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package com.ratanparai.moviedog.db.dao

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.ratanparai.moviedog.Movie
import com.ratanparai.moviedog.db.entity.Movie

@Dao
interface MovieDao {
Expand All @@ -15,4 +16,7 @@ interface MovieDao {

@Query("SELECT * FROM movies WHERE imdbId = :imdbId")
fun getMovie(imdbId : String) : LiveData<Movie>

@Insert
fun insertMovie(movie: Movie): Long
}
10 changes: 0 additions & 10 deletions app/src/main/java/com/ratanparai/moviedog/db/entity/Movie.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ data class Movie (
val description : String,
val imdbId : String,
val cardImage : String,
val backgroundImage : String?,
val videoUrl : String,
val contentType : String?,
val live : Boolean?,
val width : Int,
val height : Int,
val audioChannelConfig : String?,
val purchasePrice : String?,
val rentalPrice : String?,
val ratingStyle: Int?,
val ratingScore: Double,
val productionYear : Int,
val duration: Int
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ratanparai.moviedog.db.repository

import com.ratanparai.moviedog.db.dao.MovieDao
import com.ratanparai.moviedog.scrapper.DekhvhaiScrapper

class MovieRepository private constructor(private val movieDao : MovieDao){

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ratanparai.moviedog.utilities

/**
* Constants used throughout the app.
*/
const val DATABASE_NAME = "moviedog-db"
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ buildscript {
roomVersion = '1.1.1'
archLifecycleVersion = '1.1.1'
coroutines = '0.26.1'
lifecycleVersion = '2.1.0-alpha04'
}
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
Expand All @@ -25,7 +26,7 @@ allprojects {
repositories {
google()
jcenter()

}
}

Expand Down

0 comments on commit a036b8b

Please sign in to comment.