Skip to content

Commit

Permalink
Upgrade PermissionDispatcher
Browse files Browse the repository at this point in the history
Reason for the change:

Based on the issue: permissions-dispatcher#779

Bug reason:

When upgrading Kotlin or base library versions, some libraries that use KotlinPoet may also be upgraded to version 1.11 or higher.

KotlinPoet 1.11.0 has the following release note:

• Fix: Enforce only allowed parameter modifiers in ParameterSpec (i.e. crossinline, vararg, and noinline).

For more details, please refer to: https://github.com/square/kotlinpoet/releases

When generating the PermissionRequest class for methods annotated with @NeedsPermission, the private constructor will cause an error.

Reproduction steps:

Upgrade KotlinPoet to 1.11.0
Add a new parameter "callback" to the MainActivity#showCamera method in the sample
Modification:

Do not pass the KModifier.*PRIVATE parameter
  • Loading branch information
chenglei01 committed Nov 10, 2023
1 parent 74b532b commit 778b91b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ANDROIDX_LIBRARY_VERSION= 1.0.0
LIFECYCLE_VERSION = 2.2.0
ARC_TESTING_VERSION = 1.1.1
JAVAPOET_VERSION = 1.9.0
KOTLINPOET_VERSION = 1.3.0
KOTLINPOET_VERSION = 1.11.0
JUNIT_VERSION = 4.12
MOCKITO_VERSION = 2.28.2
MOCKITO_KOTLIN_VERSION = 2.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class KotlinBaseProcessorUnit : KtProcessorUnit {

override fun createFile(rpe: RuntimePermissionsElement, requestCodeProvider: RequestCodeProvider): FileSpec {
return FileSpec.builder(rpe.packageName, rpe.generatedClassName)
.addComment(FILE_COMMENT)
.addFileComment(FILE_COMMENT, arrayOf<Any>())
.addAnnotation(createJvmNameAnnotation(rpe.generatedClassName))
.addProperties(createProperties(rpe, requestCodeProvider))
.addFunctions(createWithPermissionCheckFuns(rpe))
Expand Down Expand Up @@ -449,7 +449,7 @@ abstract class KotlinBaseProcessorUnit : KtProcessorUnit {
val targetParam = "target"
val constructorSpec = FunSpec.constructorBuilder().addParameter(targetParam, rpe.ktTypeName)
needsMethod.parameters.forEach {
constructorSpec.addParameter(it.simpleString(), it.asPreparedType(), KModifier.PRIVATE)
constructorSpec.addParameter(it.simpleString(), it.asPreparedType())
}
builder.primaryConstructor(constructorSpec.build())

Expand Down
10 changes: 10 additions & 0 deletions sample/src/main/kotlin/permissions/dispatcher/sample/Callback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package permissions.dispatcher.sample

/**
* @author chenglei01
* @date 2023/11/10
* @time 12:03
*/
interface Callback {
fun onSuccess()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
val buttonCamera: Button = findViewById(R.id.button_camera)
buttonCamera.setOnClickListener {
showCameraWithPermissionCheck()
showCameraWithPermissionCheck(object : Callback {
override fun onSuccess() {}
})
}
val buttonContacts: Button = findViewById(R.id.button_contacts)
buttonContacts.setOnClickListener {
Expand All @@ -34,12 +36,13 @@ class MainActivity : AppCompatActivity() {
}

@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
fun showCamera(callback: Callback) {
// NOTE: Perform action that requires the permission. If this is run by PermissionsDispatcher, the permission will have been granted
supportFragmentManager.beginTransaction()
.replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
.addToBackStack("camera")
.commitAllowingStateLoss()
callback.onSuccess()
}

@OnPermissionDenied(Manifest.permission.CAMERA)
Expand All @@ -58,6 +61,7 @@ class MainActivity : AppCompatActivity() {

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

Expand Down

0 comments on commit 778b91b

Please sign in to comment.