Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(bank-sdk): Skonto + RA validation implementation #618

Merged
merged 30 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
44f4293
feature(bank-sdk): Skonto. Amounts validation message
ndubkov-distcotech Nov 5, 2024
9ba08b2
Merge branch 'main' into PP-763-skonto-validation
ndubkov-distcotech Nov 5, 2024
252ae9f
feature(bank-sdk): Code refactor
ndubkov-distcotech Nov 5, 2024
7a84154
feature(bank-sdk): Implemented keyboard hide detection
ndubkov-distcotech Nov 6, 2024
92ce8c1
feature(bank-sdk): Added tests
ndubkov-distcotech Nov 6, 2024
109538e
feature(bank-sdk): Tests refactor
ndubkov-distcotech Nov 7, 2024
115cafe
feature(bank-sdk): Skonto + RA validation implementation
ndubkov-distcotech Nov 12, 2024
bfd1930
feature(bank-sdk): Validators refactor
ndubkov-distcotech Nov 12, 2024
ede938b
Merge branch 'PP-763-skonto-validation' into PP-795-skonto-ra-validation
ndubkov-distcotech Nov 12, 2024
c79be12
feature(bank-sdk): Code refactor
ndubkov-distcotech Nov 12, 2024
67e6ec5
feature(bank-sdk): Code refactor
ndubkov-distcotech Nov 12, 2024
f70c625
Merge branch 'PP-763-skonto-validation' into PP-795-skonto-ra-validation
ndubkov-distcotech Nov 12, 2024
6897ffe
feature(bank-sdk): Skonto. MVI Orbit implementation
ndubkov-distcotech Nov 13, 2024
84f627c
feature(bank-sdk): Skonto. Code refactor
ndubkov-distcotech Nov 15, 2024
c0418d5
feature(bank-sdk): Skonto. Code refactor
ndubkov-distcotech Nov 15, 2024
fa382b0
feature(bank-sdk): Skonto. Code refactor
ndubkov-distcotech Nov 17, 2024
2677e9d
Merge branch 'PP-763-skonto-validation' into PP-763-skonto-validation…
ndubkov-distcotech Nov 17, 2024
9aa0ad7
feature(bank-sdk): Skonto. Tests fix
ndubkov-distcotech Nov 18, 2024
b6a6158
Merge branch 'PP-763-skonto-validation-mvi-orbit' into PP-795-skonto-…
ndubkov-distcotech Nov 18, 2024
ca620ae
feature(bank-sdk): Skonto + RA. MVI orbit update
ndubkov-distcotech Nov 18, 2024
4636487
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Nov 20, 2024
d945049
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Nov 21, 2024
9ca03a9
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 2, 2024
00a13ed
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 2, 2024
b9546ee
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 2, 2024
69ac396
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 2, 2024
f8b7d64
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 2, 2024
3be6e18
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 3, 2024
a9757f4
Merge branch 'main' into PP-795-skonto-ra-validation
ndubkov-distcotech Dec 3, 2024
25d0458
feature(bank-sdk): Skonto + RA. Code refactor
ndubkov-distcotech Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.colors.section.Di
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.colors.section.DigitalInvoiceSkontoInfoDialogColors
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.colors.section.DigitalInvoiceSkontoInvoicePreviewSectionColors
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.colors.section.DigitalInvoiceSkontoSectionColors
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.mapper.toErrorMessage
import net.gini.android.bank.sdk.capture.skonto.mapper.toErrorMessage
import net.gini.android.bank.sdk.capture.skonto.model.SkontoData
import net.gini.android.bank.sdk.capture.skonto.model.SkontoEdgeCase
import net.gini.android.bank.sdk.di.koin.giniBankViewModel
import net.gini.android.bank.sdk.util.disallowScreenshots
import net.gini.android.bank.sdk.util.ui.keyboardAsState
import net.gini.android.capture.Amount
import net.gini.android.capture.GiniCapture
import net.gini.android.capture.internal.util.ActivityHelper
Expand Down Expand Up @@ -217,6 +220,11 @@ private fun ScreenContent(
BackHandler { navigateBack() }

val state by viewModel.stateFlow.collectAsState()
val keyboardState by keyboardAsState()

LaunchedEffect(keyboardState) {
viewModel.onKeyboardStateChanged(keyboardState)
}

viewModel.collectSideEffect {
when (it) {
Expand All @@ -240,7 +248,7 @@ private fun ScreenContent(
onInfoDialogDismissed = viewModel::onInfoDialogDismissed,
onInvoiceClicked = viewModel::onInvoiceClicked,
customBottomNavBarAdapter = customBottomNavBarAdapter,
onHelpClicked = viewModel::onHelpClicked
onHelpClicked = viewModel::onHelpClicked,
)
}

Expand Down Expand Up @@ -355,6 +363,7 @@ private fun ScreenReadyState(
onDueDateChanged = onDueDateChanged,
edgeCase = state.edgeCase,
onInfoBannerClicked = onInfoBannerClicked,
skontoAmountValidationError = state.skontoAmountValidationError,
)
}
}
Expand Down Expand Up @@ -521,9 +530,11 @@ private fun SkontoSection(
onInfoBannerClicked: () -> Unit,
edgeCase: SkontoEdgeCase?,
colors: DigitalInvoiceSkontoSectionColors,
skontoAmountValidationError: DigitalInvoiceSkontoScreenState.Ready.SkontoAmountValidationError?,
modifier: Modifier = Modifier,
) {
val dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy")
val resources = LocalContext.current.resources

var isDatePickerVisible by remember { mutableStateOf(false) }
Card(
Expand Down Expand Up @@ -638,6 +649,10 @@ private fun SkontoSection(
)
}
},
isError = skontoAmountValidationError != null,
supportingText = skontoAmountValidationError?.toErrorMessage(
resources = resources,
)
)

val dueDateOnClickSource = remember { MutableInteractionSource() }
Expand Down Expand Up @@ -888,4 +903,5 @@ private val previewState = DigitalInvoiceSkontoScreenState.Ready(
paymentMethod = SkontoData.SkontoPaymentMethod.PayPal,
edgeCase = SkontoEdgeCase.PayByCashOnly,
edgeCaseInfoDialogVisible = false,
skontoAmountValidationError = null
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.gini.android.bank.sdk.capture.digitalinvoice.skonto

import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.args.DigitalInvoiceSkontoArgs
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.validation.DigitalInvoiceSkontoAmountValidator
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

Expand All @@ -12,7 +13,9 @@ val digitalInvoiceSkontoScreenModule = module {
getSkontoEdgeCaseUseCase = get(),
getSkontoRemainingDaysUseCase = get(),
lastAnalyzedDocumentProvider = get(),
skontoInvoicePreviewTextLinesFactory = get()
skontoInvoicePreviewTextLinesFactory = get(),
digitalInvoiceSkontoAmountValidator = get(),
)
}
factory { DigitalInvoiceSkontoAmountValidator() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ import net.gini.android.capture.Amount
import java.math.BigDecimal
import java.time.LocalDate

internal sealed class DigitalInvoiceSkontoScreenState {
internal sealed interface DigitalInvoiceSkontoScreenState {

data class Ready(
val isSkontoSectionActive: Boolean,
val paymentInDays: Int,
val skontoPercentage: BigDecimal,
val skontoAmount: Amount,
val skontoAmountValidationError: SkontoAmountValidationError?,
val fullAmount: Amount,
val discountDueDate: LocalDate,
val paymentMethod: SkontoData.SkontoPaymentMethod,
val edgeCase: SkontoEdgeCase?,
val edgeCaseInfoDialogVisible: Boolean,
) : DigitalInvoiceSkontoScreenState()
) : DigitalInvoiceSkontoScreenState {

sealed interface SkontoAmountValidationError {
object SkontoAmountMoreThanFullAmount : SkontoAmountValidationError
}
}
}

internal sealed interface DigitalInvoiceSkontoSideEffect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.args.DigitalInvoiceSkontoArgs
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.args.DigitalInvoiceSkontoResultArgs
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.validation.DigitalInvoiceSkontoAmountValidator
import net.gini.android.bank.sdk.capture.skonto.factory.lines.SkontoInvoicePreviewTextLinesFactory
import net.gini.android.bank.sdk.capture.skonto.model.SkontoData
import net.gini.android.bank.sdk.capture.skonto.usecase.GetSkontoDiscountPercentageUseCase
Expand All @@ -23,6 +24,7 @@ internal class DigitalInvoiceSkontoViewModel(
private val getSkontoEdgeCaseUseCase: GetSkontoEdgeCaseUseCase,
private val getSkontoRemainingDaysUseCase: GetSkontoRemainingDaysUseCase,
private val skontoInvoicePreviewTextLinesFactory: SkontoInvoicePreviewTextLinesFactory,
private val digitalInvoiceSkontoAmountValidator: DigitalInvoiceSkontoAmountValidator,
) : ViewModel() {

val stateFlow: MutableStateFlow<DigitalInvoiceSkontoScreenState> =
Expand Down Expand Up @@ -70,16 +72,26 @@ internal class DigitalInvoiceSkontoViewModel(
paymentMethod = paymentMethod,
edgeCase = edgeCase,
edgeCaseInfoDialogVisible = edgeCase != null,
skontoAmountValidationError = null,
)
}

fun onSkontoAmountFieldChanged(newValue: BigDecimal) = viewModelScope.launch {
val currentState =
stateFlow.value as? DigitalInvoiceSkontoScreenState.Ready ?: return@launch

if (newValue > currentState.fullAmount.value) {
val skontoAmountValidationError = digitalInvoiceSkontoAmountValidator(
newValue,
currentState.fullAmount.value
)

if (skontoAmountValidationError != null) {
stateFlow.emit(
currentState.copy(skontoAmount = currentState.skontoAmount)
currentState.copy(
skontoAmount = currentState.skontoAmount,
skontoAmountValidationError = DigitalInvoiceSkontoScreenState.Ready
.SkontoAmountValidationError.SkontoAmountMoreThanFullAmount
)
)
return@launch
}
Expand All @@ -95,10 +107,12 @@ internal class DigitalInvoiceSkontoViewModel(
currentState.copy(
skontoAmount = newSkontoAmount,
skontoPercentage = discount,
skontoAmountValidationError = null,
)
)
}


fun onSkontoDueDateChanged(newDate: LocalDate) = viewModelScope.launch {
val currentState =
stateFlow.value as? DigitalInvoiceSkontoScreenState.Ready ?: return@launch
Expand All @@ -115,6 +129,17 @@ internal class DigitalInvoiceSkontoViewModel(
)
}

fun onKeyboardStateChanged(isVisible: Boolean) = viewModelScope.launch {
if (isVisible) return@launch
val currentState =
stateFlow.value as? DigitalInvoiceSkontoScreenState.Ready ?: return@launch
stateFlow.emit(
currentState.copy(
skontoAmountValidationError = null
)
)
}

fun onInfoBannerClicked() = viewModelScope.launch {
val currentState =
stateFlow.value as? DigitalInvoiceSkontoScreenState.Ready ?: return@launch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.gini.android.bank.sdk.capture.digitalinvoice.skonto.mapper

import android.content.res.Resources
import net.gini.android.bank.sdk.R
import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.DigitalInvoiceSkontoScreenState

internal fun DigitalInvoiceSkontoScreenState.Ready.SkontoAmountValidationError.toErrorMessage(
resources: Resources,
): String = when (this) {
is DigitalInvoiceSkontoScreenState.Ready.SkontoAmountValidationError.SkontoAmountMoreThanFullAmount ->
resources.getString(
R.string.gbs_skonto_section_discount_field_amount_validation_error_skonto_amount_more_than_full_amount
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.gini.android.bank.sdk.capture.digitalinvoice.skonto.validation

import net.gini.android.bank.sdk.capture.digitalinvoice.skonto.DigitalInvoiceSkontoScreenState
import java.math.BigDecimal

internal class DigitalInvoiceSkontoAmountValidator {

operator fun invoke(newSkontoAmount: BigDecimal, fullAmount: BigDecimal)
: DigitalInvoiceSkontoScreenState.Ready.SkontoAmountValidationError? = when {
newSkontoAmount > fullAmount ->
DigitalInvoiceSkontoScreenState.Ready.SkontoAmountValidationError.SkontoAmountMoreThanFullAmount

else -> null
}
}
Loading