Created
August 16, 2022 23:38
-
-
Save ozgurg/092362eb7d3ba560f4750b9a1f2a3ef6 to your computer and use it in GitHub Desktop.
Useful Kotlin extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> MutableList<T>.addOrRemove(item: T) { | |
if (this.contains(item)) { | |
this.remove(item) | |
} else { | |
this.add(item) | |
} | |
} | |
fun <T> MutableList<T>.findIndex(predicate: (T) -> Boolean): Int { | |
val item = this.find(predicate) | |
return this.indexOf(item) | |
} | |
fun <R> CoroutineScope.asyncTask( | |
onPreExecute: () -> Unit, | |
doInBackground: () -> R, | |
onPostExecute: (R) -> Unit | |
) = launch { | |
onPreExecute() | |
val result = withContext(Dispatchers.IO) { | |
doInBackground() | |
} | |
onPostExecute(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment