Skip to content

Commit 4db6026

Browse files
committed
Added Session KeepAlive watcher
As long as the App is in the foreground, the session will keep getting extended
1 parent 79e4b9b commit 4db6026

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

app/src/main/kotlin/com/darkrockstudios/app/securecamera/MainActivity.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ import androidx.activity.ComponentActivity
99
import androidx.activity.compose.setContent
1010
import androidx.activity.enableEdgeToEdge
1111
import androidx.compose.runtime.mutableStateOf
12+
import androidx.lifecycle.Lifecycle
13+
import androidx.lifecycle.lifecycleScope
14+
import androidx.lifecycle.repeatOnLifecycle
1215
import androidx.navigation.NavHostController
1316
import androidx.navigation.compose.rememberNavController
1417
import com.darkrockstudios.app.securecamera.auth.AuthorizationRepository
1518
import com.darkrockstudios.app.securecamera.navigation.AppDestinations
1619
import com.darkrockstudios.app.securecamera.preferences.AppPreferencesDataSource
20+
import kotlinx.coroutines.delay
1721
import kotlinx.coroutines.flow.firstOrNull
22+
import kotlinx.coroutines.isActive
23+
import kotlinx.coroutines.launch
1824
import kotlinx.coroutines.runBlocking
1925
import org.koin.android.ext.android.inject
26+
import timber.log.Timber
27+
import kotlin.time.Duration.Companion.seconds
2028

2129
class MainActivity : ComponentActivity() {
2230
private var capturePhoto = mutableStateOf<Boolean?>(null)
@@ -42,6 +50,8 @@ class MainActivity : ComponentActivity() {
4250
navController = rememberNavController()
4351
App(capturePhoto, startDestination, navController)
4452
}
53+
54+
startKeepAliveWatcher()
4555
}
4656

4757
private fun determineStartRoute(): String {
@@ -85,6 +95,24 @@ class MainActivity : ComponentActivity() {
8595
locationRepository.refreshPermissionStatus()
8696
}
8797

98+
private fun startKeepAliveWatcher() {
99+
lifecycleScope.launch {
100+
repeatOnLifecycle(Lifecycle.State.RESUMED) {
101+
Timber.d("Starting session keep-alive watcher")
102+
while (isActive) {
103+
if (authorizationRepository.checkSessionValidity()) {
104+
authorizationRepository.keepAliveSession()
105+
Timber.d("Session keep-alive ping sent")
106+
} else {
107+
Timber.d("No valid session, do not send keep-alive")
108+
}
109+
delay(30.seconds)
110+
}
111+
Timber.d("Stopping session keep-alive watcher")
112+
}
113+
}
114+
}
115+
88116
private fun receiveFiles(): List<Uri> {
89117
val intent = getIntent()
90118

0 commit comments

Comments
 (0)