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-07-19 20:10:20 +02:00
|
|
|
package com.keylesspalace.tusky.components.search
|
|
|
|
|
|
|
|
import android.util.Log
|
2021-06-17 18:54:56 +02:00
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import androidx.paging.Pager
|
|
|
|
import androidx.paging.PagingConfig
|
|
|
|
import androidx.paging.cachedIn
|
|
|
|
import com.keylesspalace.tusky.components.search.adapter.SearchPagingSourceFactory
|
2019-07-19 20:10:20 +02:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
2021-06-17 18:54:56 +02:00
|
|
|
import com.keylesspalace.tusky.entity.DeletedStatus
|
|
|
|
import com.keylesspalace.tusky.entity.Poll
|
2020-01-13 13:57:44 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Status
|
2019-07-19 20:10:20 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
|
|
|
import com.keylesspalace.tusky.network.TimelineCases
|
2021-06-17 18:54:56 +02:00
|
|
|
import com.keylesspalace.tusky.util.RxAwareViewModel
|
|
|
|
import com.keylesspalace.tusky.util.toViewData
|
2019-07-19 20:10:20 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData
|
2021-05-16 19:53:27 +02:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.core.Single
|
2019-07-19 20:10:20 +02:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class SearchViewModel @Inject constructor(
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi: MastodonApi,
|
|
|
|
private val timelineCases: TimelineCases,
|
|
|
|
private val accountManager: AccountManager
|
2020-01-13 13:57:44 +01:00
|
|
|
) : RxAwareViewModel() {
|
2019-07-19 20:10:20 +02:00
|
|
|
|
2019-11-17 20:58:54 +01:00
|
|
|
var currentQuery: String = ""
|
2019-07-19 20:10:20 +02:00
|
|
|
|
|
|
|
var activeAccount: AccountEntity?
|
|
|
|
get() = accountManager.activeAccount
|
|
|
|
set(value) {
|
|
|
|
accountManager.activeAccount = value
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:57:44 +01:00
|
|
|
val mediaPreviewEnabled = activeAccount?.mediaPreviewEnabled ?: false
|
|
|
|
val alwaysShowSensitiveMedia = activeAccount?.alwaysShowSensitiveMedia ?: false
|
|
|
|
val alwaysOpenSpoiler = activeAccount?.alwaysOpenSpoiler ?: false
|
2019-07-19 20:10:20 +02:00
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
private val loadedStatuses: MutableList<Pair<Status, StatusViewData.Concrete>> = mutableListOf()
|
|
|
|
|
|
|
|
private val statusesPagingSourceFactory = SearchPagingSourceFactory(mastodonApi, SearchType.Status, loadedStatuses) {
|
|
|
|
it.statuses.map { status -> Pair(status, status.toViewData(alwaysShowSensitiveMedia, alwaysOpenSpoiler)) }
|
|
|
|
.apply {
|
|
|
|
loadedStatuses.addAll(this)
|
2021-06-11 20:15:40 +02:00
|
|
|
}
|
2021-06-17 18:54:56 +02:00
|
|
|
}
|
|
|
|
private val accountsPagingSourceFactory = SearchPagingSourceFactory(mastodonApi, SearchType.Account) {
|
|
|
|
it.accounts
|
|
|
|
}
|
|
|
|
private val hashtagsPagingSourceFactory = SearchPagingSourceFactory(mastodonApi, SearchType.Hashtag) {
|
|
|
|
it.hashtags
|
|
|
|
}
|
|
|
|
|
|
|
|
val statusesFlow = Pager(
|
|
|
|
config = PagingConfig(pageSize = DEFAULT_LOAD_SIZE, initialLoadSize = DEFAULT_LOAD_SIZE),
|
|
|
|
pagingSourceFactory = statusesPagingSourceFactory
|
|
|
|
).flow
|
|
|
|
.cachedIn(viewModelScope)
|
|
|
|
|
|
|
|
val accountsFlow = Pager(
|
|
|
|
config = PagingConfig(pageSize = DEFAULT_LOAD_SIZE, initialLoadSize = DEFAULT_LOAD_SIZE),
|
|
|
|
pagingSourceFactory = accountsPagingSourceFactory
|
|
|
|
).flow
|
|
|
|
.cachedIn(viewModelScope)
|
2019-07-19 20:10:20 +02:00
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
val hashtagsFlow = Pager(
|
|
|
|
config = PagingConfig(pageSize = DEFAULT_LOAD_SIZE, initialLoadSize = DEFAULT_LOAD_SIZE),
|
|
|
|
pagingSourceFactory = hashtagsPagingSourceFactory
|
|
|
|
).flow
|
|
|
|
.cachedIn(viewModelScope)
|
|
|
|
|
|
|
|
fun search(query: String) {
|
|
|
|
loadedStatuses.clear()
|
|
|
|
statusesPagingSourceFactory.newSearch(query)
|
|
|
|
accountsPagingSourceFactory.newSearch(query)
|
|
|
|
hashtagsPagingSourceFactory.newSearch(query)
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun removeItem(status: Pair<Status, StatusViewData.Concrete>) {
|
|
|
|
timelineCases.delete(status.first.id)
|
2021-06-17 18:54:56 +02:00
|
|
|
.subscribe({
|
|
|
|
if (loadedStatuses.remove(status))
|
|
|
|
statusesPagingSourceFactory.invalidate()
|
|
|
|
}, {
|
|
|
|
err -> Log.d(TAG, "Failed to delete status", err)
|
|
|
|
})
|
|
|
|
.autoDispose()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun expandedChange(status: Pair<Status, StatusViewData.Concrete>, expanded: Boolean) {
|
|
|
|
val idx = loadedStatuses.indexOf(status)
|
|
|
|
if (idx >= 0) {
|
2021-06-17 18:54:56 +02:00
|
|
|
loadedStatuses[idx] = Pair(status.first, status.second.copy(isExpanded = expanded))
|
|
|
|
statusesPagingSourceFactory.invalidate()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun reblog(status: Pair<Status, StatusViewData.Concrete>, reblog: Boolean) {
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.reblog(status.first.id, reblog)
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
|
|
|
{ setRebloggedForStatus(status, reblog) },
|
2021-06-17 18:54:56 +02:00
|
|
|
{ t -> Log.d(TAG, "Failed to reblog status ${status.first.id}", t) }
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
.autoDispose()
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
private fun setRebloggedForStatus(status: Pair<Status, StatusViewData.Concrete>, reblog: Boolean) {
|
2019-07-19 20:10:20 +02:00
|
|
|
status.first.reblogged = reblog
|
|
|
|
status.first.reblog?.reblogged = reblog
|
2021-06-17 18:54:56 +02:00
|
|
|
statusesPagingSourceFactory.invalidate()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun contentHiddenChange(status: Pair<Status, StatusViewData.Concrete>, isShowing: Boolean) {
|
|
|
|
val idx = loadedStatuses.indexOf(status)
|
|
|
|
if (idx >= 0) {
|
2021-06-17 18:54:56 +02:00
|
|
|
loadedStatuses[idx] = Pair(status.first, status.second.copy(isShowingContent = isShowing))
|
|
|
|
statusesPagingSourceFactory.invalidate()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun collapsedChange(status: Pair<Status, StatusViewData.Concrete>, collapsed: Boolean) {
|
|
|
|
val idx = loadedStatuses.indexOf(status)
|
|
|
|
if (idx >= 0) {
|
2021-06-17 18:54:56 +02:00
|
|
|
loadedStatuses[idx] = Pair(status.first, status.second.copy(isCollapsed = collapsed))
|
|
|
|
statusesPagingSourceFactory.invalidate()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun voteInPoll(status: Pair<Status, StatusViewData.Concrete>, choices: MutableList<Int>) {
|
|
|
|
val votedPoll = status.first.actionableStatus.poll!!.votedCopy(choices)
|
|
|
|
updateStatus(status, votedPoll)
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.voteInPoll(status.first.id, votedPoll.id, choices)
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
|
|
|
{ newPoll -> updateStatus(status, newPoll) },
|
2021-06-17 18:54:56 +02:00
|
|
|
{ t -> Log.d(TAG, "Failed to vote in poll: ${status.first.id}", t) }
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
.autoDispose()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateStatus(status: Pair<Status, StatusViewData.Concrete>, newPoll: Poll) {
|
|
|
|
val idx = loadedStatuses.indexOf(status)
|
|
|
|
if (idx >= 0) {
|
2021-06-11 20:15:40 +02:00
|
|
|
val newStatus = status.first.copy(poll = newPoll)
|
|
|
|
val newViewData = status.second.copy(status = newStatus)
|
|
|
|
loadedStatuses[idx] = Pair(newStatus, newViewData)
|
2021-06-17 18:54:56 +02:00
|
|
|
statusesPagingSourceFactory.invalidate()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun favorite(status: Pair<Status, StatusViewData.Concrete>, isFavorited: Boolean) {
|
2021-06-11 20:15:40 +02:00
|
|
|
status.first.favourited = isFavorited
|
2021-06-17 18:54:56 +02:00
|
|
|
statusesPagingSourceFactory.invalidate()
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.favourite(status.first.id, isFavorited)
|
|
|
|
.onErrorReturnItem(status.first)
|
|
|
|
.subscribe()
|
|
|
|
.autoDispose()
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:15:32 +01:00
|
|
|
fun bookmark(status: Pair<Status, StatusViewData.Concrete>, isBookmarked: Boolean) {
|
2021-06-11 20:15:40 +02:00
|
|
|
status.first.bookmarked = isBookmarked
|
2021-06-17 18:54:56 +02:00
|
|
|
statusesPagingSourceFactory.invalidate()
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.bookmark(status.first.id, isBookmarked)
|
|
|
|
.onErrorReturnItem(status.first)
|
|
|
|
.subscribe()
|
|
|
|
.autoDispose()
|
2019-11-19 10:15:32 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 20:10:20 +02:00
|
|
|
fun getAllAccountsOrderedByActive(): List<AccountEntity> {
|
|
|
|
return accountManager.getAllAccountsOrderedByActive()
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:37:41 +02:00
|
|
|
fun muteAccount(accountId: String, notifications: Boolean, duration: Int?) {
|
2021-01-15 21:05:36 +01:00
|
|
|
timelineCases.mute(accountId, notifications, duration)
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun pinAccount(status: Status, isPin: Boolean) {
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.pin(status.id, isPin)
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun blockAccount(accountId: String) {
|
|
|
|
timelineCases.block(accountId)
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:54:46 +02:00
|
|
|
fun deleteStatus(id: String): Single<DeletedStatus> {
|
|
|
|
return timelineCases.delete(id)
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 21:06:04 +01:00
|
|
|
fun muteConversation(status: Pair<Status, StatusViewData.Concrete>, mute: Boolean) {
|
|
|
|
val idx = loadedStatuses.indexOf(status)
|
|
|
|
if (idx >= 0) {
|
2021-06-11 20:15:40 +02:00
|
|
|
val newStatus = status.first.copy(muted = mute)
|
|
|
|
val newPair = Pair(
|
|
|
|
newStatus,
|
|
|
|
status.second.copy(status = newStatus)
|
|
|
|
)
|
2020-03-24 21:06:04 +01:00
|
|
|
loadedStatuses[idx] = newPair
|
2021-06-17 18:54:56 +02:00
|
|
|
statusesPagingSourceFactory.invalidate()
|
2020-03-24 21:06:04 +01:00
|
|
|
}
|
2021-06-11 20:15:40 +02:00
|
|
|
timelineCases.muteConversation(status.first.id, mute)
|
|
|
|
.onErrorReturnItem(status.first)
|
|
|
|
.subscribe()
|
|
|
|
.autoDispose()
|
2020-03-24 21:06:04 +01:00
|
|
|
}
|
2019-07-19 20:10:20 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "SearchViewModel"
|
2021-06-17 18:54:56 +02:00
|
|
|
private const val DEFAULT_LOAD_SIZE = 20
|
2019-07-19 20:10:20 +02:00
|
|
|
}
|
|
|
|
}
|