Skip to content

Commit f9b8ad1

Browse files
author
Ahsan Arif
committed
chore: moved to flow.catch from try/catch in course outline and container viewmodels
1 parent 45b39b8 commit f9b8ad1

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

course/src/main/java/org/openedx/course/presentation/container/CourseContainerViewModel.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,23 @@ class CourseContainerViewModel(
170170
_showProgress.value = true
171171

172172
viewModelScope.launch {
173-
try {
174-
val courseStructureFlow = interactor.getCourseStructureFlow(courseId)
175-
.catch { e ->
176-
handleFetchError(e)
177-
emit(null)
178-
}
179-
val courseDetailsFlow = interactor.getEnrollmentDetailsFlow(courseId)
180-
.catch { emit(null) }
181-
courseStructureFlow.combine(courseDetailsFlow) { courseStructure, courseEnrollmentDetails ->
182-
courseStructure to courseEnrollmentDetails
183-
}.collect { (courseStructure, courseEnrollmentDetails) ->
184-
when {
185-
courseEnrollmentDetails != null -> handleCourseEnrollment(courseEnrollmentDetails)
186-
courseStructure != null -> handleCourseStructureOnly(courseStructure)
187-
else -> _courseAccessStatus.value = CourseAccessError.UNKNOWN
188-
}
173+
val courseStructureFlow = interactor.getCourseStructureFlow(courseId)
174+
.catch { e ->
175+
handleFetchError(e)
176+
emit(null)
189177
}
190-
} catch (e: Exception) {
178+
val courseDetailsFlow = interactor.getEnrollmentDetailsFlow(courseId)
179+
.catch { emit(null) }
180+
courseStructureFlow.combine(courseDetailsFlow) { courseStructure, courseEnrollmentDetails ->
181+
courseStructure to courseEnrollmentDetails
182+
}.catch { e ->
191183
handleFetchError(e)
184+
}.collect { (courseStructure, courseEnrollmentDetails) ->
185+
when {
186+
courseEnrollmentDetails != null -> handleCourseEnrollment(courseEnrollmentDetails)
187+
courseStructure != null -> handleCourseStructureOnly(courseStructure)
188+
else -> _courseAccessStatus.value = CourseAccessError.UNKNOWN
189+
}
192190
}
193191
}
194192
}

course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.SharedFlow
88
import kotlinx.coroutines.flow.StateFlow
99
import kotlinx.coroutines.flow.asSharedFlow
1010
import kotlinx.coroutines.flow.asStateFlow
11+
import kotlinx.coroutines.flow.catch
1112
import kotlinx.coroutines.flow.combine
1213
import kotlinx.coroutines.launch
1314
import org.openedx.core.BlockType
@@ -183,28 +184,27 @@ class CourseOutlineViewModel(
183184

184185
private fun getCourseDataInternal() {
185186
viewModelScope.launch {
186-
try {
187-
val courseStructureFlow = interactor.getCourseStructureFlow(courseId, false)
188-
val courseStatusFlow = interactor.getCourseStatusFlow(courseId)
189-
val courseDatesFlow = interactor.getCourseDatesFlow(courseId)
190-
combine(
191-
courseStructureFlow,
192-
courseStatusFlow,
193-
courseDatesFlow
194-
) { courseStructure, courseStatus, courseDatesResult ->
195-
Triple(courseStructure, courseStatus, courseDatesResult)
196-
}.collect { (courseStructure, courseStatus, courseDates) ->
197-
if (courseStructure == null) return@collect
198-
val blocks = courseStructure.blockData
199-
val datesBannerInfo = courseDates.courseBanner
200-
201-
checkIfCalendarOutOfDate(courseDates.datesSection.values.flatten())
202-
updateOutdatedOfflineXBlocks(courseStructure)
203-
204-
initializeCourseData(blocks, courseStructure, courseStatus, datesBannerInfo)
205-
}
206-
} catch (e: Exception) {
187+
val courseStructureFlow = interactor.getCourseStructureFlow(courseId, false)
188+
.catch { emit(null) }
189+
val courseStatusFlow = interactor.getCourseStatusFlow(courseId)
190+
val courseDatesFlow = interactor.getCourseDatesFlow(courseId)
191+
combine(
192+
courseStructureFlow,
193+
courseStatusFlow,
194+
courseDatesFlow
195+
) { courseStructure, courseStatus, courseDatesResult ->
196+
Triple(courseStructure, courseStatus, courseDatesResult)
197+
}.catch { e ->
207198
handleCourseDataError(e)
199+
}.collect { (courseStructure, courseStatus, courseDates) ->
200+
if (courseStructure == null) return@collect
201+
val blocks = courseStructure.blockData
202+
val datesBannerInfo = courseDates.courseBanner
203+
204+
checkIfCalendarOutOfDate(courseDates.datesSection.values.flatten())
205+
updateOutdatedOfflineXBlocks(courseStructure)
206+
207+
initializeCourseData(blocks, courseStructure, courseStatus, datesBannerInfo)
208208
}
209209
}
210210
}

course/src/test/java/org/openedx/course/presentation/container/CourseContainerViewModelTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.mockk.verify
1111
import kotlinx.coroutines.Dispatchers
1212
import kotlinx.coroutines.ExperimentalCoroutinesApi
1313
import kotlinx.coroutines.flow.emptyFlow
14+
import kotlinx.coroutines.flow.flow
1415
import kotlinx.coroutines.flow.flowOf
1516
import kotlinx.coroutines.test.StandardTestDispatcher
1617
import kotlinx.coroutines.test.advanceUntilIdle
@@ -236,10 +237,10 @@ class CourseContainerViewModelTest {
236237
every { networkConnection.isOnline() } returns true
237238
coEvery {
238239
interactor.getCourseStructureFlow(any(), any())
239-
} returns flowOf(courseStructure)
240+
} returns flowOf(null)
240241
coEvery {
241242
interactor.getEnrollmentDetailsFlow(any())
242-
} throws Exception()
243+
} returns flow { throw Exception() }
243244
every {
244245
analytics.logScreenEvent(
245246
CourseAnalyticsEvent.DASHBOARD.eventName,

course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class CourseOutlineViewModelTest {
267267
any()
268268
)
269269
} returns Unit
270-
coEvery { interactor.getCourseStatusFlow(any()) } throws UnknownHostException()
270+
coEvery { interactor.getCourseStatusFlow(any()) } returns flow { throw UnknownHostException() }
271271

272272
val viewModel = CourseOutlineViewModel(
273273
"",
@@ -306,7 +306,7 @@ class CourseOutlineViewModelTest {
306306
coEvery { interactor.getCourseStructureFlow(any(), any()) } returns flowOf(courseStructure)
307307
every { networkConnection.isOnline() } returns true
308308
every { downloadDao.getAllDataFlow() } returns flow { emit(emptyList()) }
309-
coEvery { interactor.getCourseStatusFlow(any()) } throws Exception()
309+
coEvery { interactor.getCourseStatusFlow(any()) } returns flow { throw Exception() }
310310
val viewModel = CourseOutlineViewModel(
311311
"",
312312
"",

0 commit comments

Comments
 (0)