fix: Refactor session repository methods to use coroutine context for improved performance #4445
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



Overview
This PR refactors the
SessionRepositoryImplto properly handle blocking operations using coroutine dispatchers, ensuring that all D2 SDK blocking calls execute on the appropriate thread context. This improves performance and follows Android best practices for coroutine usage.Problem
The session repository was performing blocking D2 SDK operations without explicit thread management, which could lead to:
Solution
Wrapped all blocking operations in
SessionRepositoryImplwithwithContext(dispatcher.io)to ensure they execute on the I/O dispatcher:Changes
Core Implementation
SessionRepositoryImpl: Refactored all methods to use
withContext(dispatcher.io):savePin()- PIN storage operationsgetStoredPin()- PIN retrieval operationsdeletePin()- PIN deletion operationssetSessionLocked()- Session lock state managementlogout()- User logout operationsPinModule: Added
Dispatcherdependency injection to support the refactored repositorySession Lock Logic Improvement
isSessionLocked()to determine lock state based on PIN existence rather than a separate preference flag, simplifying the state managementTesting
CustomIntentActivityResultContractTest.kt(475+ lines)FormRepositoryImplTest.kt,FormViewModelTest.kt, andLoginViewModelTest.ktto accommodate the changesUI Updates
ServerValidationContent.ktandPinBottomSheet.ktfor better integrationBenefits
Files Changed
loginmodule with supporting updates informmodule and main appTesting
All existing tests pass with the new implementation. New tests verify:
Note: This PR is based on the
ANDROAPP-7266-PIN-blocks-login-when-multiple-accounts-exist-and-missing-auto-focusbranch and should be merged intodevelop.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.