Skip to content

Commit

Permalink
✨ 네트워크 연결 상태 레포지토리 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghanJu committed Sep 21, 2023
1 parent 79c4c87 commit 4027aa8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
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
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()
}

0 comments on commit 4027aa8

Please sign in to comment.