diff --git a/CHANGELOG.md b/CHANGELOG.md index f87b577c..b2917476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Energy unit converter ([#192]) +- Pressure unit converter ([#191]) +- Speed unit converter ([#190]) ### Fixed - Fixed issue that occurs after doing a percent operation ([#160]) @@ -64,6 +68,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#44]: https://github.com/FossifyOrg/Calculator/issues/44 [#64]: https://github.com/FossifyOrg/Calculator/issues/64 [#160]: https://github.com/FossifyOrg/Calculator/issues/160 +[#190]: https://github.com/FossifyOrg/Calculator/issues/190 +[#191]: https://github.com/FossifyOrg/Calculator/issues/191 +[#192]: https://github.com/FossifyOrg/Calculator/issues/192 [Unreleased]: https://github.com/FossifyOrg/Calculator/compare/1.2.0...HEAD [1.2.0]: https://github.com/FossifyOrg/Calculator/compare/1.1.0...1.2.0 diff --git a/app/src/main/kotlin/org/fossify/math/helpers/converters/Base.kt b/app/src/main/kotlin/org/fossify/math/helpers/converters/Base.kt index 06c4756b..71d50a0c 100644 --- a/app/src/main/kotlin/org/fossify/math/helpers/converters/Base.kt +++ b/app/src/main/kotlin/org/fossify/math/helpers/converters/Base.kt @@ -13,7 +13,10 @@ interface Converter { VolumeConverter, MassConverter, TemperatureConverter, - TimeConverter + TimeConverter, + SpeedConverter, + PressureConverter, + EnergyConverter ) } diff --git a/app/src/main/kotlin/org/fossify/math/helpers/converters/EnergyConverter.kt b/app/src/main/kotlin/org/fossify/math/helpers/converters/EnergyConverter.kt new file mode 100644 index 00000000..b8edc623 --- /dev/null +++ b/app/src/main/kotlin/org/fossify/math/helpers/converters/EnergyConverter.kt @@ -0,0 +1,144 @@ +package org.fossify.math.helpers.converters + +import org.fossify.math.R +import java.math.BigDecimal + +/*** + * Base unit: joule. + * + * References: + * - https://en.wikipedia.org/wiki/Joule + * - https://en.wikipedia.org/wiki/Calorie + * - https://en.wikipedia.org/wiki/Kilowatt-hour + * - https://en.wikipedia.org/wiki/Electronvolt + * - https://en.wikipedia.org/wiki/British_thermal_unit + * - https://en.wikipedia.org/wiki/Foot-pound_(energy) + * - https://en.wikipedia.org/wiki/Erg + * - https://en.wikipedia.org/wiki/Therm + */ +object EnergyConverter : Converter { + override val nameResId: Int = R.string.unit_energy + override val imageResId: Int = R.drawable.ic_energy_vector + override val key: String = "EnergyConverter" + + sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) : + Converter.Unit(nameResId, symbolResId, factor, key) { + data object Joule : Unit( + nameResId = R.string.unit_energy_joule, + symbolResId = R.string.unit_energy_joule_symbol, + factor = BigDecimal.ONE, + key = "Joule" + ) + + data object Kilojoule : Unit( + nameResId = R.string.unit_energy_kilojoule, + symbolResId = R.string.unit_energy_kilojoule_symbol, + factor = BigDecimal("1000"), + key = "Kilojoule" + ) + + data object Megajoule : Unit( + nameResId = R.string.unit_energy_megajoule, + symbolResId = R.string.unit_energy_megajoule_symbol, + factor = BigDecimal("1000000"), + key = "Megajoule" + ) + + data object Gigajoule : Unit( + nameResId = R.string.unit_energy_gigajoule, + symbolResId = R.string.unit_energy_gigajoule_symbol, + factor = BigDecimal("1000000000"), + key = "Gigajoule" + ) + + data object Calorie : Unit( + nameResId = R.string.unit_energy_calorie, + symbolResId = R.string.unit_energy_calorie_symbol, + factor = BigDecimal("4.184"), + key = "Calorie" + ) + + data object Kilocalorie : Unit( + nameResId = R.string.unit_energy_kilocalorie, + symbolResId = R.string.unit_energy_kilocalorie_symbol, + factor = BigDecimal("4184"), + key = "Kilocalorie" + ) + + data object WattHour : Unit( + nameResId = R.string.unit_energy_watt_hour, + symbolResId = R.string.unit_energy_watt_hour_symbol, + factor = BigDecimal("3600"), + key = "WattHour" + ) + + data object KilowattHour : Unit( + nameResId = R.string.unit_energy_kilowatt_hour, + symbolResId = R.string.unit_energy_kilowatt_hour_symbol, + factor = BigDecimal("3600000"), + key = "KilowattHour" + ) + + data object MegawattHour : Unit( + nameResId = R.string.unit_energy_megawatt_hour, + symbolResId = R.string.unit_energy_megawatt_hour_symbol, + factor = BigDecimal("3600000000"), + key = "MegawattHour" + ) + + data object Electronvolt : Unit( + nameResId = R.string.unit_energy_electronvolt, + symbolResId = R.string.unit_energy_electronvolt_symbol, + factor = BigDecimal("1.602176634E-19"), + key = "Electronvolt" + ) + + data object BritishThermalUnit : Unit( + nameResId = R.string.unit_energy_btu, + symbolResId = R.string.unit_energy_btu_symbol, + factor = BigDecimal("1055.05585262"), + key = "BritishThermalUnit" + ) + + data object Therm : Unit( + nameResId = R.string.unit_energy_therm, + symbolResId = R.string.unit_energy_therm_symbol, + factor = BigDecimal("105505585.262"), + key = "Therm" + ) + + data object FootPound : Unit( + nameResId = R.string.unit_energy_foot_pound, + symbolResId = R.string.unit_energy_foot_pound_symbol, + factor = BigDecimal("1.3558179483314004"), + key = "FootPound" + ) + + data object Erg : Unit( + nameResId = R.string.unit_energy_erg, + symbolResId = R.string.unit_energy_erg_symbol, + factor = BigDecimal("0.0000001"), + key = "Erg" + ) + } + + override val units: List = listOf( + Unit.Joule, + Unit.Kilojoule, + Unit.Megajoule, + Unit.Gigajoule, + Unit.Calorie, + Unit.Kilocalorie, + Unit.WattHour, + Unit.KilowattHour, + Unit.MegawattHour, + Unit.Electronvolt, + Unit.BritishThermalUnit, + Unit.Therm, + Unit.FootPound, + Unit.Erg, + ) + + override val defaultTopUnit: Unit = Unit.Kilocalorie + override val defaultBottomUnit: Unit = Unit.Kilojoule +} diff --git a/app/src/main/kotlin/org/fossify/math/helpers/converters/PressureConverter.kt b/app/src/main/kotlin/org/fossify/math/helpers/converters/PressureConverter.kt new file mode 100644 index 00000000..7f707344 --- /dev/null +++ b/app/src/main/kotlin/org/fossify/math/helpers/converters/PressureConverter.kt @@ -0,0 +1,109 @@ +package org.fossify.math.helpers.converters + +import org.fossify.math.R +import java.math.BigDecimal + +/** + * Base unit: pascal. + * + * Main references: + * - https://en.wikipedia.org/wiki/Pascal_(unit) + * - https://en.wikipedia.org/wiki/Bar_(unit) + * - https://en.wikipedia.org/wiki/Atmosphere_(unit) + * - https://en.wikipedia.org/wiki/Pounds_per_square_inch + * - https://en.wikipedia.org/wiki/Torr + * - https://en.wikipedia.org/wiki/Millimetre_of_mercury + */ +object PressureConverter : Converter { + override val nameResId: Int = R.string.unit_pressure + override val imageResId: Int = R.drawable.ic_pressure_vector + override val key: String = "PressureConverter" + + sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) : + Converter.Unit(nameResId, symbolResId, factor, key) { + data object Pascal : Unit( + nameResId = R.string.unit_pressure_pascal, + symbolResId = R.string.unit_pressure_pascal_symbol, + factor = BigDecimal.ONE, + key = "Pascal" + ) + + data object Kilopascal : Unit( + nameResId = R.string.unit_pressure_kilopascal, + symbolResId = R.string.unit_pressure_kilopascal_symbol, + factor = BigDecimal("1000"), + key = "Kilopascal" + ) + + data object Megapascal : Unit( + nameResId = R.string.unit_pressure_megapascal, + symbolResId = R.string.unit_pressure_megapascal_symbol, + factor = BigDecimal("1000000"), + key = "Megapascal" + ) + + data object Bar : Unit( + nameResId = R.string.unit_pressure_bar, + symbolResId = R.string.unit_pressure_bar_symbol, + factor = BigDecimal("100000"), + key = "Bar" + ) + + data object Millibar : Unit( + nameResId = R.string.unit_pressure_millibar, + symbolResId = R.string.unit_pressure_millibar_symbol, + factor = BigDecimal("100"), + key = "Millibar" + ) + + data object Atmosphere : Unit( + nameResId = R.string.unit_pressure_atmosphere, + symbolResId = R.string.unit_pressure_atmosphere_symbol, + factor = BigDecimal("101325"), + key = "Atmosphere" + ) + + data object Psi : Unit( + nameResId = R.string.unit_pressure_psi, + symbolResId = R.string.unit_pressure_psi_symbol, + factor = BigDecimal("6894.757293168"), + key = "Psi" + ) + + data object Torr : Unit( + nameResId = R.string.unit_pressure_torr, + symbolResId = R.string.unit_pressure_torr_symbol, + factor = BigDecimal("133.32236842105"), + key = "Torr" + ) + + data object MillimeterOfMercury : Unit( + nameResId = R.string.unit_pressure_mmhg, + symbolResId = R.string.unit_pressure_mmhg_symbol, + factor = BigDecimal("133.322387415"), + key = "MillimeterOfMercury" + ) + + data object InchOfMercury : Unit( + nameResId = R.string.unit_pressure_inhg, + symbolResId = R.string.unit_pressure_inhg_symbol, + factor = BigDecimal("3386.389"), + key = "InchOfMercury" + ) + } + + override val units: List = listOf( + Unit.Pascal, + Unit.Kilopascal, + Unit.Megapascal, + Unit.Bar, + Unit.Millibar, + Unit.Atmosphere, + Unit.Psi, + Unit.Torr, + Unit.MillimeterOfMercury, + Unit.InchOfMercury + ) + override val defaultTopUnit: Unit = Unit.Bar + override val defaultBottomUnit: Unit = Unit.Psi +} diff --git a/app/src/main/kotlin/org/fossify/math/helpers/converters/SpeedConverter.kt b/app/src/main/kotlin/org/fossify/math/helpers/converters/SpeedConverter.kt new file mode 100644 index 00000000..0b24d428 --- /dev/null +++ b/app/src/main/kotlin/org/fossify/math/helpers/converters/SpeedConverter.kt @@ -0,0 +1,95 @@ +package org.fossify.math.helpers.converters + +import org.fossify.commons.helpers.HOUR_SECONDS +import org.fossify.math.R +import org.fossify.math.helpers.MATH_CONTEXT +import java.math.BigDecimal + +/** + * Base unit: meter per second. + * + * Main references: + * - https://en.wikipedia.org/wiki/Metre_per_second + * - https://en.wikipedia.org/wiki/Kilometres_per_hour + * - https://en.wikipedia.org/wiki/Miles_per_hour + * - https://en.wikipedia.org/wiki/Knot_(unit) + * - https://en.wikipedia.org/wiki/Mach_number + * - https://en.wikipedia.org/wiki/Speed_of_light + */ +object SpeedConverter : Converter { + override val nameResId: Int = R.string.unit_speed + override val imageResId: Int = R.drawable.ic_speed_vector + override val key: String = "SpeedConverter" + + sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) : + Converter.Unit(nameResId, symbolResId, factor, key) { + data object MeterPerSecond : Unit( + nameResId = R.string.unit_speed_meter_per_second, + symbolResId = R.string.unit_speed_meter_per_second_symbol, + factor = BigDecimal.ONE, + key = "MeterPerSecond" + ) + + data object KilometerPerSecond : Unit( + nameResId = R.string.unit_speed_kilometer_per_second, + symbolResId = R.string.unit_speed_kilometer_per_second_symbol, + factor = BigDecimal("1000"), + key = "KilometerPerSecond" + ) + + data object KilometerPerHour : Unit( + nameResId = R.string.unit_speed_kilometer_per_hour, + symbolResId = R.string.unit_speed_kilometer_per_hour_symbol, + factor = BigDecimal("1000").divide(BigDecimal("3600"), MATH_CONTEXT), + key = "KilometerPerHour" + ) + + data object MilePerHour : Unit( + nameResId = R.string.unit_speed_mile_per_hour, + symbolResId = R.string.unit_speed_mile_per_hour_symbol, + factor = BigDecimal("0.44704"), + key = "MilePerHour" + ) + + data object Knot : Unit( + nameResId = R.string.unit_speed_knot, + symbolResId = R.string.unit_speed_knot_symbol, + factor = BigDecimal("1852").divide(BigDecimal("3600"), MATH_CONTEXT), + key = "Knot" + ) + + data object FootPerSecond : Unit( + nameResId = R.string.unit_speed_foot_per_second, + symbolResId = R.string.unit_speed_foot_per_second_symbol, + factor = BigDecimal("0.3048"), + key = "FootPerSecond" + ) + + data object Mach : Unit( + nameResId = R.string.unit_speed_mach, + symbolResId = R.string.unit_speed_mach_symbol, + factor = BigDecimal("340.27"), // ISA: dry air, 15 °C, sea level + key = "Mach" + ) + + data object SpeedOfLight : Unit( + nameResId = R.string.unit_speed_speed_of_light, + symbolResId = R.string.unit_speed_speed_of_light_symbol, + factor = BigDecimal("299792458"), + key = "SpeedOfLight" + ) + } + + override val units: List = listOf( + Unit.MeterPerSecond, + Unit.KilometerPerSecond, + Unit.KilometerPerHour, + Unit.MilePerHour, + Unit.Knot, + Unit.FootPerSecond, + Unit.Mach, + Unit.SpeedOfLight + ) + override val defaultTopUnit: Unit = Unit.KilometerPerHour + override val defaultBottomUnit: Unit = Unit.MilePerHour +} diff --git a/app/src/main/res/drawable/ic_energy_vector.xml b/app/src/main/res/drawable/ic_energy_vector.xml new file mode 100644 index 00000000..2ce9e79c --- /dev/null +++ b/app/src/main/res/drawable/ic_energy_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/app/src/main/res/drawable/ic_pressure_vector.xml b/app/src/main/res/drawable/ic_pressure_vector.xml new file mode 100644 index 00000000..da3e1d2a --- /dev/null +++ b/app/src/main/res/drawable/ic_pressure_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/app/src/main/res/drawable/ic_speed_vector.xml b/app/src/main/res/drawable/ic_speed_vector.xml new file mode 100644 index 00000000..ed090ba3 --- /dev/null +++ b/app/src/main/res/drawable/ic_speed_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 40324cac..6d7df6b7 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -20,6 +20,7 @@ au pc ly + km² cm² @@ -30,6 +31,7 @@ sq in ac ha + dm³ cm³ @@ -55,6 +57,7 @@ pt (imp) gi (imp) fl oz (imp) + g kg mg @@ -69,10 +72,12 @@ sh tn kt ct + °C °F °R K + h m s @@ -80,4 +85,39 @@ d wk y + + m/s + km/s + km/h + mph + kn + ft/s + Ma + c + + Pa + kPa + MPa + bar + mbar + atm + psi + Torr + mmHg + inHg + + J + kJ + MJ + GJ + cal + kcal + Wh + kWh + MWh + eV + BTU + thm + ft⋅lbf + erg diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c6b0a46..779a7111 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,6 +35,7 @@ Astronomical Unit Parsec Light Year + Area Square Kilometer Square Meter @@ -46,6 +47,7 @@ Square Inch Acre Hectare + Volume Cubic Meter Cubic Decimeter @@ -72,6 +74,7 @@ Pint (Imperial) Gill (Imperial) Fluid Ounce (Imperial) + Mass Gram Kilogram @@ -87,11 +90,13 @@ Short Ton Carat Carat (Metric) + Temperature Celsius Fahrenheit Rankine Kelvin + Time Hour Minute @@ -101,6 +106,44 @@ Week Year + Speed + Meter per second + Kilometer per second + Kilometer per hour + Mile per hour + Knot + Foot per second + Mach + Speed of light + + Pressure + Pascal + Kilopascal + Megapascal + Bar + Millibar + Atmosphere + Pound per square inch + Torr + Millimeter of mercury + Inch of mercury + + Energy + Joule + Kilojoule + Megajoule + Gigajoule + Calorie + Kilocalorie + Watt hour + Kilowatt hour + Megawatt hour + Electronvolt + British thermal unit + Therm + Foot-pound + Erg + How does the C (Clear) button work? Tapping on it will remove one character at a time. Long pressing it will reset all fields at once.