-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move intent callback handling to login viewmodel
- Loading branch information
Showing
4 changed files
with
90 additions
and
60 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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/xyz/wingio/dimett/ui/components/IntentHandler.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,35 @@ | ||
package xyz.wingio.dimett.ui.components | ||
|
||
import android.content.Intent | ||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.core.util.Consumer | ||
|
||
@Composable | ||
fun IntentHandler(enabled: Boolean = true, onIntent: (intent: Intent) -> Unit) { | ||
val activity = LocalContext.current as? ComponentActivity ?: return // Make sure we are actually in a `ComponentActivity` | ||
val currentOnIntent by rememberUpdatedState(onIntent) // Handle new `onIntent` lambdas safely | ||
|
||
// Try not to recreate the callback on every composition | ||
val intentCallback = remember { | ||
Consumer<Intent> { intent -> if(enabled) currentOnIntent(intent) } | ||
} | ||
|
||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
DisposableEffect(lifecycleOwner) { | ||
// Add callback to the current activity | ||
activity.addOnNewIntentListener(intentCallback) | ||
|
||
// Remove callback when effect leaves the composition | ||
onDispose { | ||
activity.removeOnNewIntentListener(intentCallback) | ||
} | ||
} | ||
} |
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