From 4f4dab9da6ee5cb2ef36ae43bc041ce0766f5827 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 2 Apr 2024 08:15:18 +0800 Subject: [PATCH 1/8] Revert "fix swipe-refresh spinner showing forever when refreshing AccountActivity (#4345)" This reverts commit 5343766886fec9bd1812c28d4383c1c7955b961a. --- .../tusky/components/account/AccountViewModel.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index 91d6dd0ea9..c21e679d2b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -21,13 +21,9 @@ import com.keylesspalace.tusky.util.Success import com.keylesspalace.tusky.util.getDomain import javax.inject.Inject import kotlinx.coroutines.Job -import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch @@ -46,8 +42,8 @@ class AccountViewModel @Inject constructor( private val _noteSaved = MutableStateFlow(false) val noteSaved: StateFlow = _noteSaved.asStateFlow() - private val _isRefreshing = MutableSharedFlow(1, onBufferOverflow = BufferOverflow.DROP_OLDEST) - val isRefreshing: SharedFlow = _isRefreshing.asSharedFlow() + private val _isRefreshing = MutableStateFlow(false) + val isRefreshing: StateFlow = _isRefreshing.asStateFlow() private var isDataLoading = false @@ -88,13 +84,13 @@ class AccountViewModel @Inject constructor( _accountData.value = Success(account) isDataLoading = false - _isRefreshing.emit(false) + _isRefreshing.value = false }, { t -> Log.w(TAG, "failed obtaining account", t) _accountData.value = Error(cause = t) isDataLoading = false - _isRefreshing.emit(false) + _isRefreshing.value = false } ) } From b932917acc28652e4b83fd5cf8a996c73e188d43 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 2 Apr 2024 08:17:04 +0800 Subject: [PATCH 2/8] Set _isRefreshing to true first --- .../keylesspalace/tusky/components/account/AccountViewModel.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index c21e679d2b..21d5831e49 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -73,6 +73,7 @@ class AccountViewModel @Inject constructor( private fun obtainAccount(reload: Boolean = false) { if (_accountData.value == null || reload) { isDataLoading = true + _isRefreshing.value = true _accountData.value = Loading() viewModelScope.launch { From 2d44f3c24537b70cc3149b4b818ec9d71a52b75f Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 2 Apr 2024 08:18:21 +0800 Subject: [PATCH 3/8] Remove redundant isDataLoading --- .../tusky/components/account/AccountViewModel.kt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index 21d5831e49..e2f7d407de 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -45,8 +45,6 @@ class AccountViewModel @Inject constructor( private val _isRefreshing = MutableStateFlow(false) val isRefreshing: StateFlow = _isRefreshing.asStateFlow() - private var isDataLoading = false - lateinit var accountId: String var isSelf = false @@ -72,7 +70,6 @@ class AccountViewModel @Inject constructor( private fun obtainAccount(reload: Boolean = false) { if (_accountData.value == null || reload) { - isDataLoading = true _isRefreshing.value = true _accountData.value = Loading() @@ -84,13 +81,11 @@ class AccountViewModel @Inject constructor( isFromOwnDomain = domain == activeAccount.domain _accountData.value = Success(account) - isDataLoading = false _isRefreshing.value = false }, { t -> Log.w(TAG, "failed obtaining account", t) _accountData.value = Error(cause = t) - isDataLoading = false _isRefreshing.value = false } ) @@ -313,7 +308,7 @@ class AccountViewModel @Inject constructor( } private fun reload(isReload: Boolean = false) { - if (isDataLoading) { + if (_isRefreshing.value) { return } accountId.let { From 8925269b2740cf3dde7fb91eb017c63ee58da1ef Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 2 Apr 2024 19:12:12 +0800 Subject: [PATCH 4/8] Single source --- .../tusky/components/account/AccountActivity.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index 35ec65c7ae..22e1bca57b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -488,8 +488,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide private fun setupRefreshLayout() { binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() } lifecycleScope.launch { - viewModel.isRefreshing.collect { isRefreshing -> - binding.swipeToRefreshLayout.isRefreshing = isRefreshing == true + viewModel.isRefreshing.collect { + binding.swipeToRefreshLayout.isRefreshing = it } } binding.swipeToRefreshLayout.setColorSchemeResources(R.color.tusky_blue) @@ -1072,7 +1072,6 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide return true } R.id.action_refresh -> { - binding.swipeToRefreshLayout.isRefreshing = true onRefresh() return true } From 3d283bd5b97ff9a06ed6b5fa189de6e2a1662445 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 5 Apr 2024 12:44:20 +0800 Subject: [PATCH 5/8] Use refreshing state --- .../components/account/AccountActivity.kt | 12 ++++++++++-- .../components/account/AccountViewModel.kt | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index d200a7663f..e5f58789ec 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -488,8 +488,16 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide private fun setupRefreshLayout() { binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() } lifecycleScope.launch { - viewModel.isRefreshing.collect { - binding.swipeToRefreshLayout.isRefreshing = it + viewModel.refreshState.collect { + when (it) { + AccountViewModel.RefreshState.INITIAL -> Unit + AccountViewModel.RefreshState.REFRESHING -> { + binding.swipeToRefreshLayout.isRefreshing = true + } + AccountViewModel.RefreshState.IDLE -> { + binding.swipeToRefreshLayout.isRefreshing = false + } + } } } binding.swipeToRefreshLayout.setColorSchemeResources(R.color.tusky_blue) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index e2f7d407de..04ca7cdffe 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -42,8 +42,8 @@ class AccountViewModel @Inject constructor( private val _noteSaved = MutableStateFlow(false) val noteSaved: StateFlow = _noteSaved.asStateFlow() - private val _isRefreshing = MutableStateFlow(false) - val isRefreshing: StateFlow = _isRefreshing.asStateFlow() + private val _refreshState = MutableStateFlow(RefreshState.INITIAL) + val refreshState: StateFlow = _refreshState.asStateFlow() lateinit var accountId: String var isSelf = false @@ -70,7 +70,7 @@ class AccountViewModel @Inject constructor( private fun obtainAccount(reload: Boolean = false) { if (_accountData.value == null || reload) { - _isRefreshing.value = true + _refreshState.value = RefreshState.REFRESHING _accountData.value = Loading() viewModelScope.launch { @@ -81,12 +81,12 @@ class AccountViewModel @Inject constructor( isFromOwnDomain = domain == activeAccount.domain _accountData.value = Success(account) - _isRefreshing.value = false + _refreshState.value = RefreshState.IDLE }, { t -> Log.w(TAG, "failed obtaining account", t) _accountData.value = Error(cause = t) - _isRefreshing.value = false + _refreshState.value = RefreshState.IDLE } ) } @@ -308,7 +308,7 @@ class AccountViewModel @Inject constructor( } private fun reload(isReload: Boolean = false) { - if (_isRefreshing.value) { + if (_refreshState.value == RefreshState.REFRESHING) { return } accountId.let { @@ -336,6 +336,12 @@ class AccountViewModel @Inject constructor( UNSUBSCRIBE } + enum class RefreshState { + INITIAL, + REFRESHING, + IDLE + } + companion object { const val TAG = "AccountViewModel" } From 965dc2eadd2258aebf7b7bfcd52fd3b4c85ea7ca Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Thu, 11 Apr 2024 08:08:18 +0800 Subject: [PATCH 6/8] Update app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt Co-authored-by: Konrad Pozniak --- .../tusky/components/account/AccountViewModel.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index 04ca7cdffe..74860370f1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -70,7 +70,9 @@ class AccountViewModel @Inject constructor( private fun obtainAccount(reload: Boolean = false) { if (_accountData.value == null || reload) { - _refreshState.value = RefreshState.REFRESHING + if (reload) { + _refreshState.value = RefreshState.REFRESHING + } _accountData.value = Loading() viewModelScope.launch { From 6e48b6ad793ab8efd74a684d47403c6645139484 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 11 Apr 2024 08:16:14 +0800 Subject: [PATCH 7/8] Revert "Use refreshing state" This reverts commit 3d283bd5 --- .../components/account/AccountActivity.kt | 12 ++---------- .../components/account/AccountViewModel.kt | 18 ++++++------------ 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index e5f58789ec..d200a7663f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -488,16 +488,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide private fun setupRefreshLayout() { binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() } lifecycleScope.launch { - viewModel.refreshState.collect { - when (it) { - AccountViewModel.RefreshState.INITIAL -> Unit - AccountViewModel.RefreshState.REFRESHING -> { - binding.swipeToRefreshLayout.isRefreshing = true - } - AccountViewModel.RefreshState.IDLE -> { - binding.swipeToRefreshLayout.isRefreshing = false - } - } + viewModel.isRefreshing.collect { + binding.swipeToRefreshLayout.isRefreshing = it } } binding.swipeToRefreshLayout.setColorSchemeResources(R.color.tusky_blue) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt index 74860370f1..8f785ab61c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountViewModel.kt @@ -42,8 +42,8 @@ class AccountViewModel @Inject constructor( private val _noteSaved = MutableStateFlow(false) val noteSaved: StateFlow = _noteSaved.asStateFlow() - private val _refreshState = MutableStateFlow(RefreshState.INITIAL) - val refreshState: StateFlow = _refreshState.asStateFlow() + private val _isRefreshing = MutableStateFlow(false) + val isRefreshing: StateFlow = _isRefreshing.asStateFlow() lateinit var accountId: String var isSelf = false @@ -71,7 +71,7 @@ class AccountViewModel @Inject constructor( private fun obtainAccount(reload: Boolean = false) { if (_accountData.value == null || reload) { if (reload) { - _refreshState.value = RefreshState.REFRESHING + _isRefreshing.value = true } _accountData.value = Loading() @@ -83,12 +83,12 @@ class AccountViewModel @Inject constructor( isFromOwnDomain = domain == activeAccount.domain _accountData.value = Success(account) - _refreshState.value = RefreshState.IDLE + _isRefreshing.value = false }, { t -> Log.w(TAG, "failed obtaining account", t) _accountData.value = Error(cause = t) - _refreshState.value = RefreshState.IDLE + _isRefreshing.value = false } ) } @@ -310,7 +310,7 @@ class AccountViewModel @Inject constructor( } private fun reload(isReload: Boolean = false) { - if (_refreshState.value == RefreshState.REFRESHING) { + if (_isRefreshing.value) { return } accountId.let { @@ -338,12 +338,6 @@ class AccountViewModel @Inject constructor( UNSUBSCRIBE } - enum class RefreshState { - INITIAL, - REFRESHING, - IDLE - } - companion object { const val TAG = "AccountViewModel" } From 593ad4b2feb3c953ce7e1234e0abb75136f236cd Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Fri, 13 Sep 2024 18:30:11 +0200 Subject: [PATCH 8/8] prevent refreshing before something actually loaded --- .../tusky/components/account/AccountActivity.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index ceb298bd22..ee2bd0e4cc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -387,7 +387,10 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide viewModel.accountData.collect { if (it == null) return@collect when (it) { - is Success -> onAccountChanged(it.data) + is Success -> { + onAccountChanged(it.data) + binding.swipeToRefreshLayout.isEnabled = true + } is Error -> { Snackbar.make( binding.accountCoordinatorLayout, @@ -396,6 +399,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide ) .setAction(R.string.action_retry) { viewModel.refresh() } .show() + binding.swipeToRefreshLayout.isEnabled = true } is Loading -> { } } @@ -438,6 +442,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide * Setup swipe to refresh layout */ private fun setupRefreshLayout() { + binding.swipeToRefreshLayout.isEnabled = false // will only be enabled after the first load completed binding.swipeToRefreshLayout.setOnRefreshListener { onRefresh() } lifecycleScope.launch { viewModel.isRefreshing.collect {