Skip to content

Commit

Permalink
feat: Check if recording is available on app resume
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Nov 19, 2023
1 parent b6bfac4 commit bc2e88e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
) {
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/app/myzel394/alibi/ui/effects/force-update.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Lifecycle.Event> = 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc2e88e

Please sign in to comment.