2021-06-17 18:54:56 +02:00
|
|
|
/* Copyright 2021 Tusky Contributors
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
package com.keylesspalace.tusky.components.conversation
|
|
|
|
|
|
|
|
import android.util.Log
|
2021-06-17 18:54:56 +02:00
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import androidx.paging.ExperimentalPagingApi
|
|
|
|
import androidx.paging.Pager
|
|
|
|
import androidx.paging.PagingConfig
|
|
|
|
import androidx.paging.cachedIn
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
|
|
|
import com.keylesspalace.tusky.db.AppDatabase
|
2021-06-17 18:54:56 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.network.TimelineCases
|
2020-01-02 18:50:58 +01:00
|
|
|
import com.keylesspalace.tusky.util.RxAwareViewModel
|
2021-06-17 18:54:56 +02:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.rx3.await
|
2019-02-12 19:22:37 +01:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
2019-02-15 20:41:10 +01:00
|
|
|
class ConversationsViewModel @Inject constructor(
|
2021-06-11 20:15:40 +02:00
|
|
|
private val timelineCases: TimelineCases,
|
|
|
|
private val database: AppDatabase,
|
2021-06-17 18:54:56 +02:00
|
|
|
private val accountManager: AccountManager,
|
|
|
|
private val api: MastodonApi
|
2020-01-02 18:50:58 +01:00
|
|
|
) : RxAwareViewModel() {
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
@ExperimentalPagingApi
|
|
|
|
val conversationFlow = Pager(
|
|
|
|
config = PagingConfig(pageSize = 10, enablePlaceholders = false, initialLoadSize = 20),
|
|
|
|
remoteMediator = ConversationsRemoteMediator(accountManager.activeAccount!!.id, api, database),
|
|
|
|
pagingSourceFactory = { database.conversationDao().conversationsForAccount(accountManager.activeAccount!!.id) }
|
|
|
|
)
|
|
|
|
.flow
|
|
|
|
.cachedIn(viewModelScope)
|
|
|
|
|
|
|
|
fun favourite(favourite: Boolean, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
try {
|
|
|
|
timelineCases.favourite(conversation.lastStatus.id, favourite).await()
|
|
|
|
|
|
|
|
val newConversation = conversation.copy(
|
|
|
|
lastStatus = conversation.lastStatus.copy(favourited = favourite)
|
|
|
|
)
|
|
|
|
|
|
|
|
database.conversationDao().insert(newConversation)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "failed to favourite status", e)
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun bookmark(bookmark: Boolean, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
try {
|
|
|
|
timelineCases.bookmark(conversation.lastStatus.id, bookmark).await()
|
2019-04-22 10:11:00 +02:00
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
val newConversation = conversation.copy(
|
|
|
|
lastStatus = conversation.lastStatus.copy(bookmarked = bookmark)
|
|
|
|
)
|
2019-04-22 10:11:00 +02:00
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
database.conversationDao().insert(newConversation)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "failed to bookmark status", e)
|
|
|
|
}
|
2019-11-19 10:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun voteInPoll(choices: List<Int>, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
try {
|
|
|
|
val poll = timelineCases.voteInPoll(conversation.lastStatus.id, conversation.lastStatus.poll?.id!!, choices).await()
|
|
|
|
val newConversation = conversation.copy(
|
|
|
|
lastStatus = conversation.lastStatus.copy(poll = poll)
|
|
|
|
)
|
|
|
|
|
|
|
|
database.conversationDao().insert(newConversation)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "failed to vote in poll", e)
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun expandHiddenStatus(expanded: Boolean, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
2019-02-12 19:22:37 +01:00
|
|
|
val newConversation = conversation.copy(
|
2021-06-11 20:15:40 +02:00
|
|
|
lastStatus = conversation.lastStatus.copy(expanded = expanded)
|
2019-02-12 19:22:37 +01:00
|
|
|
)
|
2019-02-15 20:41:10 +01:00
|
|
|
saveConversationToDb(newConversation)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun collapseLongStatus(collapsed: Boolean, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
2019-02-12 19:22:37 +01:00
|
|
|
val newConversation = conversation.copy(
|
2021-06-11 20:15:40 +02:00
|
|
|
lastStatus = conversation.lastStatus.copy(collapsed = collapsed)
|
2019-02-12 19:22:37 +01:00
|
|
|
)
|
2019-02-15 20:41:10 +01:00
|
|
|
saveConversationToDb(newConversation)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun showContent(showing: Boolean, conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
2019-02-12 19:22:37 +01:00
|
|
|
val newConversation = conversation.copy(
|
2021-06-11 20:15:40 +02:00
|
|
|
lastStatus = conversation.lastStatus.copy(showingHiddenContent = showing)
|
2019-02-12 19:22:37 +01:00
|
|
|
)
|
2019-02-15 20:41:10 +01:00
|
|
|
saveConversationToDb(newConversation)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
fun remove(conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
try {
|
|
|
|
api.deleteConversation(conversationId = conversation.id)
|
|
|
|
|
|
|
|
database.conversationDao().delete(conversation)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "failed to delete conversation", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun muteConversation(conversation: ConversationEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
try {
|
|
|
|
val newStatus = timelineCases.muteConversation(
|
|
|
|
conversation.lastStatus.id,
|
|
|
|
!conversation.lastStatus.muted
|
|
|
|
).await()
|
|
|
|
|
|
|
|
val newConversation = conversation.copy(
|
|
|
|
lastStatus = newStatus.toEntity()
|
|
|
|
)
|
|
|
|
|
|
|
|
database.conversationDao().insert(newConversation)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.w(TAG, "failed to mute conversation", e)
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
suspend fun saveConversationToDb(conversation: ConversationEntity) {
|
2019-06-17 13:43:11 +02:00
|
|
|
database.conversationDao().insert(conversation)
|
2019-02-15 20:41:10 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
companion object {
|
|
|
|
private const val TAG = "ConversationsViewModel"
|
|
|
|
}
|
2020-01-29 19:25:55 +01:00
|
|
|
}
|