From 19b4f1690ac896028b21ae89d9455c70c1da7f99 Mon Sep 17 00:00:00 2001 From: droid Date: Thu, 8 Aug 2024 10:11:06 +0200 Subject: [PATCH] fix: crash when internet disconnected right after opening a course --- .../outline/CourseOutlineScreen.kt | 4 ++ .../outline/CourseOutlineUIState.kt | 1 + .../outline/CourseOutlineViewModel.kt | 1 + .../videos/CourseVideoViewModel.kt | 42 ++++++++++--------- .../outline/CourseOutlineViewModelTest.kt | 4 +- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineScreen.kt b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineScreen.kt index ccf87dd18..1c176a56d 100644 --- a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineScreen.kt +++ b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineScreen.kt @@ -342,6 +342,10 @@ private fun CourseOutlineUI( } } + CourseOutlineUIState.Error -> { + NoContentScreen(noContentScreenType = NoContentScreenType.COURSE_OUTLINE) + } + CourseOutlineUIState.Loading -> { CircularProgress() } diff --git a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineUIState.kt b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineUIState.kt index 580e3b2a8..e3b3cd7e6 100644 --- a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineUIState.kt +++ b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineUIState.kt @@ -17,5 +17,6 @@ sealed class CourseOutlineUIState { val datesBannerInfo: CourseDatesBannerInfo, ) : CourseOutlineUIState() + data object Error : CourseOutlineUIState() data object Loading : CourseOutlineUIState() } diff --git a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt index d9b9e5838..d1106922f 100644 --- a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt +++ b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt @@ -218,6 +218,7 @@ class CourseOutlineViewModel( datesBannerInfo = datesBannerInfo ) } catch (e: Exception) { + _uiState.value = CourseOutlineUIState.Error if (e.isInternetError()) { _uiMessage.emit(UIMessage.SnackBarMessage(resourceManager.getString(R.string.core_error_no_connection))) } else { diff --git a/course/src/main/java/org/openedx/course/presentation/videos/CourseVideoViewModel.kt b/course/src/main/java/org/openedx/course/presentation/videos/CourseVideoViewModel.kt index 352fe72a3..ddb36ffb4 100644 --- a/course/src/main/java/org/openedx/course/presentation/videos/CourseVideoViewModel.kt +++ b/course/src/main/java/org/openedx/course/presentation/videos/CourseVideoViewModel.kt @@ -145,27 +145,31 @@ 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 + } 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, getDownloadModelsStatus(), courseSubSections, + courseSectionsState, subSectionsDownloadsCount, getDownloadModelsSize() + ) + } + courseNotifier.send(CourseLoading(false)) + } catch (e: Exception) { _uiState.value = CourseVideosUIState.Empty - } 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, getDownloadModelsStatus(), courseSubSections, - courseSectionsState, subSectionsDownloadsCount, getDownloadModelsSize() - ) } - courseNotifier.send(CourseLoading(false)) } } diff --git a/course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt b/course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt index 1ade4c028..7b0d16ca0 100644 --- a/course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt +++ b/course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt @@ -275,7 +275,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 @@ -310,7 +310,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