Skip to content

Commit

Permalink
fix: crash when internet disconnected right after opening a course
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid committed Oct 7, 2024
1 parent 75ac68d commit 81db699
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ private fun CourseOutlineUI(
}
}

CourseOutlineUIState.Error -> {}

CourseOutlineUIState.Loading -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ sealed class CourseOutlineUIState {
val useRelativeDates: Boolean,
) : CourseOutlineUIState()

data object Error : CourseOutlineUIState()
data object Loading : CourseOutlineUIState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class CourseOutlineViewModel(
)
courseNotifier.send(CourseLoading(false))
} catch (e: Exception) {
_uiState.value = CourseOutlineUIState.Error
if (e.isInternetError()) {
_uiMessage.emit(UIMessage.SnackBarMessage(resourceManager.getString(R.string.core_error_no_connection)))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,40 @@ class CourseVideoViewModel(

fun getVideos() {
viewModelScope.launch {
var courseStructure = interactor.getCourseStructureForVideos(courseId)
val blocks = courseStructure.blockData
if (blocks.isEmpty()) {
try {
var courseStructure = interactor.getCourseStructureForVideos(courseId)
val blocks = courseStructure.blockData
if (blocks.isEmpty()) {
_uiState.value = CourseVideosUIState.Empty(
message = resourceManager.getString(R.string.course_does_not_include_videos)
)
} else {
setBlocks(courseStructure.blockData)
courseSubSections.clear()
courseSubSectionUnit.clear()
courseStructure = courseStructure.copy(blockData = sortBlocks(blocks))
initDownloadModelsStatus()

val courseSectionsState =
(_uiState.value as? CourseVideosUIState.CourseData)?.courseSectionsState.orEmpty()

_uiState.value =
CourseVideosUIState.CourseData(
courseStructure = courseStructure,
downloadedState = getDownloadModelsStatus(),
courseSubSections = courseSubSections,
courseSectionsState = courseSectionsState,
subSectionsDownloadsCount = subSectionsDownloadsCount,
downloadModelsSize = getDownloadModelsSize(),
useRelativeDates = preferencesManager.isRelativeDatesEnabled
)
}
courseNotifier.send(CourseLoading(false))
} catch (e: Exception) {
_uiState.value = CourseVideosUIState.Empty(
message = resourceManager.getString(R.string.course_does_not_include_videos)
)
} else {
setBlocks(courseStructure.blockData)
courseSubSections.clear()
courseSubSectionUnit.clear()
courseStructure = courseStructure.copy(blockData = sortBlocks(blocks))
initDownloadModelsStatus()

val courseSectionsState =
(_uiState.value as? CourseVideosUIState.CourseData)?.courseSectionsState.orEmpty()

_uiState.value =
CourseVideosUIState.CourseData(
courseStructure = courseStructure,
downloadedState = getDownloadModelsStatus(),
courseSubSections = courseSubSections,
courseSectionsState = courseSectionsState,
subSectionsDownloadsCount = subSectionsDownloadsCount,
downloadModelsSize = getDownloadModelsSize(),
useRelativeDates = preferencesManager.isRelativeDatesEnabled
)
}
courseNotifier.send(CourseLoading(false))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class CourseOutlineViewModelTest {
coVerify(exactly = 2) { interactor.getCourseStatus(any()) }

assertEquals(noInternet, message.await()?.message)
assert(viewModel.uiState.value is CourseOutlineUIState.Loading)
assert(viewModel.uiState.value is CourseOutlineUIState.Error)
}

@Test
Expand Down Expand Up @@ -319,7 +319,7 @@ class CourseOutlineViewModelTest {
coVerify(exactly = 2) { interactor.getCourseStatus(any()) }

assertEquals(somethingWrong, message.await()?.message)
assert(viewModel.uiState.value is CourseOutlineUIState.Loading)
assert(viewModel.uiState.value is CourseOutlineUIState.Error)
}

@Test
Expand Down

0 comments on commit 81db699

Please sign in to comment.