Skip to content

Commit

Permalink
Fix currently taking and medications that might help sections (#176)
Browse files Browse the repository at this point in the history
# Currently taking and medications that might help sections

## ♻️ Current situation & Problem
- Adapted according to iOS implementation:
https://github.com/StanfordBDHG/ENGAGE-HF-iOS/blob/main/ENGAGEHF/Medications/MedicationsList.swift#L38
- Fixes #156 


## ⚙️ Release Notes 
*Add a bullet point list summary of the feature and possible migration
guides if this is a breaking change so this section can be added to the
release notes.*
*Include code snippets that provide examples of the feature implemented
or links to the documentation if it appends or changes the public
interface.*


## 📚 Documentation
*Please ensure that you properly document any additions in conformance
to [Spezi Documentation
Guide](https://github.com/StanfordSpezi/.github/blob/main/DOCUMENTATIONGUIDE.md).*
*You can use this section to describe your solution, but we encourage
contributors to document your reasoning and changes using in-line
documentation.*


## ✅ Testing
*Please ensure that the PR meets the testing requirements set by CodeCov
and that new functionality is appropriately tested.*
*This section describes important information about the tests and why
some elements might not be testable.*


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [ ] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
eldcn authored Jan 28, 2025
1 parent 0e0d806 commit 29f28ae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,15 @@ fun MedicationCard(
}
}
}
if (model.dosageInformation != null) {
HorizontalDivider(
modifier = Modifier.padding(
top = Spacings.small,
bottom = Spacings.small
)
HorizontalDivider(
modifier = Modifier.padding(
top = Spacings.small,
bottom = Spacings.small
)
DosageInformation(dosageInformationUiModel = model.dosageInformation)
VerticalSpacer()
MedicationProgressBar(progress = model.dosageInformation.progress)
}
)
DosageInformation(dosageInformationUiModel = model.dosageInformation)
VerticalSpacer()
MedicationProgressBar(progress = model.dosageInformation.progress)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class MedicationRecommendation(
val description: String,
val type: MedicationRecommendationType,
val videoPath: String?,
val dosageInformation: DosageInformation?,
val dosageInformation: DosageInformation,
)

@Suppress("MagicNumber")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ class MedicationRecommendationMapper @Inject constructor(
}
}

private fun getDosageInformation(jsonMap: JsonMap?): DosageInformation? {
private fun getDosageInformation(jsonMap: JsonMap?): DosageInformation {
val dosageInformationMap = jsonMap?.get("dosageInformation") as? JsonMap
val unit = dosageInformationMap?.get("unit") as? String
val currentScheduleMap = getDosage(key = "currentSchedule", jsonMap = dosageInformationMap)
val targetSchedule = getDosage(key = "targetSchedule", jsonMap = dosageInformationMap)
return if (unit != null) {
DosageInformation(
currentSchedule = currentScheduleMap,
targetSchedule = targetSchedule,
unit = unit,
)
} else {
null
}
return DosageInformation(
currentSchedule = currentScheduleMap,
targetSchedule = targetSchedule,
unit = unit ?: "",
)
}

private fun getDosage(key: String, jsonMap: JsonMap?): List<DoseSchedule> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class MedicationCardUiModel(
val isExpanded: Boolean,
val statusIconResId: Int?,
val statusColor: MedicationColor,
val dosageInformation: DosageInformationUiModel?,
val dosageInformation: DosageInformationUiModel,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class MedicationUiStateMapper @Inject constructor(
MedicationUiState.Success(
medicationsTaking =
Medications(
medications = recommendations.filter { it.type != MedicationRecommendationType.NOT_STARTED }
medications = recommendations.filter { it.dosageInformation.currentDailyIntake > 0.0 }
.sortedByDescending { it.type.priority }
.map { map(it) },
expanded = true,
),
medicationsThatMayHelp = Medications(
medications = recommendations.filter { it.type == MedicationRecommendationType.NOT_STARTED }
medications = recommendations.filter { it.dosageInformation.currentDailyIntake == 0.0 }
.sortedByDescending { it.type.priority }
.map { map(it) },
expanded = false,
Expand Down Expand Up @@ -134,8 +134,7 @@ class MedicationUiStateMapper @Inject constructor(
}
}

private fun mapDosageInformation(dosageInformation: DosageInformation?): DosageInformationUiModel? {
dosageInformation ?: return null
private fun mapDosageInformation(dosageInformation: DosageInformation): DosageInformationUiModel {
val currentDailyIntake = dosageInformation.currentDailyIntake
val targetDailyIntake = dosageInformation.targetDailyIntake
val progress = if (targetDailyIntake == 0.0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,31 @@ class MedicationUiStateMapperTest {
}

@Test
fun `given medication details when mapMedicationUiState then return filtered success state`() {
fun `it should map medications taking and that may help correctly if current daily intake is greater zero`() {
// given
val recommendations = getRecommendations()
val recommendations = getRecommendations(currentDailyIntake = 20.0)

// when
val result = medicationUiStateMapper.mapMedicationUiState(recommendations)

// then
assertThat(result).isInstanceOf(MedicationUiState.Success::class.java)
assertThat((result as MedicationUiState.Success).medicationsTaking.medications).hasSize(1)
assertThat(result.medicationsTaking.medications[0].id).isEqualTo("1")
assertThat((result).medicationsThatMayHelp.medications).isEmpty()
}

@Test
fun `it should map medications taking and that may help correctly if current daily intake is zero`() {
// given
val recommendations = getRecommendations(currentDailyIntake = 0.0)

// when
val result = medicationUiStateMapper.mapMedicationUiState(recommendations)

// then
assertThat(result).isInstanceOf(MedicationUiState.Success::class.java)
assertThat((result as MedicationUiState.Success).medicationsTaking.medications).isEmpty()
assertThat((result).medicationsThatMayHelp.medications).hasSize(1)
}

@Test
Expand Down Expand Up @@ -108,20 +122,6 @@ class MedicationUiStateMapperTest {
assertThat((result as MedicationUiState.NoData).message).isEqualTo("some-string")
}

@Test
fun `given recommendations with dosage information when mapMedicationUiState then return success state with dosage information`() {
// given
val recommendations = getRecommendationsWithDosage()

// when
val result = medicationUiStateMapper.mapMedicationUiState(recommendations)

// then
assertThat(result).isInstanceOf(MedicationUiState.Success::class.java)
assertThat((result as MedicationUiState.Success).medicationsTaking.medications).hasSize(1)
assertThat(result.medicationsTaking.medications[0].dosageInformation).isNotNull()
}

@Test
fun `given SuccessState when toggleItemExpand then return updated SuccessState`() {
// given
Expand Down Expand Up @@ -171,7 +171,7 @@ class MedicationUiStateMapperTest {
isExpanded: Boolean = false,
statusIconResId: Int? = null,
statusColor: MedicationColor = MedicationColor.BLUE,
dosageInformation: DosageInformationUiModel? = null,
dosageInformation: DosageInformationUiModel = mockk(),
videoPath: String = "",
) = MedicationCardUiModel(
id = id,
Expand All @@ -185,28 +185,7 @@ class MedicationUiStateMapperTest {
videoPath = videoPath
)

private fun getRecommendations() = listOf(
MedicationRecommendation(
id = "2",
title = "Medication B",
subtitle = "Subtitle B",
description = "Description B",
type = MedicationRecommendationType.NOT_STARTED,
dosageInformation = null,
videoPath = null
),
MedicationRecommendation(
id = "1",
title = "Medication A",
subtitle = "Subtitle A",
description = "Description A",
type = MedicationRecommendationType.TARGET_DOSE_REACHED, // higher priority than NOT_STARTED
dosageInformation = null,
videoPath = "/videoSections/1/videos/1"
)
)

private fun getRecommendationsWithDosage() = listOf(
private fun getRecommendations(currentDailyIntake: Double = 1.0) = listOf(
MedicationRecommendation(
id = "1",
title = "Medication A",
Expand All @@ -218,7 +197,7 @@ class MedicationUiStateMapperTest {
currentSchedule = listOf(
DoseSchedule(
frequency = 2.0,
quantity = listOf(20.0)
quantity = listOf(currentDailyIntake)
)
),
targetSchedule = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.google.common.truth.Truth.assertThat
import edu.stanford.bdh.engagehf.bluetooth.data.mapper.MessageActionMapper
import edu.stanford.bdh.engagehf.education.EngageEducationRepository
import edu.stanford.bdh.engagehf.medication.data.MedicationRecommendation
import edu.stanford.bdh.engagehf.medication.data.MedicationRecommendationType
import edu.stanford.bdh.engagehf.medication.data.MedicationRepository
import edu.stanford.bdh.engagehf.messages.MessagesAction
import edu.stanford.spezi.core.navigation.Navigator
Expand All @@ -29,7 +28,7 @@ class MedicationViewModelTest {

private val medicationRepository: MedicationRepository = mockk()
private val medicationUiStateMapper: MedicationUiStateMapper = mockk()
private val recommendations = getMedicationRecommendations()
private val recommendations: List<MedicationRecommendation> = mockk()
private val uiModels: List<MedicationCardUiModel> = mockk()
private val navigator: Navigator = mockk(relaxed = true)
private val engageEducationRepository: EngageEducationRepository = mockk()
Expand Down Expand Up @@ -166,25 +165,4 @@ class MedicationViewModelTest {
// then
verify { navigator.navigateTo(EducationNavigationEvent.VideoSectionClicked(video)) }
}

private fun getMedicationRecommendations() = listOf(
MedicationRecommendation(
id = "1",
title = "Medication A",
subtitle = "Subtitle A",
description = "Description A",
type = MedicationRecommendationType.TARGET_DOSE_REACHED,
dosageInformation = null,
videoPath = null
),
MedicationRecommendation(
id = "2",
title = "Medication B",
subtitle = "Subtitle B",
description = "Description B",
type = MedicationRecommendationType.NOT_STARTED,
dosageInformation = null,
videoPath = null
)
)
}

0 comments on commit 29f28ae

Please sign in to comment.