Skip to content

Commit

Permalink
feat: refactor network availability observer in MainActivity
Browse files Browse the repository at this point in the history
This commit moves the observer for network availability from the `onCreate` method to the `onStart` method in the MainActivity. The observer is also removed when the activity stops. This ensures the observer only runs when the activity is in the foreground, improving the app's performance and user experience.
  • Loading branch information
IRedDragonICY committed Mar 17, 2024
1 parent fcff6bb commit 28de0b5
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/src/main/java/com/uad/portal/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ class MainActivity : ComponentActivity() {

mainViewModel.initSessionManager(this)

mainViewModel.isNetworkAvailable.observe(this) { isAvailable ->
if (isAvailable) {
mainViewModel.initAttendanceWorker(this)
Toast.makeText(this, "Internet connection is available", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Internet connection is not available", Toast.LENGTH_LONG).show()
}
}


setContent {
PortalUADTheme {
Surface(
Expand All @@ -55,11 +45,21 @@ class MainActivity : ComponentActivity() {
override fun onStart() {
super.onStart()
mainViewModel.registerNetworkCallback(this)

mainViewModel.isNetworkAvailable.observe(this) { isAvailable ->
if (isAvailable) {
mainViewModel.initAttendanceWorker(this)
Toast.makeText(this, "Internet connection is available", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Internet connection is not available", Toast.LENGTH_LONG).show()
}
}
}

override fun onStop() {
super.onStop()
mainViewModel.unregisterNetworkCallback(this)
mainViewModel.isNetworkAvailable.removeObservers(this)
}

private fun createNotificationChannel() {
Expand Down

0 comments on commit 28de0b5

Please sign in to comment.