2.1 KiB
2.1 KiB
Final Implementation Plan - Fix Image Reloading
This plan eliminates the persistent image flickering by ensuring Instance Stability in the ViewModel. This prevents Compose from thinking that every image in the chat has changed when a single reaction is added.
User Review Required
Important
The previous fix stabilized the click callbacks, but the data instances themselves were still being recreated for every message on every update. This plan makes the data mapping "smart" so only modified messages get new UI model instances.
Proposed Changes
1. ViewModel Optimization (Instance Stability)
[MODIFY] ChatViewModel
- Message Instance Cache: Maintain a private
Map<String, Pair<Message, MessageUiModel>>in the ViewModel. - Smart Mapping:
- When the repository emits a new list of
Messageobjects, compare each message with its cached version. - If the raw
Messagedata is identical (including reactions), reuse the existingMessageUiModelinstance. - Only call
toUiModel()for messages that have actually changed.
- When the repository emits a new list of
- Why?: This allows Jetpack Compose to use its "Smart Recomposition" feature. If the object instance is the same, Compose can skip the entire
MessageRowand its internalAsyncImagecompletely.
2. UI Refinement
[MODIFY] Message_Messaging.kt
- Ensure the
AsyncImagemodel is strictly the URL string to prevent any ambiguity.
Verification Plan
Manual Verification
- Stress Test Reactions: Rapidly add and remove reactions. Confirm that only the affected message's reaction bubble updates, and zero other images in the chat flicker or show a shimmer.
- Scroll Test: Ensure the cache management doesn't negatively impact scroll performance.
- Memory Check: Verify that the cache is cleared when the ViewModel is cleared.