The Unit Converter application is a simple and intuitive tool designed to convert various units of measurement. It supports a wide range of unit categories, including length, weight, volume, temperature, and more. This application provides an easy-to-use interface for quick and accurate conversions, making it a valuable tool for students, professionals, and anyone needing unit conversions.
Note
This repository is under constant development/refactor since the project is designed to showcase the basics of Open Harmony development and is not intended for production use.
- Length - convertion between meter, kilometer, mile, foot, inch.
- Weight - convertion between gram, kilogram, pound.
- Volume - convertion between liter, US gallon, UK gallon.
- Temperature - convertion between celsius, kelvin, fahrenheit.
- Pressure - convertion beetween pascal, bar, psi.
Here is a screenshot showing the main view of the application:
- Open the application.
- Select the category of units you wish to convert (e.g., Length, Weight).
- Select the input unit
- Select the output unit
- Enter the value to see the converted value in real-time.
- Prepare the
enum
for the area units and store inmodel/AreaUnit.ts
:
export enum AreaUnit {
SquareMeater = 'square_meter',
Hectare = 'hectare',
...
}
- Add
AreaUnits
toUnit
type union inmodel/Units.ts
:
export type Unit = LengthUnit | PressureUnit | TemperatureUnit | VolumeUnit | WeightUnit | AreaUnit;
- Prepare
MeasureFactory
implementation for area units convertion and save infeature/AreaFactory.ts
:
function convertSquareMeterTo(unit: AreaUnit, value: number): number {
switch(unit) {
case AreaUnit.SquareMeater: return value;
case AreaUnit.Hectare: return ...;
...
}
}
function convertHectareTo(unit: AreaUnit, value: number): number {
switch(unit) {
case AreaUnit.SquareMeater: return ...;
case AreaUnit.Hectare: return value;
...
}
}
type VolumeConverters = {
[key in AreaUnit]: (unit: AreaUnit, value: number) => number;
};
const areaConverters: AreaConverters = {
square_meter: convertSquareMeterTo,
hectare: convertHectareTo,
...
}
export class AreaFactory implements MeasureFactory {
static create(): AreaFactory {
return new AreaFactory();
}
getUnits(): Unit[] {
return Object.values(AreaUnit);
}
convert(fromUnit: Unit, toUnit: Unit, value: number): number {
return areaConverters[fromUnit as AreaUnit](toUnit as AreaeUnit, value);
}
}
- Extend
resources/measureFactories
map infeature/MeasureFactory.ts
:
export const measureFactories: MeasureFactories = {
...
area: () => AreaFactory.create()
}
- Add Area and area unit string labels in appropriate resource files (
string.json
):
{
"string": [
...
{ "name": "measure_area", "value": "Area" },
{ "name": "unit_square_meter", "value": "Square meter [㎡]" },
{ "name": "unit_hectare", "value": "Hectare [ha]" }
]
}
- Add area label to
measureLabels
map inresources/measureLabels.ets
:
export const measureLabels = {
...
area: $r('app.string.measure_area')
};
- Add area unit labels to
unitLabels
map inresources/UnitLabels.ets
:
export const unitLabels = {
...
square_meter: $r('app.string.unit_square_meter'),
hectare: $r('app.string.unit_hectare'),
...
};
We welcome contributions from the community! If you have suggestions for improvements or new features, please submit a pull request or open an issue on GitHub.
This project is licensed under the Apache License version 2.0. See the LICENSE file for more details.
For any questions or support, please contact us.