Skip to content

Commit

Permalink
use coroutine instead of executor
Browse files Browse the repository at this point in the history
  • Loading branch information
kulloveth committed Apr 28, 2020
1 parent ab0f043 commit 392964a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

implementation 'com.google.code.gson:gson:2.8.6'
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".data.ui.MainActivity">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface ContinentDao {
fun getAllContinent(): LiveData<List<ContinentEntity>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(continent: ContinentEntity)
suspend fun insert(continent: ContinentEntity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continent
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.ContinentEntity
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Countrys
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.concurrent.Executors

@Database(entities = arrayOf(ContinentEntity::class), version = 1, exportSchema = false)
@Database(entities = [ContinentEntity::class], version = 1, exportSchema = false)

public abstract class PlaceDatabase : RoomDatabase() {
abstract class PlaceDatabase : RoomDatabase() {

abstract fun continentDao(): ContinentDao

Expand All @@ -24,22 +26,22 @@ public abstract class PlaceDatabase : RoomDatabase() {
@Volatile
private var INSTANCE: PlaceDatabase? = null

fun getDatabase(context: Context): PlaceDatabase {
fun getDatabase(context: Context, scope: CoroutineScope): PlaceDatabase {

return INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also {
INSTANCE ?: buildDatabase(context, scope).also {
INSTANCE = it
}
}
}

private fun buildDatabase(context: Context): PlaceDatabase {
private fun buildDatabase(context: Context, scope: CoroutineScope): PlaceDatabase {
return Room.databaseBuilder(context, PlaceDatabase::class.java, "place_db")
.addCallback(object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

Executors.newSingleThreadExecutor().execute {
scope.launch {
INSTANCE?.let {
for (continent: ContinentEntity in DataGenerator.getContinents()) {
it.continentDao().insert(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data.ui
package com.developer.kulloveth.expandablelistsamplewithroom.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Expand All @@ -9,6 +9,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.developer.kulloveth.expandablelistsamplewithroom.R
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.ContinentEntity
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
import com.developer.kulloveth.expandablelistsamplewithroom.data.ui.ContinentAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data.ui
package com.developer.kulloveth.expandablelistsamplewithroom.ui

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import com.developer.kulloveth.expandablelistsamplewithroom.data.db.PlaceDatabase
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.ContinentEntity
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
Expand All @@ -16,7 +17,7 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica


init {
val continentDao = PlaceDatabase.getDatabase(application).continentDao()
val continentDao = PlaceDatabase.getDatabase(application, viewModelScope).continentDao()
repository = Repository(continentDao)
continents = repository.allContinents
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/countrys_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="50dp"
android:padding="0dp">

<TextView
Expand Down

0 comments on commit 392964a

Please sign in to comment.