Skip to content

Commit

Permalink
Use NSLibPhoneNumber (#5)
Browse files Browse the repository at this point in the history
* use nslibphonenumber

* remove instrumented tests

* update iOS deps
  • Loading branch information
chrisjenx authored Oct 10, 2024
1 parent 4b26783 commit 9cc4146
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ xcuserdata/
Pods/
*.jks
*yarn.lock
*.podspec
11 changes: 10 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
44 changes: 12 additions & 32 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -155,13 +142,6 @@ android {
@Suppress("UnstableApiUsage")
testOptions {
targetSdk = 34
managedDevices.devices {
maybeCreate<ManagedVirtualDevice>("pixel5").apply {
device = "Pixel 5"
apiLevel = 34
systemImageSource = "aosp"
}
}
unitTests {
isIncludeAndroidResources = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
Expand Down
17 changes: 9 additions & 8 deletions library/src/iosMain/kotlin/com/chrisjenx/yakcov/RegexExt.ios.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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.
*/
@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
}
}

0 comments on commit 9cc4146

Please sign in to comment.