-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from KazaKago/feature/fix_too_many_requests_ex…
…ception Fix TooManyRequestsException
- Loading branch information
Showing
4 changed files
with
74 additions
and
60 deletions.
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
53 changes: 53 additions & 0 deletions
53
swr/src/main/java/com/kazakago/swr/compose/internal/NetworkCallbackManager.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,53 @@ | ||
package com.kazakago.swr.compose.internal | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
|
||
internal class NetworkCallbackManager private constructor( | ||
private val context: Context, | ||
) { | ||
|
||
companion object { | ||
@SuppressLint("StaticFieldLeak") | ||
private var instance: NetworkCallbackManager? = null | ||
|
||
fun getInstance(context: Context): NetworkCallbackManager { | ||
var instance = instance | ||
return if (instance != null && instance.context == context.applicationContext) { | ||
instance | ||
} else { | ||
instance = NetworkCallbackManager(context.applicationContext) | ||
this.instance = instance | ||
instance | ||
} | ||
} | ||
} | ||
|
||
private var isFirstTime = true | ||
private val _onAvailable = MutableSharedFlow<Network>(extraBufferCapacity = 1) | ||
val onAvailable = _onAvailable.asSharedFlow() | ||
|
||
init { | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val request = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
.build() | ||
val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
if (isFirstTime) { | ||
isFirstTime = false | ||
return | ||
} | ||
_onAvailable.tryEmit(network) | ||
} | ||
} | ||
connectivityManager.registerNetworkCallback(request, networkCallback) | ||
} | ||
} |
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