android / android-ktx

A set of Kotlin extensions for Android app development.
https://android.github.io/android-ktx/core-ktx/
7.47k stars 563 forks source link

Adding extensions for AudioEffect #543

Closed RamV13 closed 6 years ago

RamV13 commented 6 years ago

If you're planning on including extensions for the android.media package, I'd like to propose the extensions of doOnControlStatusChange and doOnEnableStatusChange to AudioEffect. The advantage here is leveraging the receiver parameter to avoid explicitly naming the effect parameter. Further, parameters can even be omitted altogether by simply utilizing the it syntax.

Before

equalizer.setControlStatusListener { effect, controlGranted ->
    if (controlGranted) {
        effect.enabled = true
    }
}

After

equalizer.doOnControlStatusChange { controlGranted ->
    if (controlGranted) {
        enabled = true
    }
}

* test convention for waiting on asynchronous callbacks adapted from AOSP

romainguy commented 6 years ago

Thanks, I like it but it looks like a more scalable solution would be to use this: https://kotlinlang.org/docs/reference/compiler-plugins.html#sam-with-receiver-compiler-plugin (we would have to annotated Android APIs but it wouldn't require new implementations/tests/etc. for every SAM we want to improve this way).

RamV13 commented 6 years ago

Makes sense!