-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create database context with minimizing model
- Loading branch information
1 parent
ad50a6d
commit a036b8b
Showing
7 changed files
with
51 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/ratanparai/moviedog/db/AppDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
app/src/main/java/com/ratanparai/moviedog/db/repository/MovieRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/ratanparai/moviedog/utilities/Constant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters