ln-12 / multiplatform-connectivity-status

A Kotlin multiplatform mobile library to monitor the connectivity status of the device
Apache License 2.0
61 stars 2 forks source link

Decreasing min android sdk #1

Closed iamthevoid closed 2 years ago

iamthevoid commented 2 years ago

Why can not you decrease android min sdk?

At now it impossible use this lib on android api lower than 24 (gradle build fails)

if you decrease min sdk and restrict usage with annotations @RequiresApi(24) user could use you library anyway, just with add own implementation to lower api.

But if i remember correct - there are no reasons to use 24 api as min, as the ConnectivityManager.NetworkCallback available from api 21.

Could you desrease min api please?

iamthevoid commented 2 years ago

I've mistaken. Your code requires 24 api, so you can not just decrease min api version.

But you can also use RequiresApi annotation

ln-12 commented 2 years ago

Hey, thanks for your suggestion. If you have an idea how to implement it for lower APIs, a PR is welcomed.

iamthevoid commented 2 years ago

I wanted, but can not build your project. I tried to fix build (problem with cocoapods plugin and ruby i assume) but with no luck and have no more time to do it anymore.

But i can suggest my realization for api 21+

class AndroidConnectivityStatus(private val context: Context) : ConnectivityStatus() {

    private var connectivityManager: ConnectivityManager? = null

    private val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) = onNetworkAvailable()
        override fun onLost(network: Network) = onNetworkLost()
    }

    override fun start() {
        connectivityManager = context.getSystemService<ConnectivityManager>()?.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                registerDefaultNetworkCallback(networkCallback)
            } else {
                registerNetworkCallback(networkRequest(), networkCallback)
            }
        }
    }

    override fun stop() {
        connectivityManager?.unregisterNetworkCallback(networkCallback)
    }

    private fun networkRequest(): NetworkRequest {
        return 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()
    }
}
iamthevoid commented 2 years ago

I also have somewhere code for lower api, but i should to dig deeper in my old projects. Maybe i find and post to you later

ln-12 commented 2 years ago

Closed with 7684f38d74c67db96449334b5e9b7d14e415fa7f.