Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,49 @@ object DataStore {
}

fun <T : Any> Context.getKey(path: String, valueType: Class<T>): T? {
try {
return try {
val json: String = getSharedPrefs().getString(path, null) ?: return null
return parseJson(json, valueType.kotlin)
} catch (e: Exception) {
return null
parseJson(json, valueType.kotlin)
} catch (_: Exception) {
null
}
}

fun <T> Context.setKey(folder: String, path: String, value: T) {
setKey(getFolderName(folder, path), value)
}

@Deprecated(
message = "Use parseJson<T>(this) directly instead.",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith(
expression = "parseJson<T>(this)",
imports = ["com.lagradost.cloudstream3.utils.AppUtils.parseJson"],
),
)
inline fun <reified T : Any> String.toKotlinObject(): T {

@fire-light42 fire-light42 Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we keep this extension function? It should not cause type erasure and it could be renamed for clarity.

Having extension functions helps with chained calls such as:

text.lowercase()
    .substringAfter("[")
    .substringBefore("]")
    .trim()
    .parseJson<>()

And it would be a good complement to toJson()

@Luna712 Luna712 Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still deprecate this and add the new version to AppUtils then I think. I am a little busy IRL now to work on things though but should be back more next week again.

return parseJson(this)
}

@Deprecated(
message = "Use parseJson<T>(this) directly instead.",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith(
expression = "parseJson<T>(this)",
imports = ["com.lagradost.cloudstream3.utils.AppUtils.parseJson"],
),
)
fun <T : Any> String.toKotlinObject(valueType: Class<T>): T {
return parseJson(this, valueType.kotlin)
}

// GET KEY GIVEN PATH AND DEFAULT VALUE, NULL IF ERROR
inline fun <reified T : Any> Context.getKey(path: String, defVal: T?): T? {
try {
return try {
val json: String = getSharedPrefs().getString(path, null) ?: return defVal
return json.toKotlinObject()
} catch (e: Exception) {
return null
parseJson<T>(json)
} catch (_: Exception) {
null
}
}

Expand Down