Skip to content
Merged
Changes from 1 commit
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 @@ -7,6 +7,8 @@ import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import com.eatssu.android.presentation.util.showDialog

Expand All @@ -21,6 +23,9 @@ class NetworkConnection(private val context: Context) :
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) // 와이파이 사용 관련 감지
.build()

// NetworkCallback은 백그라운드 스레드에서 호출되므로 Dialog 등 UI 작업은 메인 스레드에서 실행해야 함
private val mainHandler = Handler(Looper.getMainLooper())

// 네트워크 연결 안 되어있을 때 보여줄 다이얼로그
private val dialog: Dialog by lazy {
context.showDialog("네트워크 연결 안 됨", "Wi-Fi, 모바일 데이터를 확인해주세요") {
Expand Down Expand Up @@ -59,18 +64,23 @@ class NetworkConnection(private val context: Context) :
override fun onAvailable(network: Network) {
super.onAvailable(network)

if (getConnectivityStatus() == null) {
// 네트워크 연결 안 되어 있을 때
dialog.show()
} else {
// 네트워크 연결 되어 있을 때
dialog.dismiss()
mainHandler.post {
if (getConnectivityStatus() == null) {
// 네트워크 연결 안 되어 있을 때
dialog.show()
} else {
// 네트워크 연결 되어 있을 때
dialog.dismiss()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lifecycleScope.launch {
if (getConnectivityStatus() == null) {
dialog.show()
} else {
dialog.dismiss()
}
}

아마 이렇게 하면 코루틴 사용해서 생명주기 부분이 더 안정성이 잇는 것으로 압니다..!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오우 그러네요! 제미니 반영까지 해서 올리겠습니당

}
}

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

mainHandler.post {
dialog.show()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

onLost 콜백은 등록된 NetworkRequest에 맞는 네트워크 중 하나가 끊겼을 때 호출됩니다. 예를 들어, Wi-Fi와 모바일 데이터가 모두 연결된 상태에서 Wi-Fi만 끊겨도 onLost가 호출될 수 있습니다. 이 경우, 모바일 데이터 연결은 여전히 활성화되어 있으므로 '네트워크 연결 안 됨' 다이얼로그를 표시하는 것은 사용자에게 혼란을 줄 수 있습니다.

다이얼로그를 표시하기 전에 getConnectivityStatus()를 통해 현재 활성화된 네트워크가 없는지 확인하는 로직을 추가하는 것이 좋습니다.

        mainHandler.post {
            if (getConnectivityStatus() == null) {
                dialog.show()
            }
        }

}
}