Skip to content

Instantly share code, notes, and snippets.

@elifry
Created October 3, 2021 21:52
Show Gist options
  • Save elifry/648b14b47aed8fbd40a3d76295d6567c to your computer and use it in GitHub Desktop.
Save elifry/648b14b47aed8fbd40a3d76295d6567c to your computer and use it in GitHub Desktop.
Kotlin wrapper for checking multiple permissions
val hasPermissions: Boolean
get() {
return ArePermissionsEnabled fromList stupidList
}
//**/
/ * This is a helper class used for checking permissions/
/ */
/ * Usage:/
/ * Check permissions permissionsList/
/ * ArePermissionsEnabled fromList permissionsList/
/ *//
class ArePermissionsEnabled {
companion object {
infix fun fromList(list: Array<String>) : Boolean {
var result: Int
val listPermissionsNeeded = ArrayList<String>()
for (p in MainActivity.PERMISSIONS) {
result = ContextCompat.checkSelfPermission(/appCtx/, p)
if (result != PackageManager./PERMISSION_GRANTED/) {
listPermissionsNeeded.add(p)
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(
requireActivity(),
listPermissionsNeeded./toTypedArray/(),
/MULTIPLE_PERMISSIONS/
)
return false
}
return true
}
}
}
val /hasPermissions/= fun(): Boolean {
return ArePermissionsEnabled fromList /PERMISSIONS/
}
const val /MULTIPLE_PERMISSIONS/= 10 // code you want.
var /PERMISSIONS/= /arrayOf/(
Manifest.permission./WRITE_EXTERNAL_STORAGE/,
Manifest.permission./ACCESS_COARSE_LOCATION/,
Manifest.permission./ACCESS_FINE_LOCATION/
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment