Merge pull request #206 from hargata/Hargata/persist.fuel.settings

persist gas tab settings.
This commit is contained in:
Hargata Softworks 2024-01-31 17:28:00 -07:00 committed by GitHub
commit c747889f85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 140 additions and 15 deletions

View File

@ -608,6 +608,15 @@ namespace CarCareTracker.Controllers
var result = _gasRecordDataAccess.DeleteGasRecordById(gasRecordId);
return Json(result);
}
[HttpPost]
public IActionResult SaveUserGasTabPreferences(string gasUnit, string fuelMileageUnit)
{
var currentConfig = _config.GetUserConfig(User);
currentConfig.PreferredGasUnit = gasUnit;
currentConfig.PreferredGasMileageUnit = fuelMileageUnit;
var result = _config.SaveUserConfig(User, currentConfig);
return Json(result);
}
#endregion
#region "Service Records"
[TypeFilter(typeof(CollaboratorFilter))]

View File

@ -108,6 +108,8 @@ namespace CarCareTracker.Helper
UseThreeDecimalGasCost = bool.Parse(_config[nameof(UserConfig.UseThreeDecimalGasCost)]),
EnableAutoReminderRefresh = bool.Parse(_config[nameof(UserConfig.EnableAutoReminderRefresh)]),
EnableAutoOdometerInsert = bool.Parse(_config[nameof(UserConfig.EnableAutoOdometerInsert)]),
PreferredGasMileageUnit = _config[nameof(UserConfig.PreferredGasMileageUnit)],
PreferredGasUnit = _config[nameof(UserConfig.PreferredGasUnit)],
VisibleTabs = _config.GetSection("VisibleTabs").Get<List<ImportMode>>(),
DefaultTab = (ImportMode)int.Parse(_config[nameof(UserConfig.DefaultTab)])
};

View File

@ -13,6 +13,8 @@
public bool UseMarkDownOnSavedNotes { get; set; }
public bool EnableAutoReminderRefresh { get; set; }
public bool EnableAutoOdometerInsert { get; set; }
public string PreferredGasUnit { get; set; } = string.Empty;
public string PreferredGasMileageUnit { get; set; } = string.Empty;
public string UserNameHash { get; set; }
public string UserPasswordHash { get; set;}
public List<ImportMode> VisibleTabs { get; set; } = new List<ImportMode>() {

View File

@ -12,6 +12,8 @@
var gasCostFormat = useThreeDecimals ? "C3" : "C2";
var useKwh = Model.UseKwh;
var useHours = Model.UseHours;
string preferredFuelEconomyUnit = userConfig.PreferredGasMileageUnit;
string preferredGasUnit = userConfig.PreferredGasUnit;
string consumptionUnit;
string fuelEconomyUnit;
string distanceUnit = useHours ? "h" : (useMPG ? "mi." : "km");
@ -118,3 +120,14 @@
</div>
</div>
</div>
<script>
@if (!string.IsNullOrWhiteSpace(preferredFuelEconomyUnit))
{
@:convertFuelMileageUnits(decodeHTMLEntities('@fuelEconomyUnit'), decodeHTMLEntities('@preferredFuelEconomyUnit'));
}
@if (!string.IsNullOrWhiteSpace(preferredGasUnit))
{
@:convertGasConsumptionUnits(decodeHTMLEntities('@consumptionUnit'), decodeHTMLEntities('@preferredGasUnit'));
}
</script>

View File

@ -16,7 +16,9 @@
"EnableAutoOdometerInsert": false,
"UseUKMPG": false,
"UseThreeDecimalGasCost": true,
"UseMarkDownOnSavedNotes": false,
"UseMarkDownOnSavedNotes": false,
"PreferredGasMileageUnit": "",
"PreferredGasUnit": "",
"VisibleTabs": [ 0, 1, 4, 2, 3, 6, 5, 8 ],
"DefaultTab": 8,
"UserNameHash": "",

View File

@ -134,13 +134,21 @@ function getAndValidateGasRecordValues() {
}
}
function toggleUnits(sender) {
event.preventDefault();
//check which column to convert.
sender = $(sender);
if (sender.attr("data-gas") == "consumption") {
switch (sender.attr("data-unit")) {
case "US gal":
function saveUserGasTabPreferences() {
var gasUnit = $("[data-gas='consumption']").attr("data-unit");
var fuelMileageUnit = $("[data-gas='fueleconomy']").attr("data-unit");
$.post('/Vehicle/SaveUserGasTabPreferences', { gasUnit: gasUnit, fuelMileageUnit: fuelMileageUnit }, function (data) {
if (!data) {
errorToast("Error Saving User Preferences");
}
});
}
function convertGasConsumptionUnits(currentUnit, destinationUnit) {
var sender = $("[data-gas='consumption']");
if (currentUnit == "US gal") {
switch (destinationUnit) {
case "l":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) * 3.785;
elem.innerText = convertedAmount.toFixed(2);
@ -152,8 +160,40 @@ function toggleUnits(sender) {
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
case "l":
case "imp gal":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) / 1.201;
elem.innerText = convertedAmount.toFixed(2);
sender.text(sender.text().replace(sender.attr("data-unit"), "imp gal"));
sender.attr("data-unit", "imp gal");
});
$("[data-gas-type='unitcost']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) * 1.201;
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
}
} else if (currentUnit == "l") {
switch (destinationUnit) {
case "US gal":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) / 3.785;
elem.innerText = convertedAmount.toFixed(2);
sender.text(sender.text().replace(sender.attr("data-unit"), "US gal"));
sender.attr("data-unit", "US gal");
});
$("[data-gas-type='unitcost']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) * 3.785;
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
case "imp gal":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) / 4.546;
elem.innerText = convertedAmount.toFixed(2);
@ -165,8 +205,12 @@ function toggleUnits(sender) {
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
case "imp gal":
}
} else if (currentUnit == "imp gal") {
switch (destinationUnit) {
case "US gal":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) * 1.201;
elem.innerText = convertedAmount.toFixed(2);
@ -178,11 +222,31 @@ function toggleUnits(sender) {
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
case "l":
$("[data-gas-type='consumption']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) * 4.546;
elem.innerText = convertedAmount.toFixed(2);
sender.text(sender.text().replace(sender.attr("data-unit"), "l"));
sender.attr("data-unit", "l");
});
$("[data-gas-type='unitcost']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText) / 4.546;
var decimalPoints = getGlobalConfig().useThreeDecimals ? 3 : 2;
elem.innerText = `${getGlobalConfig().currencySymbol}${convertedAmount.toFixed(decimalPoints)}`;
});
setDebounce(saveUserGasTabPreferences);
break;
}
} else if (sender.attr("data-gas") == "fueleconomy") {
switch (sender.attr("data-unit")) {
case "l/100km":
}
}
function convertFuelMileageUnits(currentUnit, destinationUnit) {
var sender = $("[data-gas='fueleconomy']");
if (currentUnit == "l/100km") {
switch (destinationUnit) {
case "km/l":
$("[data-gas-type='fueleconomy']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText);
if (convertedAmount > 0) {
@ -208,8 +272,12 @@ function toggleUnits(sender) {
sender.text(sender.text().replace(sender.attr("data-unit"), "km/l"));
sender.attr("data-unit", "km/l");
});
setDebounce(saveUserGasTabPreferences);
break;
case "km/l":
}
} else if (currentUnit == "km/l") {
switch (destinationUnit) {
case "l/100km":
$("[data-gas-type='fueleconomy']").map((index, elem) => {
var convertedAmount = globalParseFloat(elem.innerText);
if (convertedAmount > 0) {
@ -234,6 +302,35 @@ function toggleUnits(sender) {
sender.text(sender.text().replace(sender.attr("data-unit"), "l/100km"));
sender.attr("data-unit", "l/100km");
});
setDebounce(saveUserGasTabPreferences);
break;
}
}
}
function toggleUnits(sender) {
event.preventDefault();
//check which column to convert.
sender = $(sender);
if (sender.attr("data-gas") == "consumption") {
switch (sender.attr("data-unit")) {
case "US gal":
convertGasConsumptionUnits("US gal", "l");
break;
case "l":
convertGasConsumptionUnits("l", "imp gal");
break;
case "imp gal":
convertGasConsumptionUnits("imp gal", "US gal");
break;
}
} else if (sender.attr("data-gas") == "fueleconomy") {
switch (sender.attr("data-unit")) {
case "l/100km":
convertFuelMileageUnits("l/100km", "km/l");
break;
case "km/l":
convertFuelMileageUnits("km/l", "l/100km");
break;
}
}