package com.suno.android.ui.screens.onboarding.allow_notifications import android.Manifest import android.content.Intent import android.os.Build import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.suno.android.common_ui.components.buttons.OnboardingBottomButton import com.suno.android.common_ui.components.displays.top_bars.OnboardingTopBar import com.suno.android.common_ui.theme.ExtendedTheme import com.suno.android.common_res.R as CommonResR @Composable fun OnboardingAllowNotificationsScreen( vm: OnboardingAllowNotificationsScreenVM = hiltViewModel(), onReadyForNextScreen: () -> Unit, stepCountDisplay: String, ) { val viewState = vm.state.collectAsStateWithLifecycle().value val context = LocalContext.current // Notification permission launcher val notificationPermissionLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.RequestPermission(), onResult = { isGranted: Boolean -> vm.sendEvent( OnboardingAllowNotificationsScreenEvent.OnNotificationPermissionResult( isGranted = isGranted, ), ) }, ) LaunchedEffect(Unit) { vm.effects.collect { effect -> when (effect) { OnboardingAllowNotificationsScreenEffect.NavigateToNextScreen -> { onReadyForNextScreen.invoke() } OnboardingAllowNotificationsScreenEffect.RequestNotificationPermission -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } else { // For older versions, open app notification settings val intent = Intent().apply { action = Settings.ACTION_APP_NOTIFICATION_SETTINGS putExtra( Settings.EXTRA_APP_PACKAGE, context.packageName, ) } context.startActivity(intent) } } } } } Scaffold( topBar = { OnboardingTopBar( title = stringResource(CommonResR.string.just_one_more_thing), stepCountDisplay = stepCountDisplay, actionVisible = viewState.skipActionIsVisible, actionClick = { onReadyForNextScreen.invoke() }, ) }, ) { innerPadding -> Column( modifier = Modifier .padding(innerPadding) .fillMaxSize() .background(ExtendedTheme.colors.backgroundPrimary), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, ) { Column( modifier = Modifier.weight(1f), verticalArrangement = Arrangement.SpaceEvenly, horizontalAlignment = Alignment.CenterHorizontally, ) { Text( text = stringResource(CommonResR.string.allow_notifications), style = ExtendedTheme.typography.largeBody, color = ExtendedTheme.colors.textPrimary, ) Text( modifier = Modifier.padding(16.dp), text = stringResource(CommonResR.string.allow_notifications_description), style = ExtendedTheme.typography.body, color = ExtendedTheme.colors.textPrimary, textAlign = TextAlign.Center, ) } OnboardingBottomButton( modifier = Modifier.padding(horizontal = 8.dp), buttonText = stringResource(CommonResR.string.sure), onClick = { vm.sendEvent( OnboardingAllowNotificationsScreenEvent.RequestNotificationPermission, ) }, ) OnboardingBottomButton( modifier = Modifier.padding(horizontal = 8.dp, vertical = 16.dp), buttonText = stringResource(CommonResR.string.not_now), backgroundColor = ExtendedTheme.colors.backgroundPrimary, textColor = ExtendedTheme.colors.textPrimary, onClick = { onReadyForNextScreen.invoke() }, ) } } } @Preview(showSystemUi = true) @Composable private fun OnboardingAllowNotificationsScreen_Preview() { OnboardingAllowNotificationsScreen( onReadyForNextScreen = {}, stepCountDisplay = "4/4", ) }