android / android-ktx

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

Convenience DSL for Media Controller Callback event handling #574

Open dpmedeiros opened 6 years ago

dpmedeiros commented 6 years ago

Hi,

Internally for our project I have written a simple Kotlin DSL to make dealing with state change events in media controller callbacks simpler and easier to write/read.

As an example, the DSL allows you to do things like the following:

val canceler = session.whenSession { // start configuring a listener for a media session
    stateChanges { state -> // whenever the state changes...
        configureUIForState(state) // ...update the UI for the new state
    }

   startsPlaying { // when playing state is entered...
        onStartPlaying() //...handle it
   }

   metadataChanges { metadata -> // when metadata for the session changes...
       setupUI(metadata) // ...update the UI
    }

    positionChanges { position -> // when media position changes...
        updateSeekProgress(position) // ...update seek progress
    }
}
// ... some time later ...
canceler.stopListening() // stop listening to events

A first draft implementation (missing features that could be added and unit tests) is here

I'm wondering if you will consider this an appropriate addition to ktx and would like to get your feedback before I put more work into it.

I do however realize you are not yet supporting new APIs for the support library