Skip to content

Commit 9318ffa

Browse files
Refactor: Implement caching and optimize data loading in MainViewModel
This commit introduces a caching layer for app and contact lists in `MainViewModel.kt` to improve performance and responsiveness. Key changes include: - **Caching Mechanism:** - Implemented in-memory and file-based caching for both app and contact lists. - Apps and contacts are now loaded from the cache first for instant display, with background refreshes to update data. - Cache files (`apps_cache.json`, `contacts_cache.json`) are stored in the app's internal storage. - Added `AtomicBoolean` flags (`appsRefreshing`, `contactsRefreshing`) to prevent concurrent refresh operations. - **App List Loading Optimization:** - `getAppsList` now loads from memory/file cache immediately and triggers a background refresh. - Heavy app list processing (querying `LauncherApps`, handling profiles) is moved to `Dispatchers.IO`. - User profiles are processed in parallel using `async` to speed up loading. - Improved logging for app list generation. - Scroll map (`_appScrollMap`) updates are posted to the main thread. - Introduced `normalizeForSort` helper function for efficient and consistent sorting of app names. - **Contact List Loading Optimization:** - `getContactList` now loads from memory/file cache immediately and triggers a background refresh. - Contact data fetching (names, phones, emails) is moved to `Dispatchers.IO`. - Optimized contact querying by first fetching basic contact info and then batch querying phone numbers and emails for all contacts. - Scroll map (`_contactScrollMap`) updates are posted to the main thread. - Introduced `normalizeForSort` helper function for efficient and consistent sorting of contact names. - Added a `ContentObserver` (`contactsObserver`) to monitor changes in `ContactsContract.Contacts.CONTENT_URI`. This invalidates the contact cache and triggers a background refresh when contacts are updated externally. - **Lifecycle Management:** - The `contactsObserver` is registered in `init` and unregistered in `onCleared`. - **JSON Caching:** - Implemented simple JSON serialization/deserialization for app and contact list items using `org.json` for file caching.
1 parent 6a2801f commit 9318ffa

File tree

1 file changed

+426
-153
lines changed

1 file changed

+426
-153
lines changed

0 commit comments

Comments
 (0)