Skip to content

Commit

Permalink
Added permission pop up with permisson rationale
Browse files Browse the repository at this point in the history
  • Loading branch information
RamziJabali committed Jun 25, 2024
1 parent f8345bc commit 1c3a021
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,11 +16,22 @@ import ramzi.eljabali.justjog.viewmodel.StatisticsViewModel
fun JustJogNavigation(
navController: NavHostController,
statisticsViewModel: StatisticsViewModel,
askForPermission: (List<String>, (String, Boolean) -> Unit) -> Unit,
shouldShowRequestPermissionRationale: (String) -> Boolean,
openSettings: () -> Unit,
) {
NavHost(navController = navController, startDestination = StatisticsScreen) {
composable<StatisticsScreen> {
statisticsViewModel.onLaunch()
StatisticsPage(statisticsViewModel.statisticsViewState.collectAsStateWithLifecycle(), statisticsViewModel::onFabClicked)
StatisticsPage(
statisticsViewModel.statisticsViewState.collectAsStateWithLifecycle(),
statisticsViewModel::onFabClicked,
statisticsViewModel::dismissDialog,
statisticsViewModel::onPermissionResult,
shouldShowRequestPermissionRationale,
openSettings,
askForPermission
)
}
composable<CalendarScreen> {
JustJogCalendarView()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -60,16 +62,66 @@ import ramzi.eljabali.justjog.viewstate.StatisticsViewState


@Composable
fun StatisticsPage(statisticsViewState: State<StatisticsViewState>, startService: () -> Unit) {
val isBlurred by remember { mutableStateOf(false) }
val isHidden by remember { mutableStateOf(false) }
fun StatisticsPage(
statisticsViewState: State<StatisticsViewState>,
startService: () -> Unit,
dismissDialog: () -> Unit,
onPermissionResult: (String, Boolean) -> Unit,
shouldShowRequestPermissionRationale: (String) -> Boolean,
openSettings: () -> Unit,
askForPermission: (List<String>, (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) {
0.5F
} 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()
Expand All @@ -79,7 +131,6 @@ fun StatisticsPage(statisticsViewState: State<StatisticsViewState>, startService
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally,
) {

ElevatedCard(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -232,6 +283,11 @@ fun StatisticsPage(statisticsViewState: State<StatisticsViewState>, startService
horizontalArrangement = Arrangement.End
) {
JoggingFAB {
if (isBlurSupported()) {
isBlurred = true
} else {
isHidden = true
}
startService()
}
}
Expand Down Expand Up @@ -276,7 +332,7 @@ fun PreviewStatisticsPage() {
perJogStatisticsBreakDown = listOf("0 Miles", "No Weekly Data", "No Weekly Data")
)
).collectAsState()
StatisticsPage(statisticsViewState, {})
// StatisticsPage(statisticsViewState, {})

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ramzi.eljabali.justjog.viewmodel

import android.Manifest
import android.app.Application
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ data class StatisticsViewState(
),
val weeklyStatisticsBreakDown: List<String> = listOf("", "", ""),
val perJogStatisticsBreakDown: List<String> = listOf("", "", ""),
val listOfPermissions: List<String> = emptyList(),
val shouldBlur: Boolean = false,
)

0 comments on commit 1c3a021

Please sign in to comment.