Permalink
6b4bbfe Jul 10, 2018
2 contributors

Users who have contributed to this file

@hotchemi @phicdy
80 lines (59 sloc) 2.68 KB

Fully Kotlin support

From 3.0.0 we started support .kt file generation.

Let's think about the same example as described in README.md.

0. Preparation

Add the following line to AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />

Include the following in your app module build.gradle file:

${latest.version} is Download

apply plugin: 'kotlin-kapt'

dependencies {
  implementation "com.github.hotchemi:permissionsdispatcher:${latest.version}"
  kapt "com.github.hotchemi:permissionsdispatcher-processor:${latest.version}"
}

1. Attach annotations

NOTE: Annotated methods must not be private.

@RuntimePermissions
class MainActivity : AppCompatActivity(), View.OnClickListener {

    @NeedsPermission(Manifest.permission.CAMERA)
    fun showCamera() {
        supportFragmentManager.beginTransaction()
                .replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
                .addToBackStack("camera")
                .commitAllowingStateLoss()
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    fun showRationaleForCamera(request: PermissionRequest) {
        showRationaleDialog(R.string.permission_camera_rationale, request)
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    fun onCameraDenied() {
        Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show()
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    fun onCameraNeverAskAgain() {
        Toast.makeText(this, R.string.permission_camera_never_askagain, Toast.LENGTH_SHORT).show()
    }
}

2. Delegate to generated functions

Now generated functions become much more concise and intuitive than Java version!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById(R.id.button_camera).setOnClickListener {
            // NOTE: delegate the permission handling to generated function
            showCameraWithPermissionCheck()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        // NOTE: delegate the permission handling to generated function
        onRequestPermissionsResult(requestCode, grantResults)
    }

Check out the sample for more details.