-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from myofficework000/network-connection-broadc…
…ast-receiver Added broadcast receiver to check network connection
- Loading branch information
Showing
4 changed files
with
82 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
19 changes: 19 additions & 0 deletions
19
...a/com/abhishek/pathak/kotlin/android/githubcompose/ui/feature/common/BroadcastReceiver.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,19 @@ | ||
package com.abhishek.pathak.kotlin.android.githubcompose.ui.feature.common | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.ConnectivityManager | ||
|
||
class BroadcastReceiver(private val onConnectivityChange: (Boolean) -> Unit) : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
val isConnected = isNetworkConnected(context) | ||
onConnectivityChange(isConnected) | ||
} | ||
private fun isNetworkConnected(context: Context):Boolean{ | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val networkInfo = connectivityManager.activeNetworkInfo | ||
return networkInfo != null && networkInfo.isConnected | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
.../com/abhishek/pathak/kotlin/android/githubcompose/ui/feature/common/ConnectionSnackBar.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,39 @@ | ||
package com.abhishek.pathak.kotlin.android.githubcompose.ui.feature.common | ||
|
||
import androidx.compose.material.Snackbar | ||
import androidx.compose.material.SnackbarDuration | ||
import androidx.compose.material.SnackbarHost | ||
import androidx.compose.material.SnackbarHostState | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
|
||
@Composable | ||
fun ConnectionSnackBar(isConnected: Boolean) { | ||
val snackBarHotState = remember { | ||
SnackbarHostState() | ||
} | ||
LaunchedEffect(isConnected) { | ||
if(!isConnected){ | ||
snackBarHotState.showSnackbar( | ||
message = "No internet connection", | ||
duration = SnackbarDuration.Long | ||
) | ||
} | ||
} | ||
SnackbarHost(hostState = snackBarHotState, | ||
snackbar = { | ||
Snackbar(action = { | ||
TextButton(onClick = { snackBarHotState.currentSnackbarData?.dismiss() }) { | ||
Text(text = "Dismiss") | ||
} | ||
}, | ||
backgroundColor = Color.Yellow) { | ||
Text(text = "No internet connection", color = Color.Black) | ||
} | ||
}) | ||
} |
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