Skip to content

Commit

Permalink
♻️ Refactor ResultPanel UI component for custom grid cells
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
CXwudi committed Nov 28, 2023
1 parent ecd2051 commit 61e2c7c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,6 +43,7 @@ fun RestOfPart(isAllFinished: Boolean) = if (isAllFinished) {
}


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun PreviewMainScreen() {
Expand Down Expand Up @@ -73,7 +76,9 @@ fun PreviewMainScreen() {
)
)
)
)
) {
DebugUsedCell(it, Modifier.animateItemPlacement())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import mikufan.cx.songfinder.ui.theme.spacing
fun ResultPanel() {
val controller = getSpringBean<ResultPanelController>()
val resultList = controller.currentResultState
RealResultPanel(resultList)
RealResultPanel(resultList) { result ->
ResultGridCell(result)
}
}

/**
Expand All @@ -34,15 +36,17 @@ fun ResultPanel() {
* @param modifier The modifier to be applied to the panel.
*/
@Composable
fun RealResultPanel(resultList: List<SongSearchResult>, modifier: Modifier = Modifier) {
fun RealResultPanel(
resultList: List<SongSearchResult>,
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)
}
}

Expand Down

0 comments on commit 61e2c7c

Please sign in to comment.