Skip to content

fix: share single SettingsViewModel to prevent duplicate monitoring#116

Merged
torlando-tech merged 1 commit intomainfrom
fix/duplicate-settingsviewmodel-battery-drain
Dec 17, 2025
Merged

fix: share single SettingsViewModel to prevent duplicate monitoring#116
torlando-tech merged 1 commit intomainfrom
fix/duplicate-settingsviewmodel-battery-drain

Conversation

@torlando-tech
Copy link
Copy Markdown
Owner

@torlando-tech torlando-tech commented Dec 17, 2025

Summary

  • Fix battery drain caused by multiple SettingsViewModel instances each starting their own shared instance monitoring loop
  • Share single SettingsViewModel from MainActivity's NavHost across SettingsScreen, MyIdentityScreen, and IdentityScreen
  • Reduces polling from 3-4 simultaneous calls every 5 seconds to just 1 call

Test plan

  • Verified via logcat: only 1 check_shared_instance_available call per 5-second interval (previously 3-4)
  • All existing SettingsViewModel tests pass
  • Added new tests for monitoring behavior
  • ktlint, detekt, cpd checks pass
  • Manual testing: navigate between Settings, My Identity, and Network Status screens

Changes

  • SettingsScreen.kt: Remove default hiltViewModel() for SettingsViewModel
  • MyIdentityScreen.kt: Remove default hiltViewModel() for SettingsViewModel
  • IdentityScreen.kt: Remove default hiltViewModel() for SettingsViewModel
  • MainActivity.kt: Pass shared settingsViewModel to all screens
  • SettingsViewModelTest.kt: Add tests for monitoring behavior

🤖 Generated with Claude Code

Multiple screens (SettingsScreen, MyIdentityScreen, IdentityScreen) were
each creating their own SettingsViewModel via hiltViewModel(), causing
each to start its own startSharedInstanceMonitor() loop. This resulted
in 3-4 simultaneous polling loops every 5 seconds, significantly
increasing battery drain.

Fix:
- Remove default hiltViewModel() from screen composables
- Pass shared SettingsViewModel from MainActivity's NavHost
- All screens now share one ViewModel instance = one monitoring loop

Verified via logcat: now shows 1 check_shared_instance_available call
per 5 seconds instead of 3-4 simultaneous calls.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 17, 2025

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...p/src/main/java/com/lxmf/messenger/MainActivity.kt 0.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@torlando-tech torlando-tech merged commit 692dd05 into main Dec 17, 2025
4 of 5 checks passed
@torlando-tech torlando-tech deleted the fix/duplicate-settingsviewmodel-battery-drain branch January 18, 2026 06:45
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.

1 participant