Skip to content

manager: Refactor ViewModels with repository pattern and StateFlow#3241

Merged
YuKongA merged 6 commits into
mainfrom
manager
Mar 1, 2026
Merged

manager: Refactor ViewModels with repository pattern and StateFlow#3241
YuKongA merged 6 commits into
mainfrom
manager

Conversation

@YuKongA

@YuKongA YuKongA commented Mar 1, 2026

Copy link
Copy Markdown
Collaborator

This commit introduces a major refactoring of the manager's data and state management by implementing the repository pattern and migrating from mutable State objects to StateFlow and UiState data classes in the ViewModels.

  • Introduce Repository Pattern:

    • Create repository and model packages to decouple data sources from the UI layer.
    • Implement SuperUserRepository, ModuleRepository, ModuleRepoRepository, and TemplateRepository to handle data fetching and business logic.
    • Move data fetching logic (local and remote) from ViewModels into these new repository classes.
  • State Management with StateFlow and UiState:

    • Replace individual mutableStateOf properties in SuperUserViewModel, ModuleViewModel, ModuleRepoViewModel, and TemplateViewModel with a single UiState data class per ViewModel.
    • Use StateFlow to expose the UiState, providing a single source of truth for the UI and improving state predictability.
    • Update Composable screens to collect state from the uiState StateFlow.
  • SuperUser Screen Enhancements:

    • The logic for grouping and sorting applications is now centralized within SuperUserViewModel.
    • GroupedApps now includes a pre-calculated ownerName to simplify UI logic.
    • Improved label and package name display by using TextOverflow.Ellipsis instead of basicMarquee.
    • App list updates, including filtering and sorting, are now handled more efficiently.
  • Performance & Caching:

    • Add AppIconCache to implement a memory-based LruCache for application icons.
    • This improves scrolling performance and reduces redundant icon loading, especially for high-resolution icons.
  • Minor Fixes & Cleanups:

    • Simplify logic in ExecuteModuleAction.kt and WebViewHelper.kt by directly using repositories.
    • Corrected the application of the enable_blur setting in the settings screen.

This commit introduces a major refactoring of the manager's data and state management by implementing the repository pattern and migrating from mutable `State` objects to `StateFlow` and `UiState` data classes in the ViewModels.

- **Introduce Repository Pattern:**
    - Create `repository` and `model` packages to decouple data sources from the UI layer.
    - Implement `SuperUserRepository`, `ModuleRepository`, `ModuleRepoRepository`, and `TemplateRepository` to handle data fetching and business logic.
    - Move data fetching logic (local and remote) from ViewModels into these new repository classes.

- **State Management with `StateFlow` and `UiState`:**
    - Replace individual `mutableStateOf` properties in `SuperUserViewModel`, `ModuleViewModel`, `ModuleRepoViewModel`, and `TemplateViewModel` with a single `UiState` data class per ViewModel.
    - Use `StateFlow` to expose the `UiState`, providing a single source of truth for the UI and improving state predictability.
    - Update Composable screens to collect state from the `uiState` `StateFlow`.

- **SuperUser Screen Enhancements:**
    - The logic for grouping and sorting applications is now centralized within `SuperUserViewModel`.
    - `GroupedApps` now includes a pre-calculated `ownerName` to simplify UI logic.
    - Improved label and package name display by using `TextOverflow.Ellipsis` instead of `basicMarquee`.
    - App list updates, including filtering and sorting, are now handled more efficiently.

- **Performance & Caching:**
    - Add `AppIconCache` to implement a memory-based `LruCache` for application icons.
    - This improves scrolling performance and reduces redundant icon loading, especially for high-resolution icons.

- **Minor Fixes & Cleanups:**
    - Simplify logic in `ExecuteModuleAction.kt` and `WebViewHelper.kt` by directly using repositories.
    - Corrected the application of the `enable_blur` setting in the settings screen.
Copilot AI review requested due to automatic review settings March 1, 2026 06:20

This comment was marked as outdated.

Comment thread manager/app/src/main/java/me/weishu/kernelsu/ui/util/AppIconCache.kt Outdated

This comment was marked as outdated.

Comment thread manager/app/src/main/java/me/weishu/kernelsu/ui/component/AppIconImage.kt Outdated
Comment thread manager/app/src/main/java/me/weishu/kernelsu/ui/util/AppIconCache.kt Outdated
YuKongA added 2 commits March 1, 2026 15:22
4
java.lang.IllegalStateException: This method can only be called on the main thread
	at com.topjohnwu.superuser.internal.RootServiceManager.enforceMainThread(RootServiceManager.java:122)
	at com.topjohnwu.superuser.internal.RootServiceManager.unbind(RootServiceManager.java:299)
	at com.topjohnwu.superuser.ipc.RootService.unbind(RootService.java:157)
	at me.weishu.kernelsu.ui.viewmodel.SuperUserViewModel.fetchAppList$lambda$0$reconnect(SuperUserViewModel.kt:208)
	at me.weishu.kernelsu.ui.viewmodel.SuperUserViewModel.access$fetchAppList$lambda$0$reconnect(SuperUserViewModel.kt:41)
	at me.weishu.kernelsu.ui.viewmodel.SuperUserViewModel$fetchAppList$2$allPackagesSlice$1.invokeSuspend(SuperUserViewModel.kt:224)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:34)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:124)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@57a530e, Dispatchers.Main.immediate]
@YuKongA YuKongA merged commit 0037d7d into main Mar 1, 2026
16 checks passed
@YuKongA YuKongA deleted the manager branch March 1, 2026 08:15
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.

3 participants