permissions-dispatcher / PermissionsDispatcher

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

Kotlin generation doesn't handle ()->Unit. #503

Closed shiehnpin closed 5 years ago

shiehnpin commented 6 years ago

FAQs


Overview

@NeedsPermission(Manifest.permission.CAMERA)
fun test(f:()->Unit){
    f.invoke()
}

Expected

Successfully rebuild and generate code to be invoked like this:

fun Activity.testWithPermissionCheck(f:()->Unit) {
…
}

Actual

I can't rebuild project and get following error:

Type mismatch: inferred type is kotlin.jvm.functions.Function0<Unit> but () -> Unit was expected

fun MainActivity.testWithPermissionCheck(f: Function0<Unit>) {
  if (PermissionUtils.hasSelfPermissions(this, *PERMISSION_TEST)) {
    test(f)
  } else {
    PENDING_TEST = MainActivityTestPermissionRequest(this, f)
    ActivityCompat.requestPermissions(this, PERMISSION_TEST, REQUEST_TEST)
  }
}
private class MainActivityTestPermissionRequest(target: MainActivity, private val f: Function0<Unit>) : GrantableRequest {
  private val weakTarget: WeakReference<MainActivity> = WeakReference(target)

  override fun proceed() {
    val target = weakTarget.get() ?: return
    ActivityCompat.requestPermissions(target, PERMISSION_TEST, REQUEST_TEST)
  }

  override fun cancel() {
  }

  override fun grant() {
    val target = weakTarget.get() ?: return
    target.test(f)
  }
}

Environment

4.0.0-alpha

hotchemi commented 6 years ago

This might be related to KotlinPoet again

shiehnpin commented 6 years ago

Just like issue #427 ?

hotchemi commented 6 years ago

👁 https://github.com/square/kotlinpoet/releases/tag/kotlinpoet-1.0.0-RC1

hotchemi commented 6 years ago

I've updated KotlinPoet to 1.0.0-RC1

hotchemi commented 5 years ago

Updated kotlinpoet to 1.0.0 but the issue hasn't been addressed🤔

hotchemi commented 5 years ago

@shiehnpin this issue is from KotlinPoet but in this case you should annotate argument f with NeedsPermission right?

hotchemi commented 5 years ago

let me close the issue since it's been a long time since opened.

JuliusHenke commented 3 years ago

This issue still persists when trying to use functions as parameters. As a workaround I defined a new class containing my callback functions and passed this class as the parameter:

class LocationCallbacks(
    val success: () -> Unit,
    val error: () -> Unit,
)

@NeedsPermission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
fun locationAction(callbacks: LocationCallbacks) {}