From 4e72b484667a615d72c796ad759dc954020235d7 Mon Sep 17 00:00:00 2001 From: Chagai Yisroel Wild Date: Tue, 10 Nov 2026 23:25:17 +0200 Subject: [PATCH] feat(measures): add Rav Chaim Naeh, volume measures and metric units - Rav Chaim Naeh as an amma opinion (etzba 2 cm, amma 48 cm), cited from Kitzur Shulchan Aruch Yalkut Yosef, OC 630:4. - A length/volume switch. Volumes: revi'is, log, kav, se'ah, eifah, kor (Rambam Eruvin 1:13, Arachin 4:4). Each opinion's revi'is is its etzba cubed times 2x2x2.7 (Pesachim 109a): Naeh 86.4 ml, Chazon Ish ~152 ml. The Igros Moshe is left out of volumes, since R' Moshe's revi'is is not derived from his amma. - Metric units in the picker: mm, cm, m, km and ml, liter. Between Torah and metric units the result is a range over the opinions shown. - An opinion added after the user saved a choice is shown until unticked. - Fix an init cycle between MeasuresWidget and MeasuresSettings. Signed-off-by: Chagai Yisroel Wild Co-Authored-By: Claude Opus 5.5 --- .../home/widgets/measures/MeasuresWidget.kt | 376 +++++++++++++++--- .../home/widgets/measures/MeasuresTest.kt | 106 ++++- 2 files changed, 433 insertions(+), 49 deletions(-) diff --git a/SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresWidget.kt b/SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresWidget.kt index c437cc21d..a99f93bef 100644 --- a/SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresWidget.kt +++ b/SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresWidget.kt @@ -59,6 +59,7 @@ import java.text.DecimalFormat import java.text.DecimalFormatSymbols import java.util.Locale import java.util.UUID +import kotlin.math.pow /** * A length in one of the Torah's measures converted into another, and how long it is today and how long it takes to @@ -79,17 +80,21 @@ internal object MeasuresWidget : HomeWidget { override val settings get() = MeasuresSettings // The opinions shown, as saved: read by the grid for the card's height as well as by the card - private var shown by mutableStateOf(MeasuresSettings.shown.default) + // Not MeasuresSettings.shown.default: MeasuresSettings needs this object's id, so reading it here is a cycle + private var shown by mutableStateOf(ALL_OPINION_IDS) override fun applySettings(appSettings: AppSettings) { - shown = appSettings[MeasuresSettings.shown] + shown = shownOpinions(appSettings[MeasuresSettings.shown], appSettings[MeasuresSettings.offered]) } - /** Tall enough for every opinion picked, never one hidden for want of room. */ - override fun heightAt(width: Dp): Dp { + /** Tall enough for every opinion picked in either kind, so switching kind never resizes the card. */ + override fun heightAt(width: Dp): Dp = Kind.entries.maxOf { heightOf(it) } + + internal fun heightOf(kind: Kind): Dp { var height = PADDING * 2 + TITLE_HEIGHT + BLOCK_GAP + INPUT_HEIGHT + BLOCK_GAP + RESULT_HEIGHT + LINK_HEIGHT - // A kind with no opinion shown is left out, its title too - for (rows in listOf(AMMA_OPINIONS.count { it.id in shown }, MIL_OPINIONS.count { it.id in shown })) { + // A section with no opinion shown is left out, its title too; volume has no walking time + val walk = if (kind == Kind.LENGTH) MIL_OPINIONS.count { it.id in shown } else 0 + for (rows in listOf(opinionsFor(kind, shown).size, walk)) { if (rows > 0) height += BLOCK_GAP + SECTION_TITLE_HEIGHT + (ROW_GAP + OPINION_ROW_HEIGHT) * rows } return height @@ -113,13 +118,26 @@ internal object MeasuresWidget : HomeWidget { modifier: Modifier, ) { val field = rememberTextFieldState("1") - var from by remember { mutableStateOf(LengthUnit.AMMA) } - var to by remember { mutableStateOf(LengthUnit.TEFACH) } + var kind by remember { mutableStateOf(Kind.LENGTH) } + // Each kind starts on its own pair of units + var from by remember(kind) { mutableStateOf(kind.defaultFrom) } + var to by remember(kind) { mutableStateOf(kind.defaultTo) } val typed = field.text.trim().toString() val amount = typed.replace(',', '.').toDoubleOrNull()?.takeIf { it >= 0 } - val etzbaos = amount?.let { it * from.etzbaos } - val ammaOpinions = AMMA_OPINIONS.filter { it.id in shown } - val milOpinions = MIL_OPINIONS.filter { it.id in shown } + val opinions = opinionsFor(kind, shown) + // Walking time is per mil: only for a Torah length, a metric one would first need an amma opinion + val walkFrom = from as? LengthUnit + val milOpinions = if (walkFrom != null) MIL_OPINIONS.filter { it.id in shown } else emptyList() + // Between a Torah measure and today's, the answer is the opinion's: one per opinion, the range on top + val perOpinion = needsOpinion(from, to) + val main = + amount?.let { a -> + if (perOpinion) { + opinions.mapNotNull { convert(a, from, to, it) }.takeIf { it.isNotEmpty() }?.let { range(it.min(), it.max(), to) } + } else { + convert(a, from, to, null)?.let { quantity(it, to) } + } + } val accent = rememberAccentColor(JewelTheme.isDark) val open = rememberOpenSource(state) PanelCard(modifier) { @@ -136,6 +154,17 @@ internal object MeasuresWidget : HomeWidget { maxLines = 1, modifier = Modifier.weight(1f), ) + Kind.entries.forEach { k -> + HoverBox(onClick = { kind = k }, tinted = k == kind, modifier = Modifier.testTag("measures-kind-${k.name}")) { + Text( + k.title, + fontSize = 12.sp, + fontWeight = if (k == kind) FontWeight.SemiBold else FontWeight.Normal, + color = if (k == kind) accent else JewelTheme.globalColors.text.info, + modifier = Modifier.padding(horizontal = 6.dp, vertical = 1.dp), + ) + } + } // Its options at hand, as in its menu IconButton( onClick = { state.optionsOpen = MeasuresWidget }, @@ -154,7 +183,7 @@ internal object MeasuresWidget : HomeWidget { verticalAlignment = Alignment.CenterVertically, ) { TextField(field, Modifier.width(64.dp)) - UnitPicker(from, onPick = { from = it }, Modifier.weight(1f)) + UnitPicker(kind, from, onPick = { from = it }, Modifier.weight(1f)) HoverBox(onClick = { val was = from from = to @@ -162,11 +191,11 @@ internal object MeasuresWidget : HomeWidget { }) { Text("⇄", fontSize = 16.sp, color = accent, modifier = Modifier.padding(horizontal = 6.dp, vertical = 2.dp)) } - UnitPicker(to, onPick = { to = it }, Modifier.weight(1f)) + UnitPicker(kind, to, onPick = { to = it }, Modifier.weight(1f)) } Column { Text( - etzbaos?.let { "= ${quantity(it / to.etzbaos, to)}" } ?: "–", + main?.let { "= $it" } ?: "–", fontSize = 20.sp, fontWeight = FontWeight.SemiBold, color = accent, @@ -178,13 +207,17 @@ internal object MeasuresWidget : HomeWidget { conversionSources(from, to).forEach { SourceLink(it, open) } } } - if (ammaOpinions.isNotEmpty()) { + if (opinions.isNotEmpty()) { Column(verticalArrangement = Arrangement.spacedBy(ROW_GAP)) { - SectionTitle(TODAY_TITLE) - ammaOpinions.forEach { opinion -> + // A revi'is today is worked out from the etzba: cite where it is measured in etzbaos + Row(verticalAlignment = Alignment.CenterVertically) { + SectionTitle(if (from is MetricUnit || to is MetricUnit) BY_OPINION_TITLE else TODAY_TITLE) + if (kind == Kind.VOLUME) SourceLink(PESACHIM_109A, open, Modifier.height(SECTION_TITLE_HEIGHT)) + } + opinions.forEach { opinion -> OpinionRow( label = opinion.label, - value = etzbaos?.let { length(it / ETZBAOS_IN_AMMA * opinion.cm) }, + value = amount?.let { opinionValue(it, from, to, opinion) }, source = opinion.source, accent = accent, open = open, @@ -198,7 +231,7 @@ internal object MeasuresWidget : HomeWidget { milOpinions.forEach { opinion -> OpinionRow( label = opinion.label, - value = etzbaos?.let { duration(it / ETZBAOS_IN_AMMA / AMOS_IN_MIL * opinion.minutes * 60) }, + value = amount?.let { a -> walkFrom?.let { duration(walkMinutes(a, it, opinion) * 60) } }, source = opinion.source, accent = accent, open = open, @@ -224,20 +257,44 @@ private val ROW_GAP = 2.dp private const val TODAY_TITLE = "בימינו" private const val WALK_TITLE = "זמן הליכה" +private const val BY_OPINION_TITLE = "לפי השיטות" + +// The opinions offered before גר״ח נאה was added: the ones a choice saved by then was made among +private val FIRST_OPINION_IDS = setOf("chazon_ish", "igros_moshe", "mil_18", "mil_22_5", "mil_24") internal object MeasuresSettings : WidgetSettings(MeasuresWidget.id) { - /** The ids of the opinions shown: every one until the user picks. */ - val shown by stringSet((AMMA_OPINIONS.map { it.id } + MIL_OPINIONS.map { it.id }).toSet()) + /** The ids of the opinions ticked: every one until the user picks. */ + val shown by stringSet(ALL_OPINION_IDS) + + /** + * The ids offered when [shown] was last saved. An opinion added since is shown until the user unticks it, so a + * choice saved before it was added doesn't hide it. + */ + val offered by stringSet(FIRST_OPINION_IDS) } +/** The opinions to show: those ticked, and any added since the ticks were saved ([offered] lacks them). */ +internal fun shownOpinions( + shown: Set, + offered: Set, +): Set = shown + (ALL_OPINION_IDS - offered) + /** A box for each opinion, by kind, with its figure and source; each tick shows or hides it at once. */ @Composable private fun MeasuresOptions(state: HomeWidgetsState) { - val shown = state[MeasuresSettings.shown] - val toggle = { id: String, on: Boolean -> state[MeasuresSettings.shown] = if (on) shown + id else shown - id } + val shown = shownOpinions(state[MeasuresSettings.shown], state[MeasuresSettings.offered]) + val toggle = { id: String, on: Boolean -> + state[MeasuresSettings.shown] = if (on) shown + id else shown - id + // Every opinion now offered was seen: from here on only the ticks say what is shown + state[MeasuresSettings.offered] = ALL_OPINION_IDS + } Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(2.dp)) { OptionsGroup(TODAY_TITLE) - AMMA_OPINIONS.forEach { OptionRow(it.id, it.label, "אמה ${length(it.cm)} · ${it.source.ref}", it.id in shown, toggle) } + AMMA_OPINIONS.forEach { + // The revi'is it gives, where it gives one + val reviis = if (it.derivesVolume) " · רביעית ${volume(it.reviisMl)}" else "" + OptionRow(it.id, it.label, "אמה ${length(it.cm)}$reviis · ${it.source.ref}", it.id in shown, toggle) + } OptionsGroup(WALK_TITLE) MIL_OPINIONS.forEach { OptionRow(it.id, it.label, it.source.ref, it.id in shown, toggle) } } @@ -269,14 +326,16 @@ private fun OptionRow( @Composable private fun UnitPicker( - unit: LengthUnit, - onPick: (LengthUnit) -> Unit, + kind: Kind, + unit: Measure, + onPick: (Measure) -> Unit, modifier: Modifier = Modifier, ) { + val units = kind.units ListComboBox( - items = LengthUnit.entries.map { it.title }, - selectedIndex = unit.ordinal, - onSelectedItemChange = { onPick(LengthUnit.entries[it]) }, + items = units.map { it.title }, + selectedIndex = units.indexOf(unit).coerceAtLeast(0), + onSelectedItemChange = { onPick(units[it]) }, modifier = modifier, ) } @@ -340,14 +399,19 @@ internal data class Source( val lineIndex: Int, ) -/** What says how many [from] are in [to] or back: each unit's source from the smaller one's next to the larger. */ +/** + * What says how many [from] are in [to] or back: each unit's source from the smaller one's next to the larger. A metric + * unit has none of its own: it goes through the Torah unit its kind's opinions are given in (the amma, the revi'is). + */ internal fun conversionSources( - from: LengthUnit, - to: LengthUnit, + from: Measure, + to: Measure, ): List { - val range = minOf(from, to)..maxOf(from, to) - return LengthUnit.entries - .filter { it in range && it != range.start } + val units = from.kind.halachicUnits + val a = units.indexOf(from as? HalachicUnit ?: from.kind.anchor) + val b = units.indexOf(to as? HalachicUnit ?: to.kind.anchor) + return units + .filterIndexed { i, _ -> i > minOf(a, b) && i <= maxOf(a, b) } .map { it.source } .distinct() } @@ -380,13 +444,61 @@ private val MISHNAH_BERURAH_110_31 = Source("משנה ברורה קי, לא", " private const val ETZBAOS_IN_AMMA = 24.0 private const val AMOS_IN_MIL = 2000.0 +// "רביעית של תורה אצבעים על אצבעים ברום אצבעים וחצי אצבע וחומש אצבע": 2 × 2 × 2.7 cubic etzbaos +private const val REVIIS_CUBIC_ETZBAOS = 2.0 * 2.0 * 2.7 +internal val PESACHIM_109A = Source("פסחים קט, א", "פסחים", 3388) + +/** What is measured: each kind its Torah units, then today's. */ +internal enum class Kind( + val title: String, +) { + LENGTH("אורך"), + VOLUME("נפח"), + ; + + // Getters, not constructor values: the units' enums refer back to their kind + val halachicUnits: List + get() = + when (this) { + LENGTH -> LengthUnit.entries + VOLUME -> VolumeUnit.entries + } + + /** The unit picker's list: the Torah units, smallest first, then the metric ones. */ + val units: List get() = halachicUnits + MetricUnit.entries.filter { it.kind == this } + + /** The Torah unit an opinion gives today's size of, which every metric unit converts through. */ + val anchor: HalachicUnit + get() = + when (this) { + LENGTH -> LengthUnit.AMMA + VOLUME -> VolumeUnit.REVIIS + } + + val defaultFrom: Measure get() = if (this == LENGTH) LengthUnit.AMMA else VolumeUnit.LOG + val defaultTo: Measure get() = if (this == LENGTH) LengthUnit.TEFACH else VolumeUnit.REVIIS +} + +/** A unit of the picker: a Torah measure or a metric one. */ +internal sealed interface Measure { + val title: String + val plural: String + val kind: Kind +} + +/** A Torah measure, [size] in its kind's smallest unit (etzbaos, revi'iyos), with what says so. */ +internal sealed interface HalachicUnit : Measure { + val size: Double + val source: Source +} + /** The lengths, in etzbaos, with what says so. */ internal enum class LengthUnit( - val title: String, - val plural: String, + override val title: String, + override val plural: String, val etzbaos: Double, - val source: Source, -) { + override val source: Source, +) : HalachicUnit { // "האצבע… רחב הגודל של יד. והטפח ארבע אצבעות… אמה בת ששה טפחים" ETZBA("אצבע", "אצבעות", 1.0, RAMBAM_SHABBOS_17_36), TEFACH("טפח", "טפחים", 4.0, RAMBAM_SHABBOS_17_36), @@ -397,6 +509,53 @@ internal enum class LengthUnit( // "מיל הוא אלפים אמה ופרסה הוא ד' מילין" PARSAH("פרסה", "פרסאות", ETZBAOS_IN_AMMA * AMOS_IN_MIL * 4, MISHNAH_BERURAH_110_31), + ; + + override val size get() = etzbaos + override val kind get() = Kind.LENGTH +} + +private val RAMBAM_ERUVIN_1_13 = Source("רמב״ם עירובין א, יג", "משנה תורה, הלכות עירובין", 15) +private val RAMBAM_ARACHIN_4_4 = Source("רמב״ם ערכין ד, ד", "משנה תורה, הלכות ערכים וחרמין", 65) + +/** The volumes, in revi'iyos, with what says so. */ +internal enum class VolumeUnit( + override val title: String, + override val plural: String, + val reviios: Double, + override val source: Source, +) : HalachicUnit { + // "סאה האמורה בכל מקום ששת קבין. והקב ארבעה לוגין. והלוג ארבע רביעיות" + REVIIS("רביעית", "רביעיות", 1.0, RAMBAM_ERUVIN_1_13), + LOG("לוג", "לוגין", 4.0, RAMBAM_ERUVIN_1_13), + KAV("קב", "קבין", 16.0, RAMBAM_ERUVIN_1_13), + SEAH("סאה", "סאין", 96.0, RAMBAM_ERUVIN_1_13), + + // "החמר הוא הכור… נמצא החמר שלשים סאה שהן עשר איפות כל שלש סאין איפה" + EIFAH("איפה", "איפות", 288.0, RAMBAM_ARACHIN_4_4), + KOR("כור", "כורים", 2880.0, RAMBAM_ARACHIN_4_4), + ; + + override val size get() = reviios + override val kind get() = Kind.VOLUME +} + +/** Today's units, [size] in centimetres or millilitres: no source, their size in Torah units is the opinion's. */ +internal enum class MetricUnit( + override val title: String, + override val kind: Kind, + val size: Double, +) : Measure { + MM("מ״מ", Kind.LENGTH, 0.1), + CM("ס״מ", Kind.LENGTH, 1.0), + METER("מטר", Kind.LENGTH, 100.0), + KM("ק״מ", Kind.LENGTH, 100_000.0), + ML("מ״ל", Kind.VOLUME, 1.0), + LITER("ליטר", Kind.VOLUME, 1000.0), + ; + + // "3 מטר", "2 ליטר": the same word for any number + override val plural get() = title } internal class AmmaOpinion( @@ -404,16 +563,105 @@ internal class AmmaOpinion( val label: String, val cm: Double, val source: Source, -) + /** + * Whether its revi'is is its etzba cubed (10.8 cubic etzbaos). Not so for the Igros Moshe: R' Moshe's own revi'is + * is a separate ruling that doesn't follow from his amma, so cubing his amma would give a + * misleading ~123 cc. It is left out of volumes rather than shown with a figure he didn't hold. + */ + val derivesVolume: Boolean = true, +) { + /** Its revi'is in millilitres (cc). */ + val reviisMl: Double get() = REVIIS_CUBIC_ETZBAOS * (cm / ETZBAOS_IN_AMMA).pow(3) + + /** Today's size (cm, ml) of [kind]'s smallest Torah unit, the etzba or the revi'is. */ + fun modernPerUnit(kind: Kind): Double = + when (kind) { + Kind.LENGTH -> cm / ETZBAOS_IN_AMMA + Kind.VOLUME -> reviisMl + } +} +// Smallest amma first internal val AMMA_OPINIONS = listOf( + // "וכל אצבע, שהוא ברוחב האגודל, שני סנטימטר... וכאשר ביאר הגר"ח נאה בספרו שיעורי תורה" + AmmaOpinion( + "naeh", + "גר״ח נאה", + 48.0, + Source("קיצור שו״ע ילקוט יוסף, תרל, ד", "קיצור שע ילקוט יוסף החדש - אורח חיים", 16940), + ), + // "אמה של תורה שהוא כ"ד אצבעות מדת כ"א אינטשעס ורביע" + AmmaOpinion( + "igros_moshe", + "אגרות משה", + 21.25 * 2.54, + Source("אגרות משה או״ח א, קלו", "אגרות משה אורח חיים א", 1256), + derivesVolume = false, + ), // "ומדת האמה 58 ס"מ" (קונטרס השיעורים) AmmaOpinion("chazon_ish", "חזון איש", 58.0, Source("חזו״א, קונטרס השיעורים לט, ט", "חזון איש, אורח חיים מועד", 1533)), - // "אמה של תורה שהוא כ"ד אצבעות מדת כ"א אינטשעס ורביע" - AmmaOpinion("igros_moshe", "אגרות משה", 21.25 * 2.54, Source("אגרות משה או״ח א, קלו", "אגרות משה אורח חיים א", 1256)), ) +/** The amma opinions shown for [kind]: for volume, only those whose revi'is follows from their amma. */ +internal fun opinionsFor( + kind: Kind, + shown: Set, +): List = AMMA_OPINIONS.filter { it.id in shown && (kind == Kind.LENGTH || it.derivesVolume) } + +/** Whether going [from] → [to] crosses between Torah and metric units, so the answer depends on the opinion. */ +internal fun needsOpinion( + from: Measure, + to: Measure, +): Boolean = (from is MetricUnit) != (to is MetricUnit) + +/** [amount] [from] in [to] after [opinion]; null when the units need an opinion ([needsOpinion]) and none is given. */ +internal fun convert( + amount: Double, + from: Measure, + to: Measure, + opinion: AmmaOpinion?, +): Double? { + if (from is HalachicUnit && to is HalachicUnit) return amount * from.size / to.size + val f = modernSize(from, opinion) ?: return null + val t = modernSize(to, opinion) ?: return null + return amount * f / t +} + +/** [unit] in centimetres or millilitres; a Torah unit only after an opinion. */ +private fun modernSize( + unit: Measure, + opinion: AmmaOpinion?, +): Double? = + when (unit) { + is MetricUnit -> unit.size + is HalachicUnit -> opinion?.let { unit.size * it.modernPerUnit(unit.kind) } + } + +/** + * An opinion's row for [amount] [from] → [to]: the answer in [to] when it depends on the opinion; else what the amount + * is in the other system, today's units for a Torah measure, Torah ones for a metric one. + */ +internal fun opinionValue( + amount: Double, + from: Measure, + to: Measure, + opinion: AmmaOpinion, +): String? = + when { + needsOpinion(from, to) -> convert(amount, from, to, opinion)?.let { quantity(it, to) } + from is HalachicUnit -> modern(amount * from.size * opinion.modernPerUnit(from.kind), from.kind) + from is MetricUnit -> halachic(amount * from.size / opinion.modernPerUnit(from.kind), from.kind) + else -> null + } + +/** Minutes to walk [amount] [from] at [opinion]'s pace. */ +internal fun walkMinutes( + amount: Double, + from: LengthUnit, + opinion: MilOpinion, +): Double = amount * from.etzbaos / LengthUnit.MIL.etzbaos * opinion.minutes + internal class MilOpinion( val id: String, val label: String, @@ -433,20 +681,58 @@ internal val MIL_OPINIONS = MilOpinion("mil_24", "מיל 24 דק׳", 24.0, BIUR_HALACHA_459_2), ) +// After both lists: top-level values are initialised in the file's order +internal val ALL_OPINION_IDS = (AMMA_OPINIONS.map { it.id } + MIL_OPINIONS.map { it.id }).toSet() + /** [count] [unit]s, its name in the singular for one. */ internal fun quantity( count: Double, - unit: LengthUnit, + unit: Measure, ): String = "${amount(count)} ${if (count == 1.0) unit.title else unit.plural}" -/** [cm] in centimetres, metres or kilometres. */ +/** [min] to [max] [unit]s, one figure when they round the same. */ +internal fun range( + min: Double, + max: Double, + unit: Measure, +): String = if (amount(min) == amount(max)) quantity(min, unit) else "${amount(min)}–${amount(max)} ${unit.plural}" + +/** [x] cm or ml in the metric unit that suits it. */ +internal fun modern( + x: Double, + kind: Kind, +): String = + when (kind) { + Kind.LENGTH -> length(x) + Kind.VOLUME -> volume(x) + } + +/** [base] etzbaos or revi'iyos in the largest Torah unit it makes at least one of. */ +internal fun halachic( + base: Double, + kind: Kind, +): String { + val units = kind.halachicUnits + val unit = units.lastOrNull { base >= it.size } ?: units.first() + return quantity(base / unit.size, unit) +} + +/** [cm] in millimetres, centimetres, metres or kilometres. */ internal fun length(cm: Double): String = when { + cm > 0 && cm < 1 -> "${amount(cm * 10)} מ״מ" cm < 100 -> "${amount(cm)} ס״מ" - cm < 100_000 -> "${amount(cm / 100)} מ׳" + cm < 100_000 -> "${amount(cm / 100)} מטר" else -> "${amount(cm / 100_000)} ק״מ" } +/** [ml] in millilitres or litres. */ +internal fun volume(ml: Double): String = + when { + ml < 1000 -> "${amount(ml)} מ״ל" + else -> "${amount(ml / 1000)} ליטר" + } + /** [seconds] in seconds, minutes or hours. */ internal fun duration(seconds: Double): String = when { diff --git a/SeforimApp/src/jvmTest/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresTest.kt b/SeforimApp/src/jvmTest/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresTest.kt index 7e11af0ee..1bd91e791 100644 --- a/SeforimApp/src/jvmTest/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresTest.kt +++ b/SeforimApp/src/jvmTest/kotlin/io/github/kdroidfilter/seforimapp/features/home/widgets/measures/MeasuresTest.kt @@ -6,13 +6,16 @@ import io.github.kdroidfilter.seforimapp.features.home.widgets.minRows import io.github.kdroidfilter.seforimapp.testAppSettings import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue class MeasuresTest { @Test fun `an amma and a mil after each opinion`() { - assertEquals(listOf("58 ס״מ", "54 ס״מ"), AMMA_OPINIONS.map { length(it.cm) }) + assertEquals(listOf("48 ס״מ", "54 ס״מ", "58 ס״מ"), AMMA_OPINIONS.map { length(it.cm) }) val mil = LengthUnit.MIL.etzbaos / LengthUnit.AMMA.etzbaos - assertEquals(listOf("1.16 ק״מ", "1.08 ק״מ"), AMMA_OPINIONS.map { length(mil * it.cm) }) + assertEquals(listOf("960 מטר", "1.08 ק״מ", "1.16 ק״מ"), AMMA_OPINIONS.map { length(mil * it.cm) }) assertEquals(listOf("18 דקות", "22.5 דקות", "24 דקות"), MIL_OPINIONS.map { duration(it.minutes * 60) }) // A parsah is four mil: 72 minutes after the Shulchan Aruch assertEquals("1.2 שעות", duration(LengthUnit.PARSAH.etzbaos / LengthUnit.MIL.etzbaos * 18 * 60)) @@ -38,7 +41,11 @@ class MeasuresTest { @Test fun `every opinion is shown until the user picks`() { - assertEquals(setOf("chazon_ish", "igros_moshe", "mil_18", "mil_22_5", "mil_24"), testAppSettings()[MeasuresSettings.shown]) + val settings = testAppSettings() + assertEquals( + setOf("naeh", "chazon_ish", "igros_moshe", "mil_18", "mil_22_5", "mil_24"), + shownOpinions(settings[MeasuresSettings.shown], settings[MeasuresSettings.offered]), + ) } @Test @@ -47,7 +54,10 @@ class MeasuresTest { val settings = testAppSettings() try { MeasuresWidget.applySettings(settings) - assertEquals(4, MeasuresWidget.minRows(7, pitch)) + // Three amma opinions and three mil ones + assertEquals(5, MeasuresWidget.minRows(7, pitch)) + // Picked since every opinion was offered: only the ticks count + settings[MeasuresSettings.offered] = ALL_OPINION_IDS settings[MeasuresSettings.shown] = setOf("chazon_ish") MeasuresWidget.applySettings(settings) assertEquals(3, MeasuresWidget.minRows(7, pitch)) @@ -59,4 +69,92 @@ class MeasuresTest { MeasuresWidget.applySettings(testAppSettings()) } } + + @Test + fun `an opinion added after the user picked is shown until unticked`() { + // Saved before גר״ח נאה: offered still its default, the opinions of then + assertEquals(setOf("chazon_ish", "naeh"), shownOpinions(setOf("chazon_ish"), MeasuresSettings.offered.default)) + // Picked since: unticked stays unticked + assertEquals(setOf("chazon_ish"), shownOpinions(setOf("chazon_ish"), ALL_OPINION_IDS)) + } + + @Test + fun `the volumes convert into one another`() { + assertEquals(30.0, convert(1.0, VolumeUnit.KOR, VolumeUnit.SEAH, null)) + assertEquals(2880.0, convert(1.0, VolumeUnit.KOR, VolumeUnit.REVIIS, null)) + assertEquals(10.0, convert(1.0, VolumeUnit.KOR, VolumeUnit.EIFAH, null)) + assertEquals("3 סאין", quantity(convert(1.0, VolumeUnit.EIFAH, VolumeUnit.SEAH, null)!!, VolumeUnit.SEAH)) + assertEquals("6 קבין", quantity(convert(1.0, VolumeUnit.SEAH, VolumeUnit.KAV, null)!!, VolumeUnit.KAV)) + assertEquals("4 רביעיות", quantity(convert(1.0, VolumeUnit.LOG, VolumeUnit.REVIIS, null)!!, VolumeUnit.REVIIS)) + assertEquals(listOf("רמב״ם עירובין א, יג", "רמב״ם ערכין ד, ד"), conversionSources(VolumeUnit.KOR, VolumeUnit.LOG).map { it.ref }) + } + + @Test + fun `a revi'is is its opinion's etzba cubed`() { + val naeh = AMMA_OPINIONS.single { it.id == "naeh" } + val chazonIsh = AMMA_OPINIONS.single { it.id == "chazon_ish" } + assertEquals(86.4, naeh.reviisMl, 1e-9) + assertEquals(152.4, chazonIsh.reviisMl, 0.05) + assertEquals("86.4 מ״ל", volume(naeh.reviisMl)) + assertEquals("152 מ״ל", volume(chazonIsh.reviisMl)) + // A kor after the Chazon Ish + assertEquals("439 ליטר", opinionValue(1.0, VolumeUnit.KOR, VolumeUnit.REVIIS, chazonIsh)) + } + + @Test + fun `the Igros Moshe is left out of volumes`() { + assertEquals(listOf("naeh", "igros_moshe", "chazon_ish"), opinionsFor(Kind.LENGTH, ALL_OPINION_IDS).map { it.id }) + assertEquals(listOf("naeh", "chazon_ish"), opinionsFor(Kind.VOLUME, ALL_OPINION_IDS).map { it.id }) + } + + @Test + fun `metric units convert after an opinion`() { + val naeh = AMMA_OPINIONS.single { it.id == "naeh" } + assertTrue(needsOpinion(MetricUnit.METER, LengthUnit.AMMA)) + assertFalse(needsOpinion(MetricUnit.METER, MetricUnit.CM)) + assertFalse(needsOpinion(LengthUnit.MIL, LengthUnit.AMMA)) + // Without an opinion there is no answer + assertNull(convert(1.0, MetricUnit.METER, LengthUnit.AMMA, null)) + assertEquals(100.0 / 48, convert(1.0, MetricUnit.METER, LengthUnit.AMMA, naeh)!!, 1e-9) + assertEquals("2.08 אמות", opinionValue(1.0, MetricUnit.METER, LengthUnit.AMMA, naeh)) + assertEquals("48 ס״מ", opinionValue(1.0, LengthUnit.AMMA, MetricUnit.CM, naeh)) + assertEquals(20.0, convert(1.0, LengthUnit.ETZBA, MetricUnit.MM, naeh)!!, 1e-9) + assertEquals(10.0, convert(1.0, MetricUnit.CM, MetricUnit.MM, null)!!, 1e-9) + assertEquals("1 ליטר", quantity(convert(1.0, MetricUnit.LITER, MetricUnit.ML, null)!! / 1000, MetricUnit.LITER)) + assertEquals("86.4 מ״ל", opinionValue(1.0, VolumeUnit.REVIIS, MetricUnit.ML, naeh)) + // Metric to metric: the Torah measure it makes + assertEquals("2.5 טפחים", opinionValue(20.0, MetricUnit.CM, MetricUnit.METER, naeh)) + // The range over the opinions shown + assertEquals("1.72–2.08 אמות", range(100.0 / 58, 100.0 / 48, LengthUnit.AMMA)) + // A metric unit cites the Torah units it goes through to the amma + assertEquals(listOf("רמב״ם שבת יז, לו"), conversionSources(MetricUnit.METER, LengthUnit.ETZBA).map { it.ref }) + assertEquals(listOf("רמב״ם תפילה ד, ב"), conversionSources(MetricUnit.KM, LengthUnit.MIL).map { it.ref }) + assertEquals(emptyList(), conversionSources(MetricUnit.ML, MetricUnit.LITER)) + } + + @Test + fun `each amount in the metric unit that suits it`() { + assertEquals("5 מ״מ", length(0.5)) + assertEquals("48 ס״מ", length(48.0)) + assertEquals("2 מטר", length(200.0)) + assertEquals("1.5 ק״מ", length(150_000.0)) + assertEquals("86.4 מ״ל", volume(86.4)) + assertEquals("1 ליטר", volume(1000.0)) + assertEquals("438 ליטר", volume(438_000.0)) + assertEquals("1 כור", halachic(2880.0, Kind.VOLUME)) + assertEquals("0.5 רביעיות", halachic(0.5, Kind.VOLUME)) + } + + @Test + fun `the card is as tall in either kind`() { + val settings = testAppSettings() + try { + MeasuresWidget.applySettings(settings) + // No walking time and no Igros Moshe: volume needs less room, length sets the card's height + assertTrue(MeasuresWidget.heightOf(Kind.VOLUME) < MeasuresWidget.heightOf(Kind.LENGTH)) + assertEquals(MeasuresWidget.heightOf(Kind.LENGTH), MeasuresWidget.heightAt(MAX_GRID_WIDTH)) + } finally { + MeasuresWidget.applySettings(testAppSettings()) + } + } }