Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

네트워크 연결 상태 확인 #148

Merged
merged 4 commits into from
Nov 22, 2023
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
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package com.ohdodok.catchytape

import android.net.ConnectivityManager
import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import com.ohdodok.catchytape.core.ui.R.string as uiString

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var connectivityManager: ConnectivityManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

connectivityManager = getSystemService(ConnectivityManager::class.java)
checkNetworkState()

val networkStateObserver = NetworkStateObserver(connectivityManager, ::checkNetworkState)
lifecycle.addObserver(networkStateObserver)
}

private fun checkNetworkState() {
val activeNetwork = connectivityManager.activeNetwork
val capabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
val isNetworkAvailable = capabilities?.hasCapability(NET_CAPABILITY_VALIDATED) ?: false

if (!isNetworkAvailable) {
Toast.makeText(this, getString(uiString.check_network), Toast.LENGTH_LONG).show()
Copy link
Collaborator

Choose a reason for hiding this comment

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

toast 대신 snackbar 어떠신가요??

Copy link
Member Author

Choose a reason for hiding this comment

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

저도 스낵바 좋아하는데요. 아직 스낵바 디자인을 안 해서 토스트로 했어요!

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ohdodok.catchytape

import android.net.ConnectivityManager
import android.net.Network
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

class NetworkStateObserver(
private val connectivityManager: ConnectivityManager,
checkNetworkState: () -> Unit
) : DefaultLifecycleObserver {

private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network) {
super.onLost(network)
checkNetworkState()
}
}

override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
connectivityManager.registerDefaultNetworkCallback(networkCallback)
}

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
connectivityManager.unregisterNetworkCallback(networkCallback)
}
}
2 changes: 2 additions & 0 deletions android/core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="check_network">네트워크 연결을 확인해 주세요.</string>

<string name="home">홈</string>
<string name="search">검색</string>
<string name="playlist">재생목록</string>
Expand Down