From 1c3a0213b6e88229fcd7ec8089563400db2478a3 Mon Sep 17 00:00:00 2001 From: Ramzi Jabali Date: Tue, 25 Jun 2024 13:19:53 -0700 Subject: [PATCH] Added permission pop up with permisson rationale --- .../ramzi/eljabali/justjog/MainActivity.kt | 11 +-- .../justjog/ui/navigation/Navigation.kt | 15 +++- .../justjog/ui/views/StatisticsView.kt | 68 +++++++++++++++++-- .../justjog/viewmodel/StatisticsViewModel.kt | 19 +++--- .../justjog/viewstate/StatisticsViewState.kt | 1 + 5 files changed, 92 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt b/app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt index c7135f9..03a45b2 100644 --- a/app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt +++ b/app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt @@ -3,7 +3,6 @@ package ramzi.eljabali.justjog import android.app.Activity import android.content.Intent import android.content.pm.PackageManager -import android.Manifest import android.net.Uri import android.os.Build import android.os.Bundle @@ -27,9 +26,7 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val statisticsViewModel: StatisticsViewModel by viewModel() - val dialogQueue = statisticsViewModel.visiblePermissionDialogQueue askForPermission( Permissions.list @@ -47,7 +44,13 @@ class MainActivity : ComponentActivity() { bottomBar = { BottomNavigationView(navController) }, ) { contentPadding -> Log.d("Scaffold", "Content Padding $contentPadding") - JustJogNavigation(navController, statisticsViewModel) + JustJogNavigation( + navController, + statisticsViewModel, + this::askForPermission, + this::shouldShowRequestPermissionRationale, + ::openSettings + ) } } } diff --git a/app/src/main/java/ramzi/eljabali/justjog/ui/navigation/Navigation.kt b/app/src/main/java/ramzi/eljabali/justjog/ui/navigation/Navigation.kt index 1e49236..68a1731 100644 --- a/app/src/main/java/ramzi/eljabali/justjog/ui/navigation/Navigation.kt +++ b/app/src/main/java/ramzi/eljabali/justjog/ui/navigation/Navigation.kt @@ -1,12 +1,10 @@ package ramzi.eljabali.justjog.ui.navigation import androidx.compose.runtime.Composable -import androidx.compose.runtime.State import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable -import androidx.navigation.compose.rememberNavController import ramzi.eljabali.justjog.ui.util.CalendarScreen import ramzi.eljabali.justjog.ui.util.StatisticsScreen import ramzi.eljabali.justjog.ui.views.JustJogCalendarView @@ -18,11 +16,22 @@ import ramzi.eljabali.justjog.viewmodel.StatisticsViewModel fun JustJogNavigation( navController: NavHostController, statisticsViewModel: StatisticsViewModel, + askForPermission: (List, (String, Boolean) -> Unit) -> Unit, + shouldShowRequestPermissionRationale: (String) -> Boolean, + openSettings: () -> Unit, ) { NavHost(navController = navController, startDestination = StatisticsScreen) { composable { statisticsViewModel.onLaunch() - StatisticsPage(statisticsViewModel.statisticsViewState.collectAsStateWithLifecycle(), statisticsViewModel::onFabClicked) + StatisticsPage( + statisticsViewModel.statisticsViewState.collectAsStateWithLifecycle(), + statisticsViewModel::onFabClicked, + statisticsViewModel::dismissDialog, + statisticsViewModel::onPermissionResult, + shouldShowRequestPermissionRationale, + openSettings, + askForPermission + ) } composable { JustJogCalendarView() diff --git a/app/src/main/java/ramzi/eljabali/justjog/ui/views/StatisticsView.kt b/app/src/main/java/ramzi/eljabali/justjog/ui/views/StatisticsView.kt index ef98619..a90801d 100644 --- a/app/src/main/java/ramzi/eljabali/justjog/ui/views/StatisticsView.kt +++ b/app/src/main/java/ramzi/eljabali/justjog/ui/views/StatisticsView.kt @@ -1,5 +1,6 @@ package ramzi.eljabali.justjog.ui.views +import android.Manifest import android.os.Build import android.util.Log import androidx.compose.animation.core.animateDpAsState @@ -44,7 +45,8 @@ import com.jaikeerthick.composable_graphs.composables.line.style.LineGraphVisibi import com.jaikeerthick.composable_graphs.style.LabelPosition import kotlinx.coroutines.flow.MutableStateFlow import ramzi.eljabali.justjog.R -import ramzi.eljabali.justjog.loactionservice.ForegroundService +import ramzi.eljabali.justjog.model.permissioninformation.LocationPermissionTextProvider +import ramzi.eljabali.justjog.model.permissioninformation.NotificationPermissionTextProvider import ramzi.eljabali.justjog.ui.design.CardElevation import ramzi.eljabali.justjog.ui.design.CardSize import ramzi.eljabali.justjog.ui.design.FabElevation @@ -60,9 +62,17 @@ import ramzi.eljabali.justjog.viewstate.StatisticsViewState @Composable -fun StatisticsPage(statisticsViewState: State, startService: () -> Unit) { - val isBlurred by remember { mutableStateOf(false) } - val isHidden by remember { mutableStateOf(false) } +fun StatisticsPage( + statisticsViewState: State, + startService: () -> Unit, + dismissDialog: () -> Unit, + onPermissionResult: (String, Boolean) -> Unit, + shouldShowRequestPermissionRationale: (String) -> Boolean, + openSettings: () -> Unit, + askForPermission: (List, (String, Boolean) -> Unit) -> Unit +) { + var isBlurred by remember { mutableStateOf(false) } + var isHidden by remember { mutableStateOf(false) } val animatedBlur by animateDpAsState(targetValue = if (isBlurred) 10.dp else 0.dp, label = "") var alpha by remember { mutableFloatStateOf(1F) } alpha = if (!isBlurSupported() && isHidden) { @@ -70,6 +80,48 @@ fun StatisticsPage(statisticsViewState: State, startService } else { 1F } + + if (statisticsViewState.value.shouldBlur) { + statisticsViewState.value.listOfPermissions + .reversed() + .forEach { permission -> + PermissionDialogBox( + permissionTextProvider = when (permission) { + Manifest.permission.POST_NOTIFICATIONS -> { + NotificationPermissionTextProvider() + } + + Manifest.permission.ACCESS_FINE_LOCATION -> { + LocationPermissionTextProvider() + } + + else -> return@forEach + }, + + isPermanentlyDeclined = !shouldShowRequestPermissionRationale(permission), + onDismiss = { + if (isBlurSupported()) { + isBlurred = false + } else { + isHidden = false + } + dismissDialog() + }, + onOkClick = { + if (isBlurSupported()) { + isBlurred = false + } else { + isHidden = false + } + dismissDialog() + askForPermission(listOf(permission)) { permission, isGranted -> + onPermissionResult(permission, isGranted) + } + }, + onGoToAppSettingsClick = openSettings + ) + } + } Column( modifier = Modifier .fillMaxSize() @@ -79,7 +131,6 @@ fun StatisticsPage(statisticsViewState: State, startService verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally, ) { - ElevatedCard( modifier = Modifier .fillMaxWidth() @@ -232,6 +283,11 @@ fun StatisticsPage(statisticsViewState: State, startService horizontalArrangement = Arrangement.End ) { JoggingFAB { + if (isBlurSupported()) { + isBlurred = true + } else { + isHidden = true + } startService() } } @@ -276,7 +332,7 @@ fun PreviewStatisticsPage() { perJogStatisticsBreakDown = listOf("0 Miles", "No Weekly Data", "No Weekly Data") ) ).collectAsState() - StatisticsPage(statisticsViewState, {}) +// StatisticsPage(statisticsViewState, {}) } } \ No newline at end of file diff --git a/app/src/main/java/ramzi/eljabali/justjog/viewmodel/StatisticsViewModel.kt b/app/src/main/java/ramzi/eljabali/justjog/viewmodel/StatisticsViewModel.kt index 4c15685..f14c8e9 100644 --- a/app/src/main/java/ramzi/eljabali/justjog/viewmodel/StatisticsViewModel.kt +++ b/app/src/main/java/ramzi/eljabali/justjog/viewmodel/StatisticsViewModel.kt @@ -1,5 +1,6 @@ package ramzi.eljabali.justjog.viewmodel +import android.Manifest import android.app.Application import android.content.Context import android.content.Intent @@ -210,17 +211,17 @@ class StatisticsViewModel( } } - fun blur() { - visiblePermissionDialogQueue.removeFirst() - _statisticsViewState.update { - it.copy( - shouldBlur = true - ) - } - } - fun onFabClicked() { Log.i(this.TAG, "onFabClicked: start") + if (!visiblePermissionDialogQueue.isEmpty()) { + _statisticsViewState.update { + it.copy( + shouldBlur = true, + listOfPermissions = visiblePermissionDialogQueue + ) + } + return + } Intent(applicationContext, ForegroundService::class.java).also { it.action = ForegroundService.Actions.START.name Log.i(this.TAG, "onFabClicked: ${it.action}") diff --git a/app/src/main/java/ramzi/eljabali/justjog/viewstate/StatisticsViewState.kt b/app/src/main/java/ramzi/eljabali/justjog/viewstate/StatisticsViewState.kt index 06ffba2..f5725ea 100644 --- a/app/src/main/java/ramzi/eljabali/justjog/viewstate/StatisticsViewState.kt +++ b/app/src/main/java/ramzi/eljabali/justjog/viewstate/StatisticsViewState.kt @@ -15,5 +15,6 @@ data class StatisticsViewState( ), val weeklyStatisticsBreakDown: List = listOf("", "", ""), val perJogStatisticsBreakDown: List = listOf("", "", ""), + val listOfPermissions: List = emptyList(), val shouldBlur: Boolean = false, ) \ No newline at end of file