Skip to content

Commit

Permalink
Merge pull request #2293 from HedvigInsurance/new-design/forever-feat…
Browse files Browse the repository at this point in the history
…ure-shared-bottom-with-sheet-state

New design/forever feature shared bottom with sheet state
  • Loading branch information
StylianosGakis authored Nov 15, 2024
2 parents 82aab41 + 64c6a6b commit 9e42e6a
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ private class HedvigBottomSheetStateImpl<T>() : HedvigBottomSheetState<T> {
@Composable
fun <T> HedvigBottomSheet(
hedvigBottomSheetState: HedvigBottomSheetState<T>,
modifier: Modifier = Modifier,
sheetPadding: PaddingValues? = null,
contentPadding: PaddingValues? = null,
content: @Composable ColumnScope.(T) -> Unit,
) {
InternalHedvigBottomSheet(
Expand All @@ -147,6 +150,9 @@ fun <T> HedvigBottomSheet(
onSystemBack = {
hedvigBottomSheetState.dismiss()
},
contentPadding = contentPadding,
sheetPadding = sheetPadding,
modifier = modifier,
) {
if (hedvigBottomSheetState.data != null) {
content(hedvigBottomSheetState.data!!)
Expand All @@ -159,6 +165,7 @@ fun <T> HedvigBottomSheet(
private fun InternalHedvigBottomSheet(
isVisible: Boolean,
onVisibleChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
onSystemBack: (() -> Unit)? = { onVisibleChange(false) },
sheetPadding: PaddingValues? = null,
contentPadding: PaddingValues? = null,
Expand All @@ -185,7 +192,7 @@ private fun InternalHedvigBottomSheet(
onSystemBack = onSystemBack,
shape = bottomSheetShape.shape,
) {
Box {
Box(modifier) {
Column(
modifier = Modifier
.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class FetchCoInsuredPersonalInformationUseCaseImpl(
.safeExecute(::ErrorMessage)
.bind()
if (result.personalInformation == null) {
val birthdate = if (ssn.length>=8) convertSsnToBirthDateOrNull(ssn) else null
val birthdate = if (ssn.length >= 8) convertSsnToBirthDateOrNull(ssn) else null
CoInsuredPersonalInformation.EmptyInfo(birthdate)
} else {
CoInsuredPersonalInformation.FullInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import com.hedvig.android.core.uidata.UiMoney
import com.hedvig.android.data.contract.ContractGroup
import com.hedvig.android.data.contract.ContractType
import com.hedvig.android.data.productvariant.InsurableLimit
import com.hedvig.android.data.productvariant.InsurableLimit.InsurableLimitType.BIKE
import com.hedvig.android.data.productvariant.InsuranceVariantDocument
import com.hedvig.android.data.productvariant.InsuranceVariantDocument.InsuranceDocumentType.CERTIFICATE
import com.hedvig.android.data.productvariant.ProductVariant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,91 @@
package com.hedvig.android.shared.foreverui.ui.ui

import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.add
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.hedvig.android.design.system.hedvig.ButtonDefaults
import com.hedvig.android.design.system.hedvig.HedvigBottomSheet
import com.hedvig.android.design.system.hedvig.HedvigBottomSheetState
import com.hedvig.android.design.system.hedvig.HedvigButton
import com.hedvig.android.design.system.hedvig.HedvigText
import com.hedvig.android.design.system.hedvig.HedvigTextButton
import com.hedvig.android.design.system.hedvig.HedvigTextField
import com.hedvig.android.design.system.hedvig.HedvigTextFieldDefaults
import com.hedvig.android.shared.foreverui.ui.data.ForeverRepository
import com.hedvig.android.shared.foreverui.ui.data.ForeverRepository.ReferralError
import hedvig.resources.R
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.drop

@OptIn(ExperimentalLayoutApi::class)
@Composable
internal fun EditCodeBottomSheet(
isVisible: Boolean,
code: TextFieldValue,
referralCodeUpdateError: ForeverRepository.ReferralError?,
sheetState: HedvigBottomSheetState<String>,
referralCodeUpdateError: ReferralError?,
showedReferralCodeSubmissionError: () -> Unit,
onCodeChanged: (TextFieldValue) -> Unit,
onDismiss: () -> Unit,
onSubmitCode: () -> Unit,
referralCodeSuccessfullyChanged: Boolean,
onSubmitCode: (code: String) -> Unit,
isLoading: Boolean,
) {
val insetsAroundSheet = WindowInsets
.safeDrawing
.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal)
.add(WindowInsets.ime)
DismissSheetOnSuccessfulCodeChangeEffect(sheetState, referralCodeSuccessfullyChanged)
ClearErrorOnSheetDismissedEffect(sheetState, showedReferralCodeSubmissionError)
HedvigBottomSheet(
isVisible = isVisible,
onVisibleChange = {
onDismiss()
},
) {
hedvigBottomSheetState = sheetState,
sheetPadding = insetsAroundSheet.asPaddingValues(),
contentPadding = null,
modifier = Modifier.consumeWindowInsets(insetsAroundSheet),
) { initialCode ->
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
var textFieldValue by remember {
mutableStateOf(
TextFieldValue(
text = initialCode,
selection = TextRange(initialCode.length),
),
)
}

ClearErrorOnNewInputEffect({ textFieldValue }, showedReferralCodeSubmissionError)
HedvigText(
text = stringResource(R.string.referrals_change_change_code),
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
modifier = Modifier.fillMaxWidth(),
)
Spacer(Modifier.height(32.dp))
HedvigTextField(
textValue = code,
textValue = textFieldValue,
labelText = stringResource(R.string.referrals_empty_code_headline),
onValueChange = onCodeChanged,
onValueChange = { textFieldValue = it },
textFieldSize = HedvigTextFieldDefaults.TextFieldSize.Medium,
errorState = if (referralCodeUpdateError == null) {
HedvigTextFieldDefaults.ErrorState.NoError
Expand All @@ -70,7 +95,6 @@ internal fun EditCodeBottomSheet(
)
},
modifier = Modifier
.padding(horizontal = 16.dp)
.focusRequester(focusRequester)
.fillMaxWidth(),
)
Expand All @@ -80,30 +104,74 @@ internal fun EditCodeBottomSheet(
onClick = {
showedReferralCodeSubmissionError()
focusManager.clearFocus()
onSubmitCode()
onSubmitCode(textFieldValue.text)
},
enabled = true,
isLoading = isLoading,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth(),
modifier = Modifier.fillMaxWidth(),
)
Spacer(Modifier.height(8.dp))
HedvigTextButton(
text = stringResource(R.string.general_cancel_button),
onClick = {
onDismiss()
},
onClick = { sheetState.dismiss() },
buttonSize = ButtonDefaults.ButtonSize.Large,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth(),
modifier = Modifier.fillMaxWidth(),
)
Spacer(Modifier.height(16.dp))
Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.safeDrawing))
}
}

@Composable
private fun DismissSheetOnSuccessfulCodeChangeEffect(
sheetState: HedvigBottomSheetState<String>,
referralCodeSuccessfullyChanged: Boolean,
) {
val updatedReferralCodeSuccessfullyChanged by rememberUpdatedState(referralCodeSuccessfullyChanged)
LaunchedEffect(sheetState) {
snapshotFlow { updatedReferralCodeSuccessfullyChanged }
.drop(1)
.collect {
if (it) {
sheetState.dismiss()
}
}
}
}

@Composable
private fun ClearErrorOnSheetDismissedEffect(
sheetState: HedvigBottomSheetState<String>,
showedReferralCodeSubmissionError: () -> Unit,
) {
val updatedShowedReferralCodeSubmissionError by rememberUpdatedState(showedReferralCodeSubmissionError)
LaunchedEffect(sheetState) {
snapshotFlow { sheetState.isVisible }
.drop(1)
.collect {
if (!it) {
updatedShowedReferralCodeSubmissionError()
}
}
}
}

@Composable
private fun ClearErrorOnNewInputEffect(
textFieldValue: () -> TextFieldValue,
showedReferralCodeSubmissionError: () -> Unit,
) {
val updatedShowedReferralCodeSubmissionError by rememberUpdatedState(showedReferralCodeSubmissionError)
LaunchedEffect(Unit) {
snapshotFlow { textFieldValue() }
.drop(1)
.collectLatest {
// clear error after the member edits the code manually
updatedShowedReferralCodeSubmissionError()
}
}
}

@Composable
private fun ForeverRepository.ReferralError.toErrorMessage(): String {
return message ?: stringResource(R.string.referrals_change_code_sheet_general_error)
Expand Down
Loading

0 comments on commit 9e42e6a

Please sign in to comment.