在我的应用程序中,我提升了每个主要功能,然后我得到了一个巨大的可组合函数:
主屏幕.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函数,但在我的情况下,我觉得我所应用的方法是合适的。准确地说,TopSection是HomeScreen中的一个部分,我在其中处理了几个小功能。
2条答案
按热度按时间slwdgvem1#
您可以通过以下方式将数据封装在数据类中:
在您的视图模型中:
在可组合函数中,可以通过以下方式收集buttonUiState:
tpgth1q72#
首先创建
data class or normal class
并添加一些参数然后在
TopSection
组合函数中传递此数据类也可以从
Text
组合函数中获取引用,其中它将TextStyle
作为color , font , fontWeight etc
的参数,并且它是class
👇希望你明白这件事。