Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class BaseActivity<B : ViewBinding>(


private val networkCheck: NetworkConnection by lazy {
NetworkConnection(this)
NetworkConnection(this, lifecycleScope)
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.provider.Settings
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.launch
import com.eatssu.android.presentation.util.showDialog

// 네트워크 연결 확인을 위해 네트워크 변경 시 알람에 사용하는 클래스 NetworkCallback 을 커스터마이징
class NetworkConnection(private val context: Context) :
ConnectivityManager.NetworkCallback() {
class NetworkConnection(
private val context: Context,
private val lifecycleScope: LifecycleCoroutineScope
) : ConnectivityManager.NetworkCallback() {

private val connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
Expand Down Expand Up @@ -59,18 +63,27 @@ class NetworkConnection(private val context: Context) :
override fun onAvailable(network: Network) {
super.onAvailable(network)

if (getConnectivityStatus() == null) {
// 네트워크 연결 안 되어 있을 때
dialog.show()
} else {
// 네트워크 연결 되어 있을 때
dialog.dismiss()
lifecycleScope.launch {
if (getConnectivityStatus() == null) {
// 네트워크 연결 안 되어 있을 때
dialog.show()
} else {
// 네트워크 연결 되어 있을 때
dialog.dismiss()
}
}
}

// 네트워크 끊겼을 때 실행되는 메소드
override fun onLost(network: Network) {
super.onLost(network)
dialog.show()

// Wi-Fi와 모바일 데이터가 모두 연결된 상태에서 Wi-Fi만 끊겨도 onLost가 호출될 수 있으므로,
// 현재 활성화 네트워크 여부 검증 필요
if (getConnectivityStatus() == null) {
lifecycleScope.launch {
dialog.show()
}
}
}
}