diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModel.kt index 5cbeeae8e3..6420fff3d7 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModel.kt @@ -23,6 +23,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.android.fhir.datacapture.extensions.logicalId import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -50,6 +51,7 @@ import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid import org.smartregister.fhircore.engine.util.extension.interpolate +import org.smartregister.fhircore.engine.util.extension.retrieveRelatedEntitySyncLocationIds import org.smartregister.fhircore.geowidget.model.GeoJsonFeature import org.smartregister.fhircore.geowidget.model.Geometry import org.smartregister.fhircore.quest.ui.shared.QuestionnaireHandler @@ -63,6 +65,7 @@ constructor( val sharedPreferencesHelper: SharedPreferencesHelper, val resourceDataRulesExecutor: ResourceDataRulesExecutor, val configurationRegistry: ConfigurationRegistry, + @ApplicationContext val context: Context, ) : ViewModel() { private val _snackBarStateFlow = MutableSharedFlow() val snackBarStateFlow = _snackBarStateFlow.asSharedFlow() @@ -175,8 +178,10 @@ constructor( } } - fun showNoLocationDialog(geoWidgetConfiguration: GeoWidgetConfiguration) { - geoWidgetConfiguration.noResults?.let { _noLocationFoundDialog.postValue(true) } + suspend fun showNoLocationDialog(geoWidgetConfiguration: GeoWidgetConfiguration) { + geoWidgetConfiguration.noResults?.let { + _noLocationFoundDialog.postValue(context.retrieveRelatedEntitySyncLocationIds().isEmpty()) + } } suspend fun onQuestionnaireSubmission( diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModelTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModelTest.kt index cbe5c9c652..446b1552b4 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModelTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/geowidget/GeoWidgetLauncherViewModelTest.kt @@ -16,9 +16,12 @@ package org.smartregister.fhircore.quest.ui.geowidget +import android.content.Context +import androidx.test.core.app.ApplicationProvider import com.google.android.fhir.datacapture.extensions.logicalId import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.HiltTestApplication import io.mockk.slot import io.mockk.spyk import io.mockk.verify @@ -68,6 +71,8 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() { @Inject lateinit var resourceDataRulesExecutor: ResourceDataRulesExecutor + private lateinit var applicationContext: Context + private val configurationRegistry = Faker.buildTestConfigurationRegistry() private lateinit var viewModel: GeoWidgetLauncherViewModel private val geoWidgetConfiguration = @@ -98,6 +103,7 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() { @Before fun setUp() { hiltAndroidRule.inject() + applicationContext = ApplicationProvider.getApplicationContext() viewModel = GeoWidgetLauncherViewModel( defaultRepository = defaultRepository, @@ -105,13 +111,13 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() { sharedPreferencesHelper = sharedPreferencesHelper, resourceDataRulesExecutor = resourceDataRulesExecutor, configurationRegistry = configurationRegistry, + context = applicationContext, ) - runBlocking { defaultRepository.addOrUpdate(resource = location) } } @Test - fun testShowNoLocationDialogShouldNotSetLiveDataValueWhenConfigIsNull() { + fun testShowNoLocationDialogShouldNotSetLiveDataValueWhenConfigIsNull() = runTest { val geoWidgetConfiguration = GeoWidgetConfiguration( appId = "appId", @@ -132,7 +138,7 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() { } @Test - fun testShowNoLocationDialogShouldSetLiveDataValueWhenConfigIsPresent() { + fun testShowNoLocationDialogShouldSetLiveDataValueWhenConfigIsPresent() = runTest { viewModel.showNoLocationDialog(geoWidgetConfiguration) val value = viewModel.noLocationFoundDialog.value assertNotNull(value)