Skip to content

Commit ec39eb6

Browse files
committedMay 21, 2024
Refactor getting config sync params
1 parent 3d7e09c commit ec39eb6

File tree

4 files changed

+79
-95
lines changed

4 files changed

+79
-95
lines changed
 

‎android/dataclerk/src/main/java/org/dtree/fhircore/dataclerk/ui/home/HomeScreen.kt

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
2525
import androidx.compose.foundation.layout.padding
2626
import androidx.compose.material.ExperimentalMaterialApi
2727
import androidx.compose.material.icons.Icons
28-
import androidx.compose.material.icons.filled.BugReport
2928
import androidx.compose.material.icons.filled.Refresh
3029
import androidx.compose.material.icons.filled.Search
3130
import androidx.compose.material.icons.filled.Settings

‎android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDao.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.smartregister.fhircore.engine.data.local.register.dao
1818

19+
import ca.uhn.fhir.rest.gclient.DateClientParam
1920
import com.google.android.fhir.FhirEngine
2021
import com.google.android.fhir.logicalId
2122
import com.google.android.fhir.search.Operation
@@ -97,11 +98,10 @@ constructor(
9798
val patients =
9899
fhirEngine.search<Patient> {
99100
filter(Patient.ACTIVE, { value = of(true) })
100-
sort(Patient.NAME, Order.ASCENDING)
101-
count =
102-
if (loadAll) {
103-
countRegisterData(appFeatureName).toInt()
104-
} else PaginationConstant.DEFAULT_PAGE_SIZE
101+
sort(DateClientParam("_lastUpdated"), Order.DESCENDING)
102+
if (!loadAll) {
103+
count = PaginationConstant.DEFAULT_PAGE_SIZE
104+
}
105105
from = currentPage * PaginationConstant.DEFAULT_PAGE_SIZE
106106
}
107107

@@ -128,7 +128,7 @@ constructor(
128128
)
129129
filter(Patient.IDENTIFIER, { value = of(Identifier().apply { value = nameQuery }) })
130130
operation = Operation.OR
131-
sort(Patient.NAME, Order.ASCENDING)
131+
sort(DateClientParam("_lastUpdated"), Order.DESCENDING)
132132
}
133133

134134
return patients

‎android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncListenerManager.kt

+73-87
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ constructor(
9191
fun loadSyncParams(): Map<ResourceType, Map<String, String>> {
9292
val userInfo =
9393
sharedPreferencesHelper.read<UserClaimInfo>(SharedPreferenceKey.USER_CLAIM_INFO.name)
94-
val pairs = mutableListOf<Pair<ResourceType, Map<String, String>>>()
94+
val resourceTypeParamsMap = linkedMapOf<ResourceType, List<Pair<String, String>>>()
9595

9696
val appConfig = configurationRegistry.getAppConfigs()
9797

@@ -123,113 +123,99 @@ constructor(
123123
// ],..]
124124
sp.base.forEach { base ->
125125
val resourceType = ResourceType.fromCode(base)
126-
val pair = pairs.find { it.first == resourceType }
127-
if (pair == null) {
128-
pairs.add(
129-
Pair(
130-
resourceType,
131-
expressionValue?.let { mapOf(sp.code to expressionValue) } ?: mapOf(),
132-
),
133-
)
134-
} else {
135-
expressionValue?.let {
136-
// add another parameter if there is a matching resource type
137-
// e.g. [(Patient, {organization=105})] to [(Patient, {organization=105, _count=100})]
138-
val updatedPair = pair.second.toMutableMap().apply { put(sp.code, expressionValue) }
139-
val index = pairs.indexOfFirst { it.first == resourceType }
140-
resourceType.filterBasedOnPerResourceType(pairs)
141-
pairs.set(index, Pair(resourceType, updatedPair))
126+
expressionValue?.let { value ->
127+
resourceTypeParamsMap.merge(resourceType, listOf(sp.code to value)) { list1, list2 ->
128+
// resourceType.filterBasedOnPerResourceType(this)
129+
return@merge list1.toMutableList().apply { addAll(list2) }
142130
}
143131
}
144132
}
145133
}
146134

147-
val syncConfigParams = sharedPreferencesHelper.filterByResourceLocation(pairs)
148-
Timber.i("SYNC CONFIG $syncConfigParams")
135+
val filterByLocationParams =
136+
sharedPreferencesHelper.filterByResourceLocation(resourceTypeParamsMap)
137+
val filterBasedResourceParams =
138+
resourceTypeParamsMap
139+
.map {
140+
val resourceType = it.key
141+
val map = linkedMapOf<String, String>()
142+
resourceType.filterBasedOnPerResourceType(map)
143+
resourceType to map
144+
}
145+
.toMap()
146+
147+
val syncConfigParams =
148+
resourceTypeParamsMap
149+
// .filter { it.key == ResourceType.Patient }
150+
.map {
151+
val resourceType = it.key
152+
val paramsMap = linkedMapOf<String, String>("_total" to "none")
153+
paramsMap.putAll(filterBasedResourceParams.getOrDefault(resourceType, emptyMap()))
154+
paramsMap.putAll(filterByLocationParams.getOrDefault(resourceType, emptyList()))
155+
paramsMap.putAll(it.value)
156+
resourceType to paramsMap
157+
}
158+
.toMap()
159+
160+
val orderedSyncConfigParams =
161+
linkedMapOf<ResourceType, Map<String, String>>().apply {
162+
put(ResourceType.Binary, syncConfigParams.getOrDefault(ResourceType.Binary, emptyMap()))
163+
put(
164+
ResourceType.StructureMap,
165+
syncConfigParams.getOrDefault(ResourceType.StructureMap, emptyMap())
166+
)
167+
put(
168+
ResourceType.Questionnaire,
169+
syncConfigParams.getOrDefault(ResourceType.Questionnaire, emptyMap())
170+
)
171+
putAll(syncConfigParams)
172+
}
149173

150-
return mapOf(*syncConfigParams.toTypedArray())
174+
Timber.i("SYNC CONFIG $orderedSyncConfigParams")
175+
return orderedSyncConfigParams
151176
}
152177
}
153178

154179
private fun ResourceType.filterBasedOnPerResourceType(
155-
pairs: MutableList<Pair<ResourceType, Map<String, String>>>,
156-
) =
180+
resourceTypePair: MutableMap<String, String>,
181+
) {
157182
when (this) {
158-
ResourceType.RelatedPerson ->
159-
pairs.addParam(resourceType = this, param = RelatedPerson.SP_ACTIVE, value = true.toString())
160-
ResourceType.Patient ->
161-
pairs.addParam(resourceType = this, param = Patient.SP_ACTIVE, value = true.toString())
162-
163-
// ResourceType.CarePlan ->
164-
// pairs.addParam(
165-
// resourceType = this,
166-
// param = CarePlan.SP_STATUS,
167-
// value = CarePlan.CarePlanStatus.ACTIVE.toString().lowercase(),
168-
// )
169-
170-
ResourceType.Observation ->
171-
pairs.addParam(
172-
resourceType = this,
173-
param = Observation.SP_STATUS,
174-
value = Observation.ObservationStatus.FINAL.toString().lowercase(),
175-
)
176-
177-
// ResourceType.Task ->
178-
// pairs.addParam(
179-
// resourceType = this,
180-
// param = Task.SP_STATUS,
181-
// value =
182-
// String.format(
183-
// "%s,%s",
184-
// Task.TaskStatus.FAILED.toString().lowercase(),
185-
// Task.TaskStatus.INPROGRESS.toString().lowercase()
186-
// )
187-
// )
188-
183+
ResourceType.RelatedPerson -> resourceTypePair[RelatedPerson.SP_ACTIVE] = true.toString()
184+
ResourceType.Patient -> resourceTypePair[Patient.SP_ACTIVE] = true.toString()
189185
ResourceType.Appointment ->
190-
pairs.addParam(
191-
resourceType = this,
192-
param = Appointment.SP_STATUS,
193-
value = Appointment.AppointmentStatus.BOOKED.toString().lowercase(),
194-
)
186+
resourceTypePair[Appointment.SP_STATUS] =
187+
Appointment.AppointmentStatus.BOOKED.toString().lowercase()
195188
ResourceType.Encounter ->
196-
pairs.addParam(
197-
resourceType = this,
198-
param = Encounter.SP_STATUS,
199-
value = Encounter.EncounterStatus.INPROGRESS.toString().lowercase(),
200-
)
189+
resourceTypePair[Encounter.SP_STATUS] =
190+
Encounter.EncounterStatus.INPROGRESS.toString().lowercase()
201191
ResourceType.List ->
202-
pairs.addParam(
203-
resourceType = this,
204-
param = ListResource.SP_STATUS,
205-
value = ListResource.ListStatus.CURRENT.toString().lowercase(),
206-
)
192+
resourceTypePair[ListResource.SP_STATUS] =
193+
ListResource.ListStatus.CURRENT.toString().lowercase()
194+
ResourceType.Observation ->
195+
resourceTypePair[Observation.SP_STATUS] =
196+
Observation.ObservationStatus.FINAL.toString().lowercase()
197+
// ResourceType.CarePlan -> resourceTypePair.put(CarePlan.SP_STATUS,
198+
// CarePlan.CarePlanStatus.ACTIVE.toString().lowercase())
199+
// ResourceType.Task -> resourceTypePair.put(Task.SP_STATUS, String.format("%s,%s",
200+
// Task.TaskStatus.FAILED.toString().lowercase(),
201+
// Task.TaskStatus.INPROGRESS.toString().lowercase()))
207202
else -> Unit
208203
}
204+
}
209205

210206
private fun SharedPreferencesHelper.filterByResourceLocation(
211-
pairs: MutableList<Pair<ResourceType, Map<String, String>>>,
212-
): MutableList<Pair<ResourceType, Map<String, String>>> {
213-
val resourcesTemp = mutableListOf<Pair<ResourceType, Map<String, String>>>()
214-
val results = mutableListOf<Pair<ResourceType, Map<String, String>>>()
215-
resourcesTemp.addAll(pairs)
216-
207+
resourceTypePairsMap: Map<ResourceType, List<Pair<String, String>>>,
208+
): Map<ResourceType, List<Pair<String, String>>> {
217209
val organisationSystem = context.getString(R.string.sync_strategy_organization_system)
218210
val organisationTag = "$organisationSystem|${organisationCode()}"
219211

220-
resourcesTemp.forEach {
221-
val resourceType = it.first
222-
if (
223-
resourceType != ResourceType.Practitioner &&
224-
resourceType != ResourceType.Questionnaire &&
225-
resourceType != ResourceType.StructureMap
226-
) {
227-
val tags = mutableMapOf("_tag" to organisationTag)
228-
it.second.entries.forEach { entry -> tags[entry.key] = entry.value }
229-
results.add(Pair(resourceType, tags))
230-
} else results.add(it)
231-
}
232-
return results
212+
return resourceTypePairsMap
213+
.filter {
214+
it.key !in
215+
arrayOf(ResourceType.Practitioner, ResourceType.Questionnaire, ResourceType.StructureMap)
216+
}
217+
.map { it.key to listOf("_tag" to organisationTag) }
218+
.toMap()
233219
}
234220

235221
private fun MutableList<Pair<ResourceType, Map<String, String>>>.addParam(

‎android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/models/ProfileViewData.kt

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import org.hl7.fhir.r4.model.Task
2727
import org.smartregister.fhircore.engine.data.domain.Guardian
2828
import org.smartregister.fhircore.engine.domain.model.FormButtonData
2929
import org.smartregister.fhircore.engine.domain.model.TracingAttempt
30-
import org.smartregister.fhircore.engine.util.extension.extractedTracingCategoryIsPhone
3130
import org.smartregister.fhircore.quest.ui.family.profile.model.FamilyMemberViewState
3231

3332
sealed class ProfileViewData(

0 commit comments

Comments
 (0)
Please sign in to comment.