android Jetpack compose如何减少可组合函数中的参数数量(干净代码)?

5us2dqdw  于 2023-03-06  发布在  Android
关注(0)|答案(2)|浏览(137)

在我的应用程序中,我提升了每个主要功能,然后我得到了一个巨大的可组合函数:

主屏幕.kt

TopSection(
                filterParametersState = filterParametersState,
                itineraryCount = flightsFromApiResponse.result?.itineraryCount ?: 0,
                buttonNames = viewModel.buttonNames,
                buttonNamesFilters = viewModel.buttonNamesFilters,
                selectedButtonIndex = viewModel.selectedButtonIndex.value,
                selectedButtonName = viewModel.selectedButtonName.value,
                isThemeSwitchChecked = themeViewModel.isDarkTheme,
                selectedSort = viewModel.selectedSort.value,
                updateSelectedSort = { sortName -> viewModel.updateSelectedSort(sortName) },
                getLocation = { cityName -> viewModel.getLocation(cityName) },
                updateFlightSearchDepartureTime = { date ->
                    viewModel.updateFlightSearchDepartureTime(
                        date = date
                    )
                },
                updateFlightSearchCityDeparture = {
                    viewModel.updateFlightSearchCityDeparture(
                        city = viewModel.location.value.cityCode ?: "WAR"
                    )
                },
                updateFlightSearchCityArrival = {
                    viewModel.updateFlightSearchCityArrival(
                        city = viewModel.location.value.cityCode ?: "PAR"
                    )
                },
                updateFlightSearchPassengersCount = { passengerCount ->
                    viewModel.updateFlightSearchPassengersCount(
                        passengers = passengerCount
                    )
                },
                onDisableNextDayArrivalsClicked = { isDisabled ->
                    viewModel.updateFilterDisableNextDayArrivals(
                        isDisabled
                    )
                },
                onDurationButtonClicked = { buttonName ->
                    viewModel.updateFilterMaxDuration(
                        buttonName
                    )
                },
                onThemeSwitchClicked = { themeViewModel.switchTheme() },
                onSliderValueChange = { valueFromSlider ->
                    viewModel.updateFilterMaxPrice(
                        valueFromSlider
                    )
                },
                onParamsUpperClicked = { buttonIndex, buttonName ->
                    viewModel.updateSelectedButtonIndex(buttonIndex)
                    viewModel.updateSelectedButtonName(buttonName)
                },
                onParamsBottomClicked = { buttonName ->
                    if (buttonName != "SAVE") {
                        viewModel.updateSelectedButtonName(buttonName)
                    } else {
                        commonViewModel.updateCurrentFlightParams(flightSearchParametersState)
                        viewModel.getFlights()
                    }
                }
            )

我该如何避免这种情况呢?我知道从技术上讲,我可以将TopSection拆分为其他较小的Composable函数,但在我的情况下,我觉得我所应用的方法是合适的。准确地说,TopSectionHomeScreen中的一个部分,我在其中处理了几个小功能。

slwdgvem

slwdgvem1#

您可以通过以下方式将数据封装在数据类中:

在您的视图模型中:

class MyViewModel : ViewModel() {
    private val _buttonUiState = MutableStateFlow(ButtonUiState())
    val buttonUiState: StateFlow<ButtonUiState>
        get() = _buttonUiState.asStateFlow()
    
    fun setNewState(index: Int) {
        _buttonUiState.update { 
            it.copy(
                buttonIndex = index
            )
        }
    }
}

data class ButtonUiState(
    val buttonIndex: Int = 0,
    val buttonNames: List<String> = listOf()
    ...
)

在可组合函数中,可以通过以下方式收集buttonUiState:

@Composable
fun ParentComposable(
    viewModel: MyViewModel
) {
    val buttonUiState by viewModel.buttonUiState.collectAsState()
    TopSection(
        buttonUiState = buttonUiState
        ...
    )
}
tpgth1q7

tpgth1q72#

首先创建data class or normal class并添加一些参数

data class SomeContent(
 ...
 ...
)

然后在TopSection组合函数中传递此数据类

TopSection(
     someContent:SomeContent
    ){
....
....
    }

也可以从Text组合函数中获取引用,其中它将TextStyle作为color , font , fontWeight etc的参数,并且它是class👇

Text(text = "Hey", style = TextStyle(
        color = Color.Red,
        fontWeight = FontWeight.SemiBold,
        fontSize = 20.sp
    ))

希望你明白这件事。

相关问题