Skip to content

Commit

Permalink
Implement functionality for node selection
Browse files Browse the repository at this point in the history
Signed-off-by: Elly Kitoto <[email protected]>
  • Loading branch information
ellykits committed Mar 13, 2024
1 parent c19d72f commit 4944abf
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 50 deletions.
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 @@ -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

This file was deleted.

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 @@ -52,8 +52,8 @@ import org.smartregister.fhircore.engine.ui.theme.AppTheme
@Composable
fun <T> ColumnScope.MultiSelectView(
rootNodeId: String,
treeNodeMap: SnapshotStateMap<String, TreeNode<T>>,
selectedNodes: SnapshotStateMap<String, ToggleableState>,
treeNodeMap: Map<String, TreeNode<T>>,
selectedNodes: MutableMap<String, ToggleableState>,
depth: Int = 0,
content: @Composable (TreeNode<T>) -> Unit,
) {
Expand Down Expand Up @@ -91,7 +91,7 @@ fun <T> ColumnScope.MultiSelectView(

@Composable
fun <T> MultiSelectCheckbox(
selectedNodes: SnapshotStateMap<String, ToggleableState>,
selectedNodes: MutableMap<String, ToggleableState>,
treeNodeMap: Map<String, TreeNode<T>>,
currentTreeNode: TreeNode<T>,
depth: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object TreeMap {
fun <T> populateLookupMap(
items: List<TreeNode<T>>,
lookup: MutableMap<String, TreeNode<T>>,
): MutableMap<String, TreeNode<T>> {
): Map<String, TreeNode<T>> {
items.forEach { item ->
val childNode = findOrCreate(item.id, item, lookup)
val parentNode = findOrCreate(item.parentId, item, lookup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class MultiSelectBottomSheetFragment() : BottomSheetDialogFragment() {
title = bottomSheetArgs.screenTitle,
onDismiss = { dismiss() },
searchTextState = multiSelectViewModel.searchTextState,
onTextChanged = multiSelectViewModel::onTextChanged,
onSearchTextChanged = multiSelectViewModel::onTextChanged,
onSelectionDone = multiSelectViewModel::onSelectionDone,
)

Check warning on line 61 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetFragment.kt#L53-L61

Added lines #L53 - L61 were not covered by tests
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand All @@ -59,12 +58,13 @@ import org.smartregister.fhircore.engine.ui.theme.DividerColor
@Composable
fun MultiSelectBottomSheetView(
rootNodeIds: SnapshotStateList<String>,
treeNodeMap: SnapshotStateMap<String, TreeNode<String>>,
treeNodeMap: Map<String, TreeNode<String>>,
selectedNodes: SnapshotStateMap<String, ToggleableState>,
title: String?,
onDismiss: () -> Unit,
searchTextState: MutableState<TextFieldValue>,
onTextChanged: (String) -> Unit,
searchTextState: MutableState<String>,
onSearchTextChanged: (String) -> Unit,
onSelectionDone: (() -> Unit) -> Unit,
) {
Column(modifier = Modifier.fillMaxWidth()) {
Row(
Expand All @@ -91,20 +91,12 @@ fun MultiSelectBottomSheetView(
) {
OutlinedTextField(

Check warning on line 92 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt#L92

Added line #L92 was not covered by tests
value = searchTextState.value,
onValueChange = { value ->
searchTextState.value = value
onTextChanged(value.text)
},
onValueChange = { value -> onSearchTextChanged(value) },
modifier = Modifier.fillMaxWidth(),
textStyle = TextStyle(fontSize = 18.sp),
trailingIcon = {

Check warning on line 97 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt#L94-L97

Added lines #L94 - L97 were not covered by tests
if (searchTextState.value.text.isNotEmpty()) {
IconButton(
onClick = {
searchTextState.value = TextFieldValue("")
onTextChanged(searchTextState.value.text)
},
) {
if (searchTextState.value.isNotEmpty()) {
IconButton(onClick = { onSearchTextChanged("") }) {
Icon(
Icons.Default.Close,
contentDescription = "",
Expand Down Expand Up @@ -137,7 +129,7 @@ fun MultiSelectBottomSheetView(

item {

Check warning on line 130 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt#L130

Added line #L130 was not covered by tests
Button(
onClick = { /*TODO Get selected nodes*/},
onClick = { onSelectionDone(onDismiss) },
modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp, horizontal = 8.dp),
) {

Check warning on line 134 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectBottomSheetView.kt#L132-L134

Added lines #L132 - L134 were not covered by tests
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.snapshots.SnapshotStateMap
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.fhir.logicalId
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.datastore.PreferenceDataStore
import org.smartregister.fhircore.engine.domain.model.MultiSelectViewConfig
import org.smartregister.fhircore.engine.ui.multiselect.TreeMap
import org.smartregister.fhircore.engine.ui.multiselect.TreeNode
Expand All @@ -41,15 +42,29 @@ class MultiSelectViewModel
constructor(
val defaultRepository: DefaultRepository,
val fhirPathDataExtractor: FhirPathDataExtractor,
val preferenceDataStore: PreferenceDataStore,
) : ViewModel() {

Check warning on line 46 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L42-L46

Added lines #L42 - L46 were not covered by tests

val searchTextState: MutableState<TextFieldValue> = mutableStateOf(TextFieldValue())
val searchTextState: MutableState<String> = mutableStateOf("")
val rootNodeIds: SnapshotStateList<String> = SnapshotStateList()
val lookupMap: SnapshotStateMap<String, TreeNode<String>> = SnapshotStateMap()
val selectedNodes: SnapshotStateMap<String, ToggleableState> = SnapshotStateMap()
val lookupMap = SnapshotStateMap<String, TreeNode<String>>()

Check warning on line 51 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L48-L51

Added lines #L48 - L51 were not covered by tests

fun populateLookupMap(multiSelectViewConfig: MultiSelectViewConfig) {
// Mark previously selected nodes
viewModelScope.launch {
val previouslySelectedNodes =
preferenceDataStore.read(PreferenceDataStore.SYNC_LOCATION_IDS).firstOrNull()

Check warning on line 57 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L55-L57

Added lines #L55 - L57 were not covered by tests
if (!previouslySelectedNodes.isNullOrEmpty()) {
previouslySelectedNodes
.split(",")
.asSequence()
.map { it.split(":") }

Check warning on line 62 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L59-L62

Added lines #L59 - L62 were not covered by tests
.filter { it.size == 2 }
.map { Pair(it.first(), it.last()) }
.forEach { selectedNodes[it.first] = ToggleableState.valueOf(it.second) }

Check warning on line 65 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L64-L65

Added lines #L64 - L65 were not covered by tests
}

val repositoryResourceDataList =
defaultRepository.searchResourcesRecursively(
fhirResourceConfig = multiSelectViewConfig.resourceConfig,
Expand Down Expand Up @@ -85,6 +100,17 @@ constructor(
}

fun onTextChanged(searchTerm: String) {
searchTextState.value = TextFieldValue(searchTerm)
searchTextState.value = searchTerm

Check warning on line 103 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L103

Added line #L103 was not covered by tests
}

fun onSelectionDone(dismiss: () -> Unit) {
viewModelScope.launch {

Check warning on line 107 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L107

Added line #L107 was not covered by tests
// Consider using a proto-datastore here
preferenceDataStore.write(
PreferenceDataStore.SYNC_LOCATION_IDS,
selectedNodes.map { "${it.key}:${it.value}" }.joinToString(","),

Check warning on line 111 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L109-L111

Added lines #L109 - L111 were not covered by tests
)
dismiss()

Check warning on line 113 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/multiselect/MultiSelectViewModel.kt#L113

Added line #L113 was not covered by tests
}
}
}

0 comments on commit 4944abf

Please sign in to comment.