diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt index e5896434..940d3391 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt @@ -16,6 +16,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi import com.lagradost.cloudstream3.ui.player.SubtitleData import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.utils.AppUtils.toJson +import com.lagradost.cloudstream3.utils.Coroutines.threadSafeListOf import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.SubtitleHelper import okhttp3.Interceptor @@ -39,7 +40,7 @@ object APIHolder { private const val defProvider = 0 // ConcurrentModificationException is possible!!! - val allProviders: MutableList = arrayListOf() + val allProviders = threadSafeListOf() fun initAll() { for (api in allProviders) { @@ -52,7 +53,7 @@ object APIHolder { return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } } - var apis: List = arrayListOf() + var apis: List = threadSafeListOf() var apiMap: Map? = null fun addPluginMapping(plugin: MainAPI) { @@ -72,16 +73,19 @@ object APIHolder { fun getApiFromNameNull(apiName: String?): MainAPI? { if (apiName == null) return null - initMap() - return apiMap?.get(apiName)?.let { apis.getOrNull(it) } - ?: allProviders.firstOrNull { it.name == apiName } + synchronized(allProviders) { + initMap() + return apiMap?.get(apiName)?.let { apis.getOrNull(it) } + ?: allProviders.firstOrNull { it.name == apiName } + } } fun getApiFromUrlNull(url: String?): MainAPI? { if (url == null) return null - for (api in allProviders) { - if (url.startsWith(api.mainUrl)) - return api + synchronized(allProviders) { + allProviders.forEach { api -> + if (url.startsWith(api.mainUrl)) return api + } } return null } diff --git a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt index 538b96f8..7b4343d9 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt @@ -10,6 +10,7 @@ import com.lagradost.cloudstream3.app import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.mainWork +import com.lagradost.cloudstream3.utils.Coroutines.threadSafeListOf import com.lagradost.nicehttp.requestCreator import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking @@ -96,7 +97,7 @@ class WebViewResolver( } var fixedRequest: Request? = null - val extraRequestList = mutableListOf() + val extraRequestList = threadSafeListOf() main { // Useful for debugging diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/Coroutines.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/Coroutines.kt index 1b4c36f7..c3b244c2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/Coroutines.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/Coroutines.kt @@ -4,9 +4,8 @@ import android.os.Handler import android.os.Looper import com.lagradost.cloudstream3.mvvm.launchSafe import com.lagradost.cloudstream3.mvvm.logError -import com.lagradost.cloudstream3.utils.Coroutines.ioSafe -import com.lagradost.cloudstream3.utils.Coroutines.main import kotlinx.coroutines.* +import java.util.Collections.synchronizedList object Coroutines { fun T.main(work: suspend ((T) -> Unit)): Job { @@ -56,4 +55,13 @@ object Coroutines { work() } } + + /** + * Safe to add and remove how you want + * If you want to iterate over the list then you need to do: + * synchronized(allProviders) { code here } + **/ + fun threadSafeListOf(vararg items: T): MutableList { + return synchronizedList(items.toMutableList()) + } } \ No newline at end of file