UX Improvements

This commit is contained in:
Eric Gullickson
2025-09-26 14:45:03 -05:00
parent 56443d5b2f
commit 2e1b588270
13 changed files with 389 additions and 97 deletions

View File

@@ -37,7 +37,7 @@ export class EfficiencyCalculationService {
return null;
}
const value = UnitConversionService.calculateEfficiency(distance, currentLog.fuelUnits, unitSystem);
const value = UnitConversionService.calculateEfficiency(distance, currentLog.fuelUnits);
const labels = UnitConversionService.getUnitLabels(unitSystem);
return { value, unitSystem, label: labels.efficiencyUnits, calculationMethod: method };
}

View File

@@ -85,7 +85,17 @@ export class FuelLogsService {
if (cached) return cached;
const rows = await this.repository.findByVehicleIdEnhanced(vehicleId);
const response = rows.map(r => this.toEnhancedResponse(r, undefined, unitSystem));
const response = rows.map(r => {
const efficiency = EfficiencyCalculationService.calculateEfficiency(
{
tripDistance: r.trip_distance ?? undefined,
fuelUnits: r.fuel_units ?? undefined
},
null, // No previous odometer needed for trip distance calculation
unitSystem
);
return this.toEnhancedResponse(r, efficiency?.value ?? undefined, unitSystem);
});
await cacheService.set(cacheKey, response, this.cacheTTL);
return response;
}
@@ -96,7 +106,17 @@ export class FuelLogsService {
const cached = await cacheService.get<EnhancedFuelLogResponse[]>(cacheKey);
if (cached) return cached;
const rows = await this.repository.findByUserIdEnhanced(userId);
const response = rows.map(r => this.toEnhancedResponse(r, undefined, unitSystem));
const response = rows.map(r => {
const efficiency = EfficiencyCalculationService.calculateEfficiency(
{
tripDistance: r.trip_distance ?? undefined,
fuelUnits: r.fuel_units ?? undefined
},
null, // No previous odometer needed for trip distance calculation
unitSystem
);
return this.toEnhancedResponse(r, efficiency?.value ?? undefined, unitSystem);
});
await cacheService.set(cacheKey, response, this.cacheTTL);
return response;
}
@@ -106,7 +126,17 @@ export class FuelLogsService {
if (!row) throw new Error('Fuel log not found');
if (row.user_id !== userId) throw new Error('Unauthorized');
const { unitSystem } = await UserSettingsService.getUserSettings(userId);
return this.toEnhancedResponse(row, undefined, unitSystem);
const efficiency = EfficiencyCalculationService.calculateEfficiency(
{
tripDistance: row.trip_distance ?? undefined,
fuelUnits: row.fuel_units ?? undefined
},
null, // No previous odometer needed for trip distance calculation
unitSystem
);
return this.toEnhancedResponse(row, efficiency?.value ?? undefined, unitSystem);
}
async updateFuelLog(id: string, data: EnhancedUpdateFuelLogRequest, userId: string): Promise<EnhancedFuelLogResponse> {

View File

@@ -3,28 +3,29 @@ import { UnitSystem as CoreUnitSystem } from '../../../shared-minimal/utils/unit
export type UnitSystem = CoreUnitSystem;
export class UnitConversionService {
private static readonly MPG_TO_L100KM = 235.214;
private static readonly KM_PER_MILE = 1.60934;
private static readonly GALLONS_TO_LITERS = 3.78541;
static getUnitLabels(unitSystem: UnitSystem) {
return unitSystem === 'metric'
? { fuelUnits: 'liters', distanceUnits: 'kilometers', efficiencyUnits: 'L/100km' }
: { fuelUnits: 'gallons', distanceUnits: 'miles', efficiencyUnits: 'mpg' };
? { fuelUnits: 'liters', distanceUnits: 'kilometers', efficiencyUnits: 'km/L' }
: { fuelUnits: 'gallons', distanceUnits: 'miles', efficiencyUnits: 'MPG' };
}
static calculateEfficiency(distance: number, fuelUnits: number, unitSystem: UnitSystem): number {
static calculateEfficiency(distance: number, fuelUnits: number): number {
if (fuelUnits <= 0 || distance <= 0) return 0;
return unitSystem === 'metric'
? (fuelUnits / distance) * 100
: distance / fuelUnits;
return distance / fuelUnits; // Both km/L and MPG use distance/fuel formula
}
static convertEfficiency(efficiency: number, from: UnitSystem, to: UnitSystem): number {
if (from === to) return efficiency;
if (from === 'imperial' && to === 'metric') {
return efficiency > 0 ? this.MPG_TO_L100KM / efficiency : 0;
// MPG to km/L: MPG * (gallons_to_liters / km_per_mile)
return efficiency * (this.GALLONS_TO_LITERS / this.KM_PER_MILE);
}
if (from === 'metric' && to === 'imperial') {
return efficiency > 0 ? this.MPG_TO_L100KM / efficiency : 0;
// km/L to MPG: km/L * (km_per_mile / gallons_to_liters)
return efficiency * (this.KM_PER_MILE / this.GALLONS_TO_LITERS);
}
return efficiency;
}