From 517285854e7739e746804b42fb33185bc0528365 Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Wed, 21 Feb 2024 16:09:49 +0530 Subject: [PATCH] update stage item ui --- .../teidata/teievents/EventAdapter.kt | 11 +- .../teidata/teievents/StageViewHolder.kt | 136 ++++++++++-------- .../main/res/layout/item_stage_section.xml | 87 ----------- 3 files changed, 81 insertions(+), 153 deletions(-) delete mode 100644 app/src/main/res/layout/item_stage_section.xml diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/EventAdapter.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/EventAdapter.kt index ee383c76303..e4802c19f1e 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/EventAdapter.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/EventAdapter.kt @@ -12,6 +12,7 @@ import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor import org.dhis2.R import org.dhis2.commons.data.EventViewModel +import org.dhis2.commons.data.EventViewModelType import org.dhis2.commons.data.EventViewModelType.EVENT import org.dhis2.commons.data.EventViewModelType.STAGE import org.dhis2.commons.data.EventViewModelType.TOGGLE_BUTTON @@ -19,7 +20,6 @@ import org.dhis2.commons.data.EventViewModelType.values import org.dhis2.commons.data.StageSection import org.dhis2.commons.resources.ColorUtils import org.dhis2.databinding.ItemEventBinding -import org.dhis2.databinding.ItemStageSectionBinding import org.dhis2.usescases.teiDashboard.dashboardfragments.teidata.TEIDataPresenter import org.hisp.dhis.android.core.enrollment.Enrollment import org.hisp.dhis.android.core.program.Program @@ -59,15 +59,10 @@ class EventAdapter( } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - return when (values()[viewType]) { + return when (EventViewModelType.entries[viewType]) { STAGE -> { - val binding = ItemStageSectionBinding.inflate( - LayoutInflater.from(parent.context), - parent, - false, - ) StageViewHolder( - binding, + ComposeView(parent.context), stageSelector, presenter, colorUtils, diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/StageViewHolder.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/StageViewHolder.kt index 772182914fe..1d03a7c3c09 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/StageViewHolder.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/teievents/StageViewHolder.kt @@ -1,9 +1,27 @@ package org.dhis2.usescases.teiDashboard.dashboardfragments.teidata.teievents -import android.view.View -import androidx.compose.ui.platform.ViewCompositionStrategy +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.material.LocalTextStyle +import androidx.compose.material.Text +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.recyclerview.widget.RecyclerView -import com.google.android.material.composethemeadapter.MdcTheme import io.reactivex.processors.FlowableProcessor import org.dhis2.R import org.dhis2.commons.data.EventViewModel @@ -11,33 +29,21 @@ import org.dhis2.commons.data.StageSection import org.dhis2.commons.resources.ColorType import org.dhis2.commons.resources.ColorUtils import org.dhis2.commons.resources.ResourceManager -import org.dhis2.databinding.ItemStageSectionBinding +import org.dhis2.ui.MetadataIcon import org.dhis2.ui.MetadataIconData -import org.dhis2.ui.setUpMetadataIcon import org.dhis2.usescases.teiDashboard.dashboardfragments.teidata.TEIDataPresenter import org.dhis2.usescases.teiDashboard.ui.NewEventOptions internal class StageViewHolder( - private val binding: ItemStageSectionBinding, + val composeView: ComposeView, private val stageSelector: FlowableProcessor, private val presenter: TEIDataPresenter, private val colorUtils: ColorUtils, -) : RecyclerView.ViewHolder(binding.root) { - - init { - binding.composeProgramStageIcon.setViewCompositionStrategy( - ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed, - ) - } +) : RecyclerView.ViewHolder(composeView) { fun bind(eventItem: EventViewModel) { val stage = eventItem.stage!! - binding.programStageName.text = stage.displayName() - binding.programStageName.post { - binding.programStageName.isSelected = true - } - val color = colorUtils.getColorFrom( stage.style().color(), colorUtils.getPrimaryColor( @@ -54,48 +60,62 @@ internal class StageViewHolder( R.drawable.ic_default_outline, ) - binding.composeProgramStageIcon.setUpMetadataIcon( - MetadataIconData( - programColor = color, - iconResource = iconResource, - sizeInDp = 40, - ), - false, - ) - - binding.addStageButton.visibility = - if (eventItem.canShowAddButton()) { - View.VISIBLE - } else { - View.GONE - } - - binding.addStageButton.setContent { - MdcTheme { - NewEventOptions(presenter.getNewEventOptionsByStages(stage)) { - presenter.onAddNewEventOptionSelected(it, stage) + composeView.setContent { + Row( + modifier = Modifier + .fillMaxWidth() + .background(color = Color.White) + .padding(horizontal = 16.dp, vertical = 12.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + MetadataIcon( + metadataIconData = MetadataIconData( + programColor = color, + iconResource = iconResource, + sizeInDp = 40, + ) + ) + Spacer(modifier = Modifier.width(16.dp)) + Column( + modifier = Modifier + .weight(1f) + ) { + Text( + text = stage.displayName() ?: "", + color = colorResource(id = R.color.textPrimary), + fontSize = 14.sp, + style = LocalTextStyle.current.copy( + fontFamily = FontFamily(Font(R.font.rubik_regular)), + ), + ) + if (eventItem.eventCount < 1) { + Text( + text = stringResource(R.string.no_data), + color = colorResource(id = R.color.textSecondary), + fontSize = 12.sp, + style = LocalTextStyle.current.copy( + fontFamily = FontFamily(Font(R.font.rubik_regular)), + ), + ) + } + } + if (eventItem.canShowAddButton()) { + Box(modifier = Modifier + .clickable { + stageSelector.onNext( + StageSection( + stageUid = stage.uid(), + showOptions = true, + showAllEvents = false, + ), + ) + }) { + NewEventOptions(presenter.getNewEventOptionsByStages(stage)) { + presenter.onAddNewEventOptionSelected(it, stage) + } + } } } } - binding.addStageButton.setOnClickListener { - stageSelector.onNext( - StageSection( - stageUid = stage.uid(), - showOptions = true, - showAllEvents = false, - ), - ) - } - - binding.programNoStageText.visibility = - if (eventItem.eventCount < 1) { - View.VISIBLE - } else { - View.GONE - } - - if (eventItem.isSelected) { - eventItem.isSelected = false - } } } diff --git a/app/src/main/res/layout/item_stage_section.xml b/app/src/main/res/layout/item_stage_section.xml deleted file mode 100644 index aada2c440a0..00000000000 --- a/app/src/main/res/layout/item_stage_section.xml +++ /dev/null @@ -1,87 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file