Added Flow rate (#29)

This commit is contained in:
Sad Ellie 2023-03-28 17:54:20 +03:00
parent f7cfd65639
commit 8bb28dae7b
7 changed files with 207 additions and 1 deletions

View File

@ -1117,6 +1117,51 @@
<string name="pound_force_inch">Pound-force inch</string>
<string name="pound_force_inch_short">lbf*in</string>
<!--Flow rate-->
<string name="liter_per_hour">Liter/hour</string>
<string name="liter_per_hour_short">L/h</string>
<string name="liter_per_minute">Liter/minute</string>
<string name="liter_per_minute_short">L/m</string>
<string name="liter_per_second">Liter/second</string>
<string name="liter_per_second_short">L/s</string>
<string name="milliliter_per_hour">Milliliter/hour</string>
<string name="milliliter_per_hour_short">mL/h</string>
<string name="milliliter_per_minute">Milliliter/minute</string>
<string name="milliliter_per_minute_short">mL/m</string>
<string name="milliliter_per_second">Milliliter/second</string>
<string name="milliliter_per_second_short">mL/s</string>
<string name="cubic_meter_per_hour">Cubic Meter/hour</string>
<string name="cubic_meter_per_hour_short">m3/h</string>
<string name="cubic_meter_per_minute">Cubic Meter/minute</string>
<string name="cubic_meter_per_minute_short">m3/m</string>
<string name="cubic_meter_per_second">Cubic Meter/second</string>
<string name="cubic_meter_per_second_short">m3/s</string>
<string name="cubic_millimeter_per_hour">Cubic Millimeter/hour</string>
<string name="cubic_millimeter_per_hour_short">mm3/h</string>
<string name="cubic_millimeter_per_minute">Cubic Millimeter/minute</string>
<string name="cubic_millimeter_per_minute_short">mm3/m</string>
<string name="cubic_millimeter_per_second">Cubic Millimeter/second</string>
<string name="cubic_millimeter_per_second_short">mm3/s</string>
<string name="cubic_foot_per_hour">Cubic Foot/hour</string>
<string name="cubic_foot_per_hour_short">ft3/h</string>
<string name="cubic_foot_per_minute">Cubic Foot/minute</string>
<string name="cubic_foot_per_minute_short">ft3/m</string>
<string name="cubic_foot_per_second">Cubic Foot/second</string>
<string name="cubic_foot_per_second_short">ft3/s</string>
<string name="gallon_per_hour_us">Gallon/hour (U.S.)</string>
<string name="gallon_per_hour_us_short">gal/h</string>
<string name="gallon_per_minute_us">Gallon/minute (U.S.)</string>
<string name="gallon_per_minute_us_short">gal/m</string>
<string name="gallon_per_second_us">Gallon/second (U.S.)</string>
<string name="gallon_per_second_us_short">gal/s</string>
<string name="gallon_per_hour_imperial">Gallon/hour (Imperial)</string>
<string name="gallon_per_hour_imperial_short">gal/h</string>
<string name="gallon_per_minute_imperial">Gallon/minute (Imperial)</string>
<string name="gallon_per_minute_imperial_short">gal/m</string>
<string name="gallon_per_second_imperial">Gallon/second (Imperial)</string>
<string name="gallon_per_second_imperial_short">gal/s</string>
<!--Groups-->
<string name="length">Length</string>
<string name="time">Time</string>
@ -1139,6 +1184,7 @@
<string name="prefix">Prefix</string>
<string name="force">Force</string>
<string name="torque">Torque</string>
<string name="flow_rate">Flow</string>
<!--Screen names-->
<string name="units_screen_from">Convert from</string>

View File

@ -0,0 +1,57 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.data.model
import androidx.annotation.StringRes
import com.sadellie.unitto.core.base.MAX_PRECISION
import com.sadellie.unitto.data.common.setMinimumRequiredScale
import com.sadellie.unitto.data.common.trimZeros
import java.math.BigDecimal
/**
* Same as [MyUnit], but conversion formula is different.
*
* @see MyUnit
*/
class FlowRateUnit(
unitId: String,
basicUnit: BigDecimal,
group: UnitGroup,
@StringRes displayName: Int,
@StringRes shortName: Int,
) : AbstractUnit(
unitId = unitId,
displayName = displayName,
shortName = shortName,
basicUnit = basicUnit,
group = group,
) {
override fun convert(
unitTo: AbstractUnit,
value: BigDecimal,
scale: Int
): BigDecimal {
return unitTo.basicUnit
.setScale(MAX_PRECISION)
.div(this.basicUnit)
.multiply(value)
.setMinimumRequiredScale(scale)
.trimZeros()
}
}

View File

@ -52,4 +52,5 @@ enum class UnitGroup(
PREFIX(res = R.string.prefix),
FORCE(res = R.string.force),
TORQUE(res = R.string.torque),
FLOW_RATE(res = R.string.flow_rate),
}

View File

@ -32,6 +32,7 @@ import com.sadellie.unitto.data.units.collections.dataCollection
import com.sadellie.unitto.data.units.collections.dataTransferCollection
import com.sadellie.unitto.data.units.collections.electrostaticCapacitance
import com.sadellie.unitto.data.units.collections.energyCollection
import com.sadellie.unitto.data.units.collections.flowRateCollection
import com.sadellie.unitto.data.units.collections.fluxCollection
import com.sadellie.unitto.data.units.collections.forceCollection
import com.sadellie.unitto.data.units.collections.lengthCollection
@ -86,7 +87,8 @@ class AllUnitsRepository @Inject constructor() {
UnitGroup.ELECTROSTATIC_CAPACITANCE to electrostaticCapacitance,
UnitGroup.PREFIX to prefixCollection,
UnitGroup.FORCE to forceCollection,
UnitGroup.TORQUE to torqueCollection
UnitGroup.TORQUE to torqueCollection,
UnitGroup.FLOW_RATE to flowRateCollection,
)
}

View File

@ -593,4 +593,27 @@ object MyUnitIDS {
const val ounce_force_inch = "ounce_force_inch"
const val pound_force_foot = "pound_force_foot"
const val pound_force_inch = "pound_force_inch"
// FLOW RATE
const val liter_per_hour = "liter_per_hour"
const val liter_per_minute = "liter_per_minute"
const val liter_per_second = "liter_per_second"
const val milliliter_per_hour = "milliliter_per_hour"
const val milliliter_per_minute = "milliliter_per_minute"
const val milliliter_per_second = "milliliter_per_second"
const val cubic_meter_per_hour = "cubic_meter_per_hour"
const val cubic_meter_per_minute = "cubic_meter_per_minute"
const val cubic_meter_per_second = "cubic_meter_per_second"
const val cubic_millimeter_per_hour = "cubic_millimeter_per_hour"
const val cubic_millimeter_per_minute = "cubic_millimeter_per_minute"
const val cubic_millimeter_per_second = "cubic_millimeter_per_second"
const val cubic_foot_per_hour = "cubic_foot_per_hour"
const val cubic_foot_per_minute = "cubic_foot_per_minute"
const val cubic_foot_per_second = "cubic_foot_per_second"
const val gallons_per_hour_us = "gallons_per_hour_us"
const val gallons_per_minute_us = "gallons_per_minute_us"
const val gallons_per_second_us = "gallons_per_second_us"
const val gallons_per_hour_imperial = "gallons_per_hour_imperial"
const val gallons_per_minute_imperial = "gallons_per_minute_imperial"
const val gallons_per_second_imperial = "gallons_per_second_imperial"
}

View File

@ -0,0 +1,52 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.data.units.collections
import com.sadellie.unitto.data.model.AbstractUnit
import com.sadellie.unitto.data.model.FlowRateUnit
import com.sadellie.unitto.data.model.UnitGroup
import com.sadellie.unitto.data.units.MyUnitIDS
import com.sadellie.unitto.data.units.R
import java.math.BigDecimal
val flowRateCollection: List<AbstractUnit> by lazy {
listOf(
FlowRateUnit(MyUnitIDS.liter_per_hour, BigDecimal.valueOf(3600000), UnitGroup.FLOW_RATE, R.string.liter_per_hour, R.string.liter_per_hour_short),
FlowRateUnit(MyUnitIDS.liter_per_minute, BigDecimal.valueOf(60000), UnitGroup.FLOW_RATE, R.string.liter_per_minute, R.string.liter_per_minute_short),
FlowRateUnit(MyUnitIDS.liter_per_second, BigDecimal.valueOf(1000), UnitGroup.FLOW_RATE, R.string.liter_per_second, R.string.liter_per_second_short),
FlowRateUnit(MyUnitIDS.milliliter_per_hour, BigDecimal.valueOf(3600000000), UnitGroup.FLOW_RATE, R.string.milliliter_per_hour, R.string.milliliter_per_hour_short),
FlowRateUnit(MyUnitIDS.milliliter_per_minute, BigDecimal.valueOf(60000000), UnitGroup.FLOW_RATE, R.string.milliliter_per_minute, R.string.milliliter_per_minute_short),
FlowRateUnit(MyUnitIDS.milliliter_per_second, BigDecimal.valueOf(1000000), UnitGroup.FLOW_RATE, R.string.milliliter_per_second, R.string.milliliter_per_second_short),
FlowRateUnit(MyUnitIDS.cubic_meter_per_hour, BigDecimal.valueOf(3600), UnitGroup.FLOW_RATE, R.string.cubic_meter_per_hour, R.string.cubic_meter_per_hour_short),
FlowRateUnit(MyUnitIDS.cubic_meter_per_minute, BigDecimal.valueOf(60), UnitGroup.FLOW_RATE, R.string.cubic_meter_per_minute, R.string.cubic_meter_per_minute_short),
FlowRateUnit(MyUnitIDS.cubic_meter_per_second, BigDecimal.valueOf(1), UnitGroup.FLOW_RATE, R.string.cubic_meter_per_second, R.string.cubic_meter_per_second_short),
FlowRateUnit(MyUnitIDS.cubic_millimeter_per_hour, BigDecimal.valueOf(3600000000000), UnitGroup.FLOW_RATE, R.string.cubic_millimeter_per_hour, R.string.cubic_millimeter_per_hour_short),
FlowRateUnit(MyUnitIDS.cubic_millimeter_per_minute, BigDecimal.valueOf(60000000000), UnitGroup.FLOW_RATE, R.string.cubic_millimeter_per_minute, R.string.cubic_millimeter_per_minute_short),
FlowRateUnit(MyUnitIDS.cubic_millimeter_per_second, BigDecimal.valueOf(1000000000), UnitGroup.FLOW_RATE, R.string.cubic_millimeter_per_second, R.string.cubic_millimeter_per_second_short),
FlowRateUnit(MyUnitIDS.cubic_foot_per_hour, BigDecimal.valueOf(127132.80019736), UnitGroup.FLOW_RATE, R.string.cubic_foot_per_hour, R.string.cubic_foot_per_hour_short),
FlowRateUnit(MyUnitIDS.cubic_foot_per_minute, BigDecimal.valueOf(2118.8800032893), UnitGroup.FLOW_RATE, R.string.cubic_foot_per_minute, R.string.cubic_foot_per_minute_short),
FlowRateUnit(MyUnitIDS.cubic_foot_per_second, BigDecimal.valueOf(35.314666721489), UnitGroup.FLOW_RATE, R.string.cubic_foot_per_second, R.string.cubic_foot_per_second_short),
FlowRateUnit(MyUnitIDS.gallons_per_hour_us, BigDecimal.valueOf(951019.38848933), UnitGroup.FLOW_RATE, R.string.gallon_per_hour_us, R.string.gallon_per_hour_us_short),
FlowRateUnit(MyUnitIDS.gallons_per_minute_us, BigDecimal.valueOf(15850.323141489), UnitGroup.FLOW_RATE, R.string.gallon_per_minute_us, R.string.gallon_per_minute_us_short),
FlowRateUnit(MyUnitIDS.gallons_per_second_us, BigDecimal.valueOf(264.17205235815), UnitGroup.FLOW_RATE, R.string.gallon_per_second_us, R.string.gallon_per_second_us_short),
FlowRateUnit(MyUnitIDS.gallons_per_hour_imperial, BigDecimal.valueOf(791889.29387672), UnitGroup.FLOW_RATE, R.string.gallon_per_hour_imperial, R.string.gallon_per_hour_imperial_short),
FlowRateUnit(MyUnitIDS.gallons_per_minute_imperial, BigDecimal.valueOf(13198.154897945), UnitGroup.FLOW_RATE, R.string.gallon_per_minute_imperial, R.string.gallon_per_minute_imperial_short),
FlowRateUnit(MyUnitIDS.gallons_per_second_imperial, BigDecimal.valueOf(219.96924829909), UnitGroup.FLOW_RATE, R.string.gallon_per_second_imperial, R.string.gallon_per_second_imperial_short),
)
}

View File

@ -460,6 +460,31 @@ class AllUnitsTest {
pound_force_inch.checkWith(ounce_force_foot, "2134", "2845.33337")
}
@Test
fun testFlowRate() = testWithUnits {
liter_per_hour.checkWith(milliliter_per_minute, "312", "5200")
liter_per_minute.checkWith(cubic_foot_per_minute, "312", "11.01818")
liter_per_second.checkWith(cubic_meter_per_minute, "312", "18.72")
milliliter_per_hour.checkWith(liter_per_hour, "312", "0.312")
milliliter_per_minute.checkWith(liter_per_second, "312", "0.0052")
milliliter_per_second.checkWith(cubic_foot_per_minute, "312", "0.66109")
cubic_meter_per_hour.checkWith(cubic_foot_per_hour, "312", "11018.17602")
cubic_meter_per_minute.checkWith(liter_per_minute, "312", "312000")
cubic_meter_per_second.checkWith(milliliter_per_minute, "312", "18720000000")
cubic_millimeter_per_hour.checkWith(milliliter_per_minute, "312", "0.0052")
cubic_millimeter_per_minute.checkWith(liter_per_minute, "312", "0.00031")
cubic_millimeter_per_second.checkWith(cubic_millimeter_per_minute, "312", "18720")
cubic_foot_per_hour.checkWith(milliliter_per_hour, "312", "8834856.1367")
cubic_foot_per_minute.checkWith(cubic_meter_per_hour, "312", "530.09137")
cubic_foot_per_second.checkWith(cubic_meter_per_second, "312", "8.83486")
gallons_per_hour_us.checkWith(liter_per_hour, "312", "1181.04848")
gallons_per_minute_us.checkWith(gallons_per_hour_imperial, "312", "15587.66074")
gallons_per_second_us.checkWith(cubic_meter_per_minute, "312", "70.86291")
gallons_per_hour_imperial.checkWith(liter_per_second, "312", "0.39399")
gallons_per_minute_imperial.checkWith(cubic_foot_per_minute, "312", "50.08962")
gallons_per_second_imperial.checkWith(cubic_meter_per_minute, "312", "85.1028")
}
private fun String.checkWith(checkingId: String, value: String, expected: String) {
val unitFrom = allUnitsRepository.getById(this)
val unitTo = allUnitsRepository.getById(checkingId)