From 91fa3d881bf9f9a216e239aef1a6b40018724396 Mon Sep 17 00:00:00 2001 From: ibartishvili Date: Sun, 3 Aug 2025 19:32:37 +0400 Subject: [PATCH] pr sample 1 --- .../nowinandroid/core/ui/DevicePreviews.kt | 8 +-- .../feature/foryou/VaidatorWithError.kt | 29 ++++++++ .../VerifyMobileTopUpIdentifierValidator.kt | 67 +++++++++++++++++++ .../feature/search/SearchScreen.kt | 8 +-- 4 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VaidatorWithError.kt create mode 100644 feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VerifyMobileTopUpIdentifierValidator.kt diff --git a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DevicePreviews.kt b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DevicePreviews.kt index bb2b59466..a4bfd1373 100644 --- a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DevicePreviews.kt +++ b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DevicePreviews.kt @@ -22,8 +22,8 @@ import androidx.compose.ui.tooling.preview.Preview * Multipreview annotation that represents various device sizes. Add this annotation to a composable * to render various devices. */ -@Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480") -@Preview(name = "landscape", device = "spec:shape=Normal,width=640,height=360,unit=dp,dpi=480") -@Preview(name = "foldable", device = "spec:shape=Normal,width=673,height=841,unit=dp,dpi=480") -@Preview(name = "tablet", device = "spec:shape=Normal,width=1280,height=800,unit=dp,dpi=480") +@Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480", showBackground = true) +@Preview(name = "landscape", device = "spec:shape=Normal,width=640,height=360,unit=dp,dpi=480", showBackground = true) +@Preview(name = "foldable", device = "spec:shape=Normal,width=673,height=841,unit=dp,dpi=480", showBackground = true) +@Preview(name = "tablet", device = "spec:shape=Normal,width=1280,height=800,unit=dp,dpi=480", showBackground = true) annotation class DevicePreviews diff --git a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VaidatorWithError.kt b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VaidatorWithError.kt new file mode 100644 index 000000000..2c9f394b6 --- /dev/null +++ b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VaidatorWithError.kt @@ -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 { + fun validate(data: ParamType): ValidatorResult { + return doValidation(data) + } + + protected abstract fun doValidation(data: ParamType): ValidatorResult + sealed class ValidatorResult { + data class Success(val data: ReturnType) : ValidatorResult() + data class Failure(val error: Error) : ValidatorResult() + } +} \ No newline at end of file diff --git a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VerifyMobileTopUpIdentifierValidator.kt b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VerifyMobileTopUpIdentifierValidator.kt new file mode 100644 index 000000000..2d2f43847 --- /dev/null +++ b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/VerifyMobileTopUpIdentifierValidator.kt @@ -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 { + 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?, + val serviceProvider: MTUServiceProviderFace? = null +) : Serializable diff --git a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt index b617f98a9..2f3111098 100644 --- a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt +++ b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt @@ -549,7 +549,7 @@ private fun SearchTextField( } } -@Preview +@Preview(showBackground = true) @Composable private fun SearchToolbarPreview() { NiaTheme { @@ -562,7 +562,7 @@ private fun SearchToolbarPreview() { } } -@Preview +@Preview(showBackground = true) @Composable private fun EmptySearchResultColumnPreview() { NiaTheme { @@ -573,7 +573,7 @@ private fun EmptySearchResultColumnPreview() { } } -@Preview +@Preview(showBackground = true) @Composable private fun RecentSearchesBodyPreview() { NiaTheme { @@ -585,7 +585,7 @@ private fun RecentSearchesBodyPreview() { } } -@Preview +@Preview(showBackground = true) @Composable private fun SearchNotReadyBodyPreview() { NiaTheme {