Skip to content

Commit

Permalink
trying to add libPhoneNumberUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjenx committed Oct 8, 2024
1 parent 7383460 commit 455f25a
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 26 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
alias(libs.plugins.cocoapods).apply(false)
alias(libs.plugins.multiplatform).apply(false)
alias(libs.plugins.compose.compiler).apply(false)
alias(libs.plugins.compose).apply(false)
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ android.nonTransitiveRClass=true
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true
kotlin.apple.xcodeCompatibility.nowarn=true
kotlin.native.ignoreDisabledTargets=true
kotlin.mpp.stability.nowarn=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2

#Compose
org.jetbrains.compose.experimental.jscanvas.enabled=true
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

kotlin = "2.0.21-RC"
compose = "1.7.0-rc01"
agp = "8.6.1"
agp = "8.7.0"
androidx-activityCompose = "1.9.2"
androidx-uiTest = "1.7.3"
kotlinx-datetime = "0.6.1"
Expand Down Expand Up @@ -35,6 +35,7 @@ androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-toolin
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }

[plugins]
cocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
44 changes: 33 additions & 11 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree

plugins {
alias(libs.plugins.multiplatform)
alias(libs.plugins.android.library)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.compose)
alias(libs.plugins.android.library)
alias(libs.plugins.cocoapods)
alias(libs.plugins.maven.publish)
}

kotlin {
cocoapods {
version = "1.0.0"
summary = "Yet Another Kotlin COmpose Validation library"
homepage = "https://github.com/chrisjenx/yakcov"
ios.deploymentTarget = "14.1"
framework {
baseName = "shared"
isStatic = true
pod("libPhoneNumber-iOS")
// @OptIn(ExperimentalKotlinGradlePluginApi::class)
// transitiveExport = true
}
}

applyDefaultHierarchyTemplate()
androidTarget {
compilations.all {
compileTaskProvider {
Expand Down Expand Up @@ -59,16 +75,17 @@ kotlin {
}
}

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "ComposeApp"
isStatic = true
}
}
iosX64()
iosArm64()
iosSimulatorArm64()
// listOf(
//
// ).forEach {
// it.binaries.framework {
// baseName = "Yakcov"
// isStatic = true
// }
// }

sourceSets {
commonMain.dependencies {
Expand Down Expand Up @@ -107,6 +124,11 @@ kotlin {


}

//https://kotlinlang.org/docs/native-objc-interop.html#export-of-kdoc-comments-to-generated-objective-c-headers
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
compilations["main"].compilerOptions.options.freeCompilerArgs.add("-Xexport-kdoc")
}
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ private val phoneUtil = PhoneNumberUtil.createInstance(ClassPathResourceMetadata
/**
* Check if is phone number to best ability of each platform.
*/
actual fun String?.isPhoneNumber(): Boolean {
actual fun String?.isPhoneNumber(defaultRegion: String?): Boolean {
this ?: return false
return try {
val result = phoneUtil.parse(this, "US")
val result = phoneUtil.parse(this, defaultRegion?.uppercase())
phoneUtil.isValidNumber(result)
} catch (t: Throwable) {
t.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ fun String?.isEmail(): Boolean {

/**
* Check if is phone number to best ability of each platform.
*
* @param defaultRegion The default region to use if the number is not in international format.
* it's two digits country code. e.g. "US", "GB", "ES"
*/
expect fun String?.isPhoneNumber(): Boolean
expect fun String?.isPhoneNumber(defaultRegion: String? = null): Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ data object Email : ValueValidatorRule<String> {
}

// Phone
/**
* @param defaultRegion the default region to use for phone number validation, ISO 3166-1 alpha-2 code US, GB, ES, etc
*/
@Stable
data object Phone : ValueValidatorRule<String> {
data class Phone(val defaultRegion: String = "US") : ValueValidatorRule<String> {
override fun validate(value: String): ValidationResult {
// only validate if not empty as Required will check if not empty
return if (!value.isPhoneNumber()) {
return if (!value.isPhoneNumber(defaultRegion)) {
ResourceValidationResult.error(Res.string.rulePhone)
} else {
ResourceValidationResult.success()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class RegexTest {
class PhoneNumberTest {

@Test
fun isPhoneNumber_fail() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.chrisjenx.yakcov.strings

import com.chrisjenx.yakcov.ValidationResult.Outcome
import kotlin.test.Test
import kotlin.test.assertEquals

class PhoneRuleTest {

@Test
fun phoneNumber_invalid() {
assertEquals(Outcome.ERROR, Phone().validate("43435").outcome())
}

@Test
fun phoneNumber_noRegion() {
assertEquals(Outcome.SUCCESS, Phone().validate("6508991234").outcome())
}

@Test
fun phoneNumber_wrongRegion() {
// This is a UK number should error for US
assertEquals(Outcome.ERROR, Phone("US").validate("07745973912").outcome())
assertEquals(Outcome.SUCCESS, Phone("GB").validate("07740973910").outcome())
}

@Test
fun phoneNumber_withRegion() {
assertEquals(Outcome.SUCCESS, Phone().validate("+16508991234").outcome())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import platform.Foundation.matchesInString
* Check if is phone number to best ability of each platform.
*/
@OptIn(ExperimentalForeignApi::class)
actual fun String?.isPhoneNumber(): Boolean {
actual fun String?.isPhoneNumber(defaultRegion: String?): Boolean {
if (this.isNullOrBlank()) return false
val detector = NSDataDetector(types = NSTextCheckingTypePhoneNumber, error = null)
val range = NSMakeRange(0.toULong(), this.length.toULong())
Expand Down
4 changes: 2 additions & 2 deletions library/src/jsMain/kotlin/com/chrisjenx/yakcov/RegexExt.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package com.chrisjenx.yakcov
/**
* Check if is phone number to best ability of each platform.
*/
actual fun String?.isPhoneNumber(): Boolean {
actual fun String?.isPhoneNumber(defaultRegion: String?): Boolean {
this ?: return false
return try {
val result = parsePhoneNumber(this, "US")
val result = parsePhoneNumber(phone = this, defaultCountry = defaultRegion?.uppercase())
result?.isValid() ?: false
} catch (e: Throwable) {
false
Expand Down
4 changes: 2 additions & 2 deletions library/src/jvmMain/kotlin/com/chrisjenx/yakcov/Regex.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ private val phoneUtil = PhoneNumberUtil.getInstance()
/**
* Check if is phone number to best ability of each platform.
*/
actual fun String?.isPhoneNumber(): Boolean {
actual fun String?.isPhoneNumber(defaultRegion: String?): Boolean {
this ?: return false
return try {
val result = phoneUtil.parse(this, "US")
val result = phoneUtil.parse(this, defaultRegion?.uppercase())
phoneUtil.isValidNumber(result)
} catch (t: Throwable) {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package com.chrisjenx.yakcov
/**
* Check if is phone number to best ability of each platform.
*/
actual fun String?.isPhoneNumber(): Boolean {
actual fun String?.isPhoneNumber(defaultRegion: String?): Boolean {
this ?: return false
return try {
val result = parsePhoneNumber(this, "US")
val result = parsePhoneNumber(this, defaultRegion?.uppercase())
result?.isValid() ?: false
} catch (e: Throwable) {
false
Expand Down

0 comments on commit 455f25a

Please sign in to comment.