This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Network Rule #7
Open
canonall
wants to merge
26
commits into
main
Choose a base branch
from
network-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0055332
add konnection library
canonall 308600d
implement NetworkListener using Konnection
canonall ca34720
add NetworkListener to JobQueue
canonall 4cf22f8
implement NetworkRule
canonall bd96487
update tests
canonall 65b1a20
move network check inside willRun
canonall 8efa999
move withTimeout to JobQueue
canonall 2635030
update network rule test
canonall d206da3
remove Konnection library
canonall 4932108
implement NetworkManager without Konnection and update NetworkListener
canonall a9c940f
code clean-up
canonall a9c7358
fix flaky tests
canonall 51f2b87
add missing actual keyword for ios NetworkManager
canonall e1d8a13
@SuppressLint("MissingPermission") - AndroidManifest and permissions …
canonall 208effc
use abstract NetworkListener
canonall 0bb1b79
observe network changes in the queue
canonall bac0add
update NetworkListener:
canonall 95f862d
add new JobEvents
canonall 185bed6
refactor NetworkException to NetworkRuleTimeoutException
canonall 6ea1857
wait for network rule to be satisfied 15 seconds, otherwise cancel it
canonall ea76420
update test
canonall d92cc9d
chore: rename timeout to jobTimeout and allow user to define a networ…
canonall 4b5b720
chore: fix tests
canonall 164ce28
refactor: use named arguments and rename some variables
canonall 5377d9d
call observeNetworkState() before running the job
canonall 56801aa
refactor NetworkListeners and android network manager
canonall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.liftric.job.queue"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/androidMain/kotlin/com/liftric/job/queue/NetworkListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.liftric.job.queue | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
|
||
actual class NetworkListener( | ||
networkManager: NetworkManager, | ||
scope: CoroutineScope = CoroutineScope(context = Dispatchers.Default) | ||
) : AbstractNetworkListener( | ||
networkManager = networkManager, | ||
scope = scope | ||
) { | ||
private val _currentNetworkState = MutableStateFlow(NetworkState.NONE) | ||
override val currentNetworkState: StateFlow<NetworkState> | ||
get() = _currentNetworkState.asStateFlow() | ||
|
||
private var job: kotlinx.coroutines.Job? = null | ||
|
||
override fun observeNetworkState() { | ||
job = scope.launch { | ||
networkManager | ||
.network | ||
.collectLatest { currentNetworkState -> | ||
_currentNetworkState.emit(currentNetworkState ?: NetworkState.NONE) | ||
} | ||
} | ||
} | ||
|
||
override fun stopMonitoring() { | ||
job?.cancel() | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/androidMain/kotlin/com/liftric/job/queue/NetworkManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.liftric.job.queue | ||
|
||
import android.annotation.SuppressLint | ||
import android.annotation.TargetApi | ||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
@SuppressLint("MissingPermission") | ||
actual class NetworkManager(context: Context) { | ||
private var connectivityManager: ConnectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
private val connectionPublisher = MutableStateFlow(getCurrentNetworkConnection()) | ||
val network: Flow<NetworkState?> = connectionPublisher | ||
|
||
private fun getCurrentNetworkConnection(): NetworkState? = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
postAndroidMNetworkConnection(connectivityManager) | ||
} else { | ||
preAndroidMNetworkConnection(connectivityManager) | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lovely to have it backwards compatible. We should internally discuss which API level makes sense for Flowify. I would rather start with Android 6 or 7 to get rid of some old chunks. But nice that you've implemented it with old versions in mind! Just leave it as is at the moment, we may remove it later on! |
||
private fun postAndroidMNetworkConnection(connectivityManager: ConnectivityManager): NetworkState? { | ||
val network = connectivityManager.activeNetwork | ||
val capabilities = connectivityManager.getNetworkCapabilities(network) | ||
return getNetworkConnection(capabilities) | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun preAndroidMNetworkConnection(connectivityManager: ConnectivityManager): NetworkState? = | ||
when (connectivityManager.activeNetworkInfo?.type) { | ||
null -> null | ||
ConnectivityManager.TYPE_WIFI -> NetworkState.WIFI | ||
else -> NetworkState.MOBILE | ||
} | ||
|
||
private fun getNetworkConnection(capabilities: NetworkCapabilities?): NetworkState? = | ||
when { | ||
capabilities == null -> null | ||
Build.VERSION.SDK_INT < Build.VERSION_CODES.M | ||
&& !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) -> null | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && | ||
!(capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) -> null | ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> NetworkState.WIFI | ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> NetworkState.MOBILE | ||
else -> null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the use of named arguments.