parent
b36a3ba564
commit
91fa3d881b
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2025 The Android Open Source Project
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 com.google.samples.apps.nowinandroid.feature.foryou
|
||||
|
||||
abstract class ValidatorWithErrorType<ParamType : Any, ReturnType : Any, Error : Any> {
|
||||
fun validate(data: ParamType): ValidatorResult<ReturnType, Error> {
|
||||
return doValidation(data)
|
||||
}
|
||||
|
||||
protected abstract fun doValidation(data: ParamType): ValidatorResult<ReturnType, Error>
|
||||
sealed class ValidatorResult<out Success : Any, out Error : Any> {
|
||||
data class Success<out ReturnType : Any>(val data: ReturnType) : ValidatorResult<ReturnType, Nothing>()
|
||||
data class Failure<out Error : Any>(val error: Error) : ValidatorResult<Nothing, Error>()
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.google.samples.apps.nowinandroid.feature.foryou
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import java.io.Serializable
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class AddMobileTopUpTemplateValidator : ValidatorWithErrorType<
|
||||
AddMobileTopUpTemplateValidator.Params,
|
||||
AddMobileTopUpTemplateValidator.Params,
|
||||
AddMobileTopUpTemplateValidator.Error
|
||||
>() {
|
||||
|
||||
data class Params(
|
||||
val mobileNumber: String?,
|
||||
val serviceProvider: MTUServiceProviderFace?,
|
||||
val paymentService: MTUPaymentServiceFace?,
|
||||
val isAllDataRequired: Boolean? = null
|
||||
)
|
||||
|
||||
data class Error(
|
||||
@StringRes var emptyMobileNumber: Int? = null,
|
||||
@StringRes var emptyServiceProvider: Int? = null,
|
||||
@StringRes var emptyPaymentService: Int? = null,
|
||||
)
|
||||
}
|
||||
|
||||
class AddMobileTopUpTemplateValidatorImpl @Inject constructor() : AddMobileTopUpTemplateValidator() {
|
||||
override fun doValidation(data: Params): ValidatorResult<Params, Error> {
|
||||
var isValid = true
|
||||
val error = Error()
|
||||
if (data.mobileNumber.isNullOrEmpty()) {
|
||||
error.emptyMobileNumber = R.string.feature_foryou_done
|
||||
isValid = false
|
||||
}
|
||||
|
||||
if (data.serviceProvider == null && data.isAllDataRequired == true) {
|
||||
error.emptyServiceProvider = R.string.feature_foryou_done
|
||||
isValid = false
|
||||
}
|
||||
|
||||
if (data.paymentService == null && data.isAllDataRequired == true) {
|
||||
error.emptyPaymentService = R.string.feature_foryou_done
|
||||
isValid = false
|
||||
}
|
||||
|
||||
return if (isValid) {
|
||||
ValidatorResult.Success(data)
|
||||
} else {
|
||||
ValidatorResult.Failure(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class MTUServiceProviderFace(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val shortName: String
|
||||
) : Serializable
|
||||
|
||||
data class MTUPaymentServiceFace(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val isMobilePackage: Boolean,
|
||||
val allowedPaymentAmount: Long?,
|
||||
val packageDescriptions: Map<String, String>?,
|
||||
val serviceProvider: MTUServiceProviderFace? = null
|
||||
) : Serializable
|
Loading…
Reference in new issue