permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.23k stars 1.45k forks source link

please add feature to show dialog everytime #501

Closed dtingz closed 6 years ago

dtingz commented 6 years ago

right now in just coping the the generated code and remove the logic of the showRationaleForLocation

if you can please provide us to this more easily it'll be wonderful, thanks

// This file was generated by PermissionsDispatcher. Do not modify!
@file:JvmName("TimeClockFragmentPermissionsDispatcher")

package il.co.manpower.app.module.main.home.timeclock

import permissions.dispatcher.PermissionRequest
import permissions.dispatcher.PermissionUtils
import java.lang.ref.WeakReference

private val REQUEST_STARTLOCATION: Int = 0

private val PERMISSION_STARTLOCATION: Array<String> = arrayOf("android.permission.ACCESS_FINE_LOCATION")

fun TimeClockFragment.startLocationWithPermissionCheck() {
    if (PermissionUtils.hasSelfPermissions(this.activity, *PERMISSION_STARTLOCATION)) {
        startLocation()
    } else {
        //if (PermissionUtils.shouldShowRequestPermissionRationale(this, *PERMISSION_STARTLOCATION)) {
            showRationaleForLocation(TimeClockFragmentStartLocationPermissionRequest(this))
        //} else {
            //this.requestPermissions(PERMISSION_STARTLOCATION, REQUEST_STARTLOCATION)
        //}
    }
}

fun TimeClockFragment.onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) {
    when (requestCode) {
        REQUEST_STARTLOCATION ->
        {
            if (PermissionUtils.verifyPermissions(*grantResults)) {
                startLocation()
            } else {
                if (!PermissionUtils.shouldShowRequestPermissionRationale(this, *PERMISSION_STARTLOCATION)) {
                    showNeverAskForLocation()
                } else {
                    showDeniedForLocation()
                }
            }
        }
    }
}

private class TimeClockFragmentStartLocationPermissionRequest(target: TimeClockFragment) : PermissionRequest {
    private val weakTarget: WeakReference<TimeClockFragment> = WeakReference(target)

    override fun proceed() {
        val target = weakTarget.get() ?: return
        target.requestPermissions(PERMISSION_STARTLOCATION, REQUEST_STARTLOCATION)
    }

    override fun cancel() {
        val target = weakTarget.get() ?: return
        target.showDeniedForLocation()
    }
}
hotchemi commented 6 years ago

You can do that on your side!

dtingz commented 6 years ago

how?