-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
230 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
app/src/main/kotlin/com/muedsa/agetv/ui/features/home/latest/LatestUpdateScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package com.muedsa.agetv.ui.features.home.latest | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusProperties | ||
import androidx.compose.ui.focus.focusRequester | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.tv.foundation.lazy.grid.TvGridCells | ||
import androidx.tv.foundation.lazy.grid.TvLazyVerticalGrid | ||
import androidx.tv.foundation.lazy.grid.itemsIndexed | ||
import androidx.tv.material3.Card | ||
import androidx.tv.material3.ExperimentalTvMaterial3Api | ||
import androidx.tv.material3.MaterialTheme | ||
import androidx.tv.material3.Text | ||
import com.muedsa.agetv.model.LazyType | ||
import com.muedsa.agetv.ui.AgePosterSize | ||
import com.muedsa.agetv.ui.navigation.NavigationItems | ||
import com.muedsa.agetv.viewmodel.LatestUpdateViewModel | ||
import com.muedsa.compose.tv.model.ContentModel | ||
import com.muedsa.compose.tv.theme.CardContentPadding | ||
import com.muedsa.compose.tv.theme.ImageCardRowCardPadding | ||
import com.muedsa.compose.tv.theme.ScreenPaddingLeft | ||
import com.muedsa.compose.tv.widget.CardType | ||
import com.muedsa.compose.tv.widget.ErrorMessageBoxState | ||
import com.muedsa.compose.tv.widget.ImageContentCard | ||
import com.muedsa.compose.tv.widget.ScreenBackgroundState | ||
import com.muedsa.compose.tv.widget.ScreenBackgroundType | ||
import com.muedsa.uitl.LogUtil | ||
|
||
@OptIn(ExperimentalComposeUiApi::class, ExperimentalTvMaterial3Api::class) | ||
@Composable | ||
fun LatestUpdateScreen( | ||
viewModel: LatestUpdateViewModel = hiltViewModel(), | ||
backgroundState: ScreenBackgroundState, | ||
errorMsgBoxState: ErrorMessageBoxState, | ||
onNavigate: (NavigationItems, List<String>?) -> Unit = { _, _ -> } | ||
) { | ||
val fontScale = LocalConfiguration.current.fontScale | ||
val titleMediumFontSize = MaterialTheme.typography.titleMedium.fontSize.value | ||
val bodyMediumFontSize = MaterialTheme.typography.bodyMedium.fontSize.value | ||
val contentHeight = remember { | ||
(titleMediumFontSize * fontScale + 0.5f).dp + | ||
(bodyMediumFontSize * fontScale + 0.5f).dp + | ||
CardContentPadding * 2 | ||
} | ||
|
||
val latestUpdateLP by remember { viewModel.latestUpdateLPState } | ||
|
||
LaunchedEffect(key1 = latestUpdateLP) { | ||
if (latestUpdateLP.type == LazyType.FAILURE) { | ||
errorMsgBoxState.error(latestUpdateLP.error) | ||
} | ||
} | ||
|
||
Column(modifier = Modifier.padding(start = ScreenPaddingLeft)) { | ||
Text( | ||
text = "最近更新", | ||
color = MaterialTheme.colorScheme.onBackground, | ||
style = MaterialTheme.typography.titleLarge | ||
) | ||
|
||
if (latestUpdateLP.list.isNotEmpty()) { | ||
val gridFocusRequester = remember { FocusRequester() } | ||
|
||
TvLazyVerticalGrid( | ||
modifier = Modifier | ||
.padding(start = 0.dp, top = 20.dp, end = 20.dp, bottom = 20.dp) | ||
.focusRequester(gridFocusRequester) | ||
.focusProperties { | ||
exit = { gridFocusRequester.saveFocusedChild(); FocusRequester.Default } | ||
enter = { | ||
if (gridFocusRequester.restoreFocusedChild()) { | ||
LogUtil.d("grid restoreFocusedChild") | ||
FocusRequester.Cancel | ||
} else { | ||
LogUtil.d("grid focused default child") | ||
FocusRequester.Default | ||
} | ||
} | ||
}, | ||
columns = TvGridCells.Adaptive(AgePosterSize.width + ImageCardRowCardPadding), | ||
contentPadding = PaddingValues( | ||
top = ImageCardRowCardPadding, | ||
bottom = ImageCardRowCardPadding | ||
) | ||
) { | ||
itemsIndexed( | ||
items = latestUpdateLP.list, | ||
key = { _, item -> item.aid } | ||
) { index, item -> | ||
val itemFocusRequester = remember { | ||
FocusRequester() | ||
} | ||
ImageContentCard( | ||
modifier = Modifier | ||
.padding(end = ImageCardRowCardPadding) | ||
.focusRequester(itemFocusRequester), | ||
url = item.picSmall, | ||
imageSize = AgePosterSize, | ||
type = CardType.STANDARD, | ||
model = ContentModel( | ||
item.title, | ||
subtitle = item.newTitle | ||
), | ||
onItemFocus = { | ||
backgroundState.url = item.picSmall | ||
backgroundState.type = ScreenBackgroundType.BLUR | ||
}, | ||
onItemClick = { | ||
LogUtil.d("Click $item") | ||
onNavigate(NavigationItems.Detail, listOf(item.aid.toString())) | ||
} | ||
) | ||
|
||
LaunchedEffect(key1 = Unit) { | ||
if (latestUpdateLP.offset == index) { | ||
itemFocusRequester.requestFocus() | ||
} | ||
} | ||
} | ||
|
||
if (latestUpdateLP.type != LazyType.LOADING && latestUpdateLP.hasNext) { | ||
item { | ||
Column { | ||
Card( | ||
modifier = Modifier | ||
.size(AgePosterSize) | ||
.padding(end = ImageCardRowCardPadding), | ||
onClick = { | ||
viewModel.fetchLatestUpdate() | ||
} | ||
) { | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text(text = "继续加载") | ||
} | ||
} | ||
Spacer(modifier = Modifier.height(contentHeight)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
LaunchedEffect(key1 = Unit) { | ||
viewModel.fetchLatestUpdate() | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/kotlin/com/muedsa/agetv/viewmodel/LatestUpdateViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.muedsa.agetv.viewmodel | ||
|
||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.muedsa.agetv.model.LazyPagedList | ||
import com.muedsa.agetv.model.age.PosterAnimeModel | ||
import com.muedsa.agetv.repository.AppRepository | ||
import com.muedsa.uitl.LogUtil | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
import kotlin.math.ceil | ||
|
||
@HiltViewModel | ||
class LatestUpdateViewModel @Inject constructor( | ||
private val repo: AppRepository | ||
) : ViewModel() { | ||
|
||
val latestUpdateLPState = mutableStateOf<LazyPagedList<PosterAnimeModel>>(LazyPagedList.new()) | ||
|
||
fun fetchLatestUpdate() { | ||
val nextPage = latestUpdateLPState.value.nextPage | ||
latestUpdateLPState.value = latestUpdateLPState.value.loadingNext() | ||
viewModelScope.launch(context = Dispatchers.IO) { | ||
try { | ||
repo.update(nextPage, PAGE_SIZE).let { | ||
latestUpdateLPState.value = latestUpdateLPState.value.successNext( | ||
it.videos, | ||
ceil(it.total.toDouble() / CatalogViewModel.PAGE_SIZE).toInt() | ||
) | ||
} | ||
} catch (t: Throwable) { | ||
withContext(Dispatchers.Main) { | ||
latestUpdateLPState.value = latestUpdateLPState.value.failNext(t) | ||
} | ||
LogUtil.fb(t) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val PAGE_SIZE = 30 | ||
} | ||
} |