Skip to content

Commit

Permalink
fix: small code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
omerhabib26 committed Dec 4, 2023
1 parent d1d52ca commit 50f3a35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.core.AppUpdateState
import org.openedx.core.AppUpdateState.wasUpdateDialogClosed
import org.openedx.core.UIMessage
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.domain.model.Course
import org.openedx.core.domain.model.Media
import org.openedx.core.presentation.dialog.appupgrade.AppUpgradeDialogFragment
Expand All @@ -47,7 +46,6 @@ class DiscoveryFragment : Fragment() {

private val viewModel by viewModel<DiscoveryViewModel>()
private val router: DiscoveryRouter by inject()
private val corePreferencesManager by inject<CorePreferences>()

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -66,8 +64,7 @@ class DiscoveryFragment : Fragment() {
val appUpgradeEvent by viewModel.appUpgradeEvent.observeAsState()
val wasUpdateDialogClosed by remember { wasUpdateDialogClosed }
val querySearch = arguments?.getString(ARG_SEARCH_QUERY, "") ?: ""
val isLogistrationEnabled by viewModel.isLogistrationEnabled.observeAsState(false)
val canShowBackButton = isLogistrationEnabled && corePreferencesManager.user == null
val canShowBackButton by viewModel.canShowBackButton.observeAsState(false)

DiscoveryScreen(
windowSize = windowSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.openedx.core.BuildConfig
import org.openedx.core.R
import org.openedx.core.SingleEventLiveData
import org.openedx.core.UIMessage
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.config.Config
import org.openedx.core.domain.model.Course
import org.openedx.core.extension.isInternetError
Expand All @@ -26,7 +27,8 @@ class DiscoveryViewModel(
private val interactor: DiscoveryInteractor,
private val resourceManager: ResourceManager,
private val analytics: DiscoveryAnalytics,
private val appUpgradeNotifier: AppUpgradeNotifier
private val appUpgradeNotifier: AppUpgradeNotifier,
private val corePreferences: CorePreferences
) : BaseViewModel() {

val apiHostUrl get() = config.getApiHostURL()
Expand All @@ -51,8 +53,8 @@ class DiscoveryViewModel(
val appUpgradeEvent: LiveData<AppUpgradeEvent>
get() = _appUpgradeEvent

private val _isLogistrationEnabled = MutableLiveData<Boolean>()
val isLogistrationEnabled: LiveData<Boolean> = _isLogistrationEnabled
private val _canShowBackButton = MutableLiveData<Boolean>()
val canShowBackButton: LiveData<Boolean> = _canShowBackButton

val hasInternetConnection: Boolean
get() = networkConnection.isOnline()
Expand All @@ -62,7 +64,8 @@ class DiscoveryViewModel(
private var isLoading = false

init {
_isLogistrationEnabled.value = BuildConfig.PRE_LOGIN_EXPERIENCE_ENABLED
_canShowBackButton.value =
BuildConfig.PRE_LOGIN_EXPERIENCE_ENABLED && corePreferences.user == null
getCoursesList()
collectAppUpgradeEvent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.R
import org.openedx.core.UIMessage
import org.openedx.core.config.Config
Expand All @@ -42,6 +43,7 @@ class DiscoveryViewModelTest {
private val networkConnection = mockk<NetworkConnection>()
private val analytics = mockk<DiscoveryAnalytics>()
private val appUpgradeNotifier = mockk<AppUpgradeNotifier>()
private val corePreferences = mockk<CorePreferences>()

private val noInternet = "Slow or no internet connection"
private val somethingWrong = "Something went wrong"
Expand All @@ -52,6 +54,7 @@ class DiscoveryViewModelTest {
every { resourceManager.getString(R.string.core_error_no_connection) } returns noInternet
every { resourceManager.getString(R.string.core_error_unknown_error) } returns somethingWrong
every { appUpgradeNotifier.notifier } returns emptyFlow()
every { corePreferences.user } returns null
every { config.getApiHostURL() } returns "http://localhost:8000"
}

Expand All @@ -62,7 +65,7 @@ class DiscoveryViewModelTest {

@Test
fun `getCoursesList no internet connection`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } throws UnknownHostException()
advanceUntilIdle()
Expand All @@ -79,7 +82,7 @@ class DiscoveryViewModelTest {

@Test
fun `getCoursesList unknown exception`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } throws Exception()
advanceUntilIdle()
Expand All @@ -95,7 +98,7 @@ class DiscoveryViewModelTest {

@Test
fun `getCoursesList from cache`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns false
coEvery { interactor.getCoursesListFromCache() } returns emptyList()
advanceUntilIdle()
Expand All @@ -110,7 +113,7 @@ class DiscoveryViewModelTest {

@Test
fun `getCoursesList from network with next page`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } returns CourseList(
Pagination(
Expand All @@ -132,7 +135,7 @@ class DiscoveryViewModelTest {

@Test
fun `getCoursesList from network without next page`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } returns CourseList(
Pagination(
Expand All @@ -155,7 +158,7 @@ class DiscoveryViewModelTest {

@Test
fun `updateData no internet connection`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } throws UnknownHostException()
viewModel.updateData()
Expand All @@ -172,7 +175,7 @@ class DiscoveryViewModelTest {

@Test
fun `updateData unknown exception`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } throws Exception()
viewModel.updateData()
Expand All @@ -189,7 +192,7 @@ class DiscoveryViewModelTest {

@Test
fun `updateData success with next page`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } returns CourseList(
Pagination(
Expand All @@ -212,7 +215,7 @@ class DiscoveryViewModelTest {

@Test
fun `updateData success without next page`() = runTest {
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier)
val viewModel = DiscoveryViewModel(config, networkConnection, interactor, resourceManager, analytics, appUpgradeNotifier, corePreferences)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCoursesList(any(), any(), any()) } returns CourseList(
Pagination(
Expand Down

0 comments on commit 50f3a35

Please sign in to comment.