Several crashfixes

This commit is contained in:
Blatzar 2022-10-19 00:14:15 +02:00
parent f49d9de09b
commit 6a5286e363
3 changed files with 24 additions and 11 deletions

View file

@ -16,6 +16,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi
import com.lagradost.cloudstream3.ui.player.SubtitleData import com.lagradost.cloudstream3.ui.player.SubtitleData
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.AppUtils.toJson 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.ExtractorLink
import com.lagradost.cloudstream3.utils.SubtitleHelper import com.lagradost.cloudstream3.utils.SubtitleHelper
import okhttp3.Interceptor import okhttp3.Interceptor
@ -39,7 +40,7 @@ object APIHolder {
private const val defProvider = 0 private const val defProvider = 0
// ConcurrentModificationException is possible!!! // ConcurrentModificationException is possible!!!
val allProviders: MutableList<MainAPI> = arrayListOf() val allProviders = threadSafeListOf<MainAPI>()
fun initAll() { fun initAll() {
for (api in allProviders) { for (api in allProviders) {
@ -52,7 +53,7 @@ object APIHolder {
return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
} }
var apis: List<MainAPI> = arrayListOf() var apis: List<MainAPI> = threadSafeListOf()
var apiMap: Map<String, Int>? = null var apiMap: Map<String, Int>? = null
fun addPluginMapping(plugin: MainAPI) { fun addPluginMapping(plugin: MainAPI) {
@ -72,16 +73,19 @@ object APIHolder {
fun getApiFromNameNull(apiName: String?): MainAPI? { fun getApiFromNameNull(apiName: String?): MainAPI? {
if (apiName == null) return null if (apiName == null) return null
synchronized(allProviders) {
initMap() initMap()
return apiMap?.get(apiName)?.let { apis.getOrNull(it) } return apiMap?.get(apiName)?.let { apis.getOrNull(it) }
?: allProviders.firstOrNull { it.name == apiName } ?: allProviders.firstOrNull { it.name == apiName }
} }
}
fun getApiFromUrlNull(url: String?): MainAPI? { fun getApiFromUrlNull(url: String?): MainAPI? {
if (url == null) return null if (url == null) return null
for (api in allProviders) { synchronized(allProviders) {
if (url.startsWith(api.mainUrl)) allProviders.forEach { api ->
return api if (url.startsWith(api.mainUrl)) return api
}
} }
return null return null
} }

View file

@ -10,6 +10,7 @@ import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.Coroutines.mainWork import com.lagradost.cloudstream3.utils.Coroutines.mainWork
import com.lagradost.cloudstream3.utils.Coroutines.threadSafeListOf
import com.lagradost.nicehttp.requestCreator import com.lagradost.nicehttp.requestCreator
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -96,7 +97,7 @@ class WebViewResolver(
} }
var fixedRequest: Request? = null var fixedRequest: Request? = null
val extraRequestList = mutableListOf<Request>() val extraRequestList = threadSafeListOf<Request>()
main { main {
// Useful for debugging // Useful for debugging

View file

@ -4,9 +4,8 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import com.lagradost.cloudstream3.mvvm.launchSafe import com.lagradost.cloudstream3.mvvm.launchSafe
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.Collections.synchronizedList
object Coroutines { object Coroutines {
fun <T> T.main(work: suspend ((T) -> Unit)): Job { fun <T> T.main(work: suspend ((T) -> Unit)): Job {
@ -56,4 +55,13 @@ object Coroutines {
work() 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 <T> threadSafeListOf(vararg items: T): MutableList<T> {
return synchronizedList(items.toMutableList())
}
} }