2020-06-12 15:44:45 +02:00
|
|
|
package at.connyduck.pixelcat.components.timeline
|
|
|
|
|
2020-06-12 19:58:15 +02:00
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import androidx.paging.ExperimentalPagingApi
|
|
|
|
import androidx.paging.Pager
|
|
|
|
import androidx.paging.PagingConfig
|
|
|
|
import androidx.paging.cachedIn
|
2020-06-12 15:44:45 +02:00
|
|
|
import at.connyduck.pixelcat.db.AccountManager
|
|
|
|
import at.connyduck.pixelcat.db.AppDatabase
|
|
|
|
import at.connyduck.pixelcat.db.entitity.StatusEntity
|
2020-06-14 09:13:19 +02:00
|
|
|
import at.connyduck.pixelcat.db.entitity.toEntity
|
|
|
|
import at.connyduck.pixelcat.model.Status
|
2020-06-12 15:44:45 +02:00
|
|
|
import at.connyduck.pixelcat.network.FediverseApi
|
2020-06-14 09:13:19 +02:00
|
|
|
import at.connyduck.pixelcat.network.calladapter.NetworkResponse
|
2020-06-13 19:55:36 +02:00
|
|
|
import kotlinx.coroutines.FlowPreview
|
|
|
|
import kotlinx.coroutines.flow.asFlow
|
|
|
|
import kotlinx.coroutines.flow.flatMapConcat
|
2020-06-12 15:44:45 +02:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class TimelineViewModel @Inject constructor(
|
|
|
|
private val accountManager: AccountManager,
|
|
|
|
private val db: AppDatabase,
|
2020-06-14 09:13:19 +02:00
|
|
|
private val fediverseApi: FediverseApi
|
2020-06-12 19:58:15 +02:00
|
|
|
) : ViewModel() {
|
2020-06-12 15:44:45 +02:00
|
|
|
|
2020-06-13 19:55:36 +02:00
|
|
|
@OptIn(FlowPreview::class)
|
|
|
|
@ExperimentalPagingApi
|
|
|
|
val statusFlow = accountManager::activeAccount.asFlow()
|
|
|
|
.flatMapConcat { activeAccount ->
|
|
|
|
Pager(
|
|
|
|
config = PagingConfig(pageSize = 10, enablePlaceholders = false),
|
|
|
|
remoteMediator = TimelineRemoteMediator(activeAccount?.id!!, fediverseApi, db),
|
|
|
|
pagingSourceFactory = { db.statusDao().statuses(activeAccount.id) }
|
|
|
|
).flow
|
|
|
|
}
|
|
|
|
.cachedIn(viewModelScope)
|
2020-06-12 15:44:45 +02:00
|
|
|
|
2020-06-13 19:55:36 +02:00
|
|
|
fun onFavorite(status: StatusEntity) {
|
2020-06-12 15:44:45 +02:00
|
|
|
viewModelScope.launch {
|
2020-06-14 09:13:19 +02:00
|
|
|
val alreadyFavourited = status.favourited
|
|
|
|
if (alreadyFavourited) {
|
|
|
|
fediverseApi.unfavouriteStatus(status.actionableId)
|
|
|
|
} else {
|
|
|
|
fediverseApi.favouriteStatus(status.actionableId)
|
|
|
|
}.updateStatusInDb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onBoost(status: StatusEntity) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
val alreadyBoosted = status.reblogged
|
|
|
|
if (alreadyBoosted) {
|
|
|
|
fediverseApi.unreblogStatus(status.actionableId)
|
|
|
|
} else {
|
|
|
|
fediverseApi.reblogStatus(status.actionableId)
|
|
|
|
}.updateStatusInDb()
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:55:36 +02:00
|
|
|
fun onMediaVisibilityChanged(status: StatusEntity) {
|
2020-06-12 15:44:45 +02:00
|
|
|
viewModelScope.launch {
|
2020-06-14 09:13:19 +02:00
|
|
|
db.statusDao().changeMediaVisibility(
|
|
|
|
!status.mediaVisible,
|
|
|
|
status.id,
|
|
|
|
accountManager.activeAccount()?.id!!
|
|
|
|
)
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 09:13:19 +02:00
|
|
|
|
|
|
|
private suspend fun NetworkResponse<Status>.updateStatusInDb() {
|
|
|
|
fold<Any?>(
|
|
|
|
{ updatedStatus ->
|
|
|
|
val accountId = accountManager.activeAccount()?.id!!
|
|
|
|
val updatedStatusEntity = updatedStatus.toEntity(accountId)
|
|
|
|
db.statusDao().insertOrReplace(updatedStatusEntity)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Todo
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|