Skip to content

Instantly share code, notes, and snippets.

@nesterchung
Created May 21, 2018 07:30
Show Gist options
  • Save nesterchung/6e6edb5b8c5eb67d18e89e0340d23f20 to your computer and use it in GitHub Desktop.
Save nesterchung/6e6edb5b8c5eb67d18e89e0340d23f20 to your computer and use it in GitHub Desktop.
kotlin hepler function for RemoteCallbackList
inline fun <E : IInterface?> RemoteCallbackList<E>.forEach(f: (E) -> Unit) {
var i = beginBroadcast()
while (i > 0) {
i--
try {
f(getBroadcastItem(i))
} catch (e: RemoteException) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
finishBroadcast()
}
inline fun <E : IInterface?, R> RemoteCallbackList<E>.map(f: (E) -> R): List<R> {
val ret = mutableListOf<R>()
mapTo(ret) { f(it) }
return ret
}
inline fun <E : IInterface?, R, C : MutableCollection<R>> RemoteCallbackList<E>.mapTo(c: C, f: (E) -> R) {
forEach {
c.add(f(it))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment