1
- package dev.souravdas.hush
1
+ package dev.souravdas.hush.activities
2
2
3
3
import android.app.Activity
4
4
import android.content.Context
@@ -14,13 +14,17 @@ import androidx.activity.compose.setContent
14
14
import androidx.activity.viewModels
15
15
import androidx.core.app.NotificationManagerCompat
16
16
import androidx.lifecycle.lifecycleScope
17
+ import androidx.navigation.NavHostController
18
+ import androidx.navigation.compose.rememberNavController
17
19
import dagger.hilt.android.AndroidEntryPoint
18
20
import dev.sourav.emptycompose.ui.theme.HushTheme
19
- import dev.souravdas.hush.activities.UIKit
20
21
import dev.souravdas.hush.arch.MainActivityVM
22
+ import dev.souravdas.hush.nav.NavGraph
23
+ import dev.souravdas.hush.nav.Screens
21
24
import dev.souravdas.hush.others.Utils
22
25
import dev.souravdas.hush.services.KeepAliveService
23
26
import kotlinx.coroutines.launch
27
+ import timber.log.Timber
24
28
import javax.inject.Inject
25
29
26
30
@AndroidEntryPoint
@@ -30,18 +34,23 @@ class MainActivity : ComponentActivity() {
30
34
lateinit var utils: Utils
31
35
private val viewModel: MainActivityVM by viewModels()
32
36
private var doubleBackToExitPressedOnce = false
37
+ private lateinit var navController: NavHostController
33
38
34
39
override fun onCreate (savedInstanceState : Bundle ? ) {
35
40
super .onCreate(savedInstanceState)
36
41
setContent {
37
42
viewModel.getSelectedApp()
38
43
44
+ navController = rememberNavController()
45
+
39
46
HushTheme () {
40
- UIKit ().MainActivityScreen (onNotificationPermissionGet = {
41
- openNotificationAccessSettingsIfNeeded(this )
42
- }, checkNotificationPermission = {
43
- isNotificationListenerEnabled(this )
44
- })
47
+ NavGraph (
48
+ navController = navController,
49
+ onNotificationPermissionGet = {
50
+ openNotificationAccessSettingsIfNeeded(this )
51
+ }, checkNotificationPermission = {
52
+ isNotificationListenerEnabled(this )
53
+ })
45
54
}
46
55
}
47
56
@@ -50,14 +59,19 @@ class MainActivity : ComponentActivity() {
50
59
51
60
private fun checkService () {
52
61
lifecycleScope.launch {
53
- viewModel.getHushStatusAsFlow().collect() {value ->
54
- if (value){
62
+ viewModel.getHushStatusAsFlow().collect() { value ->
63
+ if (value) {
55
64
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
56
- startForegroundService(Intent (this @MainActivity, KeepAliveService ::class .java))
57
- }else {
65
+ startForegroundService(
66
+ Intent (
67
+ this @MainActivity,
68
+ KeepAliveService ::class .java
69
+ )
70
+ )
71
+ } else {
58
72
startService(Intent (this @MainActivity, KeepAliveService ::class .java))
59
73
}
60
- }else {
74
+ } else {
61
75
stopService(Intent (this @MainActivity, KeepAliveService ::class .java))
62
76
}
63
77
}
@@ -76,21 +90,25 @@ class MainActivity : ComponentActivity() {
76
90
return enabledPackages.contains(packageName)
77
91
}
78
92
93
+ @Deprecated(" Deprecated in Java" )
79
94
override fun onBackPressed () {
80
- if (doubleBackToExitPressedOnce) {
81
- viewModel.removeIncompleteApp()
82
- finishAffinity()
83
- return
84
- }
95
+ if (navController.currentDestination!! .route == Screens .MainScreen .route){
96
+ if (doubleBackToExitPressedOnce) {
97
+ viewModel.removeIncompleteApp()
98
+ finishAffinity()
99
+ return
100
+ }
85
101
86
- this .doubleBackToExitPressedOnce = true
87
- Toast .makeText(this , " Please click BACK again to exit" , Toast .LENGTH_SHORT ).show()
102
+ this .doubleBackToExitPressedOnce = true
103
+ Toast .makeText(this , " Please click BACK again to exit" , Toast .LENGTH_SHORT ).show()
88
104
89
- Handler (Looper .getMainLooper()).postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000 )
105
+ Handler (Looper .getMainLooper()).postDelayed(Runnable {
106
+ doubleBackToExitPressedOnce = false
107
+ }, 2000 )
108
+ }else {
109
+ super .onBackPressed()
110
+ }
90
111
}
91
112
92
- override fun onDestroy () {
93
- super .onDestroy()
94
- }
95
113
}
96
114
0 commit comments