From 9cc4146d985236d1e99abb1d85d880787a339ed8 Mon Sep 17 00:00:00 2001 From: Christopher Jenkins Date: Thu, 10 Oct 2024 10:18:55 -0600 Subject: [PATCH] Use NSLibPhoneNumber (#5) * use nslibphonenumber * remove instrumented tests * update iOS deps --- .gitignore | 1 + README.MD | 11 ++++- library/build.gradle.kts | 44 +++++-------------- .../chrisjenx/yakcov/strings/PhoneRuleTest.kt | 2 - .../com/chrisjenx/yakcov/RegexExt.ios.kt | 17 +++---- 5 files changed, 32 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 223e061..3225620 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ xcuserdata/ Pods/ *.jks *yarn.lock +*.podspec diff --git a/README.MD b/README.MD index 2eba147..6b1802c 100644 --- a/README.MD +++ b/README.MD @@ -43,7 +43,7 @@ with(emailValiator) { ## Dependencies -By default we publish +By default we publish to [Maven Central](https://central.sonatype.com/artifact/com.chrisjenx.yakcov/library). We publish all targets (Android, JVM, JS, Wasm, iOS) you can include the common library in your @@ -65,6 +65,15 @@ dependencies { } ``` +### iOS +You will need to add the following pod to your iOS project: + +```kotlin +cocoaPods { + pod("libPhoneNumber-iOS", version = "~> 1.2") +} +``` + ## Build locally - check your system with [KDoctor](https://github.com/Kotlin/kdoctor) diff --git a/library/build.gradle.kts b/library/build.gradle.kts index bf9d48d..4e01aac 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -1,19 +1,17 @@ @file:OptIn(ExperimentalWasmDsl::class) -import com.android.build.api.dsl.ManagedVirtualDevice import com.vanniktech.maven.publish.SonatypeHost import org.jetbrains.compose.ExperimentalComposeLibrary import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.JvmTarget -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.cocoapods) + alias(libs.plugins.cocoapods) alias(libs.plugins.maven.publish) } @@ -22,19 +20,17 @@ kotlin { compilerOptions { freeCompilerArgs.add("-Xexpect-actual-classes") } -// cocoapods { -// version = "1.0.0" -// summary = "Yet Another Kotlin COmpose Validation library" -// homepage = "https://github.com/chrisjenx/yakcov" -// ios.deploymentTarget = "14.1" -// framework { -// baseName = "yakcov" -//// isStatic = true -//// pod("libPhoneNumber-iOS") -//// @OptIn(ExperimentalKotlinGradlePluginApi::class) -//// transitiveExport = true -// } -// } + cocoapods { + summary = "Yet Another Kotlin COmpose Validation library" + homepage = "https://github.com/chrisjenx/yakcov" + ios.deploymentTarget = "14.1" + framework { + baseName = "yakcov" + @OptIn(ExperimentalKotlinGradlePluginApi::class) + transitiveExport = true + } + pod("libPhoneNumber-iOS", version = "~> 1.2") + } applyDefaultHierarchyTemplate() androidTarget { @@ -46,15 +42,6 @@ kotlin { } } } - //https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-test.html - @OptIn(ExperimentalKotlinGradlePluginApi::class) - instrumentedTestVariant { - sourceSetTree.set(KotlinSourceSetTree.test) - dependencies { - debugImplementation(libs.androidx.testManifest) - implementation(libs.androidx.junit4) - } - } } jvm() @@ -155,13 +142,6 @@ android { @Suppress("UnstableApiUsage") testOptions { targetSdk = 34 - managedDevices.devices { - maybeCreate("pixel5").apply { - device = "Pixel 5" - apiLevel = 34 - systemImageSource = "aosp" - } - } unitTests { isIncludeAndroidResources = true } diff --git a/library/src/commonTest/kotlin/com/chrisjenx/yakcov/strings/PhoneRuleTest.kt b/library/src/commonTest/kotlin/com/chrisjenx/yakcov/strings/PhoneRuleTest.kt index f2fc753..7203e74 100644 --- a/library/src/commonTest/kotlin/com/chrisjenx/yakcov/strings/PhoneRuleTest.kt +++ b/library/src/commonTest/kotlin/com/chrisjenx/yakcov/strings/PhoneRuleTest.kt @@ -1,6 +1,5 @@ package com.chrisjenx.yakcov.strings -import com.chrisjenx.yakcov.IOSIgnore import com.chrisjenx.yakcov.ValidationResult.Outcome import com.chrisjenx.yakcov.initPhoneNumberUtil import kotlin.test.BeforeTest @@ -30,7 +29,6 @@ class PhoneRuleTest { } @Test - @IOSIgnore // FIXME: This is failing on iOS, get cocoapods working so we can use libphonenumber on iOS. fun phoneNumber_wrongRegion() { // This is a UK number should error for US assertEquals(Outcome.ERROR, Phone("US").validate("07745973912").outcome()) diff --git a/library/src/iosMain/kotlin/com/chrisjenx/yakcov/RegexExt.ios.kt b/library/src/iosMain/kotlin/com/chrisjenx/yakcov/RegexExt.ios.kt index 4092fc0..03de0c2 100644 --- a/library/src/iosMain/kotlin/com/chrisjenx/yakcov/RegexExt.ios.kt +++ b/library/src/iosMain/kotlin/com/chrisjenx/yakcov/RegexExt.ios.kt @@ -1,10 +1,8 @@ package com.chrisjenx.yakcov +import cocoapods.libPhoneNumber_iOS.NBPhoneNumberUtil import kotlinx.cinterop.ExperimentalForeignApi -import platform.Foundation.NSDataDetector -import platform.Foundation.NSMakeRange -import platform.Foundation.NSTextCheckingTypePhoneNumber -import platform.Foundation.matchesInString + /** * Check if is phone number to best ability of each platform. @@ -12,8 +10,11 @@ import platform.Foundation.matchesInString @OptIn(ExperimentalForeignApi::class) 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()) - val matches = detector.matchesInString(this, options = 0.toULong(), range = range) - return matches.isNotEmpty() + try { + val phoneUtil = NBPhoneNumberUtil.sharedInstance() ?: return false + val phoneNumber = phoneUtil.parse(this, defaultRegion, null) + return phoneUtil.isValidNumberForRegion(phoneNumber, defaultRegion) + } catch (e: Throwable) { + return false + } }