Skip to content

Commit 9601f28

Browse files
fix: video progress caching
1 parent 23d0352 commit 9601f28

File tree

17 files changed

+105
-10
lines changed

17 files changed

+105
-10
lines changed

app/src/main/java/org/openedx/app/room/AppDatabase.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.openedx.core.data.model.room.CourseProgressEntity
1111
import org.openedx.core.data.model.room.CourseStructureEntity
1212
import org.openedx.core.data.model.room.DownloadCoursePreview
1313
import org.openedx.core.data.model.room.OfflineXBlockProgress
14+
import org.openedx.core.data.model.room.VideoProgressEntity
1415
import org.openedx.core.data.model.room.discovery.EnrolledCourseEntity
1516
import org.openedx.core.data.storage.CourseDao
1617
import org.openedx.core.module.db.CalendarDao
@@ -22,7 +23,7 @@ import org.openedx.discovery.data.converter.DiscoveryConverter
2223
import org.openedx.discovery.data.model.room.CourseEntity
2324
import org.openedx.discovery.data.storage.DiscoveryDao
2425

25-
const val DATABASE_VERSION = 3
26+
const val DATABASE_VERSION = 4
2627
const val DATABASE_NAME = "OpenEdX_db"
2728

2829
@Database(
@@ -36,11 +37,13 @@ const val DATABASE_NAME = "OpenEdX_db"
3637
CourseCalendarStateEntity::class,
3738
DownloadCoursePreview::class,
3839
CourseEnrollmentDetailsEntity::class,
40+
VideoProgressEntity::class,
3941
CourseProgressEntity::class,
4042
],
4143
autoMigrations = [
4244
AutoMigration(1, 2),
43-
AutoMigration(2, DATABASE_VERSION),
45+
AutoMigration(2, 3),
46+
AutoMigration(3, DATABASE_VERSION),
4447
],
4548
version = DATABASE_VERSION
4649
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.openedx.core.data.model.room
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
@Entity(tableName = "video_progress_table")
8+
data class VideoProgressEntity(
9+
@PrimaryKey
10+
@ColumnInfo("video_url")
11+
val videoUrl: String,
12+
@ColumnInfo("video_time")
13+
val videoTime: Long,
14+
@ColumnInfo("duration")
15+
val duration: Long,
16+
)

core/src/main/java/org/openedx/core/data/storage/CourseDao.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.room.Transaction
88
import org.openedx.core.data.model.room.CourseEnrollmentDetailsEntity
99
import org.openedx.core.data.model.room.CourseProgressEntity
1010
import org.openedx.core.data.model.room.CourseStructureEntity
11+
import org.openedx.core.data.model.room.VideoProgressEntity
1112

1213
@Dao
1314
interface CourseDao {
@@ -40,6 +41,13 @@ interface CourseDao {
4041
@Query("SELECT * FROM course_enrollment_details_table WHERE id=:id")
4142
suspend fun getCourseEnrollmentDetailsById(id: String): CourseEnrollmentDetailsEntity?
4243

44+
45+
@Insert(onConflict = OnConflictStrategy.REPLACE)
46+
suspend fun insertVideoProgressEntity(vararg videoProgressEntity: VideoProgressEntity)
47+
48+
@Query("SELECT * FROM video_progress_table WHERE video_url=:videoUrl")
49+
suspend fun getVideoProgressByVideoUrl(videoUrl: String): VideoProgressEntity?
50+
4351
@Insert(onConflict = OnConflictStrategy.REPLACE)
4452
suspend fun insertCourseProgressEntity(vararg courseProgressEntity: CourseProgressEntity)
4553

core/src/main/java/org/openedx/core/domain/model/Block.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ data class Block(
102102
}
103103
}
104104

105+
val videoUrl: String?
106+
get() = if (studentViewData?.encodedVideos?.hasVideoUrl == true) {
107+
studentViewData.encodedVideos.videoUrl
108+
} else {
109+
studentViewData?.encodedVideos?.youtube?.url
110+
}
111+
105112
val isVideoBlock get() = type == BlockType.VIDEO
106113
val isDiscussionBlock get() = type == BlockType.DISCUSSION
107114
val isHTMLBlock get() = type == BlockType.HTML

core/src/main/java/org/openedx/core/system/notifier/CourseVideoPositionChanged.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package org.openedx.core.system.notifier
33
data class CourseVideoPositionChanged(
44
val videoUrl: String,
55
val videoTime: Long,
6+
val duration: Long,
67
val isPlaying: Boolean
78
) : CourseEvent

course/src/main/java/org/openedx/course/data/repository/CourseRepository.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.openedx.core.ApiConstants
77
import org.openedx.core.data.api.CourseApi
88
import org.openedx.core.data.model.BlocksCompletionBody
99
import org.openedx.core.data.model.room.OfflineXBlockProgress
10+
import org.openedx.core.data.model.room.VideoProgressEntity
1011
import org.openedx.core.data.model.room.XBlockProgressData
1112
import org.openedx.core.data.storage.CorePreferences
1213
import org.openedx.core.data.storage.CourseDao
@@ -252,4 +253,14 @@ class CourseRepository(
252253
courseDao.insertCourseProgressEntity(response.mapToRoomEntity(courseId))
253254
trySend(response.mapToDomain())
254255
}
256+
257+
suspend fun saveVideoProgress(videoUrl: String, videoTime: Long, duration: Long) {
258+
val videoProgressEntity = VideoProgressEntity(videoUrl, videoTime, duration)
259+
courseDao.insertVideoProgressEntity(videoProgressEntity)
260+
}
261+
262+
suspend fun getVideoProgress(videoUrl: String): VideoProgressEntity {
263+
return courseDao.getVideoProgressByVideoUrl(videoUrl)
264+
?: VideoProgressEntity(videoUrl, 0L, 0L)
265+
}
255266
}

course/src/main/java/org/openedx/course/domain/interactor/CourseInteractor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,6 @@ class CourseInteractor(
122122

123123
fun getCourseProgress(courseId: String, isRefresh: Boolean) =
124124
repository.getCourseProgress(courseId, isRefresh)
125+
126+
suspend fun getVideoProgress(videoUrl: String) = repository.getVideoProgress(videoUrl)
125127
}

course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ fun CourseVideoSection(
612612
block: Block,
613613
videoBlocks: List<Block>,
614614
preview: Map<String, Any?>,
615+
progress: Map<String, Float>,
615616
downloadedStateMap: Map<String, DownloadedState>,
616617
onVideoClick: (Block) -> Unit,
617618
onDownloadClick: (blocksIds: List<String>) -> Unit,
@@ -649,6 +650,7 @@ fun CourseVideoSection(
649650
CourseVideoItem(
650651
videoBlock = block,
651652
preview = preview[block.id],
653+
progress = progress[block.videoUrl] ?: 0f,
652654
onClick = {
653655
onVideoClick(block)
654656
}
@@ -663,6 +665,7 @@ fun CourseVideoSection(
663665
fun CourseVideoItem(
664666
videoBlock: Block,
665667
preview: Any?,
668+
progress: Float,
666669
onClick: () -> Unit
667670
) {
668671
Box(
@@ -731,7 +734,7 @@ fun CourseVideoItem(
731734
)
732735

733736
// Progress bar (bottom)
734-
if (videoBlock.completion > 0.0f) {
737+
if (progress > 0.0f || videoBlock.isCompleted()) {
735738
Box(
736739
modifier = Modifier
737740
.padding(bottom = 4.dp)
@@ -745,7 +748,11 @@ fun CourseVideoItem(
745748
.height(4.dp)
746749
.padding(horizontal = 8.dp)
747750
.clip(CircleShape),
748-
progress = videoBlock.completion.toFloat(),
751+
progress = if (videoBlock.isCompleted()) {
752+
1f
753+
} else {
754+
progress
755+
},
749756
color = if (videoBlock.isCompleted()) {
750757
MaterialTheme.appColors.progressBarColor
751758
} else {

course/src/main/java/org/openedx/course/presentation/unit/video/VideoFullScreenFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class VideoFullScreenFragment : Fragment(R.layout.fragment_video_full_screen) {
204204

205205
override fun onDestroyView() {
206206
viewModel.currentVideoTime = exoPlayer?.currentPosition ?: C.TIME_UNSET
207+
viewModel.duration = exoPlayer?.duration ?: 0L
207208
viewModel.sendTime()
208209
super.onDestroyView()
209210
}

course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) {
255255
}
256256
}
257257

258+
override fun onDestroyView() {
259+
viewModel.duration = viewModel.exoPlayer?.duration ?: 0L
260+
viewModel.saveVideoProgress()
261+
super.onDestroyView()
262+
}
263+
258264
@UnstableApi
259265
override fun onDestroy() {
260266
if (!requireActivity().isChangingConfigurations) {

0 commit comments

Comments
 (0)