-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
data/src/main/java/com/whyranoid/data/repository/NetworkRepositoryImpl.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,78 @@ | ||
package com.whyranoid.data.repository | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import com.whyranoid.domain.repository.NetworkRepository | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class NetworkRepositoryImpl(private val context: Context) : | ||
NetworkRepository { | ||
|
||
private val _networkConnectionState = MutableStateFlow(getCurrentNetwork()) | ||
override fun getNetworkConnectionState() = _networkConnectionState.asStateFlow() | ||
|
||
private val connectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
private val connectivityCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
emitNetworkConnectionState(true) | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
emitNetworkConnectionState(false) | ||
} | ||
} | ||
private val connectivityReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (context != null && context.isConnected) { | ||
emitNetworkConnectionState(true) | ||
} else { | ||
emitNetworkConnectionState(false) | ||
} | ||
} | ||
} | ||
|
||
override fun addNetworkConnectionCallback() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
connectivityManager.registerDefaultNetworkCallback(connectivityCallback) | ||
} else { | ||
context.registerReceiver( | ||
connectivityReceiver, | ||
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION), | ||
) | ||
} | ||
} | ||
|
||
override fun removeNetworkConnectionCallback() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
connectivityManager.unregisterNetworkCallback(connectivityCallback) | ||
} else { | ||
context.unregisterReceiver(connectivityReceiver) | ||
} | ||
} | ||
|
||
private fun getCurrentNetwork(): Boolean { | ||
return try { | ||
val networkCapabilities = | ||
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) | ||
networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} | ||
|
||
private fun emitNetworkConnectionState(currentState: Boolean) { | ||
_networkConnectionState.value = currentState | ||
} | ||
} | ||
|
||
val Context.isConnected: Boolean | ||
get() = (getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager)?.activeNetworkInfo?.isConnected == true |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/whyranoid/domain/repository/NetworkRepository.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,10 @@ | ||
package com.whyranoid.domain.repository | ||
|
||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface NetworkRepository { | ||
|
||
fun getNetworkConnectionState(): StateFlow<Boolean> | ||
fun addNetworkConnectionCallback() | ||
fun removeNetworkConnectionCallback() | ||
} |