Skip to content

Commit

Permalink
Support for API 21+, dependency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ln-12 committed Jan 6, 2022
1 parent 727c305 commit 7684f38
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 69 deletions.
17 changes: 8 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
plugins {
id("com.android.library")
kotlin("native.cocoapods") version "1.4.30"
kotlin("multiplatform") version "1.4.30"
kotlin("native.cocoapods") version "1.6.10"
kotlin("multiplatform") version "1.6.10"
id("convention.publication")
}

group = "com.github.ln-12"
version = "1.0.0"
version = "1.1.0"

repositories {
google()
mavenCentral()
jcenter()
}

kotlin {
Expand All @@ -27,7 +26,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3-native-mt")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
}
}
val commonTest by getting {
Expand All @@ -41,7 +40,7 @@ kotlin {
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.1")
implementation("junit:junit:4.13.2")
}
}

Expand All @@ -61,11 +60,11 @@ kotlin {
}

android {
compileSdkVersion(30)
compileSdk = 31
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24) // API >= 24 needed for ConnectivityManager.registerDefaultNetworkCallback()
targetSdkVersion(30)
minSdk = 21
targetSdk = 31
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
26 changes: 11 additions & 15 deletions multiplatform_connectivity_status.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
Pod::Spec.new do |spec|
spec.name = 'multiplatform_connectivity_status'
spec.version = '1.0.0'
spec.version = '1.1.0'
spec.homepage = 'https://github.com/ln-12/multiplatform-connectivity-status'
spec.source = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" }
spec.authors = ''
spec.license = ''
spec.summary = 'A Kotlin multiplatform mobile library to monitor the connectivity status of the device'

spec.static_framework = true
spec.vendored_frameworks = "build/cocoapods/framework/multiplatform_connectivity_status.framework"
spec.libraries = "c++"
spec.module_name = "#{spec.name}_umbrella"
Expand All @@ -17,13 +16,8 @@ Pod::Spec.new do |spec|
spec.dependency 'Reachability', '3.2'

spec.pod_target_xcconfig = {
'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64',
'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm',
'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x64',
'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm',
'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64',
'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64',
'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64'
'KOTLIN_PROJECT_PATH' => ':',
'PRODUCT_MODULE_NAME' => 'multiplatform_connectivity_status',
}

spec.script_phases = [
Expand All @@ -32,14 +26,16 @@ Pod::Spec.new do |spec|
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$COCOAPODS_SKIP_KOTLIN_BUILD" ]; then
echo "Skipping Gradle build task invocation due to COCOAPODS_SKIP_KOTLIN_BUILD environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/gradlew" -p "$REPO_ROOT" ::syncFramework \
-Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION \
-Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \
-Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \
-Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS"
"$REPO_ROOT/gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION
SCRIPT
}
]
Expand Down
14 changes: 7 additions & 7 deletions sample/androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ plugins {

dependencies {
implementation(project(":shared"))
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.appcompat:appcompat:1.4.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
}

android {
compileSdkVersion(30)
compileSdk = 31
defaultConfig {
applicationId = "com.github.ln12.connectivitystatussample.androidApp"
minSdkVersion(24)
targetSdkVersion(30)
minSdk = 21
targetSdk = 31
versionCode = 1
versionName = "1.0"
}
Expand Down
6 changes: 4 additions & 2 deletions sample/androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<application
android:allowBackup="false"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
classpath("com.android.tools.build:gradle:4.0.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
classpath("com.android.tools.build:gradle:7.0.4)
}
}
Expand Down
2 changes: 1 addition & 1 deletion sample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jan 21 10:09:06 CET 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
29 changes: 14 additions & 15 deletions sample/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kotlin {
android()
ios()

version = "1.0"
version = "1.1"

cocoapods {
summary = "Common library"
Expand All @@ -18,8 +18,8 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.github.ln-12:multiplatform-connectivity-status:1.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3-native-mt")
implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
}
}
val commonTest by getting {
Expand All @@ -29,26 +29,25 @@ kotlin {
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.3.0")
}
dependencies {}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.1")
}
dependencies {}
}
val iosMain by getting {
dependencies {}
}
val iosTest by getting {
dependencies {}
}
val iosMain by getting
val iosTest by getting
}
}

android {
compileSdkVersion(30)
compileSdk = 31
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(30)
minSdk = 21
targetSdk = 31
}
}
26 changes: 11 additions & 15 deletions sample/shared/shared.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
Pod::Spec.new do |spec|
spec.name = 'shared'
spec.version = '1.0'
spec.version = '1.1'
spec.homepage = 'https://github.com/ln-12/multiplatform-connectivity-status'
spec.source = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" }
spec.authors = ''
spec.license = ''
spec.summary = 'Common library'

spec.static_framework = true
spec.vendored_frameworks = "build/cocoapods/framework/shared.framework"
spec.libraries = "c++"
spec.module_name = "#{spec.name}_umbrella"
Expand All @@ -17,13 +16,8 @@ Pod::Spec.new do |spec|


spec.pod_target_xcconfig = {
'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64',
'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm',
'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x64',
'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm',
'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64',
'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64',
'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64'
'KOTLIN_PROJECT_PATH' => ':shared',
'PRODUCT_MODULE_NAME' => 'shared',
}

spec.script_phases = [
Expand All @@ -32,14 +26,16 @@ Pod::Spec.new do |spec|
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$COCOAPODS_SKIP_KOTLIN_BUILD" ]; then
echo "Skipping Gradle build task invocation due to COCOAPODS_SKIP_KOTLIN_BUILD environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" :shared:syncFramework \
-Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION \
-Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \
-Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \
-Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION
SCRIPT
}
]
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.android" || requested.id.name == "kotlin-android-extensions") {
useModule("com.android.tools.build:gradle:4.0.1")
useModule("com.android.tools.build:gradle:7.0.4")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.github.`ln-12`.library
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import android.util.Log
import kotlinx.coroutines.flow.MutableStateFlow

Expand All @@ -25,7 +28,44 @@ actual class ConnectivityStatus(private val context: Context) {
actual fun start() {
try {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(networkCallback)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// API 24 and above
connectivityManager.registerDefaultNetworkCallback(networkCallback)

val currentNetwork = connectivityManager.activeNetwork

if(currentNetwork == null) {
isNetworkConnected.value = false

Log.d("Connectivity status", "Disconnected")
}
} else {
// API 23 and below
val networkRequest = NetworkRequest.Builder().apply {
addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
}.build()

connectivityManager.registerNetworkCallback(networkRequest, networkCallback)

val currentNetwork = connectivityManager.activeNetworkInfo

if(currentNetwork == null || (
currentNetwork.type != ConnectivityManager.TYPE_ETHERNET &&
currentNetwork.type != ConnectivityManager.TYPE_WIFI &&
currentNetwork.type != ConnectivityManager.TYPE_MOBILE
)) {
isNetworkConnected.value = false

Log.d("Connectivity status", "Disconnected")
}
}

Log.d("Connectivity status", "Started")
} catch (e: Exception) {
Log.d("Connectivity status", "Failed to start: ${e.message.toString()}")
e.printStackTrace()
Expand All @@ -35,5 +75,6 @@ actual class ConnectivityStatus(private val context: Context) {

actual fun stop() {
connectivityManager?.unregisterNetworkCallback(networkCallback)
Log.d("Connectivity status", "Stopped")
}
}

0 comments on commit 7684f38

Please sign in to comment.