Skip to content

Commit

Permalink
Implement searchable multiselect widget (#3123)
Browse files Browse the repository at this point in the history
* Implement multiselectview

Start implementation on multi-select view. Includes the checkbox and listeners

Signed-off-by: Elly Kitoto <[email protected]>

* Change preview data

Signed-off-by: Elly Kitoto <[email protected]>

* Use TristateCheckbox on MultiSelect view

Signed-off-by: Elly Kitoto <[email protected]>

* Implement functionality for generating map required in multiselectview

Signed-off-by: Elly Kitoto <[email protected]>

* Implement bottomsheet for multi select widget

Signed-off-by: Elly Kitoto <[email protected]>

* Fix multi-select checkbox select color

Signed-off-by: Elly Kitoto <[email protected]>

* Implement functionality for node selection

Signed-off-by: Elly Kitoto <[email protected]>

* Refactor multi select implementation

Use Tree data structure as required. Search Tree instead of
searching the map. Render the UI from the Tree.

Signed-off-by: Elly Kitoto <[email protected]>

* Improve UX on multi selector widget search

Hide keyboard when search action is triggered. Reset data when search
text is empty.

Signed-off-by: Elly Kitoto <[email protected]>

* Refactor MultiSelect UI to use Compose Scaffold

Signed-off-by: Elly Kitoto <[email protected]>

* Use ProtoDataStore to store SyncLocations

Also refactored how root nodes are identified. Use configuration instead
of defaulting to a node without a parent node as the root node.

Signed-off-by: Elly Kitoto <[email protected]>

* Use selected locations from multi-select widget to sync resources

Add comma separated values for _syncLocations query parameter for all requests for the
configured sync Resources.

Signed-off-by: Elly Kitoto <[email protected]>

* Refactor ApplicationConfiguration.syncStrategies to syncStrategy

Signed-off-by: Elly Kitoto <[email protected]>

* Fix child node checked state issue

* Show progress dialog

* Add no results view

* Refactor initial sync logic + disable sync progresss dialog

* Disable progress dialog on initial sync

* Fix spotless formatting errors

* Add Practitioner to SyncStrategy

* Fix failing tests

Signed-off-by: Elly Kitoto <[email protected]>

---------

Signed-off-by: Elly Kitoto <[email protected]>
Co-authored-by: Benjamin Mwalimu <[email protected]>
Co-authored-by: Hamza Ahmed Khan <[email protected]>
Co-authored-by: Hamza Ahmed Khan <[email protected]>
  • Loading branch information
4 people authored May 13, 2024
1 parent 5c2da08 commit b6719ff
Show file tree
Hide file tree
Showing 38 changed files with 1,689 additions and 66 deletions.
1 change: 0 additions & 1 deletion android/engine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ dependencies {
api(libs.jjwt)
api(libs.fhir.common.utils) { exclude(group = "org.slf4j", module = "jcl-over-slf4j") }
api(libs.runtime.livedata)
// api(libs.material3)
api(libs.foundation)
api(libs.fhir.common.utils)
api(libs.kotlinx.serialization.json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ sealed class ConfigType(
val parseAsResource: Boolean = false,
val multiConfig: Boolean = false,
) {
object Application : ConfigType("application")
data object Application : ConfigType("application")

object Sync : ConfigType(name = "sync", parseAsResource = true)
data object Sync : ConfigType(name = "sync", parseAsResource = true)

object Navigation : ConfigType("navigation")
data object Navigation : ConfigType("navigation")

object Register : ConfigType(name = "register", multiConfig = true)
data object Register : ConfigType(name = "register", multiConfig = true)

object MeasureReport : ConfigType(name = "measureReport", multiConfig = true)
data object MeasureReport : ConfigType(name = "measureReport", multiConfig = true)

object Profile : ConfigType(name = "profile", multiConfig = true)
data object Profile : ConfigType(name = "profile", multiConfig = true)

object GeoWidget : ConfigType(name = "geoWidget", multiConfig = true)
data object GeoWidget : ConfigType(name = "geoWidget", multiConfig = true)

object DataMigration : ConfigType(name = "dataMigration", multiConfig = true)
data object DataMigration : ConfigType(name = "dataMigration", multiConfig = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data class ApplicationConfiguration(
val languages: List<String> = listOf("en"),
val useDarkTheme: Boolean = false,
val syncInterval: Long = 15,
val syncStrategies: List<String> = listOf(),
val syncStrategy: List<SyncStrategy> = listOf(),
val loginConfig: LoginConfig = LoginConfig(),
val deviceToDeviceSync: DeviceToDeviceSyncConfig? = null,
val snackBarTheme: SnackBarThemeConfig = SnackBarThemeConfig(),
Expand All @@ -42,8 +42,19 @@ data class ApplicationConfiguration(
val taskBackgroundWorkerBatchSize: Int = 500,
val eventWorkflows: List<EventWorkflow> = emptyList(),
val logGpsLocation: List<LocationLogOptions> = emptyList(),
val usePractitionerAssignedLocationOnSync: Boolean =
true, // TODO This defaults to scheduling periodic sync, otherwise use sync location ids from
// location selector
) : Configuration()

enum class SyncStrategy {
Location,
CareTeam,
RelatedEntityLocation,
Organization,
Practitioner,
}

enum class LocationLogOptions {
QUESTIONNAIRE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ enum class ApplicationWorkflow {

/** A workflow that copies text to keyboard */
COPY_TEXT,

/** A workflow that launches location selector widget * */
LAUNCH_LOCATION_SELECTOR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PreferenceDataStore @Inject constructor(@ApplicationContext val context: C
companion object Keys {
val APP_ID by lazy { stringPreferencesKey("appId") }
val LANG by lazy { stringPreferencesKey("lang") }
val SYNC_LOCATION_IDS by lazy { stringPreferencesKey("syncLocationIds") }
val MIGRATION_VERSION by lazy { intPreferencesKey("migrationVersion") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import kotlinx.coroutines.flow.catch
import org.smartregister.fhircore.engine.datastore.mockdata.PractitionerDetails
import org.smartregister.fhircore.engine.datastore.mockdata.UserInfo
import org.smartregister.fhircore.engine.datastore.serializers.PractitionerDetailsDataStoreSerializer
import org.smartregister.fhircore.engine.datastore.serializers.SyncLocationIdDataStoreSerializer
import org.smartregister.fhircore.engine.datastore.serializers.UserInfoDataStoreSerializer
import org.smartregister.fhircore.engine.domain.model.SyncLocationToggleableState
import timber.log.Timber

private const val PRACTITIONER_DETAILS_DATASTORE_JSON = "practitioner_details.json"
private const val USER_INFO_DATASTORE_JSON = "user_info.json"
private const val SYNC_LOCATION_IDS = "sync_location_ids.json"
private const val TAG = "Proto DataStore"

val Context.practitionerProtoStore: DataStore<PractitionerDetails> by
Expand All @@ -46,6 +49,12 @@ val Context.userInfoProtoStore: DataStore<UserInfo> by
serializer = UserInfoDataStoreSerializer,
)

val Context.syncLocationIdsProtoStore: DataStore<List<SyncLocationToggleableState>> by
dataStore(
fileName = SYNC_LOCATION_IDS,
serializer = SyncLocationIdDataStoreSerializer,
)

@Singleton
class ProtoDataStore @Inject constructor(@ApplicationContext val context: Context) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object PractitionerDetailsDataStoreSerializer : Serializer<PractitionerDetails>
deserializer = PractitionerDetails.serializer(),
string = input.readBytes().decodeToString(),
)
} catch (e: SerializationException) {
Timber.tag(SerializerConstants.PROTOSTORE_SERIALIZER_TAG).d(e)
} catch (serializationException: SerializationException) {
Timber.e(serializationException)
defaultValue
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.engine.datastore.serializers

import androidx.datastore.core.Serializer
import java.io.InputStream
import java.io.OutputStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.apache.commons.lang3.SerializationException
import org.smartregister.fhircore.engine.domain.model.SyncLocationToggleableState
import org.smartregister.fhircore.engine.util.extension.encodeJson
import org.smartregister.fhircore.engine.util.extension.json
import timber.log.Timber

object SyncLocationIdDataStoreSerializer : Serializer<List<SyncLocationToggleableState>> {

override val defaultValue: List<SyncLocationToggleableState>
get() = emptyList()

override suspend fun readFrom(input: InputStream): List<SyncLocationToggleableState> {
return try {
json.decodeFromString<List<SyncLocationToggleableState>>(input.readBytes().decodeToString())
} catch (serializationException: SerializationException) {
Timber.e(serializationException)
defaultValue
}
}

override suspend fun writeTo(t: List<SyncLocationToggleableState>, output: OutputStream) {
withContext(Dispatchers.IO) { output.write(t.encodeJson().encodeToByteArray()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object UserInfoDataStoreSerializer : Serializer<UserInfo> {
deserializer = UserInfo.serializer(),
string = input.readBytes().decodeToString(),
)
} catch (e: SerializationException) {
Timber.tag(SerializerConstants.PROTOSTORE_SERIALIZER_TAG).d(e)
} catch (serializationException: SerializationException) {
Timber.e(serializationException)
defaultValue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ data class ActionConfig(
val resourceConfig: FhirResourceConfig? = null,
val toolBarHomeNavigation: ToolBarHomeNavigation = ToolBarHomeNavigation.OPEN_DRAWER,
val popNavigationBackStack: Boolean? = null,
val multiSelectViewConfig: MultiSelectViewConfig? = null,
) : Parcelable, java.io.Serializable {
fun paramsBundle(computedValuesMap: Map<String, Any> = emptyMap()): Bundle =
Bundle().apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.engine.domain.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable

/**
* @property resourceConfig The configuration for FHIR resource to be loaded
* @property parentIdFhirPathExpression FhirPath expression for extracting the ID for the parent
* resource
* @property contentFhirPathExpression FhirPath expression for extracting the content displayed on
* the multi select widget e.g. the name of the Location in a Location hierarchy
* @property rootNodeFhirPathExpression A key value pair containing a FHIRPath expression for
* extracting the value used to identify if the current resource is Root. The key is the FHIRPath
* expression while value is the content to compare against.
*/
@Serializable
@Parcelize
data class MultiSelectViewConfig(
val resourceConfig: FhirResourceConfig,
val parentIdFhirPathExpression: String,
val contentFhirPathExpression: String,
val rootNodeFhirPathExpression: KeyValueConfig,
) : java.io.Serializable, Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
* limitations under the License.
*/

package org.smartregister.fhircore.engine.datastore.serializers
package org.smartregister.fhircore.engine.domain.model

object SerializerConstants {
const val PROTOSTORE_SERIALIZER_TAG = "Proto DataStore"
}
import androidx.compose.ui.state.ToggleableState
import kotlinx.serialization.Serializable

@Serializable
data class SyncLocationToggleableState(
val locationId: String,
val toggleableState: ToggleableState,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@

package org.smartregister.fhircore.engine.sync

import android.content.Context
import androidx.compose.ui.state.ToggleableState
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.google.android.fhir.sync.SyncJobStatus
import dagger.hilt.android.qualifiers.ApplicationContext
import java.lang.ref.WeakReference
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.runBlocking
import org.hl7.fhir.r4.model.Parameters
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.SearchParameter
import org.smartregister.fhircore.engine.configuration.ConfigType
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.datastore.syncLocationIdsProtoStore
import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import timber.log.Timber
Expand All @@ -45,8 +52,14 @@ constructor(
val configService: ConfigService,
val configurationRegistry: ConfigurationRegistry,
val sharedPreferencesHelper: SharedPreferencesHelper,
@ApplicationContext val context: Context,
val dispatcherProvider: DefaultDispatcherProvider,
) {

private val appConfig by lazy {
configurationRegistry.retrieveConfiguration<ApplicationConfiguration>(
ConfigType.Application,
)
}
private val syncConfig by lazy {
configurationRegistry.retrieveResourceConfiguration<Parameters>(ConfigType.Sync)
}
Expand Down Expand Up @@ -87,10 +100,7 @@ constructor(

/** Retrieve registry sync params */
fun loadSyncParams(): Map<ResourceType, Map<String, String>> {
val pairs = mutableListOf<Pair<ResourceType, Map<String, String>>>()

val appConfig =
configurationRegistry.retrieveConfiguration<ApplicationConfiguration>(ConfigType.Application)
val pairs = mutableListOf<Pair<ResourceType, MutableMap<String, String>>>()

val organizationResourceTag =
configService.defineResourceTags().find { it.type == ResourceType.Organization.name }
Expand Down Expand Up @@ -143,24 +153,41 @@ constructor(
pairs.add(
Pair(
resourceType,
expressionValue?.let { mapOf(sp.code to expressionValue) } ?: mapOf(),
expressionValue?.let { mutableMapOf(sp.code to expressionValue) }
?: mutableMapOf(),
),
)
} else {
expressionValue?.let {
// add another parameter if there is a matching resource type
// e.g. [(Patient, {organization=105})] to [(Patient, {organization=105,
// _count=100})]
val updatedPair = pair.second.toMutableMap().apply { put(sp.code, expressionValue) }
val updatedPair = pair.second.apply { put(sp.code, expressionValue) }
val index = pairs.indexOfFirst { it.first == resourceType }
pairs.set(index, Pair(resourceType, updatedPair))
}
}
}
}

// Set sync locations Location query params
runBlocking {
context.syncLocationIdsProtoStore.data
.firstOrNull()
?.filter { it.toggleableState == ToggleableState.On }
?.map { it.locationId }
.takeIf { !it.isNullOrEmpty() }
?.let { locationIds ->
pairs.forEach { it.second[SYNC_LOCATION_IDS] = locationIds.joinToString(",") }
}
}

Timber.i("SYNC CONFIG $pairs")

return mapOf(*pairs.toTypedArray())
}

companion object {
private const val SYNC_LOCATION_IDS = "_syncLocations"
}
}
Loading

0 comments on commit b6719ff

Please sign in to comment.