Pixelcat-App-Android/app/src/main/kotlin/at/connyduck/pixelcat/components/profile/ProfileViewModel.kt

107 lines
3.4 KiB
Kotlin
Raw Normal View History

2020-06-17 19:07:47 +02:00
/*
* Copyright (C) 2020 Conny Duck
*
* This file is part of Pixelcat.
*
* Pixelcat 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.
*
* Pixelcat 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-12 15:44:45 +02:00
package at.connyduck.pixelcat.components.profile
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
2020-06-17 19:58:05 +02:00
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.components.util.Error
import at.connyduck.pixelcat.components.util.Success
import at.connyduck.pixelcat.components.util.UiState
import at.connyduck.pixelcat.db.AccountManager
import at.connyduck.pixelcat.model.Account
import at.connyduck.pixelcat.model.Relationship
import at.connyduck.pixelcat.network.FediverseApi
2020-06-17 19:58:05 +02:00
import kotlinx.coroutines.FlowPreview
2020-06-12 15:44:45 +02:00
import kotlinx.coroutines.launch
import javax.inject.Inject
class ProfileViewModel @Inject constructor(
2020-09-29 20:24:02 +02:00
private val api: FediverseApi,
2020-06-12 15:44:45 +02:00
private val accountManager: AccountManager
2020-06-12 19:58:15 +02:00
) : ViewModel() {
2020-06-12 15:44:45 +02:00
val profile = MutableLiveData<UiState<Account>>()
val relationship = MutableLiveData<UiState<Relationship>>()
2020-06-17 19:58:05 +02:00
@OptIn(FlowPreview::class)
@ExperimentalPagingApi
val imageFlow = Pager(
config = PagingConfig(pageSize = 10, enablePlaceholders = false),
2020-09-29 20:24:02 +02:00
pagingSourceFactory = { ProfileImagePagingSource(api, accountId, accountManager) }
2020-06-17 19:58:05 +02:00
).flow
.cachedIn(viewModelScope)
2020-06-12 15:44:45 +02:00
val isSelf: Boolean
get() = accountId == null
private var accountId: String? = null
fun load(reload: Boolean = false) {
loadAccount(reload)
if (!isSelf) {
loadRelationship(reload)
}
}
fun setAccountInfo(accountId: String?) {
this@ProfileViewModel.accountId = accountId
load(false)
}
private fun loadAccount(reload: Boolean = false) {
if (profile.value == null || reload) {
viewModelScope.launch {
2020-09-29 20:24:02 +02:00
api.account(getAccountId()).fold(
2020-06-12 19:58:15 +02:00
{
profile.value = Success(it)
},
{
profile.value = Error(cause = it)
}
)
2020-06-12 15:44:45 +02:00
}
}
}
private fun loadRelationship(reload: Boolean = false) {
if (relationship.value == null || reload) {
viewModelScope.launch {
2020-09-29 20:24:02 +02:00
api.relationships(listOf(getAccountId())).fold(
2020-06-12 19:58:15 +02:00
{
relationship.value = Success(it.first())
},
{
relationship.value = Error(cause = it)
}
)
2020-06-12 15:44:45 +02:00
}
}
}
private suspend fun getAccountId(): String {
return accountId ?: accountManager.activeAccount()?.accountId!!
}
2020-06-12 19:58:15 +02:00
}