forked from tachiyomiorg/extensions-lib
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add small utility extension functions (#8)
- Loading branch information
1 parent
ea3ba77
commit b8186cc
Showing
5 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
The extensions library used in Tachiyomi. | ||
The extensions library used in Aniyomi. | ||
|
||
``` | ||
ext { | ||
libVersion = '13' | ||
libVersion = '14' | ||
} | ||
dependencies { | ||
compileOnly "com.github.tachiyomiorg:extensions-lib:$libVersion" | ||
compileOnly "com.github.aniyomiorg:extensions-lib:$libVersion" | ||
} | ||
``` |
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
36 changes: 36 additions & 0 deletions
36
library/src/main/java/eu/kanade/tachiyomi/util/CoroutinesExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package eu.kanade.tachiyomi.util | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.runBlocking | ||
|
||
inline fun <A, B> Iterable<A>.parallelMap(crossinline f: suspend (A) -> B): List<B> = | ||
runBlocking { | ||
map { async(Dispatchers.Default) { f(it) } }.awaitAll() | ||
} | ||
|
||
inline fun <A, B> Iterable<A>.parallelMapNotNull(crossinline f: suspend (A) -> B?): List<B> = | ||
runBlocking { | ||
map { async(Dispatchers.Default) { f(it) } }.awaitAll().filterNotNull() | ||
} | ||
|
||
inline fun <A, B> Iterable<A>.parallelMapIndexed(crossinline f: suspend (index: Int, A) -> B): List<B> = | ||
runBlocking { | ||
mapIndexed { index, it -> async(Dispatchers.Default) { f(index, it) } }.awaitAll() | ||
} | ||
|
||
inline fun <A, B> Iterable<A>.parallelMapIndexedNotNull(crossinline f: suspend (index: Int, A) -> B?): List<B> = | ||
runBlocking { | ||
mapIndexed { index, it -> async(Dispatchers.Default) { f(index, it) } }.awaitAll().filterNotNull() | ||
} | ||
|
||
inline fun <A, B> Iterable<A>.parallelFlatMap(crossinline f: suspend (A) -> Iterable<B>): List<B> = | ||
runBlocking { | ||
map { async(Dispatchers.Default) { f(it) } }.awaitAll().flatten() | ||
} | ||
|
||
inline fun <A, B> Iterable<A>.parallelFlatMapIndexed(crossinline f: suspend (index: Int, A) -> Iterable<B>): List<B> = | ||
runBlocking { | ||
mapIndexed { index, it -> async(Dispatchers.Default) { f(index, it) } }.awaitAll().flatten() | ||
} |
27 changes: 27 additions & 0 deletions
27
library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package eu.kanade.tachiyomi.util | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.okio.decodeFromBufferedSource | ||
import kotlinx.serialization.serializer | ||
import okhttp3.Response | ||
import uy.kohesive.injekt.Injekt | ||
import uy.kohesive.injekt.api.get | ||
|
||
inline fun <reified T> Response.parseAs(transform: (String) -> String): T { | ||
val responseBody = use { transform(it.body.string()) } | ||
return Injekt.get<Json>().decodeFromString(responseBody) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
inline fun <reified T> Response.parseAs(): T = use { res -> | ||
res.body.source().use { | ||
Injekt.get<Json>().decodeFromBufferedSource(serializer(), it) | ||
} | ||
} | ||
|
||
inline fun <reified T> String.parseAs(transform: (String) -> String): T = | ||
Injekt.get<Json>().decodeFromString(transform(this)) | ||
|
||
inline fun <reified T> String.parseAs(): T = Injekt.get<Json>().decodeFromString(this) |
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