Skip to content

Commit

Permalink
feat: implements limits for BodyPart's and MetricSystemUnit's in Fiel…
Browse files Browse the repository at this point in the history
…ds* modification
  • Loading branch information
cromega08 committed Dec 16, 2024
1 parent 19ff8ef commit 116a9a1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cromega.studio.measurepedia.ui.activities.fields

import android.widget.Toast
import cromega.studio.measurepedia.ui.activities.generic.Activity
import cromega.studio.measurepedia.ui.activities.home.HomeActivity

Expand All @@ -22,10 +23,21 @@ class FieldsActivity : Activity<FieldsViewModel, FieldsScreen>()
screen =
FieldsScreen(
viewModel = viewModel,
resources = resources
resources = resources,
showToastFunction = ::showInsufficientToast
)
}

private fun showInsufficientToast(stringId: Int)
{
val toast: Toast = Toast(applicationContext)

toast.setText(stringId)
toast.duration = Toast.LENGTH_SHORT

toast.show()
}

private fun openHomeFunction() =
changeActivity(activityToLoad = HomeActivity::class)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import cromega.studio.measurepedia.ui.components.layouts.GenericHeaderColumn

class FieldsScreen(
viewModel: FieldsViewModel,
resources: Resources
resources: Resources,
private val showToastFunction: (Int) -> Unit
): ActivityScreen<FieldsViewModel>(
viewModel = viewModel,
resources = resources
Expand Down Expand Up @@ -224,7 +225,11 @@ class FieldsScreen(
bottom.linkTo(bodyPartActiveRef.bottom)
end.linkTo(anchor = parent.end, margin = 7.5.dp)
},
onClick = { viewModel.removeBodyPartAndPropagate(bodyPart = bodyPart) },
onClick = {
if (viewModel.validateBodyParts())
viewModel.removeBodyPartAndPropagate(bodyPart = bodyPart)
else showToastFunction(R.string.insufficient_body_parts)
},
content = { ClearIcon() }
)

Expand Down Expand Up @@ -328,7 +333,11 @@ class FieldsScreen(
bottom.linkTo(fieldActiveRef.bottom)
end.linkTo(anchor = parent.end)
},
onClick = { viewModel.removeField(field = field) },
onClick = {
if (viewModel.validateFields(bodyPart = bodyPart))
viewModel.removeField(field = field)
else showToastFunction(R.string.insufficient_fields)
},
content = { ClearIcon() }
)
}
Expand Down Expand Up @@ -434,7 +443,11 @@ class FieldsScreen(
Text(text = resources.getString(R.string.delete_unit))

FilledIconButton(
onClick = { viewModel.removeMetricSystemUnit(metricSystemUnit = metricSystemUnit) },
onClick = {
if (viewModel.validateMetricSystemUnits())
viewModel.removeMetricSystemUnit(metricSystemUnit = metricSystemUnit)
else showToastFunction(R.string.insufficient_metric_systems_units)
},
content = { ClearIcon() }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class FieldsViewModel(
functionToApply = { this.active = isActive }
)

fun validateBodyParts(): Boolean = bodyParts.size > 1

fun removeBodyPartAndPropagate(bodyPart: BodyPart)
{
bodyPartsState.remove(bodyPart)
Expand Down Expand Up @@ -274,6 +276,15 @@ class FieldsViewModel(
filterFieldsByBodyPartId(bodyPartId)
.forEach { field -> removeField(field) }

fun validateFields(bodyPart: BodyPart): Boolean
{
val relatedFields: List<Field> =
fieldsState
.filter { field -> field.bodyPartId == bodyPart.id }

return relatedFields.size > 1
}

fun removeField(field: Field)
{
fieldsState.remove(field)
Expand Down Expand Up @@ -313,6 +324,8 @@ class FieldsViewModel(
functionToApply = { this.abbreviation = newAbbreviation }
)

fun validateMetricSystemUnits(): Boolean = metricSystemUnits.size > 1

fun removeMetricSystemUnit(metricSystemUnit: MetricSystemUnit)
{
metricSystemsUnitsState.remove(metricSystemUnit)
Expand Down Expand Up @@ -408,8 +421,8 @@ class FieldsViewModel(

private fun updateMetricSystemsUnitsData()
{
flushDeleteMetricSystemUnits()
flushPositiveOperationsMetricSystemUnits()
flushDeleteMetricSystemUnits()
}

private fun flushDeleteMetricSystemUnits() =
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@
<string name="share">Compartir</string>
<string name="share_person_info_placeholder">Informacion de %s</string>
<string name="share_status_placeholder">Estado: %s</string>
<string name="insufficient_body_parts">Miembros Insuficientes</string>
<string name="insufficient_metric_systems_units">Unidades de Medida Insuficientes</string>
<string name="insufficient_fields">Campos Insuficientes</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@
<string name="share">Share</string>
<string name="share_person_info_placeholder">%s Information</string>
<string name="share_status_placeholder">Status: %s</string>
<string name="insufficient_body_parts">Insufficient Body Parts</string>
<string name="insufficient_fields">Insufficient Fields</string>
<string name="insufficient_metric_systems_units">Insufficient Metric Systems Units</string>
</resources>

0 comments on commit 116a9a1

Please sign in to comment.