Skip to content

Instantly share code, notes, and snippets.

View oussama-dz's full-sized avatar

CHERFAOUI Oussama oussama-dz

View GitHub Profile
@oussama-dz
oussama-dz / GetCurrentLocationUI.kt
Last active May 31, 2023 09:55
A jetpack compose UI for testing the location service implementation.
@Composable
fun GetCurrentLocationUI() {
var currentLocation by remember {
mutableStateOf("")
}
val scope = rememberCoroutineScope()
val context = LocalContext.current
val permissionRequest = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions(),
@oussama-dz
oussama-dz / LocationService.kt
Created May 31, 2023 07:01
A Location service class that get the current user location, and handle any exceptions that maybe thrown.
class LocationService {
@SuppressLint("MissingPermission")
suspend fun getCurrentLocation(context: Context): Location {
if (!context.hasPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
) {
@oussama-dz
oussama-dz / AndroidManifest.xml
Created May 30, 2023 18:39
Permissions to get the precise user location for android apps.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
@oussama-dz
oussama-dz / build.gradle.GRADLE
Created May 30, 2023 18:15
Needed dependencies to get the current user location using kotlin coroutines in android.
implementation 'com.google.android.gms:play-services-location:21.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.6.4'
@oussama-dz
oussama-dz / MainActivity.kt
Created May 23, 2023 18:13
Calling the permission composable in the main activity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PermissionHandlingTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
@oussama-dz
oussama-dz / Permission.kt
Last active May 23, 2023 19:07
A composable function for requesting a single permission and multiple permissions.
@Composable
fun Permissions() {
val activity = LocalContext.current as Activity
val permissionDialog = remember {
mutableStateListOf<NeededPermission>()
}
val microphonePermissionLauncher = rememberLauncherForActivityResult(
@oussama-dz
oussama-dz / PermissionAlertDialog.kt
Created May 23, 2023 17:40
An alert dialog composable to be shown when the user refuses to grant a permission.
@Composable
fun PermissionAlertDialog(
neededPermission: NeededPermission,
isPermissionDeclined: Boolean,
onDismiss: () -> Unit,
onOkClick: () -> Unit,
onGoToAppSettingsClick: () -> Unit,
) {
AlertDialog(
@oussama-dz
oussama-dz / NeededPermission.kt
Last active May 23, 2023 19:06
An enum class that define all the needed permissions for the app.
enum class NeededPermission(
val permission: String,
val title: String,
val description: String,
val permanentlyDeniedDescription: String,
) {
COARSE_LOCATION(
permission = android.Manifest.permission.ACCESS_COARSE_LOCATION,
title = "Approximate Location Permission",
description = "This permission is needed to get your approximate location. Please grant the permission.",
@oussama-dz
oussama-dz / AndroidManifest.xml
Last active May 23, 2023 19:07
requesting permissions in the android manifest file.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>