Add shared catalog action boundary

This commit is contained in:
KhooLy 2026-07-13 15:08:20 +03:00
parent ae73947f4d
commit cb99b2b1e8
10 changed files with 436 additions and 16 deletions

View file

@ -0,0 +1,75 @@
package com.fluxa.app.ui.catalog
import com.fluxa.app.data.local.UserProfile
import com.fluxa.app.shared.feature.catalog.CatalogHomeDataSource
import com.fluxa.app.shared.feature.catalog.CatalogHomeUiState
import com.fluxa.app.shared.feature.catalog.CatalogItemUiModel
import com.fluxa.app.shared.feature.catalog.CatalogResumeUiModel
import com.fluxa.app.shared.feature.catalog.CatalogRowUiModel
import com.fluxa.app.shared.feature.catalog.CatalogSourceUiModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
class AndroidCatalogHomeDataSource(
private val homeViewModel: HomeViewModel,
private val activeProfile: () -> UserProfile?
) : CatalogHomeDataSource {
override fun observeHome(): Flow<CatalogHomeUiState> = combine(
homeViewModel.categories,
homeViewModel.isLoading
) { categories, isLoading ->
val profile = activeProfile()
CatalogHomeUiState(
rows = orderHomeCategories(categories).map { category ->
CatalogRowUiModel(
id = category.id,
title = category.semanticName,
canLoadMore = category.canLoadMore,
items = category.items.map { meta ->
CatalogItemUiModel(
id = meta.id,
type = meta.type,
card = meta.toCatalogCardUiModel(
cardLayout = resolveHomeCardLayout(category, profile),
artworkPreference = resolveContinueWatchingArtworkPreference(category, profile),
profile = profile,
cardScale = 1f,
showHorizontalLogo = true,
topTenRank = null,
isContinueWatchingCard = category.isContinueWatchingCategory(),
loadArtwork = true
),
source = CatalogSourceUiModel(
addonTransportUrl = category.addonTransportUrl
?: category.catalogSources?.firstOrNull()?.transportUrl,
catalogType = category.catalogSources?.firstOrNull()?.type ?: category.type
),
resume = meta.toCatalogResumeUiModel()
)
}
)
},
isLoading = isLoading
)
}
override suspend fun refresh() {
homeViewModel.loadInitialData(activeProfile(), force = true)
}
override suspend fun loadMore(rowId: String) {
homeViewModel.loadMore(rowId)
}
}
private fun com.fluxa.app.data.remote.Meta.toCatalogResumeUiModel(): CatalogResumeUiModel? {
val positionMs = timeOffset ?: 0L
if (lastVideoId == null && positionMs <= 0L) return null
return CatalogResumeUiModel(
positionMs = positionMs,
durationMs = duration,
videoId = lastVideoId,
streamUrl = lastStreamUrl,
streamTitle = lastStreamTitle
)
}

View file

@ -1 +1,5 @@
import Foundation
func fluxaCoreVersion() -> String {
""
}

View file

@ -0,0 +1,7 @@
import Foundation
enum FluxaRustCoreRuntime {
static func version() -> String {
fluxaCoreVersion()
}
}

View file

@ -2,6 +2,8 @@ import SwiftUI
@main
struct FluxaIosApp: App {
private let coreVersion = FluxaRustCoreRuntime.version()
var body: some Scene {
WindowGroup {
FluxaRootView()

View file

@ -0,0 +1,180 @@
package com.fluxa.app.shared
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.fluxa.app.common.AppStrings
import com.fluxa.app.shared.feature.catalog.CatalogAction
import com.fluxa.app.shared.feature.catalog.CatalogHomeUiState
import com.fluxa.app.ui.catalog.CatalogCard
import com.fluxa.app.ui.catalog.FluxaColors
enum class FluxaDestination(val titleKey: String) {
Home("nav.home"),
Discover("nav.discover"),
Calendar("nav.calendar"),
Library("nav.library"),
Settings("nav.settings")
}
data class FluxaAppUiState(
val language: String? = null,
val destination: FluxaDestination = FluxaDestination.Home,
val catalogHome: CatalogHomeUiState = CatalogHomeUiState()
)
@Composable
fun FluxaApp(
state: FluxaAppUiState,
onDestinationSelected: (FluxaDestination) -> Unit,
onCatalogAction: (CatalogAction) -> Unit,
modifier: Modifier = Modifier
) {
MaterialTheme {
Column(
modifier = modifier
.fillMaxSize()
.background(FluxaColors.background)
) {
FluxaNavigationBar(
language = state.language,
destination = state.destination,
onDestinationSelected = onDestinationSelected
)
when (state.destination) {
FluxaDestination.Home -> FluxaHomeContent(
state = state,
onCatalogAction = onCatalogAction,
modifier = Modifier.weight(1f)
)
else -> FluxaDestinationPlaceholder(
language = state.language,
destination = state.destination,
modifier = Modifier.weight(1f)
)
}
}
}
}
@Composable
private fun FluxaNavigationBar(
language: String?,
destination: FluxaDestination,
onDestinationSelected: (FluxaDestination) -> Unit
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 16.dp),
horizontalArrangement = Arrangement.spacedBy(20.dp)
) {
FluxaDestination.entries.forEach { item ->
val selected = item == destination
Text(
text = AppStrings.t(language, item.titleKey),
color = if (selected) Color.White else Color.White.copy(alpha = 0.65f),
fontWeight = if (selected) FontWeight.Bold else FontWeight.Medium,
modifier = Modifier.clickable { onDestinationSelected(item) }
)
}
}
}
@Composable
private fun FluxaHomeContent(
state: FluxaAppUiState,
onCatalogAction: (CatalogAction) -> Unit,
modifier: Modifier
) {
if (state.catalogHome.isLoading && state.catalogHome.rows.isEmpty()) {
Box(modifier = modifier, contentAlignment = Alignment.Center) {
CircularProgressIndicator(color = Color.White)
}
return
}
if (state.catalogHome.rows.isEmpty()) {
FluxaDestinationPlaceholder(
language = state.language,
destination = FluxaDestination.Home,
modifier = modifier
)
return
}
Column(
modifier = modifier.padding(vertical = 8.dp),
verticalArrangement = Arrangement.spacedBy(24.dp)
) {
state.catalogHome.rows.forEach { row ->
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = row.title,
color = Color.White,
fontWeight = FontWeight.Bold
)
if (row.canLoadMore) {
Text(
text = AppStrings.t(state.language, "common.view_all"),
color = Color.White.copy(alpha = 0.7f),
modifier = Modifier.clickable {
onCatalogAction(CatalogAction.LoadMore(row.id))
}
)
}
}
LazyRow(
contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 20.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
items(row.items, key = { it.id }) { item ->
CatalogCard(
model = item.card,
onClick = { onCatalogAction(CatalogAction.ItemSelected(item)) }
)
}
}
}
}
}
}
@Composable
private fun FluxaDestinationPlaceholder(
language: String?,
destination: FluxaDestination,
modifier: Modifier
) {
Box(modifier = modifier, contentAlignment = Alignment.Center) {
Text(
text = AppStrings.t(language, destination.titleKey),
color = Color.White,
style = MaterialTheme.typography.headlineMedium
)
}
}

View file

@ -0,0 +1,50 @@
package com.fluxa.app.shared
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.fluxa.app.shared.feature.catalog.CatalogAction
import com.fluxa.app.shared.feature.catalog.CatalogHomeDataSource
import com.fluxa.app.shared.feature.catalog.CatalogHomeStore
import kotlinx.coroutines.launch
@Composable
fun FluxaAppHost(
catalogHomeDataSource: CatalogHomeDataSource,
language: String? = null,
onCatalogAction: (CatalogAction) -> Unit = {},
modifier: Modifier = Modifier
) {
val scope = rememberCoroutineScope()
val catalogHomeStore = remember(catalogHomeDataSource) {
CatalogHomeStore(catalogHomeDataSource, scope)
}
val catalogHome by catalogHomeStore.state.collectAsState()
val appState = rememberFluxaAppState()
LaunchedEffect(catalogHome) {
appState.updateCatalogHome(catalogHome)
}
LaunchedEffect(language) {
appState.updateLanguage(language)
}
LaunchedEffect(catalogHomeStore) {
catalogHomeStore.dispatch(CatalogAction.Refresh)
}
FluxaApp(
state = appState.uiState,
onDestinationSelected = appState::selectDestination,
onCatalogAction = { action ->
scope.launch {
catalogHomeStore.dispatch(action)
}
onCatalogAction(action)
},
modifier = modifier
)
}

View file

@ -0,0 +1,51 @@
package com.fluxa.app.shared.feature.catalog
import com.fluxa.app.ui.catalog.CatalogCardUiModel
import kotlinx.coroutines.flow.Flow
data class CatalogItemUiModel(
val id: String,
val type: String,
val card: CatalogCardUiModel,
val source: CatalogSourceUiModel = CatalogSourceUiModel(),
val resume: CatalogResumeUiModel? = null
)
data class CatalogSourceUiModel(
val addonTransportUrl: String? = null,
val catalogType: String? = null
)
data class CatalogResumeUiModel(
val positionMs: Long,
val durationMs: Long?,
val videoId: String?,
val streamUrl: String?,
val streamTitle: String?
)
data class CatalogRowUiModel(
val id: String,
val title: String,
val items: List<CatalogItemUiModel>,
val canLoadMore: Boolean = false
)
data class CatalogHomeUiState(
val rows: List<CatalogRowUiModel> = emptyList(),
val isLoading: Boolean = false
)
sealed interface CatalogAction {
data object Refresh : CatalogAction
data class LoadMore(val rowId: String) : CatalogAction
data class ItemSelected(val item: CatalogItemUiModel) : CatalogAction
data class PlayRequested(val item: CatalogItemUiModel) : CatalogAction
data class ResumeRequested(val item: CatalogItemUiModel) : CatalogAction
}
interface CatalogHomeDataSource {
fun observeHome(): Flow<CatalogHomeUiState>
suspend fun refresh()
suspend fun loadMore(rowId: String)
}

View file

@ -0,0 +1,24 @@
package com.fluxa.app.shared.feature.catalog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
class CatalogHomeStore(
private val dataSource: CatalogHomeDataSource,
scope: CoroutineScope
) {
val state: StateFlow<CatalogHomeUiState> = dataSource.observeHome()
.stateIn(scope, SharingStarted.WhileSubscribed(5_000), CatalogHomeUiState(isLoading = true))
suspend fun dispatch(action: CatalogAction) {
when (action) {
CatalogAction.Refresh -> dataSource.refresh()
is CatalogAction.LoadMore -> dataSource.loadMore(action.rowId)
is CatalogAction.ItemSelected,
is CatalogAction.PlayRequested,
is CatalogAction.ResumeRequested -> Unit
}
}
}

View file

@ -1,30 +1,36 @@
package com.fluxa.app.shared
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.window.ComposeUIViewController
import com.fluxa.app.common.AppStrings
import com.fluxa.app.shared.feature.catalog.CatalogAction
import com.fluxa.app.shared.feature.catalog.CatalogHomeUiState
import com.fluxa.app.shared.platform.AppleCatalogHomeDataSource
import platform.UIKit.UIViewController
object FluxaApple {
internal val catalogHomeDataSource = AppleCatalogHomeDataSource()
fun rootViewController(): UIViewController = ComposeUIViewController {
FluxaApp()
FluxaAppleApp()
}
fun updateCatalogHome(home: CatalogHomeUiState) {
catalogHomeDataSource.update(home)
}
}
@Composable
private fun FluxaApp() {
MaterialTheme {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(AppStrings.t(null, "nav.home"))
private fun FluxaAppleApp() {
FluxaAppHost(
catalogHomeDataSource = FluxaApple.catalogHomeDataSource,
onCatalogAction = { action ->
when (action) {
CatalogAction.Refresh,
is CatalogAction.LoadMore,
is CatalogAction.ItemSelected,
is CatalogAction.PlayRequested,
is CatalogAction.ResumeRequested -> Unit
}
}
}
)
}

View file

@ -0,0 +1,21 @@
package com.fluxa.app.shared.platform
import com.fluxa.app.shared.feature.catalog.CatalogHomeDataSource
import com.fluxa.app.shared.feature.catalog.CatalogHomeUiState
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
class AppleCatalogHomeDataSource : CatalogHomeDataSource {
private val state = MutableStateFlow(CatalogHomeUiState())
override fun observeHome(): Flow<CatalogHomeUiState> = state.asStateFlow()
override suspend fun refresh() = Unit
override suspend fun loadMore(rowId: String) = Unit
fun update(home: CatalogHomeUiState) {
state.value = home
}
}