Added Fuel Consumption (#29)

This commit is contained in:
Sad Ellie 2023-09-11 23:37:53 +03:00
parent 771b56eec8
commit 77440d6631
8 changed files with 216 additions and 1 deletions

View File

@ -1393,4 +1393,25 @@ Used in this dialog window. Should be short -->
<string name="language_setting_support">Change the app language</string>
<string name="system_font_setting">System font</string>
<string name="system_font_setting_support">Use system font for texts in app</string>
<string name="fuel_consumption">Fuel</string>
<string name="km_per_l">Kilometer per liter</string>
<string name="km_per_l_short">km/L</string>
<string name="l_per_km">Liter per kilometer</string>
<string name="l_per_km_short">L/km</string>
<string name="l_per_100_km">Liter per 100 Kilometer</string>
<string name="l_per_100_km_short">L/100 km</string>
<string name="mi_per_gallon_uk">Mile per gallon (UK)</string>
<string name="mi_per_gallon_uk_short">mpg (UK)</string>
<string name="mi_per_gallon_us">Mile per gallon (US)</string>
<string name="mi_per_gallon_us_short">mpg (US)</string>
<string name="mi_us_per_l">Mile (US) per liter</string>
<string name="mi_us_per_l_short">mi/L (US)</string>
<string name="gallon_us_per_mile">Gallon per mile (US)</string>
<string name="gallon_us_per_mile_short">gal/mi (US)</string>
<string name="gallon_uk_per_mile">Gallon per mile (UK)</string>
<string name="gallon_uk_per_mile_short">gal/mi (UK)</string>
<string name="gallon_us_per_100_mile">Gallon per 100 mile (US)</string>
<string name="gallon_us_per_100_mile_short">gal/100 mi (US)</string>
<string name="gallon_uk_per_100_mile">Gallon per 100 mile (UK)</string>
<string name="gallon_uk_per_100_mile_short">gal/100 mi (UK)</string>
</resources>

View File

@ -55,4 +55,5 @@ enum class UnitGroup(
TORQUE(res = R.string.torque),
FLOW_RATE(res = R.string.flow_rate),
LUMINANCE(res = R.string.luminance),
FUEL_CONSUMPTION(res = R.string.fuel_consumption)
}

View File

@ -0,0 +1,56 @@
/*
* 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.unit
import com.sadellie.unitto.core.base.MAX_PRECISION
import com.sadellie.unitto.data.model.UnitGroup
import java.math.BigDecimal
import java.math.RoundingMode
data class FuelBackward(
override val id: String,
override val basicUnit: BigDecimal,
override val group: UnitGroup,
override val displayName: Int,
override val shortName: Int,
override val isFavorite: Boolean = false,
override val pairId: String? = null,
override val counter: Int = 0,
) : DefaultUnit {
override fun convert(unitTo: DefaultUnit, value: BigDecimal): BigDecimal {
// Avoid division by zero
if (unitTo.basicUnit.compareTo(BigDecimal.ZERO) == 0) return BigDecimal.ZERO
return when (unitTo) {
is FuelForward -> this
.basicUnit
.setScale(MAX_PRECISION, RoundingMode.HALF_EVEN)
.div(unitTo.basicUnit)
.div(value)
is FuelBackward -> unitTo
.basicUnit
.setScale(MAX_PRECISION, RoundingMode.HALF_EVEN)
.div(this.basicUnit)
.multiply(value)
else -> BigDecimal.ZERO
}
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.unit
import com.sadellie.unitto.core.base.MAX_PRECISION
import com.sadellie.unitto.data.model.UnitGroup
import java.math.BigDecimal
import java.math.RoundingMode
data class FuelForward(
override val id: String,
override val basicUnit: BigDecimal,
override val group: UnitGroup,
override val displayName: Int,
override val shortName: Int,
override val isFavorite: Boolean = false,
override val pairId: String? = null,
override val counter: Int = 0,
) : DefaultUnit {
override fun convert(unitTo: DefaultUnit, value: BigDecimal): BigDecimal {
// Avoid division by zero
if (unitTo.basicUnit.compareTo(BigDecimal.ZERO) == 0) return BigDecimal.ZERO
return when (unitTo) {
is FuelForward -> this
.basicUnit
.setScale(MAX_PRECISION, RoundingMode.HALF_EVEN)
.div(unitTo.basicUnit).multiply(value)
is FuelBackward -> unitTo
.basicUnit
.setScale(MAX_PRECISION, RoundingMode.HALF_EVEN)
.div(this.basicUnit)
.div(value)
else -> BigDecimal.ZERO
}
}
}

View File

@ -642,4 +642,16 @@ object MyUnitIDS {
const val blondel = "blondel"
const val bril = "bril"
const val skot = "skot"
// FUEL
const val kilometer_per_liter = "kilometer_per_liter"
const val liter_per_kilometer = "liter_per_kilometer"
const val liter_per_100_kilometer = "liter_per_100_kilometer"
const val mile_per_gallon_uk = "mile_per_gallon_uk"
const val mile_per_gallon_us = "mile_per_gallon_us"
const val mile_us_per_liter = "mile_us_per_liter"
const val gallon_us_per_mile = "gallon_us_per_mile"
const val gallon_uk_per_mile = "gallon_uk_per_mile"
const val gallon_us_per_100_mile = "gallon_us_per_100_mile"
const val gallon_uk_per_100_mile = "gallon_uk_per_100_mile"
}

View File

@ -24,6 +24,8 @@ import com.sadellie.unitto.data.database.UnitsEntity
import com.sadellie.unitto.data.model.UnitGroup
import com.sadellie.unitto.data.model.UnitsListSorting
import com.sadellie.unitto.data.model.unit.AbstractUnit
import com.sadellie.unitto.data.model.unit.FuelBackward
import com.sadellie.unitto.data.model.unit.FuelForward
import com.sadellie.unitto.data.model.unit.NormalUnit
import com.sadellie.unitto.data.model.unit.NumberBaseUnit
import com.sadellie.unitto.data.model.unit.ReverseUnit
@ -39,6 +41,7 @@ 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.fuelConsumptionCollection
import com.sadellie.unitto.data.units.collections.lengthCollection
import com.sadellie.unitto.data.units.collections.luminanceCollection
import com.sadellie.unitto.data.units.collections.massCollection
@ -91,7 +94,8 @@ class UnitsRepository @Inject constructor(
forceCollection +
torqueCollection +
flowRateCollection +
luminanceCollection
luminanceCollection +
fuelConsumptionCollection
)
@OptIn(ExperimentalCoroutinesApi::class)
@ -120,6 +124,16 @@ class UnitsRepository @Inject constructor(
counter = based.frequency,
pairId = based.pairedUnitId
)
is FuelForward -> foundUnit.copy(
isFavorite = based.isFavorite,
counter = based.frequency,
pairId = based.pairedUnitId
)
is FuelBackward -> foundUnit.copy(
isFavorite = based.isFavorite,
counter = based.frequency,
pairId = based.pairedUnitId
)
else -> return@forEach
}

View File

@ -0,0 +1,42 @@
/*
* 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.core.base.R
import com.sadellie.unitto.data.model.UnitGroup
import com.sadellie.unitto.data.model.unit.AbstractUnit
import com.sadellie.unitto.data.model.unit.FuelBackward
import com.sadellie.unitto.data.model.unit.FuelForward
import com.sadellie.unitto.data.units.MyUnitIDS
import java.math.BigDecimal
val fuelConsumptionCollection: List<AbstractUnit> by lazy {
listOf(
FuelForward(MyUnitIDS.kilometer_per_liter, BigDecimal.valueOf(1), UnitGroup.FUEL_CONSUMPTION, R.string.km_per_l, R.string.km_per_l_short),
FuelBackward(MyUnitIDS.liter_per_kilometer, BigDecimal.valueOf(1), UnitGroup.FUEL_CONSUMPTION, R.string.l_per_km, R.string.l_per_km_short),
FuelBackward(MyUnitIDS.liter_per_100_kilometer, BigDecimal.valueOf(100), UnitGroup.FUEL_CONSUMPTION, R.string.l_per_100_km, R.string.l_per_100_km_short),
FuelForward(MyUnitIDS.mile_per_gallon_uk, BigDecimal.valueOf(0.35400619), UnitGroup.FUEL_CONSUMPTION, R.string.mi_per_gallon_uk, R.string.mi_per_gallon_uk_short),
FuelForward(MyUnitIDS.mile_per_gallon_us, BigDecimal.valueOf(0.4251437075), UnitGroup.FUEL_CONSUMPTION, R.string.mi_per_gallon_us, R.string.mi_per_gallon_us_short),
FuelForward(MyUnitIDS.mile_us_per_liter, BigDecimal.valueOf(1.609344), UnitGroup.FUEL_CONSUMPTION, R.string.mi_us_per_l, R.string.mi_us_per_l_short),
FuelBackward(MyUnitIDS.gallon_us_per_mile, BigDecimal.valueOf(0.4251437075), UnitGroup.FUEL_CONSUMPTION, R.string.gallon_us_per_mile, R.string.gallon_us_per_mile_short),
FuelBackward(MyUnitIDS.gallon_uk_per_mile, BigDecimal.valueOf(0.35400619), UnitGroup.FUEL_CONSUMPTION, R.string.gallon_uk_per_mile, R.string.gallon_uk_per_mile_short),
FuelBackward(MyUnitIDS.gallon_us_per_100_mile, BigDecimal.valueOf(42.51437075), UnitGroup.FUEL_CONSUMPTION, R.string.gallon_us_per_100_mile, R.string.gallon_us_per_100_mile_short),
FuelBackward(MyUnitIDS.gallon_uk_per_100_mile, BigDecimal.valueOf(35.400618996), UnitGroup.FUEL_CONSUMPTION, R.string.gallon_uk_per_100_mile, R.string.gallon_uk_per_100_mile_short),
)
}

View File

@ -524,6 +524,20 @@ class AllUnitsTest {
skot.checkWith(lumen_per_square_meter_per_steradian, "6723", "2.14")
}
@Test
fun testFuelConsumptionCollection() = testWithUnits {
kilometer_per_liter.checkWith(gallon_uk_per_mile, "47.45", "0.00746")
liter_per_kilometer.checkWith(kilometer_per_liter, "47.45", "0.02107")
liter_per_100_kilometer.checkWith(kilometer_per_liter, "47.45", "2.10748")
mile_per_gallon_uk.checkWith(mile_per_gallon_us, "47.45", "39.51039")
mile_per_gallon_us.checkWith(gallon_us_per_100_mile, "47.45", "2.10748")
mile_us_per_liter.checkWith(gallon_uk_per_100_mile, "47.45", "0.46358")
gallon_us_per_mile.checkWith(liter_per_100_kilometer, "47.45", "11160.93198")
gallon_uk_per_mile.checkWith(kilometer_per_liter, "47.45", "0.00746")
gallon_us_per_100_mile.checkWith(gallon_us_per_mile, "47.45", "0.4745")
gallon_uk_per_100_mile.checkWith(mile_per_gallon_us, "47.45", "1.75485")
}
private fun String.checkWith(checkingId: String, value: String, expected: String) {
val unitFrom = allUnitsRepository.getById(this)
val unitTo = allUnitsRepository.getById(checkingId)