Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

Fix live custom group counts

David Nashashc1da94499be8
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/epg/EpgViewModelTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/epg/EpgViewModelTest.kt
index c88be51f..f5cb9dc1 100644
--- a/app/src/test/java/com/streamvault/app/ui/screens/epg/EpgViewModelTest.kt
+++ b/app/src/test/java/com/streamvault/app/ui/screens/epg/EpgViewModelTest.kt
@@ -77,7 +77,7 @@ class EpgViewModelTest {
private val playerEngine: PlayerEngine = mock()
private val playerEngineProvider: InjectProvider<PlayerEngine> = mock()
private val application: Application = mock()
- private val getCustomCategories by lazy { GetCustomCategories(favoriteRepository) }
+ private val getCustomCategories by lazy { GetCustomCategories(favoriteRepository, channelRepository) }
private val createdViewModels = mutableListOf<EpgViewModel>()
private val testDispatcher = StandardTestDispatcher()
diff --git a/data/src/main/java/com/streamvault/data/local/dao/Daos.kt b/data/src/main/java/com/streamvault/data/local/dao/Daos.kt
index 0c87dbab..1095bbf4 100644
--- a/data/src/main/java/com/streamvault/data/local/dao/Daos.kt
+++ b/data/src/main/java/com/streamvault/data/local/dao/Daos.kt
@@ -3035,6 +3035,9 @@ abstract class FavoriteDao {
@Query("SELECT * FROM favorites WHERE provider_id = :providerId AND content_type = :contentType ORDER BY position ASC")
abstract fun getAllByType(providerId: Long, contentType: String): Flow<List<FavoriteEntity>>
+ @Query("SELECT * FROM favorites WHERE provider_id IN (:providerIds) AND content_type = :contentType ORDER BY provider_id ASC, position ASC")
+ abstract fun getAllByTypeForProviders(providerIds: List<Long>, contentType: String): Flow<List<FavoriteEntity>>
+
@Query(
"""
SELECT f.*
diff --git a/data/src/main/java/com/streamvault/data/repository/FavoriteRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/FavoriteRepositoryImpl.kt
index ca7c346e..94b8a26e 100644
--- a/data/src/main/java/com/streamvault/data/repository/FavoriteRepositoryImpl.kt
+++ b/data/src/main/java/com/streamvault/data/repository/FavoriteRepositoryImpl.kt
@@ -44,6 +44,12 @@ class FavoriteRepositoryImpl @Inject constructor(
return flow.map { entities -> entities.map { it.toDomain() } }
}
+ override fun getAllFavorites(providerIds: List<Long>, contentType: ContentType): Flow<List<Favorite>> {
+ if (providerIds.isEmpty()) return flowOf(emptyList())
+ return favoriteDao.getAllByTypeForProviders(providerIds, contentType.name)
+ .map { entities -> entities.map { it.toDomain() } }
+ }
+
@Deprecated(
"Use getFavorites(providerId, contentType) instead",
ReplaceWith("getFavorites(providerId, contentType)")
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index b2ab46fe..73315621 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -7,6 +7,7 @@ All notable product changes are recorded in this document.
### Fixed
- Fixed Picture-in-Picture compatibility on Android 7.1 / API 25 devices.
+- Fixed live custom-group counts so badges include grouped favorites correctly, no longer show zero for populated groups, and follow the same grouped channel count users see when opening a group.
- Fixed 4K / UHD / HDR live HLS playback stability by allowing live streams to promote to deeper HLS buffer policies when metadata or observed video format indicates high-resolution or high-bitrate playback needs.
- Fixed series episode preview fallback so missing or failed preview images still show the episode label instead of an empty tile.
- Fixed live playback recovery so decoder failures can retry with an alternate stream format after a software-decoder attempt.
diff --git a/domain/src/main/java/com/streamvault/domain/repository/FavoriteRepository.kt b/domain/src/main/java/com/streamvault/domain/repository/FavoriteRepository.kt
index 7dc7042b..86e6c698 100644
--- a/domain/src/main/java/com/streamvault/domain/repository/FavoriteRepository.kt
+++ b/domain/src/main/java/com/streamvault/domain/repository/FavoriteRepository.kt
@@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.Flow
interface FavoriteRepository {
fun getFavorites(providerId: Long, contentType: ContentType? = null): Flow<List<Favorite>>
fun getFavorites(providerIds: List<Long>, contentType: ContentType? = null): Flow<List<Favorite>>
+ fun getAllFavorites(providerIds: List<Long>, contentType: ContentType): Flow<List<Favorite>>
@Deprecated(
"Use getFavorites(providerId, contentType) instead",
replaceWith = ReplaceWith("getFavorites(providerId, contentType)")
diff --git a/domain/src/main/java/com/streamvault/domain/usecase/GetCustomCategories.kt b/domain/src/main/java/com/streamvault/domain/usecase/GetCustomCategories.kt
index 1cca555f..3d09ee58 100644
--- a/domain/src/main/java/com/streamvault/domain/usecase/GetCustomCategories.kt
+++ b/domain/src/main/java/com/streamvault/domain/usecase/GetCustomCategories.kt
@@ -1,21 +1,26 @@
package com.streamvault.domain.usecase
import com.streamvault.domain.model.Category
+import com.streamvault.domain.model.Channel
import com.streamvault.domain.model.ContentType
import com.streamvault.domain.model.Favorite
import com.streamvault.domain.model.VirtualCategoryIds
import com.streamvault.domain.model.VirtualGroup
+import com.streamvault.domain.repository.ChannelRepository
import com.streamvault.domain.repository.FavoriteRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
+import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.flow.map
import java.util.logging.Level
import java.util.logging.Logger
import javax.inject.Inject
class GetCustomCategories @Inject constructor(
- private val favoriteRepository: FavoriteRepository
+ private val favoriteRepository: FavoriteRepository,
+ private val channelRepository: ChannelRepository
) {
private val logger = Logger.getLogger("GetCustomCategories")
operator fun invoke(providerId: Long, contentType: ContentType = ContentType.LIVE): Flow<List<Category>> =
@@ -26,14 +31,31 @@ class GetCustomCategories @Inject constructor(
return flowOf(emptyList())
}
+ val favoritesFlow = favoriteRepository.getAllFavorites(providerIds, contentType)
+ val visibleLiveChannelsFlow = if (contentType == ContentType.LIVE) {
+ favoritesFlow
+ .map { favorites -> favorites.map(Favorite::contentId).distinct() }
+ .flatMapLatest { ids ->
+ if (ids.isEmpty()) {
+ flowOf(emptyList())
+ } else {
+ channelRepository.getChannelsByIds(ids)
+ }
+ }
+ } else {
+ flowOf(emptyList())
+ }
+
return combine(
favoriteRepository.getGroups(providerIds, contentType),
- favoriteRepository.getFavorites(providerIds, contentType)
- ) { groups, favorites ->
+ favoritesFlow,
+ visibleLiveChannelsFlow
+ ) { groups, favorites, visibleLiveChannels ->
buildCategories(
groups = groups,
favorites = favorites,
- contentType = contentType
+ contentType = contentType,
+ visibleLiveChannels = visibleLiveChannels
)
}.catch { e ->
logger.log(Level.WARNING, "Failed to load custom categories", e)
@@ -44,14 +66,29 @@ class GetCustomCategories @Inject constructor(
private fun buildCategories(
groups: List<VirtualGroup>,
favorites: List<Favorite>,
- contentType: ContentType
+ contentType: ContentType,
+ visibleLiveChannels: List<Channel>
): List<Category> {
- val groupCounts = favorites
- .asSequence()
- .mapNotNull(Favorite::groupId)
- .groupingBy { it }
- .eachCount()
- val globalCount = favorites.count { it.groupId == null }
+ val groupCounts = if (contentType == ContentType.LIVE) {
+ buildVisibleLiveGroupCounts(favorites, visibleLiveChannels)
+ } else {
+ favorites
+ .asSequence()
+ .mapNotNull(Favorite::groupId)
+ .groupingBy { it }
+ .eachCount()
+ }
+ val globalCount = if (contentType == ContentType.LIVE) {
+ countVisibleLiveFavorites(
+ contentIds = favorites.asSequence()
+ .filter { it.groupId == null }
+ .map(Favorite::contentId)
+ .toSet(),
+ visibleLiveChannels = visibleLiveChannels
+ )
+ } else {
+ favorites.count { it.groupId == null }
+ }
val categories = groups.map { group ->
Category(
@@ -76,4 +113,31 @@ class GetCustomCategories @Inject constructor(
return categories.toList()
}
+
+ private fun buildVisibleLiveGroupCounts(
+ favorites: List<Favorite>,
+ visibleLiveChannels: List<Channel>
+ ): Map<Long, Int> {
+ val contentIdsByGroup = favorites
+ .asSequence()
+ .mapNotNull { favorite -> favorite.groupId?.let { groupId -> groupId to favorite.contentId } }
+ .groupBy(
+ keySelector = { it.first },
+ valueTransform = { it.second }
+ )
+
+ return contentIdsByGroup.mapValues { (_, contentIds) ->
+ countVisibleLiveFavorites(contentIds.toSet(), visibleLiveChannels)
+ }
+ }
+
+ private fun countVisibleLiveFavorites(
+ contentIds: Set<Long>,
+ visibleLiveChannels: List<Channel>
+ ): Int {
+ if (contentIds.isEmpty()) return 0
+ return visibleLiveChannels.count { channel ->
+ channel.allVariantRawIds().any(contentIds::contains)
+ }
+ }
}
diff --git a/domain/src/test/java/com/streamvault/domain/usecase/GetCustomCategoriesTest.kt b/domain/src/test/java/com/streamvault/domain/usecase/GetCustomCategoriesTest.kt
index fc717310..752b510e 100644
--- a/domain/src/test/java/com/streamvault/domain/usecase/GetCustomCategoriesTest.kt
+++ b/domain/src/test/java/com/streamvault/domain/usecase/GetCustomCategoriesTest.kt
@@ -1,10 +1,13 @@
package com.streamvault.domain.usecase
import com.google.common.truth.Truth.assertThat
+import com.streamvault.domain.model.Channel
import com.streamvault.domain.model.ContentType
import com.streamvault.domain.model.Favorite
+import com.streamvault.domain.model.LiveChannelVariant
import com.streamvault.domain.model.VirtualCategoryIds
import com.streamvault.domain.model.VirtualGroup
+import com.streamvault.domain.repository.ChannelRepository
import com.streamvault.domain.repository.FavoriteRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
@@ -28,6 +31,14 @@ class GetCustomCategoriesTest {
VirtualGroup(id = 10L, providerId = 1L, name = "Sports", contentType = ContentType.LIVE),
VirtualGroup(id = 20L, providerId = 2L, name = "Kids", contentType = ContentType.LIVE)
)
+ ),
+ channelRepository = FakeChannelRepository(
+ channels = listOf(
+ rawChannel(id = 101L, name = "One", logicalGroupId = "p1_one", providerId = 1L),
+ rawChannel(id = 102L, name = "Two", logicalGroupId = "p1_two", providerId = 1L),
+ rawChannel(id = 201L, name = "Three", logicalGroupId = "p2_three", providerId = 2L),
+ rawChannel(id = 202L, name = "Four", logicalGroupId = "p2_four", providerId = 2L)
+ )
)
)
@@ -40,9 +51,40 @@ class GetCustomCategoriesTest {
.containsExactly("Sports" to 1, "Kids" to 2)
}
+ @Test
+ fun liveCustomGroupCountsFollowGroupedChannelPresentation() = runTest {
+ val useCase = GetCustomCategories(
+ favoriteRepository = FakeFavoriteRepository(
+ favorites = listOf(
+ Favorite(providerId = 1L, contentId = 101L, contentType = ContentType.LIVE, groupId = 10L),
+ Favorite(providerId = 1L, contentId = 102L, contentType = ContentType.LIVE, groupId = 10L)
+ ),
+ groups = listOf(
+ VirtualGroup(id = 10L, providerId = 1L, name = "Sports", contentType = ContentType.LIVE)
+ )
+ ),
+ channelRepository = FakeChannelRepository(
+ channels = listOf(
+ groupedChannel(
+ id = 101L,
+ providerId = 1L,
+ logicalGroupId = "p1_sports",
+ rawIds = listOf(101L, 102L)
+ )
+ )
+ )
+ )
+
+ val result = useCase(listOf(1L), ContentType.LIVE).first()
+
+ assertThat(result).hasSize(2)
+ assertThat(result[1].name).isEqualTo("Sports")
+ assertThat(result[1].count).isEqualTo(1)
+ }
+
@Test
fun returnsEmptyListForNoProviders() = runTest {
- val useCase = GetCustomCategories(FakeFavoriteRepository())
+ val useCase = GetCustomCategories(FakeFavoriteRepository(), FakeChannelRepository())
val result = useCase(emptyList(), ContentType.LIVE).first()
@@ -59,13 +101,22 @@ class GetCustomCategoriesTest {
override fun getFavorites(providerIds: List<Long>, contentType: ContentType?): Flow<List<Favorite>> =
flowOf(
favorites.filter { favorite ->
- favorite.providerId in providerIds && (contentType == null || favorite.contentType == contentType)
+ favorite.providerId in providerIds &&
+ favorite.groupId == null &&
+ (contentType == null || favorite.contentType == contentType)
+ }
+ )
+
+ override fun getAllFavorites(providerIds: List<Long>, contentType: ContentType): Flow<List<Favorite>> =
+ flowOf(
+ favorites.filter { favorite ->
+ favorite.providerId in providerIds && favorite.contentType == contentType
}
)
@Deprecated("Use getFavorites(providerId, contentType) instead")
override fun getAllFavorites(providerId: Long, contentType: ContentType): Flow<List<Favorite>> =
- getFavorites(providerId, contentType)
+ getAllFavorites(listOf(providerId), contentType)
override fun getFavoritesByGroup(groupId: Long): Flow<List<Favorite>> =
flowOf(favorites.filter { it.groupId == groupId })
@@ -117,4 +168,65 @@ class GetCustomCategoriesTest {
override suspend fun renameGroup(groupId: Long, newName: String) = error("Not used in test")
}
+
+ private class FakeChannelRepository(
+ private val channels: List<Channel> = emptyList()
+ ) : ChannelRepository {
+ override fun getChannels(providerId: Long): Flow<List<Channel>> = error("Not used in test")
+ override fun getChannelCount(providerId: Long): Flow<Int> = error("Not used in test")
+ override fun getChannelsByCategory(providerId: Long, categoryId: Long): Flow<List<Channel>> = error("Not used in test")
+ override fun getChannelsByCategoryPage(providerId: Long, categoryId: Long, limit: Int): Flow<List<Channel>> = error("Not used in test")
+ override fun getChannelsByNumber(providerId: Long, categoryId: Long): Flow<List<Channel>> = error("Not used in test")
+ override fun getChannelsWithoutErrors(providerId: Long, categoryId: Long): Flow<List<Channel>> = error("Not used in test")
+ override fun getChannelsWithoutErrorsPage(providerId: Long, categoryId: Long, limit: Int): Flow<List<Channel>> = error("Not used in test")
+ override suspend fun getChannelsByCategoryPageOffset(providerId: Long, categoryId: Long, limit: Int, offset: Int): List<Channel> = error("Not used in test")
+ override suspend fun getChannelsWithoutErrorsPageOffset(providerId: Long, categoryId: Long, limit: Int, offset: Int): List<Channel> = error("Not used in test")
+ override fun searchChannelsByCategory(providerId: Long, categoryId: Long, query: String): Flow<List<Channel>> = error("Not used in test")
+ override fun searchChannelsByCategoryPaged(providerId: Long, categoryId: Long, query: String, limit: Int): Flow<List<Channel>> = error("Not used in test")
+ override fun getCategories(providerId: Long): Flow<List<com.streamvault.domain.model.Category>> = error("Not used in test")
+ override fun searchChannels(providerId: Long, query: String): Flow<List<Channel>> = error("Not used in test")
+ override suspend fun getChannel(channelId: Long): Channel? = error("Not used in test")
+ override suspend fun getStreamInfo(channel: Channel, preferStableUrl: Boolean) = error("Not used in test")
+ override suspend fun refreshChannels(providerId: Long) = error("Not used in test")
+ override fun getChannelsByIds(ids: List<Long>): Flow<List<Channel>> =
+ flowOf(channels.filter { channel -> channel.allVariantRawIds().any(ids::contains) })
+ override suspend fun incrementChannelErrorCount(channelId: Long) = error("Not used in test")
+ override suspend fun resetChannelErrorCount(channelId: Long) = error("Not used in test")
+ }
+
+ private fun rawChannel(id: Long, name: String, logicalGroupId: String, providerId: Long): Channel = Channel(
+ id = id,
+ name = name,
+ providerId = providerId,
+ logicalGroupId = logicalGroupId,
+ selectedVariantId = id,
+ variants = listOf(
+ LiveChannelVariant(
+ rawChannelId = id,
+ logicalGroupId = logicalGroupId,
+ providerId = providerId,
+ originalName = name,
+ canonicalName = name,
+ streamUrl = "https://example.com/$id"
+ )
+ )
+ )
+
+ private fun groupedChannel(id: Long, providerId: Long, logicalGroupId: String, rawIds: List<Long>): Channel = Channel(
+ id = id,
+ name = "Grouped",
+ providerId = providerId,
+ logicalGroupId = logicalGroupId,
+ selectedVariantId = id,
+ variants = rawIds.map { rawId ->
+ LiveChannelVariant(
+ rawChannelId = rawId,
+ logicalGroupId = logicalGroupId,
+ providerId = providerId,
+ originalName = "Grouped $rawId",
+ canonicalName = "Grouped",
+ streamUrl = "https://example.com/$rawId"
+ )
+ }
+ )
}
\ No newline at end of file