Skip to content

Fix fetching Balance on every screen#3162

Merged
johnnyluo merged 2 commits into
mainfrom
aminsato/3148_issue
Jan 30, 2026
Merged

Fix fetching Balance on every screen#3162
johnnyluo merged 2 commits into
mainfrom
aminsato/3148_issue

Conversation

@aminsato
Copy link
Copy Markdown
Collaborator

@aminsato aminsato commented Jan 28, 2026

Description

Please include a summary of the change and which issue is fixed.

Fixes #3148

Which feature is affected?

  • Create vault ( Secure / Fast) - Please ensure you created a Secure vault & fast vault
  • Sending - Please attach a tx link here
  • Swap - Please attach a tx link for the swap here
  • New Chain / Chain related feature - Please attach tx link here

Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

Screenshots (if applicable):

Additional context

Summary by CodeRabbit

  • Refactor

    • Improved chain token loading workflow with enhanced async flow composition and more reliable state management.
  • Bug Fixes

    • Enhanced error handling for token operations to ensure proper state reset on failures.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 28, 2026

Walkthrough

Reworked asynchronous flow composition in ChainTokensViewModel. Replaced onCompletion-based lifecycle with onEach for refreshing state management, introduced List.hasNullAccount() extension for account validation, moved error handling earlier in the chain, and reorganized data preparation sequencing.

Changes

Cohort / File(s) Summary
Async Flow Refactoring
app/src/main/java/com/vultisig/wallet/ui/models/ChainTokensViewModel.kt
Replaced onCompletion with onEach for refreshing state toggle based on account readiness. Introduced List.hasNullAccount() extension to detect missing token/fiat values. Moved catch block earlier in chain for error handling. Reorganized data preparation: token building and UI model computation now occur after accounts are fetched. Deferred accountAddress, explorer URL, and balance computation to later pipeline stage. Added Account type import.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the primary problem being addressed: fixing balance fetching behavior that occurs unnecessarily on every screen change.
Linked Issues check ✅ Passed The code changes implement the required solution: caching balances and performing async updates instead of immediate fetching on screen changes, matching issue #3148 objectives.
Out of Scope Changes check ✅ Passed All changes are scoped to ChainTokensViewModel and directly address the balance fetching and caching requirements specified in issue #3148.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch aminsato/3148_issue

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@app/src/main/java/com/vultisig/wallet/ui/models/ChainTokensViewModel.kt`:
- Around line 232-235: The current placement of .catch in ChainTokensViewModel
is before the second combine, so exceptions during the UI-state mapping pipeline
(the second combine block that builds the view state) bypass the catch and never
call updateRefreshing(false); move the .catch to after the second combine (i.e.,
after the mapping that constructs the UI state) or add an additional
catch/finally around the second combine/mapping so that updateRefreshing(false)
is always invoked on error; reference the combine/catch chain inside the Flow in
ChainTokensViewModel and ensure updateRefreshing(false) is called in every error
path.
🧹 Nitpick comments (1)
app/src/main/java/com/vultisig/wallet/ui/models/ChainTokensViewModel.kt (1)

398-400: Consider a more descriptive function name.

The name hasNullAccount suggests checking for null Account objects in the list, but it actually checks for accounts with null tokenValue or fiatValue. A name like hasIncompleteAccount() or hasUnloadedBalances() would better convey the intent.

✏️ Suggested rename
-    private fun List<Account>.hasNullAccount() = any {
+    private fun List<Account>.hasIncompleteAccount() = any {
         it.tokenValue == null || it.fiatValue == null
     }

And update the call site on line 227:

-                    updateRefreshing(it.accounts.hasNullAccount())
+                    updateRefreshing(it.accounts.hasIncompleteAccount())

Copy link
Copy Markdown
Contributor

@johnnyluo johnnyluo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnnyluo johnnyluo merged commit 7d30e7e into main Jan 30, 2026
2 checks passed
@johnnyluo johnnyluo deleted the aminsato/3148_issue branch January 30, 2026 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Balance fetched on every screen change

2 participants