Skip to content

Commit

Permalink
✨ Implement song handling in ResultCellController, finally this becom…
Browse files Browse the repository at this point in the history
…es a usable app

This commit introduces the method handleRecord in ResultCellController, which writes the chosen song to csv and reads the next song title from the input file. If no more songs are present, the function logs this occurrence. The commit also updated the RealResultGridCell and ResultGridCell functions in ResultGridCell.kt to use the newly introduced handleRecord method. The purpose of these changes is to implement functionality that allows the user to process song search results one by one.
  • Loading branch information
CXwudi committed Dec 3, 2023
1 parent 0a1e6f8 commit 3b75df7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package mikufan.cx.songfinder.backend.controller.mainpage

import mikufan.cx.inlinelogging.KInlineLogging
import mikufan.cx.songfinder.backend.model.SongSearchResult
import mikufan.cx.songfinder.backend.service.InputFileLineReader
import mikufan.cx.songfinder.backend.service.OutputCsvLineWriter
import mikufan.cx.songfinder.backend.statemodel.ProgressStateModel
import mikufan.cx.songfinder.backend.statemodel.SearchInputStateModel
import mikufan.cx.songfinder.backend.statemodel.SearchOptionsStateModel
import mikufan.cx.songfinder.backend.statemodel.SearchRegexOption
import org.springframework.stereotype.Controller

@Controller
class ResultCellController(

private val csvLineWriter: OutputCsvLineWriter,
private val progressStateModel: ProgressStateModel,
private val inputFileLineReader: InputFileLineReader,
private val inputStateModel: SearchInputStateModel,
private val searchOptionsStateModel: SearchOptionsStateModel,
private val songSearchIntermediateController: SongSearchIntermediateController,
) {

// suspend fun
}
suspend fun handleRecord(chosenSong: SongSearchResult) {
// First, write the chosen song to csv
csvLineWriter.writeSongId(chosenSong.id.toULong())
progressStateModel.increment()

// Then, read the next song title from the input file. If no more song, then we are done
val nextSongTitle = inputFileLineReader.readNext()
if (nextSongTitle == null) {
log.info { "No more song to handle, the app should display a congratulation screen soon" }
} else {
inputStateModel.update(nextSongTitle)
searchOptionsStateModel.searchRegexOptionState.value = SearchRegexOption.Contains
songSearchIntermediateController.triggerSearch(0)
}
}
}

private val log = KInlineLogging.logger()
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun PreviewMainScreen() {
)
)
) {
RealResultGridCell(it)
RealResultGridCell(it, ResultCellCallbacks({}, { "" }))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,34 @@ import compose.icons.simpleicons.Niconico
import compose.icons.simpleicons.Soundcloud
import compose.icons.simpleicons.Youtube
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.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.format.DateTimeFormatter

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyGridItemScope.ResultGridCell(result: SongSearchResult) {
RealResultGridCell(result, Modifier.animateItemPlacement())
fun LazyGridItemScope.ResultGridCell(
result: SongSearchResult,
controller: ResultCellController = getSpringBean(),
) {
val callbacks = ResultCellCallbacks(
onCardClicked = { controller.handleRecord(it) },
provideThumbnailUrl = { TODO() }
)
RealResultGridCell(result, callbacks, Modifier.animateItemPlacement())
}

@Composable
fun LazyGridItemScope.RealResultGridCell(result: SongSearchResult, modifier: Modifier = Modifier) {
fun LazyGridItemScope.RealResultGridCell(
result: SongSearchResult,
callbacks: ResultCellCallbacks,
modifier: Modifier = Modifier
) {
val filteredPvs = result.pvs.filter {
it.pvService in listOf(
PvService.Youtube,
Expand All @@ -54,15 +67,19 @@ fun LazyGridItemScope.RealResultGridCell(result: SongSearchResult, modifier: Mod
PvService.Bilibili
)
}
MusicCardTemplate({}, modifier) {
ThumbnailImage(filteredPvs)
MusicCardTemplate(onCardClicked = { callbacks.onCardClicked(result) }, modifier) {
LazyThumbnailImage(result, filteredPvs)
MusicInfo(result, filteredPvs)
}
}

@Composable
fun ThumbnailImage(pvs: List<PVInfo>) {
//TODO: use the first ever available PV's thumbnail, if no PVs, use image not found. If no available PVs, use
fun LazyThumbnailImage(
result: SongSearchResult,
pvs: List<PVInfo>
) {
//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
Image(
painter = painterResource("image/image-not-found-icon.svg"),
contentDescription = "Thumbnail",
Expand Down Expand Up @@ -173,6 +190,11 @@ private fun PvRows(pvs: List<PVInfo>) {

/* --- Utils ---*/

data class ResultCellCallbacks(
val onCardClicked: suspend (SongSearchResult) -> Unit,
val provideThumbnailUrl: suspend (SongSearchResult) -> String,
)

internal fun getArtistString(vocals: List<String>, producers: List<String>): String {
val vocalString = vocals.joinToString(", ")
val producerString = producers.joinToString(", ")
Expand Down

0 comments on commit 3b75df7

Please sign in to comment.