|
|
|
@ -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() |
|
|
|
|
} |
|
|
|
|