Skip to content

Commit

Permalink
improve saving and loading settings
Browse files Browse the repository at this point in the history
  • Loading branch information
konekowo committed Aug 23, 2024
1 parent 7c021bd commit 666c3c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Settings/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export abstract class Setting {

public abstract getValue(): any;

public abstract getDefaultValue(): any;

public abstract setValue(value: any): void;

/** When implementing this method, do <u>**NOT**</u> save the settings. This is so that loading won't reset most settings. */
Expand Down
18 changes: 15 additions & 3 deletions src/Settings/SettingType/DropdownSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ export abstract class DropdownSetting extends Setting {
return this.value;
}

public getDefaultValue(): DropDownOption {
return this.defaultValue;
}

public setValue(value: DropDownOption) {
this.value = value;
Settings.save();
if (this.list.find((option) => option.value == value.value && option.displayName == value.displayName )) {
this.value = value;
Settings.save();
} else {
console.warn('The value provided to this DropDownSetting does not exist in the option list! Ignoring value provided.');
}
}

public loadFromSaveValue(value: DropDownOption) {
this.value = value;
if (this.list.find((option) => option.value == value.value && option.displayName == value.displayName)) {
this.value = value;
} else {
console.warn('The value provided to this DropDownSetting does not exist in the option list! Ignoring value provided.');
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Settings/SettingType/RangeSetting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Setting} from "../Setting";
import {Settings} from "../Settings";
import {MathUtil} from "../../Util/MathUtil";

export abstract class RangeSetting extends Setting {
public abstract readonly minValue: number;
Expand All @@ -11,12 +12,17 @@ export abstract class RangeSetting extends Setting {
public getValue(): number {
return this.value;
}

public getDefaultValue(): number {
return this.defaultValue;
}

public setValue(value: number) {
this.value = value;
this.value = MathUtil.clamp(this.minValue, this.maxValue, value);
Settings.save();
}

public loadFromSaveValue(value: number) {
this.value = value;
this.value = MathUtil.clamp(this.minValue, this.maxValue, value);
}
}
5 changes: 4 additions & 1 deletion src/Settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export class Settings {
let settings = this.getList();
let settingSaveData: SettingSaveData[] = [];
settings.forEach((setting: SettingData) => {
settingSaveData.push({info: setting.info, value: setting.setting.getValue()});
// only save if setting is different from default value
if (setting.setting.getValue() != setting.setting.getDefaultValue()) {
settingSaveData.push({info: setting.info, value: setting.setting.getValue()});
}
});
window.localStorage.setItem("settings", JSON.stringify(settingSaveData));
}
Expand Down

0 comments on commit 666c3c0

Please sign in to comment.