Skip to content

Commit

Permalink
update: use stateFlow in viewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 7, 2023
1 parent d4aa6ee commit b4712f5
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 587 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dependencies {
implementation(libs.core.ktx)
implementation(libs.core.splashscreen)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.lifecycle.runtime.compose)
implementation(libs.lifecycle.viewmodel.compose)
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ class MainActivity : ComponentActivity() {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
splashScreen.setKeepOnScreenCondition {
homePageViewModel.homeDataState.value.type == LazyType.LOADING
homePageViewModel.homeDataSF.value.type == LazyType.LOADING
}
homePageViewModel.fetchHome()
setContent {

TvTheme {
// A surface container using the 'background' color from the theme
Surface(
Expand All @@ -52,7 +50,6 @@ class MainActivity : ComponentActivity() {
errorMsgBoxState = errorMsgBoxState
)
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.muedsa.agetv.model

import com.muedsa.agetv.model.age.AgeCatalogOption

data class CatalogOptionsUIModel(
val order: AgeCatalogOption = AgeCatalogOption.Order[0],
val region: AgeCatalogOption = AgeCatalogOption.Regions[0],
val genre: AgeCatalogOption = AgeCatalogOption.Genres[0],
val year: AgeCatalogOption = AgeCatalogOption.Years[0],
val season: AgeCatalogOption = AgeCatalogOption.Seasons[0],
val status: AgeCatalogOption = AgeCatalogOption.Status[0],
val label: AgeCatalogOption = AgeCatalogOption.Labels[0],
val resource: AgeCatalogOption = AgeCatalogOption.Resources[0],
) {

fun default(): CatalogOptionsUIModel {
return if (this == Default) {
this
} else {
Default
}
}

companion object {
val Default = CatalogOptionsUIModel()
}
}
13 changes: 9 additions & 4 deletions app/src/main/kotlin/com/muedsa/agetv/model/LazyPagedList.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.muedsa.agetv.model

data class LazyPagedList<T>(
val list: MutableList<T> = mutableListOf(),
data class LazyPagedList<Q, R>(
val query: Q,
val list: MutableList<R> = mutableListOf(),
val page: Int = 0,
val totalPage: Int = 0,
val offset: Int = 0,
Expand All @@ -12,13 +13,15 @@ data class LazyPagedList<T>(
val hasNext get() = page == 0 || page < totalPage

fun loadingNext() = LazyPagedList(
query = query,
list = list,
page = page,
totalPage = totalPage,
type = LazyType.LOADING
)

fun successNext(appendList: List<T>, totalPage: Int) = LazyPagedList(
fun successNext(appendList: List<R>, totalPage: Int) = LazyPagedList(
query = query,
offset = list.size,
list = list.also { it.addAll(appendList) },
page = nextPage,
Expand All @@ -27,6 +30,7 @@ data class LazyPagedList<T>(
)

fun failNext(error: Throwable?) = LazyPagedList(
query = query,
list = list,
page = page,
totalPage = totalPage,
Expand All @@ -36,6 +40,7 @@ data class LazyPagedList<T>(

companion object {
@JvmStatic
fun <T> new(): LazyPagedList<T> = LazyPagedList(type = LazyType.SUCCESS)
fun <Q, R> new(query: Q): LazyPagedList<Q, R> =
LazyPagedList(query = query, type = LazyType.SUCCESS)
}
}
24 changes: 8 additions & 16 deletions app/src/main/kotlin/com/muedsa/agetv/model/age/AgeCatalogOption.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.muedsa.agetv.model.age

import java.util.Collections

sealed class AgeCatalogOption(val text: String, val value: String) {
data object ALL : AgeCatalogOption("全部", "all")

Expand Down Expand Up @@ -100,22 +98,18 @@ sealed class AgeCatalogOption(val text: String, val value: String) {
data object OrderByHits : AgeCatalogOption("点击量", "hits")

companion object {
val Regions =
listOf(ALL, RegionJP, RegionCN, RegionWW).let { Collections.unmodifiableList(it) }
val Regions = listOf(ALL, RegionJP, RegionCN, RegionWW)

val Genres =
listOf(ALL, GenreTV, GenreMovie, GenreOVA).let { Collections.unmodifiableList(it) }
val Genres = listOf(ALL, GenreTV, GenreMovie, GenreOVA)

val Seasons = listOf(ALL, SeasonQ1, SeasonQ2, SeasonQ3, SeasonQ4).let {
Collections.unmodifiableList(it)
}
val Seasons = listOf(ALL, SeasonQ1, SeasonQ2, SeasonQ3, SeasonQ4)

val Status = listOf(
ALL,
StatueSerializing,
StatueCompleteD,
StatueNotPlay
).let { Collections.unmodifiableList(it) }
)

val Years = listOf(
ALL,
Expand Down Expand Up @@ -143,7 +137,7 @@ sealed class AgeCatalogOption(val text: String, val value: String) {
Year2002,
Year2001,
Year2000AndBefore,
).let { Collections.unmodifiableList(it) }
)

val Labels = listOf(
ALL,
Expand Down Expand Up @@ -191,12 +185,10 @@ sealed class AgeCatalogOption(val text: String, val value: String) {
LabelFemaleOriented,
LabelShort,
LabelHappy
).let { Collections.unmodifiableList(it) }
)

val Resources =
listOf(ALL, ResourceBDRIP, ResourceGERIP).let { Collections.unmodifiableList(it) }
val Resources = listOf(ALL, ResourceBDRIP, ResourceGERIP)

val Order =
listOf(OrderByTime, OrderByName, OrderByHits).let { Collections.unmodifiableList(it) }
val Order = listOf(OrderByTime, OrderByName, OrderByHits)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.Whatshot
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -58,6 +59,7 @@ import com.muedsa.compose.tv.widget.ScreenBackgroundType
import com.muedsa.compose.tv.widget.StandardImageCardsRow
import com.muedsa.compose.tv.widget.rememberScreenBackgroundState
import com.muedsa.uitl.LogUtil
import kotlinx.coroutines.flow.update

@OptIn(
ExperimentalTvMaterial3Api::class,
Expand All @@ -73,11 +75,9 @@ fun AnimeDetailScreen(
val screenHeight = configuration.screenHeightDp.dp
val screenWidth = configuration.screenWidthDp.dp

val animeDetailLD by remember { viewModel.animeDetailLDState }
val commentsLP by remember { viewModel.commentsLPState }

val danSearchAnimeListLD by remember { viewModel.danSearchAnimeListLDState }
val danAnimeInfoLD by remember { viewModel.danAnimeInfoLDState }
val animeDetailLD by viewModel.animeDetailLDSF.collectAsState()
val danSearchAnimeListLD by viewModel.danSearchAnimeListLDSF.collectAsState()
val danAnimeInfoLD by viewModel.danAnimeInfoLDSF.collectAsState()

val backgroundState = rememberScreenBackgroundState(
initType = ScreenBackgroundType.SCRIM
Expand All @@ -89,17 +89,10 @@ fun AnimeDetailScreen(
} else if (animeDetailLD.type == LazyType.SUCCESS) {
if (animeDetailLD.data != null) {
backgroundState.url = animeDetailLD.data!!.video.cover
viewModel.searchDanAnime()
}
}
}

LaunchedEffect(key1 = commentsLP) {
if (commentsLP.type == LazyType.FAILURE) {
errorMsgBoxState.error(commentsLP.error)
}
}

LaunchedEffect(key1 = danSearchAnimeListLD) {
if (danSearchAnimeListLD.type == LazyType.FAILURE) {
errorMsgBoxState.error(danSearchAnimeListLD.error)
Expand Down Expand Up @@ -281,7 +274,7 @@ fun AnimeDetailScreen(
item.animeTitle
},
onSelected = { _, item ->
viewModel.fetchDanBangumi(item.animeId)
viewModel.danBangumi(item.animeId)
}
)
}
Expand Down Expand Up @@ -377,7 +370,9 @@ fun AnimeDetailScreen(
},
onItemClick = { _, anime ->
LogUtil.fb("Click $anime")
viewModel.animeIdLD.value = anime.aid.toString()
viewModel.animeIdSF.update {
anime.aid.toString()
}
}
)
Spacer(modifier = Modifier.height(25.dp))
Expand All @@ -399,7 +394,9 @@ fun AnimeDetailScreen(
},
onItemClick = { _, anime ->
LogUtil.fb("Click $anime")
viewModel.animeIdLD.value = anime.aid.toString()
viewModel.animeIdSF.update {
anime.aid.toString()
}
}
)
Spacer(modifier = Modifier.height(25.dp))
Expand All @@ -418,11 +415,9 @@ fun AnimeDetailScreen(
}
} else if (animeDetailLD.type == LazyType.FAILURE) {
ErrorScreen {
viewModel.animeIdLD.value = viewModel.animeIdLD.value
viewModel.animeIdSF.value = viewModel.animeIdSF.value
}
} else {
LoadingScreen()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Check
import androidx.compose.material3.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.tv.material3.ExperimentalTvMaterial3Api
Expand All @@ -25,9 +24,9 @@ import com.muedsa.agetv.model.age.AgeCatalogOption
@Composable
fun CatalogOptionsWidget(
title: String,
state: MutableState<AgeCatalogOption>,
selectedIndex: Int,
options: List<AgeCatalogOption>,
onChange: () -> Unit = {}
onClick: (Int, AgeCatalogOption) -> Unit = { _, _ -> }
) {
Text(
text = title,
Expand All @@ -36,11 +35,11 @@ fun CatalogOptionsWidget(
)
Spacer(modifier = Modifier.height(4.dp))
FlowRow {
for (option in options) {
options.forEachIndexed { index, option ->
FilterChip(
modifier = Modifier.padding(8.dp),
selected = option == state.value,
leadingIcon = if (option == state.value) {
selected = index == selectedIndex,
leadingIcon = if (index == selectedIndex) {
{
Icon(
modifier = Modifier.size(FilterChipDefaults.IconSize),
Expand All @@ -50,12 +49,7 @@ fun CatalogOptionsWidget(
}
} else null,
onClick = {
state.value = if (option == state.value) {
AgeCatalogOption.Order[0]
} else {
option
}
onChange()
onClick(index, option)
}
) {
Text(text = option.text)
Expand Down
Loading

0 comments on commit b4712f5

Please sign in to comment.