Skip to content
Permalink
Browse files

Upgrade to Room 2.1.0-alpha05, use withTransaction

Switch to using room-ktx artifacts and use withTransaction instead of beginTransaction, etc.
  • Loading branch information...
ianhanniballake committed Mar 13, 2019
1 parent f3567cf commit 2d04d7963d48880c325a7a107386835eb52ce392
@@ -30,7 +30,7 @@ dependencies {
api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
api "androidx.room:room-coroutines:$roomVersion"
api "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
api("com.google.firebase:firebase-core:$firebaseCoreVersion") {
exclude module: 'support-v4'
@@ -41,7 +41,7 @@ abstract class ProviderDao {
}

@Insert
abstract fun insert(provider: Provider)
abstract suspend fun insert(provider: Provider)

@Update
abstract suspend fun update(provider: Provider)
@@ -50,5 +50,5 @@ abstract class ProviderDao {
abstract suspend fun delete(provider: Provider)

@Query("DELETE FROM provider")
abstract fun deleteAll()
abstract suspend fun deleteAll()
}
@@ -40,9 +40,9 @@ abstract class SourceDao {
@Query("SELECT * FROM sources")
abstract suspend fun getSources(): List<Source>

@get:TypeConverters(ComponentNameTypeConverter::class)
@get:Query("SELECT component_name FROM sources")
abstract val sourceComponentNamesBlocking: List<ComponentName>
@TypeConverters(ComponentNameTypeConverter::class)
@Query("SELECT component_name FROM sources")
abstract suspend fun getSourceComponentNames(): List<ComponentName>

@get:Query("SELECT * FROM sources WHERE selected=1 ORDER BY component_name")
abstract val currentSource: LiveData<Source?>
@@ -58,28 +58,23 @@ abstract class SourceDao {
abstract suspend fun getCurrentSourcesThatWantNetwork(): List<Source>

@Insert
abstract fun insert(source: Source)
abstract suspend fun insert(source: Source)

@TypeConverters(ComponentNameTypeConverter::class)
@Query("SELECT component_name FROM sources WHERE component_name LIKE :packageName || '%'")
abstract fun getSourcesComponentNamesByPackageNameBlocking(packageName: String): List<ComponentName>
abstract suspend fun getSourcesComponentNamesByPackageName(packageName: String): List<ComponentName>

@TypeConverters(ComponentNameTypeConverter::class)
@Query("SELECT * FROM sources WHERE component_name = :componentName")
abstract fun getSourceByComponentNameBlocking(componentName: ComponentName): Source?
abstract suspend fun getSourceByComponentName(componentName: ComponentName): Source?

suspend fun getSourceByComponentName(
componentName: ComponentName
) = withContext(Dispatchers.Default) {
getSourceByComponentNameBlocking(componentName)
}
@Update
abstract fun update(source: Source)
abstract suspend fun update(source: Source)

@Delete
abstract fun delete(source: Source)
abstract suspend fun delete(source: Source)

@TypeConverters(ComponentNameTypeConverter::class)
@Query("DELETE FROM sources WHERE component_name IN (:componentNames)")
abstract fun deleteAll(componentNames: Array<ComponentName>)
abstract suspend fun deleteAll(componentNames: Array<ComponentName>)
}
@@ -34,19 +34,18 @@ import android.util.Log
import androidx.core.content.edit
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.room.withTransaction
import com.google.android.apps.muzei.api.internal.ProtocolConstants
import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Provider
import com.google.android.apps.muzei.util.ContentProviderClientCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.nurik.roman.muzei.androidclientcommon.BuildConfig
import java.util.concurrent.Executors

@@ -82,15 +81,11 @@ class ProviderManager private constructor(private val context: Context)
.also { instance = it }
}

suspend fun select(context: Context, authority: String) = withContext(Dispatchers.Default) {
suspend fun select(context: Context, authority: String) {
val database = MuzeiDatabase.getInstance(context)
database.beginTransaction()
try {
database.withTransaction {
database.providerDao().deleteAll()
database.providerDao().insert(Provider(authority))
database.setTransactionSuccessful()
} finally {
database.endTransaction()
}
}

@@ -39,7 +39,7 @@ buildscript {
pagingVersion = "2.1.0"
playServicesWearableVersion = "16.0.1"
recyclerViewVersion = "1.1.0-alpha02"
roomVersion = "2.1.0-alpha04"
roomVersion = "2.1.0-alpha05"
wearableVersion = "2.4.0"
wearVersion = "1.0.0"
workManagerVersion = "1.0.0"
@@ -66,5 +66,7 @@ allprojects {
repositories {
google()
jcenter()
// Required for Room 2.1.0-alpha05 due to https://youtrack.jetbrains.com/issue/KT-27991
maven { url "https://kotlin.bintray.com/kotlinx/" }
}
}
@@ -35,6 +35,7 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.observe
import androidx.room.withTransaction
import com.google.android.apps.muzei.api.MuzeiArtSource
import com.google.android.apps.muzei.api.internal.ProtocolConstants.ACTION_SUBSCRIBE
import com.google.android.apps.muzei.api.internal.ProtocolConstants.EXTRA_SUBSCRIBER_COMPONENT
@@ -53,7 +54,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.nurik.roman.muzei.BuildConfig
import net.nurik.roman.muzei.BuildConfig.SOURCES_AUTHORITY
import net.nurik.roman.muzei.R
@@ -95,30 +95,22 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li
Log.d(TAG, "Source $source selected.")
}

return withContext(Dispatchers.Default) {
database.beginTransaction()
try {
if (selectedSource != null) {
// Unselect the old source
selectedSource.selected = false
database.sourceDao().update(selectedSource)
}

// Select the new source
val newSource = database.sourceDao().getSourceByComponentName(source)?.apply {
selected = true
database.sourceDao().update(this)
} ?: Source(source).apply {
selected = true
database.sourceDao().insert(this)
}

database.setTransactionSuccessful()
return database.withTransaction {
if (selectedSource != null) {
// Unselect the old source
selectedSource.selected = false
database.sourceDao().update(selectedSource)
}

newSource
} finally {
database.endTransaction()
// Select the new source
val newSource = database.sourceDao().getSourceByComponentName(source)?.apply {
selected = true
database.sourceDao().update(this)
} ?: Source(source).apply {
selected = true
database.sourceDao().insert(this)
}
newSource
}
}

@@ -185,12 +177,11 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li
}
val pm = context.packageManager
val database = MuzeiDatabase.getInstance(context)
database.beginTransaction()
try {
database.withTransaction {
val existingSources = HashSet(if (packageName != null)
database.sourceDao().getSourcesComponentNamesByPackageNameBlocking(packageName)
database.sourceDao().getSourcesComponentNamesByPackageName(packageName)
else
database.sourceDao().sourceComponentNamesBlocking)
database.sourceDao().getSourceComponentNames())
val resolveInfos = pm.queryIntentServices(queryIntent,
PackageManager.GET_META_DATA)
if (resolveInfos != null) {
@@ -202,9 +193,6 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li
}
// Delete sources in the database that have since been removed
database.sourceDao().deleteAll(existingSources.toTypedArray())
database.setTransactionSuccessful()
} finally {
database.endTransaction()
}

// Enable or disable the SourceArtProvider based on whether
@@ -291,7 +279,7 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li
val metaData = info.metaData
val componentName = ComponentName(info.packageName, info.name)
val sourceDao = MuzeiDatabase.getInstance(context).sourceDao()
val existingSource = sourceDao.getSourceByComponentNameBlocking(componentName)
val existingSource = sourceDao.getSourceByComponentName(componentName)
if (metaData != null && metaData.containsKey("replacement")) {
// Skip sources having a replacement MuzeiArtProvider that should be used instead
if (existingSource != null) {
@@ -20,6 +20,7 @@ import android.app.IntentService
import android.content.Intent
import android.util.Log
import androidx.core.net.toUri
import androidx.room.withTransaction
import com.google.android.apps.muzei.api.MuzeiArtSource
import com.google.android.apps.muzei.api.internal.ProtocolConstants.ACTION_PUBLISH_STATE
import com.google.android.apps.muzei.api.internal.ProtocolConstants.EXTRA_STATE
@@ -28,6 +29,8 @@ import com.google.android.apps.muzei.api.internal.SourceState
import com.google.android.apps.muzei.api.provider.Artwork
import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.SourceDao
import kotlinx.coroutines.runBlocking
import net.nurik.roman.muzei.BuildConfig
import net.nurik.roman.muzei.BuildConfig.SOURCES_AUTHORITY
import java.util.ArrayList
@@ -48,12 +51,16 @@ class SourceSubscriberService : IntentService("SourceSubscriberService") {
SourceState.fromBundle(this)
} ?: return // If there's no state, there's nothing to change

update(token, state)
val database = MuzeiDatabase.getInstance(this)
runBlocking {
database.withTransaction {
update(database.sourceDao(), token, state)
}
}
}

private fun update(sourceToken: String, state: SourceState) {
val sourceDao = MuzeiDatabase.getInstance(this).sourceDao()
val source = sourceDao.currentSourceBlocking
private suspend fun update(sourceDao: SourceDao, sourceToken: String, state: SourceState) {
val source = sourceDao.getCurrentSource()
if (source == null || sourceToken != source.componentName.flattenToShortString()) {
Log.w(TAG, "Dropping update from non-selected source, token=$sourceToken " +
"does not match token for ${source?.componentName}")
@@ -36,7 +36,7 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.room:room-coroutines:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.paging:paging-runtime:$pagingVersion"

0 comments on commit 2d04d79

Please sign in to comment.
You can’t perform that action at this time.