-
Notifications
You must be signed in to change notification settings - Fork 113
[CLXR-475][Horizon] Learn offline mode #3637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5375c00
fb2b3f9
1951eea
1072fe3
df03097
544244c
bdce3e9
2144299
63c70e2
9d6bf2d
c9451c1
1b88ec5
8a63d09
f35e607
8a189ce
4133a02
02a5fb7
d6756a6
144f16d
9ad5fae
e0d76be
594e6d0
3a76019
3a4dce1
8b723eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,11 @@ enum class CollectionItemType { | |
| EXTERNAL_URL, | ||
| EXTERNAL_TOOL, | ||
| FILE, | ||
| PROGRAM | ||
| PROGRAM; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Consider adding a Crashlytics log (non-fatal) or an |
||
|
|
||
| companion object { | ||
| fun safeValueOf(name: String): CollectionItemType? = entries.find { it.name == name } | ||
| } | ||
| } | ||
|
|
||
| fun ApolloCollectionItemType.toModel(): CollectionItemType { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright (C) 2026 - present Instructure, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.instructure.horizon.espresso | ||
|
|
||
| import android.content.Context | ||
| import androidx.room.Room | ||
| import com.instructure.canvasapi2.utils.ApiPrefs | ||
| import com.instructure.horizon.database.HorizonDatabase | ||
| import com.instructure.horizon.database.dao.HorizonCourseDao | ||
| import com.instructure.horizon.database.dao.HorizonCourseModuleDao | ||
| import com.instructure.horizon.database.dao.HorizonCourseScoreDao | ||
| import com.instructure.horizon.database.dao.HorizonDashboardEnrollmentDao | ||
| import com.instructure.horizon.database.dao.HorizonDashboardModuleItemDao | ||
| import com.instructure.horizon.database.dao.HorizonFileFolderDao | ||
| import com.instructure.horizon.database.dao.HorizonLearnCollectionDao | ||
| import com.instructure.horizon.database.dao.HorizonLearnItemDao | ||
| import com.instructure.horizon.database.dao.HorizonLearnSavedItemDao | ||
| import com.instructure.horizon.database.dao.HorizonLocalFileDao | ||
| import com.instructure.horizon.database.dao.HorizonProgramDao | ||
| import com.instructure.horizon.database.dao.HorizonSyncMetadataDao | ||
| import com.instructure.horizon.di.HorizonHtmlParserQualifier | ||
| import com.instructure.horizon.di.HorizonOfflineModule | ||
| import com.instructure.horizon.offline.HorizonHtmlParserFileSource | ||
| import com.instructure.pandautils.features.offline.sync.HtmlParser | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import dagger.hilt.components.SingletonComponent | ||
| import dagger.hilt.testing.TestInstallIn | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @TestInstallIn(components = [SingletonComponent::class], replaces = [HorizonOfflineModule::class]) | ||
| object HorizonOfflineTestModule { | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideHorizonDatabase(@ApplicationContext context: Context): HorizonDatabase { | ||
| return Room.inMemoryDatabaseBuilder(context, HorizonDatabase::class.java) | ||
| .allowMainThreadQueries() | ||
| .build() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| @Provides | ||
| fun provideHorizonDashboardEnrollmentDao(db: HorizonDatabase): HorizonDashboardEnrollmentDao = db.dashboardEnrollmentDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonProgramDao(db: HorizonDatabase): HorizonProgramDao = db.programDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonDashboardModuleItemDao(db: HorizonDatabase): HorizonDashboardModuleItemDao = db.dashboardModuleItemDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonSyncMetadataDao(db: HorizonDatabase): HorizonSyncMetadataDao = db.syncMetadataDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonLearnItemDao(db: HorizonDatabase): HorizonLearnItemDao = db.learnItemDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonLearnCollectionDao(db: HorizonDatabase): HorizonLearnCollectionDao = db.learnCollectionDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonLearnSavedItemDao(db: HorizonDatabase): HorizonLearnSavedItemDao = db.learnSavedItemDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonCourseDao(db: HorizonDatabase): HorizonCourseDao = db.courseDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonCourseModuleDao(db: HorizonDatabase): HorizonCourseModuleDao = db.courseModuleDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonCourseScoreDao(db: HorizonDatabase): HorizonCourseScoreDao = db.courseScoreDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonLocalFileDao(db: HorizonDatabase): HorizonLocalFileDao = db.localFileDao() | ||
|
|
||
| @Provides | ||
| fun provideHorizonFileFolderDao(db: HorizonDatabase): HorizonFileFolderDao = db.fileFolderDao() | ||
|
|
||
| @Provides | ||
| @HorizonHtmlParserQualifier | ||
| fun provideHorizonHtmlParser( | ||
| fileSource: HorizonHtmlParserFileSource, | ||
| apiPrefs: ApiPrefs, | ||
| @ApplicationContext context: Context, | ||
| ): HtmlParser = HtmlParser(fileSource, apiPrefs, context) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Copyright (C) 2026 - present Instructure, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.instructure.horizon.data.datasource | ||
|
|
||
| import com.instructure.canvasapi2.managers.graphql.horizon.CourseWithProgress | ||
| import com.instructure.canvasapi2.managers.graphql.horizon.journey.Program | ||
| import com.instructure.canvasapi2.managers.graphql.horizon.journey.ProgramRequirement | ||
| import com.instructure.horizon.database.dao.HorizonCourseDao | ||
| import com.instructure.horizon.database.dao.HorizonProgramDao | ||
| import com.instructure.horizon.database.dao.HorizonSyncMetadataDao | ||
| import com.instructure.horizon.database.entity.HorizonCourseEntity | ||
| import com.instructure.horizon.database.entity.HorizonProgramCourseRef | ||
| import com.instructure.horizon.database.entity.HorizonProgramEntity | ||
| import com.instructure.horizon.database.entity.HorizonSyncMetadataEntity | ||
| import com.instructure.horizon.database.entity.SyncDataType | ||
| import com.instructure.journey.type.ProgramProgressCourseEnrollmentStatus | ||
| import com.instructure.journey.type.ProgramVariantType | ||
| import java.util.Date | ||
| import javax.inject.Inject | ||
|
|
||
| class CourseDetailsLocalDataSource @Inject constructor( | ||
| private val courseDao: HorizonCourseDao, | ||
| private val programDao: HorizonProgramDao, | ||
| private val syncMetadataDao: HorizonSyncMetadataDao, | ||
| ) { | ||
|
|
||
| suspend fun getCourse(courseId: Long): CourseWithProgress { | ||
| val entity = courseDao.getByCourseId(courseId) | ||
| ?: throw IllegalStateException("Course $courseId not found in cache") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing suspend fun getCourse(courseId: Long): CourseWithProgress? {
return courseDao.getByCourseId(courseId)?.toModel()
} |
||
| return entity.toCourseWithProgress() | ||
| } | ||
|
|
||
| suspend fun getProgramsForCourse(courseId: Long): List<Program> { | ||
| val allPrograms = programDao.getAll() | ||
| return allPrograms.mapNotNull { programEntity -> | ||
| val refs = programDao.getRefsForProgram(programEntity.programId) | ||
| val program = programEntity.toProgram(refs) | ||
| if (program.sortedRequirements.any { it.courseId == courseId }) program else null | ||
| } | ||
| } | ||
|
|
||
| suspend fun saveCourseDetails(course: CourseWithProgress, programs: List<Program>) { | ||
| courseDao.insertIfAbsent(listOf(course.toDefaultEntity())) | ||
| courseDao.updateCourseDetailsFields( | ||
| courseId = course.courseId, | ||
| name = course.courseName, | ||
| progress = course.progress, | ||
| imageUrl = course.courseImageUrl, | ||
| courseSyllabus = course.courseSyllabus, | ||
| ) | ||
| val programEntities = programs.map { it.toEntity() } | ||
| val refs = programs.flatMap { program -> | ||
| program.sortedRequirements.mapIndexed { index, req -> | ||
| HorizonProgramCourseRef( | ||
| programId = program.id, | ||
| courseId = req.courseId, | ||
| requirementId = req.id, | ||
| progressId = req.progressId, | ||
| required = req.required, | ||
| progress = req.progress, | ||
| enrollmentStatus = req.enrollmentStatus?.rawValue, | ||
| sortOrder = index, | ||
| ) | ||
| } | ||
| } | ||
| programDao.insertAll(programEntities) | ||
| programDao.insertAllRefs(refs) | ||
| syncMetadataDao.upsert( | ||
| HorizonSyncMetadataEntity( | ||
| dataType = SyncDataType.COURSE_DETAILS, | ||
| lastSyncedAtMs = System.currentTimeMillis(), | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private fun HorizonCourseEntity.toCourseWithProgress(): CourseWithProgress { | ||
| return CourseWithProgress( | ||
| courseId = courseId, | ||
| courseName = name, | ||
| courseImageUrl = imageUrl, | ||
| courseSyllabus = courseSyllabus, | ||
| progress = progress, | ||
| ) | ||
| } | ||
|
|
||
| private fun CourseWithProgress.toDefaultEntity(): HorizonCourseEntity { | ||
| return HorizonCourseEntity( | ||
| courseId = courseId, | ||
| name = courseName, | ||
| progress = progress, | ||
| imageUrl = courseImageUrl, | ||
| startAtMs = null, | ||
| endAtMs = null, | ||
| requirementCount = null, | ||
| requirementCompletedCount = null, | ||
| completedAtMs = null, | ||
| grade = null, | ||
| workflowState = null, | ||
| lastActivityAtMs = null, | ||
| enrolledAtMs = null, | ||
| courseSyllabus = courseSyllabus, | ||
| moduleItemsDurations = "", | ||
| ) | ||
| } | ||
|
|
||
| private fun Program.toEntity(): HorizonProgramEntity { | ||
| return HorizonProgramEntity( | ||
| programId = id, | ||
| name = name, | ||
| description = description, | ||
| startDateMs = startDate?.time, | ||
| endDateMs = endDate?.time, | ||
| variant = variant.rawValue, | ||
| estimatedDurationMinutes = null, | ||
| courseCount = sortedRequirements.size, | ||
| courseCompletionCount = courseCompletionCount, | ||
| enrolledAtMs = null, | ||
| completionPercentage = null, | ||
| enrollmentStatus = null, | ||
| ) | ||
| } | ||
|
|
||
| private fun HorizonProgramEntity.toProgram(refs: List<HorizonProgramCourseRef>): Program { | ||
| val requirements = refs.sortedBy { it.sortOrder }.map { ref -> | ||
| ProgramRequirement( | ||
| id = ref.requirementId, | ||
| progressId = ref.progressId, | ||
| courseId = ref.courseId, | ||
| required = ref.required, | ||
| progress = ref.progress, | ||
| enrollmentStatus = ref.enrollmentStatus?.let { | ||
| runCatching { ProgramProgressCourseEnrollmentStatus.valueOf(it) }.getOrNull() | ||
| }, | ||
| ) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error handling here is asymmetric: an unknown Consider logging the unrecognised value (or recording it to Crashlytics) so it surfaces during development rather than silently corrupting displayed data. |
||
| return Program( | ||
| id = programId, | ||
| name = name, | ||
| description = description, | ||
| startDate = startDateMs?.let { Date(it) }, | ||
| endDate = endDateMs?.let { Date(it) }, | ||
| variant = runCatching { ProgramVariantType.valueOf(variant) }.getOrDefault(ProgramVariantType.LINEAR), | ||
| courseCompletionCount = courseCompletionCount, | ||
| sortedRequirements = requirements, | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The progress values changed from fractions (
0.25,1.0) to percentages (25.0,100.0). If the real API contract uses a 0–1 scale this mismatch will cause test assertions to diverge from production behaviour (and vice-versa). Please confirm the expected unit and ensure both the mock data and the display logic use the same scale consistently.