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:
parent
9089a2831c
commit
27cf5f5380
|
@ -114,7 +114,7 @@ dependencies {
|
||||||
//room
|
//room
|
||||||
implementation 'androidx.room:room-runtime:2.0.0'
|
implementation 'androidx.room:room-runtime:2.0.0'
|
||||||
kapt 'androidx.room:room-compiler: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"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
implementation "com.google.dagger:dagger:$daggerVersion"
|
implementation "com.google.dagger:dagger:$daggerVersion"
|
||||||
|
@ -137,5 +137,5 @@ dependencies {
|
||||||
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
|
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
|
||||||
implementation 'com.uber.autodispose:autodispose-android-archcomponents:1.1.0'
|
implementation 'com.uber.autodispose:autodispose-android-archcomponents:1.1.0'
|
||||||
implementation 'com.uber.autodispose:autodispose-ktx: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'
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,10 @@ import com.keylesspalace.tusky.db.AppDatabase
|
||||||
import com.keylesspalace.tusky.network.TimelineCases
|
import com.keylesspalace.tusky.network.TimelineCases
|
||||||
import com.keylesspalace.tusky.util.Listing
|
import com.keylesspalace.tusky.util.Listing
|
||||||
import com.keylesspalace.tusky.util.NetworkState
|
import com.keylesspalace.tusky.util.NetworkState
|
||||||
|
import io.reactivex.Single
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.rxkotlin.addTo
|
import io.reactivex.rxkotlin.addTo
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ConversationsViewModel @Inject constructor(
|
class ConversationsViewModel @Inject constructor(
|
||||||
|
@ -20,7 +22,7 @@ class ConversationsViewModel @Inject constructor(
|
||||||
private val timelineCases: TimelineCases,
|
private val timelineCases: TimelineCases,
|
||||||
private val database: AppDatabase,
|
private val database: AppDatabase,
|
||||||
private val accountManager: AccountManager
|
private val accountManager: AccountManager
|
||||||
): ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val repoResult = MutableLiveData<Listing<ConversationEntity>>()
|
private val repoResult = MutableLiveData<Listing<ConversationEntity>>()
|
||||||
|
|
||||||
|
@ -32,7 +34,7 @@ class ConversationsViewModel @Inject constructor(
|
||||||
|
|
||||||
fun load() {
|
fun load() {
|
||||||
val accountId = accountManager.activeAccount?.id ?: return
|
val accountId = accountManager.activeAccount?.id ?: return
|
||||||
if(repoResult.value == null) {
|
if (repoResult.value == null) {
|
||||||
repository.refresh(accountId, false)
|
repository.refresh(accountId, false)
|
||||||
}
|
}
|
||||||
repoResult.value = repository.conversations(accountId)
|
repoResult.value = repository.conversations(accountId)
|
||||||
|
@ -49,12 +51,17 @@ class ConversationsViewModel @Inject constructor(
|
||||||
fun favourite(favourite: Boolean, position: Int) {
|
fun favourite(favourite: Boolean, position: Int) {
|
||||||
conversations.value?.getOrNull(position)?.let { conversation ->
|
conversations.value?.getOrNull(position)?.let { conversation ->
|
||||||
timelineCases.favourite(conversation.lastStatus.toStatus(), favourite)
|
timelineCases.favourite(conversation.lastStatus.toStatus(), favourite)
|
||||||
.subscribe({
|
.flatMap {
|
||||||
val newConversation = conversation.copy(
|
val newConversation = conversation.copy(
|
||||||
lastStatus = conversation.lastStatus.copy(favourited = favourite)
|
lastStatus = conversation.lastStatus.copy(favourited = favourite)
|
||||||
)
|
)
|
||||||
|
Single.fromCallable {
|
||||||
database.conversationDao().insert(newConversation)
|
database.conversationDao().insert(newConversation)
|
||||||
}, { t -> Log.w("ConversationViewModel", "Failed to favourite conversation", t) })
|
}
|
||||||
|
}
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.doOnError { t -> Log.w("ConversationViewModel", "Failed to favourite conversation", t) }
|
||||||
|
.subscribe()
|
||||||
.addTo(disposables)
|
.addTo(disposables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +69,10 @@ class ConversationsViewModel @Inject constructor(
|
||||||
|
|
||||||
fun expandHiddenStatus(expanded: Boolean, position: Int) {
|
fun expandHiddenStatus(expanded: Boolean, position: Int) {
|
||||||
conversations.value?.getOrNull(position)?.let { conversation ->
|
conversations.value?.getOrNull(position)?.let { conversation ->
|
||||||
|
|
||||||
val newConversation = conversation.copy(
|
val newConversation = conversation.copy(
|
||||||
lastStatus = conversation.lastStatus.copy(expanded = expanded)
|
lastStatus = conversation.lastStatus.copy(expanded = expanded)
|
||||||
)
|
)
|
||||||
database.conversationDao().insert(newConversation)
|
saveConversationToDb(newConversation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +81,7 @@ class ConversationsViewModel @Inject constructor(
|
||||||
val newConversation = conversation.copy(
|
val newConversation = conversation.copy(
|
||||||
lastStatus = conversation.lastStatus.copy(collapsed = collapsed)
|
lastStatus = conversation.lastStatus.copy(collapsed = collapsed)
|
||||||
)
|
)
|
||||||
database.conversationDao().insert(newConversation)
|
saveConversationToDb(newConversation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +90,7 @@ class ConversationsViewModel @Inject constructor(
|
||||||
val newConversation = conversation.copy(
|
val newConversation = conversation.copy(
|
||||||
lastStatus = conversation.lastStatus.copy(showingHiddenContent = showing)
|
lastStatus = conversation.lastStatus.copy(showingHiddenContent = showing)
|
||||||
)
|
)
|
||||||
database.conversationDao().insert(newConversation)
|
saveConversationToDb(newConversation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,8 +99,20 @@ class ConversationsViewModel @Inject constructor(
|
||||||
/* this is not ideal since deleting last toot from an conversation
|
/* this is not ideal since deleting last toot from an conversation
|
||||||
should not delete the conversation but show another toot of the conversation */
|
should not delete the conversation but show another toot of the conversation */
|
||||||
timelineCases.delete(conversation.lastStatus.id)
|
timelineCases.delete(conversation.lastStatus.id)
|
||||||
|
Single.fromCallable {
|
||||||
database.conversationDao().delete(conversation)
|
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() {
|
override fun onCleared() {
|
||||||
|
|
Loading…
Reference in New Issue