From e23ed6806eca0b09e4289fb7bd3ff82746f4beb6 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 15 Feb 2019 20:41:10 +0100 Subject: [PATCH] call db functions on background thread in ConversationsViewModel (#1040) * call db functions on background thread in ConversationsViewModel * use stable library versions --- app/build.gradle | 4 +- .../conversation/ConversationsViewModel.kt | 42 +++++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 252626eb..ee8099b4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -114,7 +114,7 @@ dependencies { //room implementation 'androidx.room:room-runtime:2.0.0' kapt 'androidx.room:room-compiler:2.0.0' - implementation 'android.arch.persistence.room:rxjava2:1.1.1' + implementation 'androidx.room:room-rxjava2:2.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' implementation "com.google.dagger:dagger:$daggerVersion" @@ -137,5 +137,5 @@ dependencies { implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0' implementation 'com.uber.autodispose:autodispose-android-archcomponents:1.1.0' implementation 'com.uber.autodispose:autodispose-ktx:1.1.0' - implementation 'androidx.paging:paging-runtime-ktx:2.1.0-rc01' + implementation 'androidx.paging:paging-runtime-ktx:2.1.0' } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt index dfe575f8..9b6c6928 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt @@ -11,28 +11,30 @@ import com.keylesspalace.tusky.db.AppDatabase import com.keylesspalace.tusky.network.TimelineCases import com.keylesspalace.tusky.util.Listing import com.keylesspalace.tusky.util.NetworkState +import io.reactivex.Single import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.addTo +import io.reactivex.schedulers.Schedulers import javax.inject.Inject -class ConversationsViewModel @Inject constructor( +class ConversationsViewModel @Inject constructor( private val repository: ConversationsRepository, private val timelineCases: TimelineCases, private val database: AppDatabase, private val accountManager: AccountManager -): ViewModel() { +) : ViewModel() { private val repoResult = MutableLiveData>() val conversations: LiveData> = Transformations.switchMap(repoResult) { it.pagedList } - val networkState: LiveData = Transformations.switchMap(repoResult) { it.networkState } + val networkState: LiveData = Transformations.switchMap(repoResult) { it.networkState } val refreshState: LiveData = Transformations.switchMap(repoResult) { it.refreshState } private val disposables = CompositeDisposable() fun load() { val accountId = accountManager.activeAccount?.id ?: return - if(repoResult.value == null) { + if (repoResult.value == null) { repository.refresh(accountId, false) } repoResult.value = repository.conversations(accountId) @@ -49,12 +51,17 @@ class ConversationsViewModel @Inject constructor( fun favourite(favourite: Boolean, position: Int) { conversations.value?.getOrNull(position)?.let { conversation -> timelineCases.favourite(conversation.lastStatus.toStatus(), favourite) - .subscribe({ + .flatMap { val newConversation = conversation.copy( lastStatus = conversation.lastStatus.copy(favourited = favourite) ) - database.conversationDao().insert(newConversation) - }, { t -> Log.w("ConversationViewModel", "Failed to favourite conversation", t) }) + Single.fromCallable { + database.conversationDao().insert(newConversation) + } + } + .subscribeOn(Schedulers.io()) + .doOnError { t -> Log.w("ConversationViewModel", "Failed to favourite conversation", t) } + .subscribe() .addTo(disposables) } @@ -62,11 +69,10 @@ class ConversationsViewModel @Inject constructor( fun expandHiddenStatus(expanded: Boolean, position: Int) { conversations.value?.getOrNull(position)?.let { conversation -> - val newConversation = conversation.copy( lastStatus = conversation.lastStatus.copy(expanded = expanded) ) - database.conversationDao().insert(newConversation) + saveConversationToDb(newConversation) } } @@ -75,7 +81,7 @@ class ConversationsViewModel @Inject constructor( val newConversation = conversation.copy( lastStatus = conversation.lastStatus.copy(collapsed = collapsed) ) - database.conversationDao().insert(newConversation) + saveConversationToDb(newConversation) } } @@ -84,7 +90,7 @@ class ConversationsViewModel @Inject constructor( val newConversation = conversation.copy( lastStatus = conversation.lastStatus.copy(showingHiddenContent = showing) ) - database.conversationDao().insert(newConversation) + saveConversationToDb(newConversation) } } @@ -93,10 +99,22 @@ class ConversationsViewModel @Inject constructor( /* this is not ideal since deleting last toot from an conversation should not delete the conversation but show another toot of the conversation */ timelineCases.delete(conversation.lastStatus.id) - database.conversationDao().delete(conversation) + Single.fromCallable { + database.conversationDao().delete(conversation) + } + .subscribeOn(Schedulers.io()) + .subscribe() } } + private fun saveConversationToDb(conversation: ConversationEntity) { + Single.fromCallable { + database.conversationDao().insert(conversation) + } + .subscribeOn(Schedulers.io()) + .subscribe() + } + override fun onCleared() { disposables.dispose() }