Repository navigation
feat: scope Room records and downloads storage by instance - #517
IvanStepanok wants to merge 9 commits into
Conversation
Introduces Instance and InstancesConfig: what one Open edX platform is and how it is parsed from the INSTANCES block of the bundled config. Nothing reads it yet. Required fields are NAME, API_HOST_URL and OAUTH_CLIENT_ID; a malformed entry is dropped. KEY falls back to a slug of NAME, INSTANCE_NAME is localized with an English fallback, and PLATFORM_NAME defaults to the display name. The schema matches iOS (#672).
InstanceStore holds the instance catalog and the selected instance, persists the selection by key and publishes it as a StateFlow. A single-instance catalog selects its only instance automatically. Registered in Koin as InstanceStore and InstanceProvider; nothing consumes it yet. Mirrors iOS #673.
InstanceTheme derives a palette from an instance's accent color and its optional THEME.LIGHT/THEME.DARK overrides: only the fields an instance sets change, the rest keep the compiled default, and a missing dark value falls back to the light one. Palette field names are the ones iOS uses (#675), mapped onto AppColors. OpenEdXTheme observes InstanceThemeManager, which holds the selected instance's theme. Nothing applies a theme yet, so every screen still renders the default palette.
InstanceConfigLoader resolves the catalog the app runs with: the last cached remote catalog, else the bundled INSTANCES list, else nothing. A live fetch with at least one instance replaces that baseline and becomes the cache; a failed, malformed or empty response leaves it untouched. The catalog is fetched from INSTANCES_CATALOG_URL with a plain OkHttp client: no app interceptors, no credentials, a 10 second timeout. Remote snake_case keys are normalized with the same map iOS uses (#676). Nothing calls the loader at launch yet. Unlike iOS, the YAML build configuration stays: the Gradle build already turns it into the bundled config.json the loader reads.
InstanceHostInterceptor re-targets requests built against the app's base URL at the selected instance: scheme, host and port change, path and query stay. Other hosts, and every request made with nothing selected, pass through. Each request is tagged with the instance it was sent for. Token refresh calls the selected instance's /oauth2/access_token with its OAuth client. A 401 for a request sent before a site switch neither refreshes nor logs out, and tokens returned after the learner switched are discarded. The access token is only attached to requests for the active site's host. Mirrors iOS #677.
Preferences tied to a learner on one site (tokens, push token, user, profile, server app config, relative dates, calendar sync state) are now stored per instance, so switching sites can't leak one site's session into another. Device-wide settings (video quality, Wi-Fi only downloads, review prompt, What's New) stay shared. StorageNamespace decides where an instance's data lives. The instance whose API host and OAuth client match the app-level configuration keeps the existing unprefixed keys, so an upgraded single-site install stays signed in; that choice is recorded once and never re-derived from a later catalog. Every other instance gets instance.<key>.* keys. CorePreferences can now check or clear one instance's session without selecting it, and token refresh saves rotated tokens for the site that issued them even if the learner switched away mid-refresh. Mirrors iOS #678.
The refresh resolved the selected instance twice: once for the tokens and again for the endpoint, so a switch in between could send one site's refresh token to another. The instance is now resolved once per authenticate call and used for the tokens read, the token endpoint and where the result is saved. The minimum interval between refreshes is tracked per site, so refreshing one site no longer holds back another.
Each instance gets a Room database file of its own, so cached courses, offline progress, calendar state and download records can't collide between sites, even when both use the same course and block IDs. The legacy namespace keeps the original OpenEdX_db file, so an upgraded single-site install keeps its data with no schema migration and no destructive fallback. The injected DAOs now forward every call to the selected instance's database, resolved once per call; default @transaction methods are forwarded to the generated DAO, and flows switch databases after a site switch. Downloaded files go to instances/<site> under the downloads folder; the legacy namespace keeps the existing location. The download worker carries the site that queued it and writes each file's record to that site's database, so a download that finishes after a switch can't land in the other site. Once its own queue is empty it moves on to the selected site's queue. Progress and failure events reach the UI only for the selected site. Mirrors iOS #679.
The background sync read and cleared offline progress through the selected site's database, one call at a time, so a site switch in the middle could clear the other site's records and submit the rest to the wrong site. It now works in the database of the site it started on and stops before sending anything once another site is selected.
|
Thanks for the pull request, @IvanStepanok! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Summary
Extends instance isolation to the database and downloaded files, so cached courses, offline progress, calendar state and downloads no longer bleed between sites. Port of openedx-app-ios#679.
Changes
InstanceDatabaseProvider). iOS adds aninstanceKeycolumn to four CoreData entities; with Room, a file per site isolates all twelve entities and their unique keys with no schema change. The legacy namespace keeps the originalOpenEdX_dbfile, so an upgraded install keeps its data with no migration, andfallbackToDestructiveMigration()is never reached on upgrade. Databases open lazily and stay open for the process lifetime.InstanceScopedDaos.kt), resolved once per call so one call never spans two databases. Default@Transactionmethods (clearCachedData) are forwarded explicitly to the generated DAO, which is what runs them in a transaction. Flows switch databases after a site switch.instances/<site>/under the downloads folder (two sites can serve the same file URL); the legacy namespace keeps the existing location.Tests
InstanceDatabaseProviderTest(5 instrumented cases, emulator API 36): legacy file name, identical IDs on two sites never collide, flows follow the selection, pinned access to an unselected site, transactional cleanup stays in one database.DownloadHelperInstanceFolderTest,CourseRepositoryOfflineSyncTest(a switch mid-sync touches nothing on the new site).Out of scope
Testing
./gradlew detektAll testProdDebugUnitTest :app:assembleProdDebug./gradlew :app:connectedProdDebugAndroidTest(the class above) on an API 36 emulator