Skip to content

Commit

Permalink
✨ Added controller parameters to Composables for testability
Browse files Browse the repository at this point in the history
Refactored several composable functions within our main UI package by passing controllers as default parameters. This change enhances our tests' flexibility as it allows for the use of staged controllers and objects other than the real controller objects. Additionally, an overflow property with Ellipsis was added to the FinishMessagePanel.
  • Loading branch information
CXwudi committed Dec 2, 2023
1 parent cbeebaf commit 9957f9e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow

@Composable
fun FinishMessagePanel(modifier: Modifier = Modifier) {
Text(
"All Done!",
style = MaterialTheme.typography.titleMedium,
overflow = TextOverflow.Ellipsis,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import mikufan.cx.songfinder.ui.theme.MyAppThemeWithSurface
import java.time.LocalDateTime

@Composable
fun MainScreen() {
val finishedState = getSpringBean<MainScreenController>().isFinishedState
fun MainScreen(
mainScreenController: MainScreenController = getSpringBean(),
modifier: Modifier = Modifier,
) {
val finishedState = mainScreenController.isFinishedState
RealMainScreen(finishedState)
}

Expand Down Expand Up @@ -66,7 +69,7 @@ fun PreviewMainScreen() {
{},
)
)
RealRegexMatchOption(SearchRegexOption.Exact, {})
RealRegexMatchOption(mutableStateOf(SearchRegexOption.Exact)) {}
RealResultPanel(
listOf(
SongSearchResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import mikufan.cx.songfinder.ui.theme.MyAppThemeWithSurface


@Composable
fun ProgressBar(modifier: Modifier = Modifier) {
val controller = getSpringBean<ProgressBarController>()
fun ProgressBar(
controller: ProgressBarController = getSpringBean(),
modifier: Modifier = Modifier
) {
val currentCount by controller.currentIndexState
val totalCount = remember { controller.totalCount }
RealProgressBar(currentCount, totalCount, modifier)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package mikufan.cx.songfinder.ui.component.main

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.State
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -24,10 +23,11 @@ import mikufan.cx.songfinder.ui.theme.spacing
*
*/
@Composable
fun RegexMatchOption() {
val controller = getSpringBean<RegexMatchOptionController>()
val option by controller.currentRegexOptionState
RealRegexMatchOption(option, controller::setRegexOption)
fun RegexMatchOption(
controller: RegexMatchOptionController = getSpringBean(),
modifier: Modifier = Modifier,
) {
RealRegexMatchOption(controller.currentRegexOptionState, controller::setRegexOption)
}


Expand All @@ -38,7 +38,8 @@ fun RegexMatchOption() {
* @param onOptionSet The callback function called when a regex option is selected.
*/
@Composable
fun RealRegexMatchOption(option: SearchRegexOption, onOptionSet: suspend (SearchRegexOption) -> Unit) = RowCentralizedWithSpacing(
fun RealRegexMatchOption(option: State<SearchRegexOption>, onOptionSet: suspend (SearchRegexOption) -> Unit) =
RowCentralizedWithSpacing(
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.spacing)
) {
Text("Regex Match Option: ")
Expand All @@ -51,22 +52,21 @@ fun RealRegexMatchOption(option: SearchRegexOption, onOptionSet: suspend (Search
* Composable function to render a single regex match option.
*
* @param renderedOption The regex option to render.
* @param selectedOption The currently selected regex option.
* @param selectedOptionState The currently selected regex option.
* @param onOptionSet The callback function called when a regex option is selected.
*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RegexMatchOptionButton(
renderedOption: SearchRegexOption,
selectedOption: SearchRegexOption,
selectedOptionState: State<SearchRegexOption>,
onOptionSet: suspend (SearchRegexOption) -> Unit
) = Row(
modifier = Modifier,
verticalAlignment = Alignment.CenterVertically
) {
val scope = rememberCoroutineScope()
RadioButton(
selected = renderedOption == selectedOption,
selected = renderedOption == selectedOptionState.value,
onClick = { scope.launch { onOptionSet(renderedOption) } }
)
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import mikufan.cx.songfinder.ui.theme.spacing
* Retrieves the current result state from the controller and passes it to the RealResultPanel composable.
*/
@Composable
fun ResultPanel() {
val controller = getSpringBean<ResultPanelController>()
fun ResultPanel(
controller: ResultPanelController = getSpringBean<ResultPanelController>(),
modifier: Modifier = Modifier,
) {
val resultList = controller.currentResultState
RealResultPanel(resultList) { result ->
RealResultPanel(resultList, modifier) { result ->
ResultGridCell(result)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import mikufan.cx.songfinder.ui.common.TooltipAreaWithCard
* @param modifier The modifier to be applied to the search bar.
*/
@Composable
fun SearchBar(modifier: Modifier = Modifier) {
val controller = getSpringBean<SearchBarController>()
fun SearchBar(
controller: SearchBarController = getSpringBean(),
modifier: Modifier = Modifier
) {
val inputState = controller.currentInputState
val searchStatusState = controller.currentSearchStatusState
val model = SearchBarModel(
Expand Down

0 comments on commit 9957f9e

Please sign in to comment.