From 98f3e1d282b506c8e8c99baebacbb9a1b0579d53 Mon Sep 17 00:00:00 2001 From: SeniorZhai Date: Thu, 3 Dec 2026 16:53:09 +0800 Subject: [PATCH] fix(wallet): prevent buy loading dialog from getting stuck --- .../ui/wallet/fiatmoney/CalculateFragment.kt | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/one/mixin/android/ui/wallet/fiatmoney/CalculateFragment.kt b/app/src/main/java/one/mixin/android/ui/wallet/fiatmoney/CalculateFragment.kt index 9177854a51..4eba2b83af 100644 --- a/app/src/main/java/one/mixin/android/ui/wallet/fiatmoney/CalculateFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/wallet/fiatmoney/CalculateFragment.kt @@ -92,24 +92,18 @@ class CalculateFragment : BaseFragment(R.layout.fragment_calculate) { if (currency != null && asset != null) { updateUI(currency, asset) } - } else { - runCatching { - loadingDialog.show(parentFragmentManager, LoadingProgressDialogFragment.TAG) - }.onFailure { - Timber.e(it) - } } val routeProfile = (requireActivity() as WalletActivity).routeProfile - if (routeProfile == null) { - checkData() - return - } - if (routeProfile.supportCurrencies.isEmpty()) { + if (routeProfile == null || routeProfile.supportCurrencies.isEmpty()) { + if (assetId == null && fiatMoneyViewModel.calculateState == null) { + showLoadingDialog() + } checkData() return } if (fiatMoneyViewModel.asset != null && fiatMoneyViewModel.currency != null) { + dismissLoadingDialog() return } val supportCurrencies = routeProfile.supportCurrencies @@ -147,13 +141,27 @@ class CalculateFragment : BaseFragment(R.layout.fragment_calculate) { ) } if (fiatMoneyViewModel.calculateState == null) { - refresh() + if (fiatMoneyViewModel.currency != null && fiatMoneyViewModel.asset != null) { + if (assetId == null) { + showLoadingDialog() + } + refresh() + } else { + dismissLoadingDialog() + } + } else { + dismissLoadingDialog() } } private var isLoading = false + private var loadingDialogRequested = false + private fun setLoading(loading: Boolean) { isLoading = loading + if (!loading) { + dismissLoadingDialog() + } val binding = bindingOrNull() ?: return binding.fiatLoading.isVisible = isLoading binding.assetLoading.isVisible = isLoading @@ -163,18 +171,35 @@ class CalculateFragment : BaseFragment(R.layout.fragment_calculate) { binding.assetRl.isEnabled = !loading if (!loading) { updateUI() + } + } + + private fun showLoadingDialog() { + loadingDialogRequested = true + view?.post { + if (!loadingDialogRequested || !isAdded || viewDestroyed() || parentFragmentManager.isStateSaved) { + return@post + } + if (parentFragmentManager.findFragmentByTag(LoadingProgressDialogFragment.TAG) != null) { + return@post + } runCatching { - if (loadingDialog.isAdded) { - loadingDialog.dismiss() - } + LoadingProgressDialogFragment().showNow(parentFragmentManager, LoadingProgressDialogFragment.TAG) }.onFailure { Timber.e(it) } } } - private val loadingDialog by lazy { - LoadingProgressDialogFragment() + private fun dismissLoadingDialog() { + loadingDialogRequested = false + if (!isAdded) return + runCatching { + (parentFragmentManager.findFragmentByTag(LoadingProgressDialogFragment.TAG) as? LoadingProgressDialogFragment) + ?.dismissAllowingStateLoss() + }.onFailure { + Timber.e(it) + } } private suspend fun refresh(errorHandler: (() -> Unit)? = null) { @@ -465,12 +490,18 @@ class CalculateFragment : BaseFragment(R.layout.fragment_calculate) { initData() updateUI() } else { + dismissLoadingDialog() binding.primaryTv.text = v updateUI() } } } + override fun onDestroyView() { + dismissLoadingDialog() + super.onDestroyView() + } + private fun openBankTransfer() { viewLifecycleOwner.lifecycleScope.launch { val app = fiatMoneyViewModel.findOrSyncApp(Constants.MIXIN_CASH_USER_ID) @@ -707,12 +738,12 @@ class CalculateFragment : BaseFragment(R.layout.fragment_calculate) { ) } }.catch { e -> + setLoading(false) activity?.finish() }.collectLatest { state -> fiatMoneyViewModel.calculateState = state - setLoading(false) initData() - updateUI() + setLoading(false) } } }