This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun getPermissionState(activity: Activity, result: Map<String, Boolean>): PermissionState { | |
val deniedPermissions = result.filterValues { !it }.keys | |
return when { | |
deniedPermissions.isEmpty() -> PermissionState.Granted | |
deniedPermissions.any { permission -> | |
!shouldShowRequestPermissionRationale( | |
/* activity = */ | |
activity, | |
/* permission = */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Fragment.registerPermission(onPermissionResult: (PermissionState) -> Unit): Permission { | |
return Permission( | |
this.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { | |
onPermissionResult(getPermissionState(activity, it)) | |
} | |
) | |
} | |
fun AppCompatActivity.registerPermission(onPermissionResult: (PermissionState) -> Unit): Permission { | |
return Permission( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JvmInline | |
value class Permission(val result: ActivityResultLauncher<Array<String>>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class PermissionState { | |
object Granted : PermissionState() | |
object Denied : PermissionState() | |
object PermanentlyDenied : PermissionState() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Activity and the permission is needed | |
val isPermanentluDenied= shouldShowRequestPermissionRationale(activity, android.Manifest.permission.CAMERA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a permission request | |
private val cameraPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> | |
if(granted){ | |
} | |
} | |
//launch it | |
cameraPermission.launch(android.Manifest.permission.CAMERA) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.CAMERA"/> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FlowObserver<T> ( | |
lifecycleOwner: LifecycleOwner, | |
private val flow: Flow<T>, | |
private val collector: suspend (T) -> Unit | |
) { | |
private var job: Job? = null | |
init { | |
lifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.downloadmanager | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.os.Bundle | |
import android.os.Environment | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.ContextCompat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.lagina.utils | |
import android.content.Context | |
import android.content.ContextWrapper | |
import android.content.res.Configuration | |
import android.content.res.Resources | |
import android.os.Build | |
import android.os.LocaleList | |
import java.util.* |