Skip to content
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

fix: crash when internet disconnected right after opening a course #376

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading