@@ -4,9 +4,9 @@ import android.content.Context
44import android.content.SharedPreferences
55import androidx.datastore.core.DataStore
66import androidx.datastore.preferences.core.Preferences
7+ import androidx.datastore.preferences.core.edit
78import androidx.datastore.preferences.core.stringPreferencesKey
89import androidx.datastore.preferences.preferencesDataStore
9- import androidx.datastore.preferences.core.edit
1010import com.facebook.react.bridge.Promise
1111import com.facebook.react.bridge.ReactApplicationContext
1212import com.facebook.react.bridge.ReadableMap
@@ -28,11 +28,11 @@ import java.io.FileOutputStream
2828import java.io.IOException
2929import java.io.InputStream
3030
31- class ApiClientModuleImpl (reactApplicationContext : ReactApplicationContext ) {
31+ class ApiClientModuleImpl (appContext : Context ) {
3232 companion object {
3333 const val NAME = " ApiClient"
3434
35- lateinit var context: ReactApplicationContext
35+ lateinit var context: Context
3636 private val clients = mutableMapOf<HttpUrl , NetworkClient >()
3737 private val calls = mutableMapOf<String , Call >()
3838 private lateinit var sharedPreferences: SharedPreferences
@@ -93,17 +93,18 @@ class ApiClientModuleImpl(reactApplicationContext: ReactApplicationContext) {
9393 }
9494
9595 internal fun sendJSEvent (eventName : String , data : WritableMap ? ) {
96- if (context.hasActiveReactInstance()) {
97- context.emitDeviceEvent(eventName, data)
96+ val reactApplicationContext = context as ? ReactApplicationContext
97+ if (reactApplicationContext?.hasActiveReactInstance() == true ) {
98+ reactApplicationContext.emitDeviceEvent(eventName, data)
9899
99100 }
100101 }
101102
102- private fun setCtx (reactContext : ReactApplicationContext ) {
103+ private fun setCtx (reactContext : Context ) {
103104 context = reactContext
104105 }
105106
106- private fun migrateSharedPreferences (reactContext : ReactApplicationContext ) {
107+ private fun migrateSharedPreferences (reactContext : Context ) {
107108 val sharedPreferences = reactContext.getSharedPreferences(SHARED_PREFERENCES_NAME , Context .MODE_PRIVATE )
108109
109110 if (sharedPreferences.all.isNotEmpty()) {
@@ -122,16 +123,19 @@ class ApiClientModuleImpl(reactApplicationContext: ReactApplicationContext) {
122123 }
123124 }
124125
125- private fun setCookieJar (reactContext : ReactApplicationContext ) {
126- val cookieHandler = ForwardingCookieHandler (reactContext)
127- cookieJar.setCookieJar(JavaNetCookieJar (cookieHandler))
126+ private fun setCookieJar (reactContext : Context ) {
127+ val reactApplicationContext = reactContext as ? ReactApplicationContext
128+ if (reactApplicationContext?.hasActiveReactInstance() == true ) {
129+ val cookieHandler = ForwardingCookieHandler (reactApplicationContext)
130+ cookieJar.setCookieJar(JavaNetCookieJar (cookieHandler))
131+ }
128132 }
129133 }
130134
131135 init {
132- setCtx(reactApplicationContext )
133- migrateSharedPreferences(reactApplicationContext )
134- setCookieJar(reactApplicationContext )
136+ setCtx(appContext )
137+ migrateSharedPreferences(appContext )
138+ setCookieJar(appContext )
135139 }
136140
137141 fun createClientFor (baseUrl : String , options : ReadableMap , promise : Promise ) {
0 commit comments