Last active
April 27, 2020 06:15
-
-
Save dmitriy-chernysh/47292d2699a79166d7a0dc4bd2c81547 to your computer and use it in GitHub Desktop.
Check runtime permissions in ViewModel
This file contains hidden or 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 VehicleDetailsFragmentKt : BaseFragment() { | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
observeLiveData() | |
return inflater.inflate(getLayoutResId(), container, false); | |
} | |
private fun observeLiveData() { | |
vehicleViewModel.eventRequestPermission.observe(viewLifecycleOwner, Observer { | |
it.getContentIfNotHandled()?.let { permission -> | |
when (permission.requestCode) { | |
Constants.REQUEST_CODE_CAMERA_PERMISSION -> | |
//see PermissionExtension.kt | |
requestRuntimePermissions(permission.requestCode) | |
} | |
} | |
}) | |
} | |
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
var isGranted = false | |
for (result in grantResults) { | |
if (result == PackageManager.PERMISSION_GRANTED) { | |
isGranted = true | |
} else { | |
isGranted = false | |
break | |
} | |
} | |
when (requestCode) { | |
Constants.REQUEST_CODE_CAMERA_PERMISSION -> { | |
//see ViewModel.kt | |
vehicleViewModel.onEventRequestPermissionsResult(requestCode, isGranted) | |
} | |
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} | |
} | |
} |
This file contains hidden or 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
data class Permission(val requestCode: Int) { | |
var isGranted: Boolean | |
set(value) { | |
if (value) grantedPermissions[this.requestCode] = this | |
else grantedPermissions.remove(this.requestCode) | |
} | |
get() { | |
if (grantedPermissions.isNullOrEmpty()) return false | |
return grantedPermissions[this.requestCode] != null | |
} | |
} | |
/** | |
* It removes permissions from the list of already granted permissions | |
*/ | |
fun clearGrantedPermissions() { | |
grantedPermissions.clear() | |
} | |
fun Fragment.requestRuntimePermissions(requestCode: Int) { | |
when (requestCode) { | |
Constants.REQUEST_CODE_CAMERA_PERMISSION -> requestPermissions(permissionsCaptureVideo, requestCode) | |
} | |
} | |
private var grantedPermissions = HashMap<Int, Permission>() | |
private val permissionsCaptureVideo = | |
arrayOf( | |
Manifest.permission.CAMERA, | |
Manifest.permission.RECORD_AUDIO, | |
Manifest.permission.WRITE_EXTERNAL_STORAGE | |
) |
This file contains hidden or 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 VehicleDetailsViewModel : : BaseViewModel(), LifecycleObserver { | |
private val _eventRequestPermission = MutableLiveData<Event<Permission>>() | |
val eventRequestPermission: LiveData<Event<Permission>> = _eventRequestPermission | |
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) | |
fun onStopView() { | |
//clear granted permissions to check them again when view will be visible | |
clearGrantedPermissions() | |
} | |
//it calls from xml layout | |
fun onClickCaptureButton() { | |
//check are permissions to capture video and photo granted | |
var permissionRequestCode = Constants.REQUEST_CODE_CAMERA_PERMISSION | |
var permission = Permission(permissionRequestCode) | |
if (!permission.isGranted) { | |
//ask permission | |
_eventRequestPermission.value = Event(permission) | |
return | |
} | |
//check is permission to draw overlays granted | |
permissionRequestCode = Constants.REQUEST_CODE_DRAW_OVER_APPS_PERMISSION | |
permission = Permission(permissionRequestCode) | |
if (!permission.isGranted) { | |
//ask permission | |
_eventRequestPermission.value = Event(permission) | |
return | |
} | |
//show menu to choose video or photo capture | |
showCaptureOptions() | |
} | |
fun onEventRequestPermissionsResult(requestCode: Int, isGranted: Boolean) { | |
Permission(requestCode).isGranted = isGranted | |
when (requestCode) { | |
Constants.REQUEST_CODE_CAMERA_PERMISSION -> { | |
if (!isGranted) { | |
//show alert | |
_eventNavigateTo.value = Event(Navigation(NavigateTo.ALERT_CAPTURE_PERMISSION_DENIED)) | |
} else { | |
//continue with capture | |
onClickCaptureButton() | |
} | |
} | |
} | |
} | |
private fun showCaptureOptions() { | |
//do something here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment