From 434d42cc309bb653d7a9a9518e8a6a2b41692edb Mon Sep 17 00:00:00 2001 From: CXwudi Date: Sun, 3 Dec 2023 19:41:57 -0500 Subject: [PATCH] :recycle: Refactor ResultGridCell.kt for improving readability and nullability The ResultGridCell.kt class has been updated for better readability and code handling. We are ensuring nullability and consistency by replacing nullable 'List?' with non-nullable 'List'. Small functions are broken down for better readability and understanding. The functions related to Music Information (SongTitle, ArtistField, PublishDateField, SongTypeField, and PvField) have been separated out of MusicInfo. This is to ensure the MusicInfo function is not cluttered and to isolate each field's functionality, making the code more modular and manageable. --- .../ui/component/main/ResultGridCell.kt | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/main/ResultGridCell.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/main/ResultGridCell.kt index de2adea..a365186 100644 --- a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/main/ResultGridCell.kt +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/main/ResultGridCell.kt @@ -35,11 +35,13 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import mikufan.cx.songfinder.backend.controller.mainpage.ResultCellController import mikufan.cx.songfinder.backend.db.entity.PvService +import mikufan.cx.songfinder.backend.db.entity.SongType import mikufan.cx.songfinder.backend.model.PVInfo import mikufan.cx.songfinder.backend.model.SongSearchResult import mikufan.cx.songfinder.getSpringBean import mikufan.cx.songfinder.ui.common.TooltipAreaWithCard import mikufan.cx.songfinder.ui.theme.spacing +import java.time.LocalDateTime import java.time.format.DateTimeFormatter @Composable @@ -62,7 +64,7 @@ fun LazyGridItemScope.RealResultGridCell( callbacks: ResultCellCallbacks, modifier: Modifier = Modifier ) { - val filteredPvs: List? by produceState?>(null) { + val filteredPvs: List by produceState(listOf()) { value = result.pvs.filter { it.pvService in listOf( PvService.Youtube, @@ -84,7 +86,7 @@ fun LazyGridItemScope.RealResultGridCell( @Composable fun LazyThumbnailImage( result: SongSearchResult, - pvs: List? + pvs: List ) { //TODO: use the first ever available PV's thumbnail, if no PVs, use image not found. // If exceptions (typically no available PVs), use image failed to load @@ -100,10 +102,19 @@ fun LazyThumbnailImage( ) } -@OptIn(ExperimentalFoundationApi::class) @Composable -fun MusicInfo(songInfo: SongSearchResult, filteredPvs: List?) = Column { +fun MusicInfo(songInfo: SongSearchResult, filteredPvs: List) = Column { val (id, title, type, vocals, producers, publishDate, _) = songInfo + SongTitle(id, title) + ArtistField(vocals, producers) + PublishDateField(publishDate) + SongTypeField(type) + PvField(filteredPvs) +} + +@OptIn(ExperimentalFoundationApi::class) +@Composable +private fun SongTitle(id: Long, title: String) { Row( horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.spacing, Alignment.CenterHorizontally), modifier = Modifier.fillMaxWidth() @@ -130,7 +141,11 @@ fun MusicInfo(songInfo: SongSearchResult, filteredPvs: List?) = Column { ) } } +} +@OptIn(ExperimentalFoundationApi::class) +@Composable +private fun ArtistField(vocals: List, producers: List) { val artistString by produceState("Loading Artists...") { value = getArtistString(vocals, producers) } @@ -141,6 +156,11 @@ fun MusicInfo(songInfo: SongSearchResult, filteredPvs: List?) = Column { overflow = TextOverflow.Ellipsis, modifier = Modifier.basicMarquee(), ) +} + +@OptIn(ExperimentalFoundationApi::class) +@Composable +private fun PublishDateField(publishDate: LocalDateTime?) { val publishDateText by produceState("Loading...") { value = publishDate?.format(DateTimeFormatter.ISO_DATE) ?: "Unknown" } @@ -150,24 +170,21 @@ fun MusicInfo(songInfo: SongSearchResult, filteredPvs: List?) = Column { maxLines = 1, modifier = Modifier.basicMarquee(), ) +} + +@OptIn(ExperimentalFoundationApi::class) +@Composable +private fun SongTypeField(type: SongType) { Text( text = "Song Type: $type", style = MaterialTheme.typography.bodyMedium, maxLines = 1, modifier = Modifier.basicMarquee(), ) - if (filteredPvs == null) { - Text( - text = "Loading PVs...", - style = MaterialTheme.typography.bodyMedium, - ) - } else { - PvRows(filteredPvs) - } } @Composable -private fun PvRows(pvs: List) { +private fun PvField(pvs: List) { val uriHandler = LocalUriHandler.current LazyRow( horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.spacingSmall), @@ -212,8 +229,7 @@ private fun PvRows(pvs: List) { } } - -/* --- Utils ---*/ +/* --------- Utils --------- */ data class ResultCellCallbacks( val onCardClicked: (SongSearchResult) -> Unit,