Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ package com.lagradost.api
import android.content.Context
import java.lang.ref.WeakReference

var ctx: WeakReference<Context>? = null
private var contextRef: WeakReference<Context>? = 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<Any>) {
val actualContext = context.get() as? Context
if (actualContext != null) {
ctx = WeakReference(actualContext)
}
}
actual fun setContext(context: Any?) {
contextRef = (context as? Context)?.let { WeakReference(it) }
}
11 changes: 5 additions & 6 deletions library/src/commonMain/kotlin/com/lagradost/api/ContextHelper.kt
Original file line number Diff line number Diff line change
@@ -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<Any>)
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?
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
package com.lagradost.api

import java.lang.ref.WeakReference

actual fun getContext(): Any? {
return null
}

actual fun setContext(context: WeakReference<Any>) {
}
actual fun getContext(): Any? = null
actual fun setContext(context: Any?) = Unit