-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use
ACTION_TIME_TICK
receiver for time updates
- Loading branch information
Showing
3 changed files
with
58 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/geeksville/mesh/ui/components/TimeTickWithLifecycle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |