Fix blocking operations in addon repository

- Replaced blocking map() with flatMapLatest() and flow builder
- Moved network operations to IO dispatcher using flowOn()
- Changed from imperative loop to functional mapNotNull()
- Prevents UI freezing when fetching multiple addons
- Improves responsiveness by running network calls on IO threads

This ensures that addon fetching doesn't block the main thread and
improves performance when loading multiple addons concurrently.
This commit is contained in:
CrissZollo 2026-02-01 23:28:35 +01:00
parent a5c6dbaa7c
commit a2cc1c0b69

View file

@ -7,8 +7,11 @@ import com.nuvio.tv.data.mapper.toDomain
import com.nuvio.tv.data.remote.api.AddonApi import com.nuvio.tv.data.remote.api.AddonApi
import com.nuvio.tv.domain.model.Addon import com.nuvio.tv.domain.model.Addon
import com.nuvio.tv.domain.repository.AddonRepository import com.nuvio.tv.domain.repository.AddonRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject import javax.inject.Inject
class AddonRepositoryImpl @Inject constructor( class AddonRepositoryImpl @Inject constructor(
@ -17,17 +20,16 @@ class AddonRepositoryImpl @Inject constructor(
) : AddonRepository { ) : AddonRepository {
override fun getInstalledAddons(): Flow<List<Addon>> = override fun getInstalledAddons(): Flow<List<Addon>> =
preferences.installedAddonUrls.map { urls -> preferences.installedAddonUrls.flatMapLatest { urls ->
val addons = mutableListOf<Addon>() flow {
val addons = urls.mapNotNull { url ->
for (url in urls) { when (val result = fetchAddon(url)) {
when (val result = fetchAddon(url)) { is NetworkResult.Success -> result.data
is NetworkResult.Success -> addons.add(result.data) else -> null // Skip failed addons
else -> { /* Skip failed addons */ } }
} }
} emit(addons)
}.flowOn(Dispatchers.IO)
addons
} }
override suspend fun fetchAddon(baseUrl: String): NetworkResult<Addon> { override suspend fun fetchAddon(baseUrl: String): NetworkResult<Addon> {