From 94ff2018228469ff219640333e27c1c4570dc1d0 Mon Sep 17 00:00:00 2001 From: andrekir Date: Sun, 18 Aug 2024 11:38:32 -0300 Subject: [PATCH] refactor: use `ACTION_TIME_TICK` receiver for time updates --- .../com/geeksville/mesh/ui/LastHeardInfo.kt | 3 - .../com/geeksville/mesh/ui/UsersFragment.kt | 34 +---------- .../ui/components/TimeTickWithLifecycle.kt | 56 +++++++++++++++++++ 3 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/com/geeksville/mesh/ui/components/TimeTickWithLifecycle.kt diff --git a/app/src/main/java/com/geeksville/mesh/ui/LastHeardInfo.kt b/app/src/main/java/com/geeksville/mesh/ui/LastHeardInfo.kt index cf711b5a0..a9200362b 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/LastHeardInfo.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/LastHeardInfo.kt @@ -7,9 +7,6 @@ import androidx.compose.material.Icon import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.ImageVector diff --git a/app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt index a1b0c8364..447396cce 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt @@ -1,7 +1,6 @@ package com.geeksville.mesh.ui import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,27 +17,21 @@ import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableLongStateOf -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.compose.ui.unit.dp import androidx.fragment.app.activityViewModels import androidx.hilt.navigation.compose.hiltViewModel -import androidx.lifecycle.compose.LifecycleResumeEffect import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.geeksville.mesh.NodeInfo import com.geeksville.mesh.R import com.geeksville.mesh.android.Logging import com.geeksville.mesh.model.UIViewModel import com.geeksville.mesh.ui.components.NodeFilterTextField +import com.geeksville.mesh.ui.components.rememberTimeTickWithLifecycle import com.geeksville.mesh.ui.theme.AppTheme import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.delay -import kotlin.time.Duration.Companion.minutes @AndroidEntryPoint class UsersFragment : ScreenFragment("Users"), Logging { @@ -144,7 +137,7 @@ fun NodesScreen( } } - val currentTimeMillis = getCurrentTimeMillisWithLifecycle() + val currentTimeMillis = rememberTimeTickWithLifecycle() LazyColumn( state = listState, @@ -189,26 +182,3 @@ fun NodesScreen( } } } - -@Composable -private fun getCurrentTimeMillisWithLifecycle(): Long { - var currentTimeMillis by remember { mutableLongStateOf(System.currentTimeMillis()) } - var running by remember { mutableStateOf(false) } - LaunchedEffect(running) { - if (running) { - while (true) { - delay(1.minutes) - currentTimeMillis = System.currentTimeMillis() - Log.d("NodesScreen", "NodesScreen: $currentTimeMillis") - } - } - } - - LifecycleResumeEffect(Unit) { - running = true - onPauseOrDispose { - running = false - } - } - return currentTimeMillis -} diff --git a/app/src/main/java/com/geeksville/mesh/ui/components/TimeTickWithLifecycle.kt b/app/src/main/java/com/geeksville/mesh/ui/components/TimeTickWithLifecycle.kt new file mode 100644 index 000000000..3d15b524d --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/ui/components/TimeTickWithLifecycle.kt @@ -0,0 +1,56 @@ +package com.geeksville.mesh.ui.components + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.platform.LocalContext +import androidx.lifecycle.compose.LifecycleResumeEffect + +@Composable +fun rememberTimeTickWithLifecycle(): Long { + val context = LocalContext.current + var value by remember { mutableLongStateOf(System.currentTimeMillis()) } + val receiver = TimeBroadcastReceiver { value = System.currentTimeMillis() } + + LifecycleResumeEffect(Unit) { + receiver.register(context) + value = System.currentTimeMillis() + + onPauseOrDispose { + receiver.unregister(context) + } + } + + return value +} + +private class TimeBroadcastReceiver( + val onTimeChanged: () -> Unit, +) : BroadcastReceiver() { + private var registered = false + + override fun onReceive(context: Context, intent: Intent) { + onTimeChanged() + } + + fun register(context: Context) { + if (!registered) { + val filter = IntentFilter(Intent.ACTION_TIME_TICK) + context.registerReceiver(this, filter) + registered = true + } + } + + fun unregister(context: Context) { + if (registered) { + context.unregisterReceiver(this) + registered = false + } + } +}