diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index dd7c5b5..1f1a6f0 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -538,10 +538,13 @@ namespace CarCareTracker.Controllers { computedResults = computedResults.OrderByDescending(x => DateTime.Parse(x.Date)).ThenByDescending(x => x.Mileage).ToList(); } - var vehicleIsElectric = _dataAccess.GetVehicleById(vehicleId).IsElectric; + var vehicleData = _dataAccess.GetVehicleById(vehicleId); + var vehicleIsElectric = vehicleData.IsElectric; + var vehicleUseHours = vehicleData.UseHours; var viewModel = new GasRecordViewModelContainer() { UseKwh = vehicleIsElectric, + UseHours = vehicleUseHours, GasRecords = computedResults }; return PartialView("_Gas", viewModel); @@ -564,9 +567,12 @@ namespace CarCareTracker.Controllers return Json(result); } [HttpGet] - public IActionResult GetAddGasRecordPartialView() + public IActionResult GetAddGasRecordPartialView(int vehicleId) { - return PartialView("_GasModal", new GasRecordInputContainer() { GasRecord = new GasRecordInput() }); + var vehicleData = _dataAccess.GetVehicleById(vehicleId); + var vehicleIsElectric = vehicleData.IsElectric; + var vehicleUseHours = vehicleData.UseHours; + return PartialView("_GasModal", new GasRecordInputContainer() { UseKwh = vehicleIsElectric, UseHours = vehicleUseHours, GasRecord = new GasRecordInput() }); } [HttpGet] public IActionResult GetGasRecordForEditById(int gasRecordId) @@ -585,10 +591,13 @@ namespace CarCareTracker.Controllers MissedFuelUp = result.MissedFuelUp, Notes = result.Notes }; - var vehicleIsElectric = _dataAccess.GetVehicleById(convertedResult.VehicleId).IsElectric; + var vehicleData = _dataAccess.GetVehicleById(convertedResult.VehicleId); + var vehicleIsElectric = vehicleData.IsElectric; + var vehicleUseHours = vehicleData.UseHours; var viewModel = new GasRecordInputContainer() { UseKwh = vehicleIsElectric, + UseHours = vehicleUseHours, GasRecord = convertedResult }; return PartialView("_GasModal", viewModel); diff --git a/Models/GasRecord/GasRecordInputContainer.cs b/Models/GasRecord/GasRecordInputContainer.cs index 5d9c88d..0cb4110 100644 --- a/Models/GasRecord/GasRecordInputContainer.cs +++ b/Models/GasRecord/GasRecordInputContainer.cs @@ -2,7 +2,8 @@ { public class GasRecordInputContainer { - public bool UseKwh { get; set; } - public GasRecordInput GasRecord { get; set; } + public bool UseKwh { get; set; } + public bool UseHours { get; set; } + public GasRecordInput GasRecord { get; set; } } } diff --git a/Models/GasRecord/GasRecordViewModelContainer.cs b/Models/GasRecord/GasRecordViewModelContainer.cs index 3efd468..e6a1de2 100644 --- a/Models/GasRecord/GasRecordViewModelContainer.cs +++ b/Models/GasRecord/GasRecordViewModelContainer.cs @@ -2,7 +2,8 @@ { public class GasRecordViewModelContainer { - public bool UseKwh { get; set; } - public List GasRecords { get; set; } = new List(); + public bool UseKwh { get; set; } + public bool UseHours { get; set; } + public List GasRecords { get; set; } = new List(); } } diff --git a/Models/Vehicle.cs b/Models/Vehicle.cs index cd01f98..5ae3bc8 100644 --- a/Models/Vehicle.cs +++ b/Models/Vehicle.cs @@ -9,5 +9,6 @@ public string Model { get; set; } public string LicensePlate { get; set; } public bool IsElectric { get; set; } = false; + public bool UseHours { get; set; } = false; } } diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index e6e9b0b..e861d27 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -11,28 +11,29 @@ var useThreeDecimals = userConfig.UseThreeDecimalGasCost; var gasCostFormat = useThreeDecimals ? "C3" : "C2"; var useKwh = Model.UseKwh; + var useHours = Model.UseHours; string consumptionUnit; string fuelEconomyUnit; - string distanceUnit = useMPG ? "mi." : "km"; + string distanceUnit = useHours ? "h" : (useMPG ? "mi." : "km"); if (useKwh) { consumptionUnit = "kWh"; - fuelEconomyUnit = useMPG ? "mi./kWh" : "kWh/100km"; + fuelEconomyUnit = useMPG ? $"{distanceUnit}/kWh" : $"kWh/100{distanceUnit}"; } else if (useMPG && useUKMPG) { consumptionUnit = "imp gal"; - fuelEconomyUnit = "mpg"; + fuelEconomyUnit = useHours ? "h/g" : "mpg"; } else if (useUKMPG) { - fuelEconomyUnit = "l/100mi."; + fuelEconomyUnit = useHours ? "l/100h" : "l/100mi."; consumptionUnit = "l"; - distanceUnit = "mi."; + distanceUnit = useHours ? "h" : "mi."; } else { consumptionUnit = useMPG ? "US gal" : "l"; - fuelEconomyUnit = useMPG ? "mpg" : "l/100km"; + fuelEconomyUnit = useHours ? (useMPG ? "h/g" : "l/100h") : (useMPG ? "mpg" : "l/100km"); } }
diff --git a/Views/Vehicle/_GasModal.cshtml b/Views/Vehicle/_GasModal.cshtml index caa2e41..8986375 100644 --- a/Views/Vehicle/_GasModal.cshtml +++ b/Views/Vehicle/_GasModal.cshtml @@ -5,6 +5,7 @@ var useMPG = config.GetUserConfig(User).UseMPG; var useUKMPG = config.GetUserConfig(User).UseUKMPG; var useKwh = Model.UseKwh; + var useHours = Model.UseHours; var isNew = Model.GasRecord.Id == 0; string consumptionUnit; string distanceUnit; @@ -19,7 +20,11 @@ { consumptionUnit = useMPG ? "gallons" : "liters"; } - if (useUKMPG) + if (useHours) + { + distanceUnit = "hours"; + } + else if (useUKMPG) { distanceUnit = "miles"; } diff --git a/Views/Vehicle/_VehicleHistory.cshtml b/Views/Vehicle/_VehicleHistory.cshtml index 575efac..2187cf1 100644 --- a/Views/Vehicle/_VehicleHistory.cshtml +++ b/Views/Vehicle/_VehicleHistory.cshtml @@ -5,22 +5,24 @@ var useMPG = config.GetUserConfig(User).UseMPG; var useUKMPG = config.GetUserConfig(User).UseUKMPG; var useKwh = Model.VehicleData.IsElectric; + var useHours = Model.VehicleData.UseHours; string fuelEconomyUnit; if (useKwh) { - fuelEconomyUnit = useMPG ? "mi./kWh" : "kWh/100km"; + var distanceUnit = useHours ? "h" : (useMPG ? "mi." : "km"); + fuelEconomyUnit = useMPG ? $"{distanceUnit}/kWh" : $"kWh/100{distanceUnit}"; } else if (useMPG && useUKMPG) { - fuelEconomyUnit = "mpg"; + fuelEconomyUnit = useHours ? "h/g" : "mpg"; } else if (useUKMPG) { - fuelEconomyUnit = "l/100mi."; + fuelEconomyUnit = useHours ? "l/100h" : "l/100mi."; } else { - fuelEconomyUnit = useMPG ? "mpg" : "l/100km"; + fuelEconomyUnit = useHours ? (useMPG ? "h/g" : "l/100h") : (useMPG ? "mpg" : "l/100km"); } } @model VehicleHistoryViewModel diff --git a/Views/Vehicle/_VehicleModal.cshtml b/Views/Vehicle/_VehicleModal.cshtml index f9d209c..9cd8bb7 100644 --- a/Views/Vehicle/_VehicleModal.cshtml +++ b/Views/Vehicle/_VehicleModal.cshtml @@ -33,6 +33,10 @@
+
+ + +
@if (!string.IsNullOrWhiteSpace(Model.ImageLocation)) { diff --git a/wwwroot/js/gasrecord.js b/wwwroot/js/gasrecord.js index df7359e..d984aad 100644 --- a/wwwroot/js/gasrecord.js +++ b/wwwroot/js/gasrecord.js @@ -1,5 +1,5 @@ function showAddGasRecordModal() { - $.get('/Vehicle/GetAddGasRecordPartialView', function (data) { + $.get(`/Vehicle/GetAddGasRecordPartialView?vehicleId=${GetVehicleId().vehicleId}`, function (data) { if (data) { $("#gasRecordModalContent").html(data); //initiate datepicker diff --git a/wwwroot/js/shared.js b/wwwroot/js/shared.js index 9dcb791..b95ca16 100644 --- a/wwwroot/js/shared.js +++ b/wwwroot/js/shared.js @@ -38,6 +38,7 @@ function saveVehicle(isEdit) { var vehicleModel = $("#inputModel").val(); var vehicleLicensePlate = $("#inputLicensePlate").val(); var vehicleIsElectric = $("#inputIsElectric").is(":checked"); + var vehicleUseHours = $("#inputUseHours").is(":checked"); //validate var hasError = false; if (vehicleYear.trim() == '' || parseInt(vehicleYear) < 1900) { @@ -74,7 +75,8 @@ function saveVehicle(isEdit) { make: vehicleMake, model: vehicleModel, licensePlate: vehicleLicensePlate, - isElectric: vehicleIsElectric + isElectric: vehicleIsElectric, + useHours: vehicleUseHours }, function (data) { if (data) { if (!isEdit) {