From 1828ec398c3042ce2417b9cf270fd5ee90a87fec Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Sat, 23 May 2026 19:03:50 -0600 Subject: [PATCH] Improve ContextHelper API for KMP --- .../com/lagradost/api/ContextHelper.android.kt | 15 +++++---------- .../kotlin/com/lagradost/api/ContextHelper.kt | 11 +++++------ .../kotlin/com/lagradost/api/ContextHelper.jvm.kt | 10 ++-------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/library/src/androidMain/kotlin/com/lagradost/api/ContextHelper.android.kt b/library/src/androidMain/kotlin/com/lagradost/api/ContextHelper.android.kt index 5e5897deb2d..6ace780ecb6 100644 --- a/library/src/androidMain/kotlin/com/lagradost/api/ContextHelper.android.kt +++ b/library/src/androidMain/kotlin/com/lagradost/api/ContextHelper.android.kt @@ -3,19 +3,14 @@ package com.lagradost.api import android.content.Context import java.lang.ref.WeakReference -var ctx: WeakReference? = null +private var contextRef: WeakReference? = null /** * Helper function for Android specific context. Not usable in JVM. * Do not use this unless absolutely necessary. */ -actual fun getContext(): Any? { - return ctx?.get() -} +actual fun getContext(): Any? = contextRef?.get() -actual fun setContext(context: WeakReference) { - val actualContext = context.get() as? Context - if (actualContext != null) { - ctx = WeakReference(actualContext) - } -} \ No newline at end of file +actual fun setContext(context: Any?) { + contextRef = (context as? Context)?.let { WeakReference(it) } +} diff --git a/library/src/commonMain/kotlin/com/lagradost/api/ContextHelper.kt b/library/src/commonMain/kotlin/com/lagradost/api/ContextHelper.kt index fb54e3ca944..451ec80e841 100644 --- a/library/src/commonMain/kotlin/com/lagradost/api/ContextHelper.kt +++ b/library/src/commonMain/kotlin/com/lagradost/api/ContextHelper.kt @@ -1,16 +1,15 @@ package com.lagradost.api -import java.lang.ref.WeakReference - /** - * Set context for android specific code such as webview. - * Does nothing on JVM. + * Set context for Android-specific code such as webview. + * Does nothing on non-Android platforms. */ -expect fun setContext(context: WeakReference) +expect fun setContext(context: Any?) + /** * Helper function for Android specific context. * Do not use this unless absolutely necessary. * setContext() must be called before this is called. - * @return Context if on android, null if not. + * @return Context if on Android, null if not. */ expect fun getContext(): Any? diff --git a/library/src/jvmMain/kotlin/com/lagradost/api/ContextHelper.jvm.kt b/library/src/jvmMain/kotlin/com/lagradost/api/ContextHelper.jvm.kt index a30810b89a4..83cae11a5c6 100644 --- a/library/src/jvmMain/kotlin/com/lagradost/api/ContextHelper.jvm.kt +++ b/library/src/jvmMain/kotlin/com/lagradost/api/ContextHelper.jvm.kt @@ -1,10 +1,4 @@ package com.lagradost.api -import java.lang.ref.WeakReference - -actual fun getContext(): Any? { - return null -} - -actual fun setContext(context: WeakReference) { -} \ No newline at end of file +actual fun getContext(): Any? = null +actual fun setContext(context: Any?) = Unit