Skip to content

Commit

Permalink
Pass the context down to retrieve-locations-functn (#3709)
Browse files Browse the repository at this point in the history
Signed-off-by: Lentumunai-Mark <[email protected]>
  • Loading branch information
Lentumunai-Mark authored Jan 31, 2025
1 parent 4ddd5db commit 4359a24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ class GeoWidgetLauncherFragment : Fragment(), OnSyncListener {
searchQuery = searchViewModel.searchQuery,
search = { searchText ->
geoWidgetLauncherViewModel.run {
onEvent(GeoWidgetEvent.ClearMap)
onEvent(GeoWidgetEvent.ClearMap, context)
onEvent(
GeoWidgetEvent.RetrieveFeatures(
searchQuery = SearchQuery(searchText, SearchMode.KeyboardInput),
geoWidgetConfig = geoWidgetConfiguration,
),
context,
)
}
},
Expand Down Expand Up @@ -227,13 +228,14 @@ class GeoWidgetLauncherFragment : Fragment(), OnSyncListener {
is CurrentSyncJobStatus.Failed, -> {
appMainViewModel.updateAppDrawerUIState(currentSyncJobStatus = syncJobStatus)
if (syncJobStatus is CurrentSyncJobStatus.Succeeded) {
geoWidgetLauncherViewModel.onEvent(GeoWidgetEvent.ClearMap)
geoWidgetLauncherViewModel.onEvent(GeoWidgetEvent.ClearMap, context = requireContext())
}
geoWidgetLauncherViewModel.onEvent(
GeoWidgetEvent.RetrieveFeatures(
geoWidgetConfig = geoWidgetConfiguration,
searchQuery = searchViewModel.searchQuery.value,
),
context = requireContext(),
)
}
else -> appMainViewModel.updateAppDrawerUIState(currentSyncJobStatus = syncJobStatus)
Expand All @@ -252,12 +254,13 @@ class GeoWidgetLauncherFragment : Fragment(), OnSyncListener {
is AppEvent.OnSubmitQuestionnaire, -> {
appMainViewModel.countRegisterData()
geoWidgetLauncherViewModel.run {
onEvent(GeoWidgetEvent.ClearMap)
onEvent(GeoWidgetEvent.ClearMap, context = requireContext())
onEvent(
GeoWidgetEvent.RetrieveFeatures(
geoWidgetConfig = geoWidgetConfiguration,
searchQuery = searchViewModel.searchQuery.value,
),
context = requireContext(),
)
}
}
Expand Down Expand Up @@ -296,6 +299,7 @@ class GeoWidgetLauncherFragment : Fragment(), OnSyncListener {
geoWidgetConfig = geoWidgetConfiguration,
searchQuery = searchViewModel.searchQuery.value,
),
context = requireContext(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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.asSharedFlow
Expand Down Expand Up @@ -67,7 +66,6 @@ constructor(
val sharedPreferencesHelper: SharedPreferencesHelper,
val rulesExecutor: RulesExecutor,
val configurationRegistry: ConfigurationRegistry,
@ApplicationContext val context: Context,
) : ViewModel() {
val clearMapLiveData: MutableLiveData<Boolean> = MutableLiveData()
val geoJsonFeatures: MutableLiveData<List<GeoJsonFeature>> = MutableLiveData()
Expand All @@ -88,17 +86,22 @@ constructor(

private val decodedImageMap = mutableStateMapOf<String, Bitmap>()

fun onEvent(geoWidgetEvent: GeoWidgetEvent) {
fun onEvent(geoWidgetEvent: GeoWidgetEvent, context: Context) {
when (geoWidgetEvent) {
is GeoWidgetEvent.RetrieveFeatures ->
retrieveLocations(geoWidgetEvent.geoWidgetConfig, geoWidgetEvent.searchQuery.query)
retrieveLocations(
geoWidgetEvent.geoWidgetConfig,
geoWidgetEvent.searchQuery.query,
context,
)
GeoWidgetEvent.ClearMap -> clearMapLiveData.postValue(true)
}
}

private fun retrieveLocations(
geoWidgetConfig: GeoWidgetConfiguration,
searchText: String?,
context: Context,
) {
viewModelScope.launch {
_isSyncing.postValue(true)
Expand Down Expand Up @@ -235,7 +238,10 @@ constructor(
}
}

suspend fun showNoLocationDialog(geoWidgetConfiguration: GeoWidgetConfiguration) {
suspend fun showNoLocationDialog(
geoWidgetConfiguration: GeoWidgetConfiguration,
context: Context,
) {
geoWidgetConfiguration.noResults?.let {
_noLocationFoundDialog.postValue(
context.retrieveRelatedEntitySyncLocationState(MultiSelectViewAction.SYNC_DATA).isEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() {
sharedPreferencesHelper = sharedPreferencesHelper,
rulesExecutor = rulesExecutor,
configurationRegistry = configurationRegistry,
context = applicationContext,
)
runBlocking { defaultRepository.addOrUpdate(resource = location) }
}
Expand All @@ -125,15 +124,15 @@ class GeoWidgetLauncherViewModelTest : RobolectricTest() {
servicePointConfig = null,
)

viewModel.showNoLocationDialog(geoWidgetConfiguration)
viewModel.showNoLocationDialog(geoWidgetConfiguration, context = applicationContext)

val value = viewModel.noLocationFoundDialog.value
assertNull(value)
}

@Test
fun testShowNoLocationDialogShouldSetLiveDataValueWhenConfigIsPresent() = runTest {
viewModel.showNoLocationDialog(geoWidgetConfiguration)
viewModel.showNoLocationDialog(geoWidgetConfiguration, applicationContext)
val value = viewModel.noLocationFoundDialog.value
assertNotNull(value)
assertTrue(value!!)
Expand Down

0 comments on commit 4359a24

Please sign in to comment.