call db functions on background thread in ConversationsViewModel (#1040)

* call db functions on background thread in ConversationsViewModel

* use stable library versions
This commit is contained in:
Konrad Pozniak 2019-02-15 20:41:10 +01:00 committed by GitHub
parent 9089a2831c
commit 27cf5f5380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -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'
}

View File

@ -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<Listing<ConversationEntity>>()
val conversations: LiveData<PagedList<ConversationEntity>> = Transformations.switchMap(repoResult) { it.pagedList }
val networkState: LiveData<NetworkState> = Transformations.switchMap(repoResult) { it.networkState }
val networkState: LiveData<NetworkState> = Transformations.switchMap(repoResult) { it.networkState }
val refreshState: LiveData<NetworkState> = 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()
}