This commit is contained in:
Zongle Wang 2024-04-28 23:20:38 +05:00 committed by GitHub
commit 9dcc6deb47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 17 deletions

View File

@ -479,8 +479,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
private fun setupRefreshLayout() { private fun setupRefreshLayout() {
binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() } binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() }
lifecycleScope.launch { lifecycleScope.launch {
viewModel.isRefreshing.collect { isRefreshing -> viewModel.isRefreshing.collect {
binding.swipeToRefreshLayout.isRefreshing = isRefreshing == true binding.swipeToRefreshLayout.isRefreshing = it
} }
} }
binding.swipeToRefreshLayout.setColorSchemeResources(R.color.tusky_blue) binding.swipeToRefreshLayout.setColorSchemeResources(R.color.tusky_blue)
@ -1063,7 +1063,6 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
return true return true
} }
R.id.action_refresh -> { R.id.action_refresh -> {
binding.swipeToRefreshLayout.isRefreshing = true
onRefresh() onRefresh()
return true return true
} }

View File

@ -21,13 +21,9 @@ import com.keylesspalace.tusky.util.Success
import com.keylesspalace.tusky.util.getDomain import com.keylesspalace.tusky.util.getDomain
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -46,10 +42,8 @@ class AccountViewModel @Inject constructor(
private val _noteSaved = MutableStateFlow(false) private val _noteSaved = MutableStateFlow(false)
val noteSaved: StateFlow<Boolean> = _noteSaved.asStateFlow() val noteSaved: StateFlow<Boolean> = _noteSaved.asStateFlow()
private val _isRefreshing = MutableSharedFlow<Boolean>(1, onBufferOverflow = BufferOverflow.DROP_OLDEST) private val _isRefreshing = MutableStateFlow(false)
val isRefreshing: SharedFlow<Boolean> = _isRefreshing.asSharedFlow() val isRefreshing: StateFlow<Boolean> = _isRefreshing.asStateFlow()
private var isDataLoading = false
lateinit var accountId: String lateinit var accountId: String
var isSelf = false var isSelf = false
@ -76,7 +70,9 @@ class AccountViewModel @Inject constructor(
private fun obtainAccount(reload: Boolean = false) { private fun obtainAccount(reload: Boolean = false) {
if (_accountData.value == null || reload) { if (_accountData.value == null || reload) {
isDataLoading = true if (reload) {
_isRefreshing.value = true
}
_accountData.value = Loading() _accountData.value = Loading()
viewModelScope.launch { viewModelScope.launch {
@ -87,14 +83,12 @@ class AccountViewModel @Inject constructor(
isFromOwnDomain = domain == activeAccount.domain isFromOwnDomain = domain == activeAccount.domain
_accountData.value = Success(account) _accountData.value = Success(account)
isDataLoading = false _isRefreshing.value = false
_isRefreshing.emit(false)
}, },
{ t -> { t ->
Log.w(TAG, "failed obtaining account", t) Log.w(TAG, "failed obtaining account", t)
_accountData.value = Error(cause = t) _accountData.value = Error(cause = t)
isDataLoading = false _isRefreshing.value = false
_isRefreshing.emit(false)
} }
) )
} }
@ -316,7 +310,7 @@ class AccountViewModel @Inject constructor(
} }
private fun reload(isReload: Boolean = false) { private fun reload(isReload: Boolean = false) {
if (isDataLoading) { if (_isRefreshing.value) {
return return
} }
accountId.let { accountId.let {