From bc2e88ef0436a7ab568838d4b54624a671799dbf Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 19 Nov 2023 19:40:26 +0100 Subject: [PATCH] feat: Check if recording is available on app resume --- .../AudioRecorder/molecules/StartRecording.kt | 7 ++- .../myzel394/alibi/ui/effects/force-update.kt | 44 +++++++++++++++++++ .../alibi/ui/screens/AudioRecorderScreen.kt | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt index 6ea90d284..f1bb88937 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt @@ -41,6 +41,7 @@ import app.myzel394.alibi.dataStore import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_SIZE import app.myzel394.alibi.ui.components.atoms.PermissionRequester +import app.myzel394.alibi.ui.effects.rememberForceUpdateOnLifeCycleChange import app.myzel394.alibi.ui.models.AudioRecorderModel import java.time.format.DateTimeFormatter import java.time.format.FormatStyle @@ -129,9 +130,13 @@ fun StartRecording( .fillMaxWidth(), textAlign = TextAlign.Center, ) + + val forceUpdate = rememberForceUpdateOnLifeCycleChange() if (appSettings.lastRecording?.hasRecordingsAvailable(context) == true) { Column( - modifier = Modifier.weight(1f), + modifier = Modifier + .weight(1f) + .then(forceUpdate), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Bottom, ) { diff --git a/app/src/main/java/app/myzel394/alibi/ui/effects/force-update.kt b/app/src/main/java/app/myzel394/alibi/ui/effects/force-update.kt index be81a62a6..a46a01a95 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/effects/force-update.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/effects/force-update.kt @@ -1,11 +1,20 @@ package app.myzel394.alibi.ui.effects import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.layout.onPlaced +import androidx.compose.ui.platform.LocalLifecycleOwner +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.LifecycleOwner import kotlinx.coroutines.delay @Composable @@ -20,4 +29,39 @@ fun rememberForceUpdate( } return tickTack +} + +@Composable +fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { + val eventHandler = rememberUpdatedState(onEvent) + val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current) + + DisposableEffect(lifecycleOwner.value) { + val lifecycle = lifecycleOwner.value.lifecycle + val observer = LifecycleEventObserver { owner, event -> + eventHandler.value(owner, event) + } + + lifecycle.addObserver(observer) + onDispose { + lifecycle.removeObserver(observer) + } + } +} + +@Composable +fun rememberForceUpdateOnLifeCycleChange( + events: Array = arrayOf( + Lifecycle.Event.ON_RESUME + ), +): Modifier { + var tickTack by rememberSaveable { mutableStateOf(1f) } + + OnLifecycleEvent { owner, event -> + if (events.contains(event)) { + tickTack = if (tickTack == 1f) 0.99f else 1f + } + } + + return Modifier.alpha(tickTack) } \ No newline at end of file diff --git a/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt b/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt index 597a77a8d..abc7d6788 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt @@ -33,6 +33,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusManager import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp