network #28

Merged
AmineB merged 28 commits from davidoskky/ReaderForSelfoss-multiplatform:network into master 2022-08-22 19:01:16 +00:00
12 changed files with 5 additions and 240 deletions
Showing only changes of commit df4903cae5 - Show all commits

View File

@ -210,5 +210,5 @@ dependencies {
// TODO: When updating this library, check if the todo in RepositoryImpl.startNetwork can be resolved
// TODO: Include this library once this is merged https://github.com/ln-12/multiplatform-connectivity-status/pull/4
// implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
implementation(project(":connectionstatus"))
implementation(files("../shared/libs/multiplatform-connectivity-status.aar"))
}

View File

@ -22,7 +22,7 @@ import bou.amine.apps.readerforselfossv2.repository.Repository
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.ftinc.scoop.Scoop
import com.github.ln12.library.ConnectivityStatus
import com.github.`ln-12`.library.ConnectivityStatus
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
import com.mikepenz.materialdrawer.util.DrawerImageLoader
import com.russhwolf.settings.Settings

View File

@ -1 +0,0 @@
/build

View File

@ -1,65 +0,0 @@
plugins {
id("com.android.library")
kotlin("multiplatform")
}
group = "com.github.ln-12"
version = "1.1.0"
repositories {
google()
mavenCentral()
}
kotlin {
android {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
publishLibraryVariants("release", "debug")
}
ios()
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdk = 31
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 31
}
}
// metadata is currently not supported for iOS
// https://youtrack.jetbrains.com/issue/KT-44459#focus=Comments-27-4645829.0-0
kotlin.metadata {
compilations.matching { it.name == "iosMain" }.all {
compileKotlinTaskProvider.configure { enabled = false }
}
}

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.github.ln12.library">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

View File

@ -1,85 +0,0 @@
package com.github.ln12.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
// From library com.github.ln-12:multiplatform-connectivity-status:1.1.0
// https://github.com/ln-12/multiplatform-connectivity-status
// Copyright 2021 Lorenzo Neumann
// Edited by davidoskky as here: https://github.com/ln-12/multiplatform-connectivity-status/pull/4
actual class ConnectivityStatus(private val context: Context) {
actual val isNetworkConnected = MutableStateFlow(false)
private var connectivityManager: ConnectivityManager? = null
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
Log.d("Connectivity status", "Connected")
isNetworkConnected.value = true
}
override fun onLost(network: Network) {
Log.d("Connectivity status", "Disconnected")
isNetworkConnected.value = false
}
}
actual fun start() {
try {
if (connectivityManager == null) {
connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
}
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()
isNetworkConnected.value = false
}
}
actual fun stop() {
connectivityManager?.unregisterNetworkCallback(networkCallback)
Log.d("Connectivity status", "Stopped")
}
}

View File

@ -1,12 +0,0 @@
package com.github.ln12.library
import kotlinx.coroutines.flow.MutableStateFlow
// From library com.github.ln-12:multiplatform-connectivity-status:1.1.0
// https://github.com/ln-12/multiplatform-connectivity-status
// Copyright 2021 Lorenzo Neumann
expect class ConnectivityStatus {
val isNetworkConnected: MutableStateFlow<Boolean>
fun start()
fun stop()
}

View File

@ -1,67 +0,0 @@
package com.github.ln12.library
import kotlinx.coroutines.flow.MutableStateFlow
import cocoapods.Reachability.*
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import platform.Foundation.NSLog
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_main_queue
import kotlin.native.concurrent.freeze
// From library com.github.ln-12:multiplatform-connectivity-status:1.1.0
// https://github.com/ln-12/multiplatform-connectivity-status
// Copyright 2021 Lorenzo Neumann
actual class ConnectivityStatus {
actual val isNetworkConnected = MutableStateFlow(false)
private var reachability: Reachability? = null
// Swift can't directly use a MutableStateFlow, so the status
// is exposed via a lambda/closure
fun getStatus(success: (Boolean) -> Unit) {
MainScope().launch {
isNetworkConnected.collect { status ->
success(status)
}
}
}
actual fun start() {
dispatch_async(dispatch_get_main_queue()) {
reachability = Reachability.reachabilityForInternetConnection()
val reachableCallback = { reach: Reachability? ->
dispatch_async(dispatch_get_main_queue(), {
NSLog("Connected")
isNetworkConnected.value = true
}.freeze())
}.freeze()
reachability?.reachableBlock = reachableCallback
val unreachableCallback = { reach: Reachability? ->
dispatch_async(dispatch_get_main_queue(), {
NSLog("Disconnected")
isNetworkConnected.value = false
}.freeze())
}.freeze()
reachability?.unreachableBlock = unreachableCallback
reachability?.startNotifier()
dispatch_async(dispatch_get_main_queue(), {
isNetworkConnected.value = reachability?.isReachable() ?: false
NSLog("Initial reachability: ${reachability?.isReachable()}")
}.freeze())
}
}
actual fun stop() {
reachability?.stopNotifier()
}
}

View File

@ -8,5 +8,4 @@ pluginManagement {
rootProject.name = "ReaderForSelfossV2"
include(":androidApp")
include(":shared")
include(":connectionstatus")
include(":shared")

View File

@ -40,7 +40,7 @@ kotlin {
// Network information
// TODO: Include this library once this is merged https://github.com/ln-12/multiplatform-connectivity-status/pull/4
//implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
implementation(project(":connectionstatus"))
implementation(files("libs/multiplatform-connectivity-status.aar"))
}
}
val commonTest by getting {

Binary file not shown.

View File

@ -5,7 +5,7 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import bou.amine.apps.readerforselfossv2.utils.DateUtils
import bou.amine.apps.readerforselfossv2.utils.ItemType
import com.github.ln12.library.ConnectivityStatus
import com.github.`ln-12`.library.ConnectivityStatus
import com.russhwolf.settings.Settings
import io.github.aakira.napier.Napier
import kotlinx.coroutines.CoroutineScope