Skip to content

Commit

Permalink
Merge pull request #13 from niscy-eudiw/bugfix/presentation_fields
Browse files Browse the repository at this point in the history
Bugfix/presentation fields
  • Loading branch information
stzouvaras authored Feb 7, 2024
2 parents b2d364e + 330506c commit 8414e87
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import eu.europa.ec.commonfeature.ui.request.model.RequestDataUi
import eu.europa.ec.resourceslogic.R
import eu.europa.ec.uilogic.component.AppIcons
import eu.europa.ec.uilogic.component.content.ContentScreen
Expand Down Expand Up @@ -264,10 +263,7 @@ fun StickyBottomSection(
VSpacer.ExtraSmall()

AnimatedVisibility(
visible = state.items.any {
it is RequestDataUi.OptionalField
&& !it.optionalFieldItemUi.requestDocumentItemUi.checked
}
visible = state.showWarningCard
) {
Column {
WarningCard(warningText = state.warningText)
Expand All @@ -277,7 +273,7 @@ fun StickyBottomSection(

WrapPrimaryButton(
modifier = Modifier.fillMaxWidth(),
enabled = !state.isLoading && !state.noItems,
enabled = !state.isLoading && state.allowShare,
onClick = { onEventSend(Event.PrimaryButtonPressed) }
) {
Text(text = stringResource(id = R.string.request_primary_button_text))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import eu.europa.ec.commonfeature.ui.request.model.OptionalFieldItemUi
import eu.europa.ec.commonfeature.ui.request.model.RequestDataUi
import eu.europa.ec.commonfeature.ui.request.model.RequestDocumentItemUi
import eu.europa.ec.commonfeature.ui.request.model.RequiredFieldsItemUi
import eu.europa.ec.resourceslogic.R
import eu.europa.ec.resourceslogic.theme.values.textPrimaryDark
Expand Down Expand Up @@ -97,16 +97,17 @@ fun <T> Request(
}

is RequestDataUi.OptionalField -> {
OptionalField(
item = item.optionalFieldItemUi,
Field(
item = item.optionalFieldItemUi.requestDocumentItemUi,
showFullDetails = isShowingFullUserInfo,
onEventSend = onEventSend,
)
}

is RequestDataUi.RequiredFields -> {
RequiredFields(
RequiredCard(
item = item.requiredFieldsItemUi,
showFullDetails = isShowingFullUserInfo,
onEventSend = onEventSend,
contentPadding = contentPadding,
)
Expand Down Expand Up @@ -148,32 +149,32 @@ fun DocumentCard(
}

@Composable
fun <T> OptionalField(
item: OptionalFieldItemUi<T>,
fun <T> Field(
item: RequestDocumentItemUi<T>,
showFullDetails: Boolean,
onEventSend: (T) -> Unit,
) {
CheckboxWithContent(
modifier = Modifier.fillMaxWidth(),
checkboxData = CheckboxData(
isChecked = item.requestDocumentItemUi.checked,
enabled = item.requestDocumentItemUi.enabled,
isChecked = item.checked,
enabled = item.enabled,
onCheckedChange = {
item.requestDocumentItemUi.event?.let { event ->
item.event?.let { event ->
onEventSend(event)
}
}
)
) {
val infoName = item.requestDocumentItemUi.readableName
val infoValueStyle = if (item.requestDocumentItemUi.checked) {
val infoName = item.readableName
val infoValueStyle = if (item.checked) {
MaterialTheme.typography.titleMedium
} else {
MaterialTheme.typography.bodyLarge
}

if (showFullDetails) {
if (item.requestDocumentItemUi.keyIsBase64) {
if (item.keyIsBase64) {
InfoTextWithNameAndIconData(
title = infoName,
icon = AppIcons.User,
Expand All @@ -183,7 +184,7 @@ fun <T> OptionalField(
InfoTextWithNameAndValue(
itemData = InfoTextWithNameAndValueData.create(
title = infoName,
item.requestDocumentItemUi.value,
item.value,
),
infoValueTextStyle = infoValueStyle
)
Expand All @@ -198,10 +199,11 @@ fun <T> OptionalField(
}

@Composable
fun <T> RequiredFields(
fun <T> RequiredCard(
item: RequiredFieldsItemUi<T>,
showFullDetails: Boolean,
onEventSend: (T) -> Unit,
contentPadding: PaddingValues,
contentPadding: PaddingValues
) {
val requiredFieldsTextStyle = MaterialTheme.typography.titleMedium
val requiredFieldsTitlePadding = PaddingValues(
Expand Down Expand Up @@ -236,20 +238,11 @@ fun <T> RequiredFields(
cardTitlePadding = requiredFieldsTitlePadding,
cardContent = {
item.requestDocumentItemsUi.forEach { requiredUserIdentificationUi ->
val checkboxData = CheckboxData(
isChecked = requiredUserIdentificationUi.checked,
enabled = requiredUserIdentificationUi.enabled,
onCheckedChange = null
Field(
item = requiredUserIdentificationUi,
showFullDetails = showFullDetails,
onEventSend = onEventSend
)
CheckboxWithContent(
modifier = Modifier.fillMaxWidth(),
checkboxData = checkboxData
) {
Text(
text = requiredUserIdentificationUi.readableName,
style = requiredFieldsTextStyle
)
}
}
},
cardContentPadding = PaddingValues(all = SPACING_SMALL.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ data class State(

val items: List<RequestDataUi<Event>> = emptyList(),
val noItems: Boolean = false,
val showWarningCard: Boolean = false,
val allowShare: Boolean = false
) : ViewState

sealed class Event : ViewEvent {
Expand Down Expand Up @@ -99,6 +101,7 @@ abstract class RequestViewModel : MviViewModel<Event, State, Effect>() {
protected var viewModelJob: Job? = null

abstract fun getScreenSubtitle(): String
abstract fun getScreenTitle(): TitleWithBadge
abstract fun getScreenClickableSubtitle(): String?
abstract fun getWarningText(): String
abstract fun getNextScreen(): String
Expand All @@ -109,21 +112,28 @@ abstract class RequestViewModel : MviViewModel<Event, State, Effect>() {
*
* Kill presentation scope.
*
* Therefore kill [EudiWalletInteractor]
* */
open fun cleanUp() {
getOrCreatePresentationScope().close()
}

open fun updateData(updatedItems: List<RequestDataUi<Event>>) {
open fun updateData(updatedItems: List<RequestDataUi<Event>>, allowShare: Boolean? = null) {
setState {
copy(items = updatedItems)
copy(
items = updatedItems,
showWarningCard = updatedItems.any {
it is RequestDataUi.OptionalField
&& it.optionalFieldItemUi.requestDocumentItemUi.enabled
&& !it.optionalFieldItemUi.requestDocumentItemUi.checked
},
allowShare = allowShare ?: updatedItems.isNotEmpty()
)
}
}

override fun setInitialState(): State {
return State(
screenTitle = TitleWithBadge(isTrusted = false),
screenTitle = getScreenTitle(),
screenSubtitle = getScreenSubtitle(),
screenClickableSubtitle = getScreenClickableSubtitle(),
warningText = getWarningText(),
Expand Down Expand Up @@ -246,7 +256,7 @@ abstract class RequestViewModel : MviViewModel<Event, State, Effect>() {
item
}
}
updateData(updatedItems)
updateData(updatedItems, viewState.value.allowShare)
}

private fun updateUserIdentificationItem(id: String) {
Expand All @@ -267,7 +277,19 @@ abstract class RequestViewModel : MviViewModel<Event, State, Effect>() {
item
}
}
updateData(updatedList)

val hasVerificationItems = updatedList.any { it is RequestDataUi.RequiredFields }

val hasAtLeastOneFieldSelected = updatedList
.filterIsInstance<RequestDataUi.OptionalField<Event>>().any {
it.optionalFieldItemUi.requestDocumentItemUi.enabled
&& it.optionalFieldItemUi.requestDocumentItemUi.checked
}

updateData(
updatedItems = updatedList,
allowShare = hasAtLeastOneFieldSelected || hasVerificationItems
)
}

private fun showBottomSheet(sheetContent: RequestBottomSheetContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ fun <T> DocItem.toRequestDocumentItemUi(
readableName: String,
value: String,
optional: Boolean,
isChecked: Boolean,
event: T?
): RequestDocumentItemUi<T> {
return RequestDocumentItemUi(
id = uID,
domainPayload = docPayload,
checked = true,
checked = isChecked,
enabled = optional,
readableName = readableName,
docItem = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ import eu.europa.ec.resourceslogic.provider.ResourceProvider
import org.json.JSONObject

private fun getMandatoryFields(docType: DocumentTypeUi): List<String> = when (docType) {
DocumentTypeUi.MDL -> listOf(
"family_name",
"given_name",
"birth_date",
"issue_date",
"expiry_date",
"issuing_country",
"issuing_authority",
"document_number",
"portrait",
"driving_privileges",
"un_distinguishing_sign",
)

DocumentTypeUi.PID -> listOf(
"age_over_18",
Expand Down Expand Up @@ -116,6 +103,7 @@ object RequestTransformer {
elementIdentifier = docItem.elementIdentifier,
),
optional = false,
isChecked = true,
event = null,
readableName = resourceProvider.getReadableElementIdentifier(docItem.elementIdentifier),
value = value
Expand All @@ -137,6 +125,7 @@ object RequestTransformer {
elementIdentifier = docItem.elementIdentifier,
),
optional = isAvailable,
isChecked = isAvailable,
event = Event.UserIdentificationClicked(itemId = uID),
readableName = resourceProvider.getReadableElementIdentifier(docItem.elementIdentifier),
value = value
Expand All @@ -154,16 +143,18 @@ object RequestTransformer {
items += RequestDataUi.Space()

// Add required fields item.
items += RequestDataUi.RequiredFields(
requiredFieldsItemUi = RequiredFieldsItemUi(
id = docIndex,
requestDocumentItemsUi = required,
expanded = false,
title = requiredFieldsTitle,
event = Event.ExpandOrCollapseRequiredDataList(id = docIndex)
if (required.isNotEmpty()) {
items += RequestDataUi.RequiredFields(
requiredFieldsItemUi = RequiredFieldsItemUi(
id = docIndex,
requestDocumentItemsUi = required,
expanded = false,
title = requiredFieldsTitle,
event = Event.ExpandOrCollapseRequiredDataList(id = docIndex)
)
)
)
items += RequestDataUi.Space()
items += RequestDataUi.Space()
}
}

return items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class PresentationRequestViewModel(
return resourceProvider.getString(R.string.request_warning_text)
}

override fun getScreenTitle(): TitleWithBadge {
return constructTitle()
}

override fun getNextScreen(): String {
return generateComposableNavigationLink(
screen = CommonScreens.Biometric,
Expand Down Expand Up @@ -124,7 +128,7 @@ class PresentationRequestViewModel(
isLoading = false,
error = null,
verifierName = response.verifierName,
screenTitle = getScreenTitle(
screenTitle = constructTitle(
verifierName = response.verifierName,
verifierIsTrusted = response.verifierIsTrusted
),
Expand All @@ -143,7 +147,7 @@ class PresentationRequestViewModel(
isLoading = false,
error = null,
verifierName = response.verifierName,
screenTitle = getScreenTitle(
screenTitle = constructTitle(
verifierName = response.verifierName,
verifierIsTrusted = response.verifierIsTrusted
),
Expand All @@ -156,8 +160,8 @@ class PresentationRequestViewModel(
}
}

override fun updateData(updatedItems: List<RequestDataUi<Event>>) {
super.updateData(updatedItems)
override fun updateData(updatedItems: List<RequestDataUi<Event>>, allowShare: Boolean?) {
super.updateData(updatedItems, allowShare)
interactor.updateRequestedDocuments(updatedItems)
}

Expand All @@ -166,7 +170,10 @@ class PresentationRequestViewModel(
interactor.stopPresentation()
}

private fun getScreenTitle(verifierName: String?, verifierIsTrusted: Boolean): TitleWithBadge {
private fun constructTitle(
verifierName: String? = null,
verifierIsTrusted: Boolean = false
): TitleWithBadge {
val textBeforeBadge = if (verifierName.isNullOrBlank()) {
resourceProvider.getString(R.string.request_title_before_badge)
} else {
Expand Down
Loading

0 comments on commit 8414e87

Please sign in to comment.