diff --git a/app/build.gradle b/app/build.gradle index e61b642..d6936d0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,13 +5,13 @@ apply plugin: 'kotlin-kapt' apply plugin: "androidx.navigation.safeargs.kotlin" android { - compileSdkVersion 29 + compileSdkVersion 30 buildToolsVersion "30.0.0" defaultConfig { applicationId "com.example.todoapp" minSdkVersion 26 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" @@ -42,8 +42,8 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.core:core-ktx:1.3.0' - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.core:core-ktx:1.3.1' + implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13' @@ -67,12 +67,12 @@ dependencies { // Kotlin components implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72" - api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5" - api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5" + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7" + api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7" // DataBinding kapt "com.android.databinding:compiler:3.2.0-alpha10" - kapt "androidx.databinding:databinding-common:4.0.0" + kapt "androidx.databinding:databinding-common:4.0.1" // RecyclerView Animator implementation 'jp.wasabeef:recyclerview-animators:3.0.0' diff --git a/app/src/main/java/com/example/todoapp/data/ToDoDatabase.kt b/app/src/main/java/com/example/todoapp/data/ToDoDatabase.kt index 0e6d5bd..9c31cd9 100644 --- a/app/src/main/java/com/example/todoapp/data/ToDoDatabase.kt +++ b/app/src/main/java/com/example/todoapp/data/ToDoDatabase.kt @@ -9,29 +9,47 @@ import com.example.todoapp.data.models.ToDoData @Database(entities = [ToDoData::class], version = 1, exportSchema = false) @TypeConverters(Converter::class) -abstract class ToDoDatabase: RoomDatabase() { +abstract class ToDoDatabase : RoomDatabase() { abstract fun toDoDao(): ToDoDao +// companion object { +// @Volatile +// private var INSTANCE: ToDoDatabase? = null +// +// fun getDatabase(context: Context): ToDoDatabase { +// val tempInstance = INSTANCE +// if (tempInstance != null) { +// return tempInstance +// } +// synchronized(this) { +// val instance = Room.databaseBuilder( +// context.applicationContext, +// ToDoDatabase::class.java, +// "todo_database" +// ).build() +// INSTANCE = instance +// return instance +// } +// } +// } + companion object { @Volatile private var INSTANCE: ToDoDatabase? = null - fun getDatabase(context: Context): ToDoDatabase { - val tempInstance = INSTANCE - if(tempInstance != null){ - return tempInstance + fun getDatabase(context: Context): ToDoDatabase = + INSTANCE ?: synchronized(this) { + INSTANCE + ?: buildDatabase(context).also { INSTANCE = it } } - synchronized(this){ - val instance = Room.databaseBuilder( - context.applicationContext, - ToDoDatabase::class.java, - "todo_database" - ).build() - INSTANCE = instance - return instance - } - } + + private fun buildDatabase(context: Context) = + Room.databaseBuilder( + context.applicationContext, + ToDoDatabase::class.java, "todo_database" + ) + .build() } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6424a1e..a9a3584 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,8 +9,8 @@