Skip to content

Commit

Permalink
feat: Add small utility extension functions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky authored Aug 26, 2023
1 parent ea3ba77 commit b8186cc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
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"
}
```
14 changes: 9 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ android {
}

dependencies {
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.11"
implementation "io.reactivex:rxjava:1.3.8"
implementation "io.reactivex:rxandroid:1.2.1"
implementation "org.jsoup:jsoup:1.15.3"
implementation "com.github.inorichi.injekt:injekt-core:65b0440"
compileOnly("com.squareup.okhttp3:okhttp:5.0.0-alpha.11")
compileOnly("io.reactivex:rxjava:1.3.8")
compileOnly("io.reactivex:rxandroid:1.2.1")
compileOnly("org.jsoup:jsoup:1.15.3")
compileOnly("com.github.inorichi.injekt:injekt-core:65b0440")
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
def json = "1.5.1"
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json:$json")
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json-okio:$json")
}

task androidSourcesJar(type: Jar) {
Expand Down
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 library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt
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)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import org.jsoup.nodes.Document
*/
fun Response.asJsoup(html: String? = null): Document {
throw Exception("Stub!")
}
}

0 comments on commit b8186cc

Please sign in to comment.