Skip to content

Commit

Permalink
fix: set dataValue to true for CourseOutline (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid authored Nov 18, 2024
1 parent fa38468 commit 1a3826a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -164,10 +162,7 @@ class CourseContainerFragment : Fragment(R.layout.fragment_course_container) {
}
}
viewModel.errorMessage.observe(viewLifecycleOwner) {
snackBar = Snackbar.make(binding.root, it, Snackbar.LENGTH_INDEFINITE)
.setAction(org.openedx.core.R.string.core_error_try_again) {
viewModel.fetchCourseDetails()
}
snackBar = Snackbar.make(binding.root, it, Snackbar.LENGTH_SHORT)
snackBar?.show()
}
viewLifecycleOwner.lifecycleScope.launch {
Expand All @@ -180,6 +175,8 @@ class CourseContainerFragment : Fragment(R.layout.fragment_course_container) {
private fun onRefresh(currentPage: Int) {
if (viewModel.courseAccessStatus.value == CourseAccessError.NONE) {
viewModel.onRefresh(CourseContainerTab.entries[currentPage])
} else {
viewModel.fetchCourseDetails()
}
}

Expand Down Expand Up @@ -390,7 +387,7 @@ fun CourseDashboard(
isInternetConnectionShown = true
},
onReloadClick = {
isInternetConnectionShown = true
isInternetConnectionShown = viewModel.hasInternetConnection
onRefresh(pagerState.currentPage)
}
)
Expand Down Expand Up @@ -520,7 +517,7 @@ private fun DashboardPager(

@Composable
private fun CourseAccessErrorView(
viewModel: CourseContainerViewModel?,
viewModel: CourseContainerViewModel,
accessError: CourseAccessError?,
fragmentManager: FragmentManager,
) {
Expand All @@ -532,7 +529,7 @@ private fun CourseAccessErrorView(
R.string.course_error_expired_not_upgradeable_title,
TimeUtils.getCourseAccessFormattedDate(
LocalContext.current,
viewModel?.courseDetails?.courseAccessDetails?.auditAccessExpires ?: Date()
viewModel.courseDetails?.courseAccessDetails?.auditAccessExpires ?: Date()
)
)
}
Expand All @@ -541,7 +538,7 @@ private fun CourseAccessErrorView(
icon = painterResource(id = R.drawable.course_ic_calendar)
message = stringResource(
R.string.course_error_not_started_title,
viewModel?.courseDetails?.courseInfoOverview?.startDisplay ?: ""
viewModel.courseDetails?.courseInfoOverview?.startDisplay ?: ""
)
}

Expand Down Expand Up @@ -595,6 +592,7 @@ private fun CourseAccessErrorView(
)
}
SetupCourseAccessErrorButtons(
viewModel = viewModel,
accessError = accessError,
fragmentManager = fragmentManager,
)
Expand All @@ -604,20 +602,29 @@ private fun CourseAccessErrorView(

@Composable
private fun SetupCourseAccessErrorButtons(
viewModel: CourseContainerViewModel,
accessError: CourseAccessError?,
fragmentManager: FragmentManager,
) {
when (accessError) {
CourseAccessError.AUDIT_EXPIRED_NOT_UPGRADABLE,
CourseAccessError.NOT_YET_STARTED,
CourseAccessError.UNKNOWN,
-> {
OpenEdXButton(
text = stringResource(R.string.course_label_back),
onClick = { fragmentManager.popBackStack() },
)
}

CourseAccessError.UNKNOWN -> {
if (viewModel.hasInternetConnection) {
OpenEdXButton(
text = stringResource(R.string.course_label_back),
onClick = { fragmentManager.popBackStack() },
)
}
}

else -> {}
}
}
Expand All @@ -628,17 +635,3 @@ private fun scrollToDates(scope: CoroutineScope, pagerState: PagerState) {
pagerState.animateScrollToPage(CourseContainerTab.entries.indexOf(CourseContainerTab.DATES))
}
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun CourseAccessErrorViewPreview() {
val context = LocalContext.current
OpenEdXTheme {
CourseAccessErrorView(
viewModel = null,
accessError = CourseAccessError.AUDIT_EXPIRED_NOT_UPGRADABLE,
fragmentManager = (context as? FragmentActivity)?.supportFragmentManager!!
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class CourseContainerViewModel(
delay(500L)
courseNotifier.send(CourseOpenBlock(resumeBlockId))
}
_dataReady.value = true
}
} ?: run {
_courseAccessStatus.value = CourseAccessError.UNKNOWN
Expand Down Expand Up @@ -276,14 +277,9 @@ class CourseContainerViewModel(
viewModelScope.launch {
try {
interactor.getCourseStructure(courseId, isNeedRefresh = true)
} catch (e: Exception) {
if (e.isInternetError()) {
_errorMessage.value =
resourceManager.getString(CoreR.string.core_error_no_connection)
} else {
_errorMessage.value =
resourceManager.getString(CoreR.string.core_error_unknown_error)
}
} catch (ignore: Exception) {
_errorMessage.value =
resourceManager.getString(CoreR.string.core_error_unknown_error)
}
_refreshing.value = false
courseNotifier.send(CourseStructureUpdated(courseId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import org.openedx.course.presentation.CourseAnalyticsEvent
import org.openedx.course.presentation.CourseRouter
import org.openedx.course.utils.ImageProcessor
import org.openedx.foundation.system.ResourceManager
import java.net.UnknownHostException
import java.util.Date

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -216,49 +215,6 @@ class CourseContainerViewModelTest {
Dispatchers.resetMain()
}

@Test
fun `getCourseEnrollmentDetails internet connection exception`() = runTest {
val viewModel = CourseContainerViewModel(
"",
"",
"",
config,
interactor,
resourceManager,
courseNotifier,
networkConnection,
corePreferences,
analytics,
imageProcessor,
calendarSyncScheduler,
courseRouter,
)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseStructure(any(), any()) } returns courseStructure
coEvery { interactor.getEnrollmentDetails(any()) } throws UnknownHostException()
every {
analytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
} returns Unit
viewModel.fetchCourseDetails()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getEnrollmentDetails(any()) }
verify(exactly = 1) {
analytics.logScreenEvent(
CourseAnalyticsEvent.DASHBOARD.eventName,
any()
)
}

val message = viewModel.errorMessage.value
assertEquals(noInternet, message)
assert(!viewModel.refreshing.value)
assert(viewModel.courseAccessStatus.value == null)
}

@Test
fun `getCourseEnrollmentDetails unknown exception`() = runTest {
val viewModel = CourseContainerViewModel(
Expand Down Expand Up @@ -380,35 +336,6 @@ class CourseContainerViewModelTest {
assert(viewModel.courseAccessStatus.value != null)
}

@Test
fun `updateData no internet connection exception`() = runTest {
val viewModel = CourseContainerViewModel(
"",
"",
"",
config,
interactor,
resourceManager,
courseNotifier,
networkConnection,
corePreferences,
analytics,
imageProcessor,
calendarSyncScheduler,
courseRouter
)
coEvery { interactor.getCourseStructure(any(), true) } throws UnknownHostException()
coEvery { courseNotifier.send(CourseStructureUpdated("")) } returns Unit
viewModel.updateData()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getCourseStructure(any(), true) }

val message = viewModel.errorMessage.value
assertEquals(noInternet, message)
assert(!viewModel.refreshing.value)
}

@Test
fun `updateData unknown exception`() = runTest {
val viewModel = CourseContainerViewModel(
Expand Down

0 comments on commit 1a3826a

Please sign in to comment.