mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-27 12:00:03 +03:00
Make permission utils instantiable and throw when permission request is null
This commit is contained in:
parent
580bbd60e7
commit
6ec6d41aa9
2 changed files with 21 additions and 17 deletions
|
@ -72,6 +72,7 @@ class WidgetFragment @Inject constructor() :
|
|||
|
||||
private val fragmentArgs: WidgetArgs by args()
|
||||
private val viewModel: WidgetViewModel by activityViewModel()
|
||||
private val permissionUtils = WebviewPermissionUtils()
|
||||
|
||||
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomWidgetBinding {
|
||||
return FragmentRoomWidgetBinding.inflate(inflater, container, false)
|
||||
|
@ -277,16 +278,17 @@ class WidgetFragment @Inject constructor() :
|
|||
}
|
||||
|
||||
private val permissionResultLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
|
||||
WebviewPermissionUtils.onPermissionResult(result)
|
||||
permissionUtils.onPermissionResult(result)
|
||||
}
|
||||
|
||||
override fun onPermissionRequest(request: PermissionRequest) {
|
||||
WebviewPermissionUtils.promptForPermissions(
|
||||
permissionUtils.promptForPermissions(
|
||||
title = R.string.room_widget_resource_permission_title,
|
||||
request = request,
|
||||
context = requireContext(),
|
||||
activity = requireActivity(),
|
||||
activityResultLauncher = permissionResultLauncher)
|
||||
activityResultLauncher = permissionResultLauncher
|
||||
)
|
||||
}
|
||||
|
||||
private fun displayTerms(displayTerms: WidgetViewEvents.DisplayTerms) {
|
||||
|
|
|
@ -25,8 +25,9 @@ import androidx.fragment.app.FragmentActivity
|
|||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.utils.checkPermissions
|
||||
import java.lang.NullPointerException
|
||||
|
||||
object WebviewPermissionUtils {
|
||||
class WebviewPermissionUtils {
|
||||
|
||||
private var permissionRequest: PermissionRequest? = null
|
||||
private var selectedPermissions = listOf<String>()
|
||||
|
@ -73,7 +74,9 @@ object WebviewPermissionUtils {
|
|||
}
|
||||
|
||||
fun onPermissionResult(result: Map<String, Boolean>) {
|
||||
permissionRequest?.let { request ->
|
||||
if (permissionRequest == null) {
|
||||
throw NullPointerException("permissionRequest was null! Make sure to call promptForPermissions first.")
|
||||
}
|
||||
val grantedPermissions = selectedPermissions.filter { webPermission ->
|
||||
val androidPermission = webPermissionToAndroidPermission(webPermission)
|
||||
?: return@filter true // No corresponding Android permission exists
|
||||
|
@ -81,13 +84,12 @@ object WebviewPermissionUtils {
|
|||
?: return@filter true // Android permission already granted before
|
||||
}
|
||||
if (grantedPermissions.isNotEmpty()) {
|
||||
request.grant(grantedPermissions.toTypedArray())
|
||||
permissionRequest?.grant(grantedPermissions.toTypedArray())
|
||||
} else {
|
||||
request.deny()
|
||||
permissionRequest?.deny()
|
||||
}
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
private fun reset() {
|
||||
permissionRequest = null
|
||||
|
|
Loading…
Reference in a new issue