Offline course progress tracking
Track course progress when the user is offline and sync up with the server when internet is available.
Current Implementation -
The current implementation is completely online, where the content is consumed completely in a course after the content is closed and returned to the Content Details page, handlePageResume() → updateContentProgress() is called, which updates the content state to the server using updateContentState api of courseService.
On the success case of updateContentState api, a broadcast COURSE_STATUS_UPDATED_SUCCESSFULLY is fired, which is subscribed in the courses page and this will initiate the call to getEnrolledCourses(true) to fetch all the courses from the server, which contains the progress of the course.
Offline course progress tracking -
When the content is consumed completely in a course, after the content is closed and returned to Content Details page
onResume of ContentDetails handlePageResume() → updateContentProgress() is called, which updates the content state to the server using updateContentState api of courseService.
But, when offline, in the error case of the above api, we need to store the details in NoSql table with all the details required to update it later when the internet is available like [contentId, batchId, userId, status, progress] which are the parameters required by updateContentState api.
Both in case of success or error of the updateContentState api, a broadcast COURSE_STATUS_UPDATED_SUCCESSFULLY is fired, which is subscribed in the courses page and this will initiate the call to getEnrolledCourses(true) to fetch all the courses from the server.
But, since the user is in the offline state, getEnrolledCourses should check if there are any progress details of a course is available in the NoSql table, then those have to be fetched and checked against each courseId from getEnrolledCourses and progress needs to be updated and returned as the response.
And when the internet is available, there are two ways the progress of the content of a course can be updated to server
Listen to the network state and when the network is available, check if the progress details are available in the NoSql table, then update them by calling updateContentState api of courseService and erase all the data from the NoSql table.
Before calling getEnrolledCourses() every time, check if the progress details are available in the NoSql table, then update them by calling updateContentState api of courseService and then make a call to getEnrolledCourses, [Caveat - There can be a delay when updating the status of the content and immediately getting the progress of it] and erase all the data from the NoSql table.
Comments after design discussion -
Do not rely on the user to come back on the Content Details page after consuming the content, use the END_EVENT for an alternative to this, to make an API call or storing it to DB locally.
Check the timestamp of the getEnrolledCourses fetch time and the timestamp of the content state that is updated locally in the DB, if the local DB content update timestamp is greater than getEnrolledCourses then we need to merge the progress from the getEnrolledCourses and the local DB and give the updated details in the getEnrolledCourses, only during offline state
When making a call to getEnrolledCourses during the online state, check if the content update NoSQL table has some data and if it has, then update the content state to server and then do getEnrolledCourses API
IMPORTANT - updateContentState should have bulk update, otherwise user will have the experience problem, as we will want to hit the number of APIs as many as updateContentState
We need to check if there is a delay when updating the status of the content and immediately getting the progress of it in getEnrolledCourses
Scenarios -
When the user is offline and is going through course, and the user finishes the content of a course, now the course progress has to be updated on the progress bar on the courses page as well as the course detail page.
When the user comes online after consuming the course and making some progress, the same has to be updated to the server and the same updated progress has to be got back in the getEnrolledCourses API.