From 61e2c7c90dbc1313b9c388b255315563981c67f4 Mon Sep 17 00:00:00 2001 From: CXwudi Date: Tue, 28 Nov 2023 14:08:53 -0500 Subject: [PATCH] :recycle: Refactor ResultPanel UI component for custom grid cells Made changes to the `ResultPanel.kt` and `MainScreen.kt` in the song finder-app. This involves refactoring the 'RealResultPanel' component in `ResultPanel.kt` to accept a '@Composable' function as its argument which represents the content of each grid cell. Primarily, this was done to enable greater customizability in terms of how each grid cell in the 'ResultPanel' is displayed. Corresponding changes were made in `MainScreen.kt` to pass a 'DebugUsedCell' Composable function to the 'RealResultPanel' function as an argument. Also, updated the visibility of 'DebugUsedCell' function in `ResultGridCell.kt` to public to make it accessible in `MainScreen.kt`. --- .../cx/songfinder/ui/component/MainScreen.kt | 7 ++++++- .../ui/component/mainpage/ResultGridCell.kt | 2 +- .../ui/component/mainpage/ResultPanel.kt | 14 +++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/MainScreen.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/MainScreen.kt index 0548506..6ca0b4e 100644 --- a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/MainScreen.kt +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/MainScreen.kt @@ -1,10 +1,12 @@ package mikufan.cx.songfinder.ui.component import androidx.compose.desktop.ui.tooling.preview.Preview +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.material3.Divider import androidx.compose.runtime.Composable import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.Modifier import mikufan.cx.songfinder.backend.controller.MainScreenController import mikufan.cx.songfinder.backend.db.entity.PvService import mikufan.cx.songfinder.backend.db.entity.PvType @@ -41,6 +43,7 @@ fun RestOfPart(isAllFinished: Boolean) = if (isAllFinished) { } +@OptIn(ExperimentalFoundationApi::class) @Preview @Composable fun PreviewMainScreen() { @@ -73,7 +76,9 @@ fun PreviewMainScreen() { ) ) ) - ) + ) { + DebugUsedCell(it, Modifier.animateItemPlacement()) + } } } } \ No newline at end of file diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultGridCell.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultGridCell.kt index 6f30c07..c181a75 100644 --- a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultGridCell.kt +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultGridCell.kt @@ -14,6 +14,6 @@ fun LazyGridItemScope.ResultGridCell(result: SongSearchResult) { } @Composable -private fun DebugUsedCell(result: SongSearchResult, modifier: Modifier = Modifier) { +fun DebugUsedCell(result: SongSearchResult, modifier: Modifier = Modifier) { Text(result.toString(), modifier = modifier) } \ No newline at end of file diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultPanel.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultPanel.kt index 9b291fe..8100875 100644 --- a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultPanel.kt +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/ui/component/mainpage/ResultPanel.kt @@ -24,7 +24,9 @@ import mikufan.cx.songfinder.ui.theme.spacing fun ResultPanel() { val controller = getSpringBean() val resultList = controller.currentResultState - RealResultPanel(resultList) + RealResultPanel(resultList) { result -> + ResultGridCell(result) + } } /** @@ -34,15 +36,17 @@ fun ResultPanel() { * @param modifier The modifier to be applied to the panel. */ @Composable -fun RealResultPanel(resultList: List, modifier: Modifier = Modifier) { +fun RealResultPanel( + resultList: List, + modifier: Modifier = Modifier, + cellContent: @Composable LazyGridItemScope.(SongSearchResult) -> Unit, +) { if (resultList.isEmpty()) { RowCentralizedWithSpacing(furtherModifier = modifier) { Text("No result found", style = MaterialTheme.typography.titleMedium) } } else { - ResultPanelGrid(resultList, modifier) { result -> - ResultGridCell(result) - } + ResultPanelGrid(resultList, modifier, cellContent) } }