I wrote a composable function in kotlin compose. This composable function takes an ArrayList, in the same way, on another screen, there is an ArrayList that takes the same composable but a different data class.
What I want to do here is to have a composable to avoid code duplication because the codes are the same, only the type of the arraylist it receives is different as data class.
My composables are like this
ItemsCategory composable
@Composable
fun ItemsCategory(
items: ArrayList<HealthCategoryDetailItems>
) {
val boxQueryWidth = ScreenQuery(
limits = arrayListOf(
ScreenQueryLimit(
max = 321,
value = 135,
),
ScreenQueryLimit(
min = 321,
max = 361,
value = 160,
)
),
direction = ScreenDirection.HORIZONTAL,
defaultValue = 170
).value().dp
val itemsList = items.chunked(2)
itemsList.forEach { listItem ->
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 15.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
listItem.forEach { item ->
HealthItem(
width = boxQueryWidth,
imageSize = boxQueryWidth,
layoutPadding = 0.dp,
itemId = item.articleTypeId,
itemName = item.title ?: "",
image = item.image ?: "",
onClick = { itemId, itemName -> /* Handle onClick logic */ },
detailText = item.date,
detailTextColor = MaterialTheme.colors.grayColor,
shouldShowAdditionalImage = item.articleTypeId == 0,
additionalImg = painterResource(id = R.drawable.ic_home_content_recipe),
content = {}
)
}
}
}
}
ItemsCategory VM
private fun getHealthCategoryDetail(healthCategoryDetailRequestModel: HealthCategoryDetailRequestModel) {
healthCategoryDetailUseCase.invoke(healthCategoryDetailRequestModel).onEach { result ->
val data = result.data
when (result) {
is Resource.Success -> {
_state.update {
it.copy(
isLoading = false,
response = data,
error = data?.isError == 1
)
}
}
...
data class HealthCategoryDetailScreenState(
val isLoading:Boolean = false,
val error:Boolean = false,
val response: HealthCategoryDetailResponseModel?=null
)
TagDetailItem composable
@Composable
fun TagDetailItem(
items: ArrayList<HealthTagDetailItems>
) {
val boxQueryWidth = ScreenQuery(
limits = arrayListOf(
ScreenQueryLimit(
max = 321,
value = 135,
),
ScreenQueryLimit(
min = 321,
max = 361,
value = 160,
)
),
direction = ScreenDirection.HORIZONTAL,
defaultValue = 170
).value().dp
val itemsList = items.chunked(2)
itemsList.forEach { listItem ->
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 15.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
listItem.forEach { item ->
HealthItem(
width = boxQueryWidth,
imageSize = boxQueryWidth,
layoutPadding = 0.dp,
itemId = item.articleTypeId,
itemName = item.title ?: "",
image = item.image ?: "",
onClick = { itemId, itemName -> /* Handle onClick logic */ },
detailText = item.date,
detailTextColor = MaterialTheme.colors.grayColor,
shouldShowAdditionalImage = item.articleTypeId == 0,
additionalImg = painterResource(id = R.drawable.ic_home_content_recipe),
content = {}
)
}
}
}
}
TagDetailItem VM
private fun getHealthCategoryDetail(healthTagDetailRequestModel: HealthContentDetailTagRequestModel) {
tagDetailUseCase.invoke(healthTagDetailRequestModel).onEach { result ->
val data = result.data
when (result) {
is Resource.Success -> {
_state.update {
it.copy(
isLoading = false,
response = data,
error = data?.isError == 1
)
}
}
...
data class HealthTagDetailScreenState(
val isLoading: Boolean = false,
val error: Boolean = false,
val response: HealthContentDetailTagResponseModel? = null
)
Data classes
HealthTagDetailItems for TagDetailItem composable
data class HealthContentDetailTagResponseModel(
@SerializedName("is_error" ) var isError : Int? = null,
@SerializedName("data" ) var data : Data? = Data(),
@SerializedName("description" ) var description : String? = null
)
data class HealthTagDetailItems (
@SerializedName("article_type_id" ) var articleTypeId : Int? = null,
@SerializedName("date" ) var date : String? = null,
@SerializedName("image" ) var image : String? = null,
@SerializedName("id" ) var id : Int? = null,
@SerializedName("title" ) var title : String? = null
)
data class Data (
@SerializedName("article_type_name" ) var articleTypeName : String? = null,
@SerializedName("article_type_id" ) var articleTypeId : Int? = null,
@SerializedName("total_page" ) var totalPage : Int? = null,
@SerializedName("items" ) var items : ArrayList<HealthTagDetailItems> = arrayListOf()
)
HealthCategoryDetailItems for ItemsCategory composable
data class HealthCategoryDetailResponseModel(
@SerializedName("is_error" ) var isError : Int? = null,
@SerializedName("data" ) var data : HealthCategoryDetailData? = HealthCategoryDetailData(),
@SerializedName("description" ) var description : String? = null
)
data class HealthCategoryDetailItems (
@SerializedName("article_type_id" ) override var articleTypeId : Int? = null,
@SerializedName("date" ) override var date : String? = null,
@SerializedName("image" ) override var image : String? = null,
@SerializedName("id" ) override var id : Int? = null,
@SerializedName("title" ) override var title : String? = null
)
data class HealthCategoryDetailData (
@SerializedName("article_type_name" ) var articleTypeName : String? = null,
@SerializedName("article_type_id" ) var articleTypeId : Int? = null,
@SerializedName("total_page" ) var totalPage : Int? = null,
@SerializedName("items" ) var items : ArrayList<HealthCategoryDetailItems> = arrayListOf()
)