add helper utils (#7683)

* add some helper utilities

* adjust parameter
This commit is contained in:
AwkwardPeak7 2025-02-22 12:32:48 +05:00 committed by Draff
parent fd194f60f1
commit 7210f658e2
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
8 changed files with 114 additions and 0 deletions

View File

@ -38,6 +38,7 @@ kotlinter {
dependencies {
compileOnly(versionCatalogs.named("libs").findBundle("common").get())
implementation(project(":utils"))
}
tasks {

View File

@ -103,6 +103,7 @@ dependencies {
if (theme != null) implementation(theme) // Overrides core launcher icons
implementation(project(":core"))
compileOnly(libs.bundles.common)
implementation(project(":utils"))
}
tasks.register("writeManifestFile") {

View File

@ -1,4 +1,5 @@
include(":core")
include(":utils")
// Load all modules under /lib
File(rootDir, "lib").eachDir { include("lib:${it.name}") }

33
utils/build.gradle.kts Normal file
View File

@ -0,0 +1,33 @@
plugins {
id("com.android.library")
kotlin("android")
}
android {
compileSdk = AndroidConfig.compileSdk
defaultConfig {
minSdk = AndroidConfig.minSdk
}
namespace = "keiyoushi.utils"
sourceSets {
named("main") {
java.setSrcDirs(listOf("src"))
}
}
buildFeatures {
resValues = false
shaders = false
}
kotlinOptions {
freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
}
}
dependencies {
compileOnly(versionCatalogs.named("libs").findBundle("common").get())
}

View File

@ -0,0 +1,13 @@
package keiyoushi.utils
/**
* Returns the first element that is an instances of specified type parameter T.
*
* @throws [NoSuchElementException] if no such element is found.
*/
inline fun <reified T> Iterable<*>.firstInstance(): T = first { it is T } as T
/**
* Returns the first element that is an instances of specified type parameter T, or `null` if element was not found.
*/
inline fun <reified T> Iterable<*>.firstInstanceOrNull(): T? = firstOrNull { it is T } as? T

View File

@ -0,0 +1,15 @@
package keiyoushi.utils
import java.text.ParseException
import java.text.SimpleDateFormat
@Suppress("NOTHING_TO_INLINE")
inline fun SimpleDateFormat.tryParse(date: String?): Long {
date ?: return 0L
return try {
parse(date)?.time ?: 0L
} catch (_: ParseException) {
0L
}
}

View File

@ -0,0 +1,21 @@
package keiyoushi.utils
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import okhttp3.Response
import uy.kohesive.injekt.injectLazy
val jsonInstance: Json by injectLazy()
/**
* Parses and serializes the String as the type <T>.
*/
inline fun <reified T> String.parseAs(json: Json = jsonInstance): T =
json.decodeFromString(this)
/**
* Parse and serialize the response body as the type <T>.
*/
inline fun <reified T> Response.parseAs(json: Json = jsonInstance): T =
json.decodeFromStream(body.byteStream())

View File

@ -0,0 +1,29 @@
package keiyoushi.utils
import android.app.Application
import android.content.SharedPreferences
import eu.kanade.tachiyomi.source.online.HttpSource
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
/**
* Returns the [SharedPreferences] associated with current source id
*/
inline fun HttpSource.getPreferences(
migration: SharedPreferences.() -> Unit = { },
): SharedPreferences = getPreferences(id).also(migration)
/**
* Lazily returns the [SharedPreferences] associated with current source id
*/
inline fun HttpSource.getPreferencesLazy(
crossinline migration: SharedPreferences.() -> Unit = { }
) = lazy { getPreferences(migration) }
/**
* Returns the [SharedPreferences] associated with passed source id
*/
@Suppress("NOTHING_TO_INLINE")
inline fun getPreferences(sourceId: Long): SharedPreferences =
Injekt.get<Application>().getSharedPreferences("source_$sourceId", 0x0000)