Skip to content

Commit

Permalink
refactor: use ACTION_TIME_TICK receiver for time updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Aug 18, 2024
1 parent f5cc7cf commit 94ff201
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/geeksville/mesh/ui/LastHeardInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 2 additions & 32 deletions app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -144,7 +137,7 @@ fun NodesScreen(
}
}

val currentTimeMillis = getCurrentTimeMillisWithLifecycle()
val currentTimeMillis = rememberTimeTickWithLifecycle()

LazyColumn(
state = listState,
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 94ff201

Please sign in to comment.