Skip to content

Commit

Permalink
feat: added permission box
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Jun 24, 2024
1 parent 59992cd commit cde1986
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.mifos.core.designsystem.component

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver

@Composable
fun PermissionBox(
requiredPermissions: List<String>,
title: Int,
description: Int? = null,
confirmButtonText: Int,
dismissButtonText: Int,
onGranted: @Composable (() -> Unit)? = null,
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current

var permissionGranted by remember {
mutableStateOf(
requiredPermissions.all {
ContextCompat.checkSelfPermission(
context,
it
) == PackageManager.PERMISSION_GRANTED
}
)
}

var shouldShowPermissionRationale =
requiredPermissions.all {
(context as? Activity)?.let { it1 ->
ActivityCompat.shouldShowRequestPermissionRationale(
it1, it
)
} == true
}

var shouldDirectUserToApplicationSettings by remember {
mutableStateOf(false)
}

val decideCurrentPermissionStatus: (Boolean, Boolean) -> String =
{ permissionGranted, shouldShowPermissionRationale ->
if (permissionGranted) "Granted"
else if (shouldShowPermissionRationale) "Rejected"
else "Denied"
}

var currentPermissionStatus by remember {
mutableStateOf(
decideCurrentPermissionStatus(
permissionGranted,
shouldShowPermissionRationale
)
)
}

val multiplePermissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions(),
onResult = { permissionResults ->
val isGranted =
requiredPermissions.all { permissionResults[it] ?: false }

permissionGranted = isGranted

if (!isGranted) {
shouldShowPermissionRationale =
requiredPermissions.all {
ActivityCompat.shouldShowRequestPermissionRationale(
context as Activity,
it
)
}
}
shouldDirectUserToApplicationSettings =
!shouldShowPermissionRationale && !permissionGranted
currentPermissionStatus = decideCurrentPermissionStatus(
permissionGranted,
shouldShowPermissionRationale
)
})

DisposableEffect(key1 = lifecycleOwner, effect = {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START &&
!permissionGranted &&
!shouldShowPermissionRationale
) {
multiplePermissionLauncher.launch(requiredPermissions.toTypedArray())
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
})

if (shouldShowPermissionRationale) {
MifosDialogBox(
showDialogState = shouldShowPermissionRationale,
onDismiss = { shouldShowPermissionRationale = false },
title = title,
message = description,
confirmButtonText = confirmButtonText,
onConfirm = {
shouldShowPermissionRationale = false
multiplePermissionLauncher.launch(requiredPermissions.toTypedArray())
},
dismissButtonText = dismissButtonText
)
}

if (shouldDirectUserToApplicationSettings) {
Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", context.packageName, null)
).also {
context.startActivity(it)
}
}

if (permissionGranted) {
if (onGranted != null) {
onGranted()
}
}
}
2 changes: 1 addition & 1 deletion feature/path-tracking/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
namespace = "com.mifos.feature.path_tracking"
namespace = "com.mifos.feature.path.tracking"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@file:OptIn(
ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class,
ExperimentalMaterialApi::class,
ExperimentalPermissionsApi::class
)

Expand All @@ -25,7 +25,6 @@ import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedCard
Expand All @@ -37,6 +36,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -54,8 +54,6 @@ import androidx.core.content.ContextCompat.registerReceiver
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.gson.Gson
Expand All @@ -67,11 +65,13 @@ import com.mifos.core.common.utils.Constants
import com.mifos.core.designsystem.component.MifosCircularProgress
import com.mifos.core.designsystem.component.MifosScaffold
import com.mifos.core.designsystem.component.MifosSweetError
import com.mifos.core.designsystem.component.PermissionBox
import com.mifos.core.designsystem.icon.MifosIcons
import com.mifos.core.designsystem.theme.Black
import com.mifos.core.designsystem.theme.White
import com.mifos.core.objects.user.UserLatLng
import com.mifos.core.objects.user.UserLocation
import com.mifos.feature.path.tracking.R

@Composable
fun PathTrackingScreen(
Expand Down Expand Up @@ -145,33 +145,37 @@ fun PathTrackingScreen(
refreshing = refreshState,
onRefresh = onRefresh
)
val permissionState = rememberMultiplePermissionsState(
permissions = listOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
var checkPermission by remember { mutableStateOf(false) }


if (checkPermission) {
PermissionBox(
requiredPermissions = listOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
),
title = R.string.feature_path_tracking_permission_required,
description = R.string.feature_path_tracking_approve_permission_description_location,
confirmButtonText = R.string.feature_path_tracking_proceed,
dismissButtonText = R.string.feature_path_tracking_dismiss,
onGranted = {
updateUserStatus(true)
}
)
)
}

MifosScaffold(
icon = MifosIcons.arrowBack,
title = stringResource(id = R.string.feature_path_tracking_track_my_path),
onBackPressed = onBackPressed,
actions = {
IconButton(
onClick = {
if (userStatus) {
// TODO stop Path Service
updateUserStatus(false)
} else {
permissionState.permissions.all { per ->
per.status.isGranted
}.let { allGranted ->
if (allGranted) {
// TODO Run Path Service
updateUserStatus(true)
} else {
permissionState.launchMultiplePermissionRequest()
}
}
checkPermission = true
}
}
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.mifos.core.common.utils.Resource
import com.mifos.core.datastore.PrefManager
import com.mifos.core.domain.use_cases.GetUserPathTrackingUseCase
import com.mifos.feature.path.tracking.R
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down
5 changes: 5 additions & 0 deletions feature/path-tracking/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@

<string name="feature_path_tracking_failed_to_load_path_tracking">Failed to load path tracking</string>
<string name="feature_path_tracking_no_path_tracking_found">No path tracking found</string>

<string name="feature_path_tracking_permission_required">Permissions Required</string>
<string name="feature_path_tracking_approve_permission_description_location">You need to approve these permissions in order to access Location.</string>
<string name="feature_path_tracking_proceed">Proceed</string>
<string name="feature_path_tracking_dismiss">Dismiss</string>
</resources>

0 comments on commit cde1986

Please sign in to comment.