-
-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Efimov
committed
Sep 22, 2022
1 parent
e0692c1
commit 6266f29
Showing
2 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
const s_maxFavoritePresetsCount = 50; | ||
const s_favoritePresetsListConfigStorageName = "FavoritePresetsList"; | ||
|
||
class FavoritePreset { | ||
constructor(presetPath){ | ||
this.presetPath = presetPath; | ||
this.lastPickDate = Date.now(); | ||
} | ||
} | ||
|
||
|
||
class FavoritePresetsData { | ||
constructor() { | ||
this._favoritePresetsList = []; | ||
} | ||
|
||
_sort() { | ||
this._favoritePresetsList.sort((a, b) => (a.lastPickDate - b.lastPickDate)); | ||
} | ||
|
||
_purgeOldPresets() { | ||
this._favoritePresetsList.splice(s_maxFavoritePresetsCount + 1, this._favoritePresetsList.length); | ||
} | ||
|
||
loadFromStorage() { | ||
this._favoritePresetsList = []; | ||
const obj = ConfigStorage.get(s_favoritePresetsListConfigStorageName); | ||
|
||
if (obj[s_favoritePresetsListConfigStorageName]) { | ||
this._favoritePresetsList = obj[s_favoritePresetsListConfigStorageName]; | ||
} | ||
} | ||
|
||
saveToStorage() { | ||
const obj = {}; | ||
obj[s_favoritePresetsListConfigStorageName] = this._favoritePresetsList; | ||
ConfigStorage.set(obj); | ||
} | ||
|
||
add(presetPath) { | ||
let preset = this.findPreset(presetPath); | ||
|
||
if (!preset) { | ||
preset = new FavoritePreset(presetPath); | ||
this._favoritePresetsList.push(preset); | ||
} | ||
|
||
preset.lastPickDate = Date.now(); | ||
this._sort(); | ||
this._purgeOldPresets(); | ||
|
||
return preset; | ||
} | ||
|
||
findPreset(presetPath) { | ||
return this._favoritePresetsList.find((preset) => preset.presetPath === presetPath); | ||
} | ||
} | ||
|
||
|
||
class FavoritePresetsClass { | ||
constructor() { | ||
this._favoritePresetsData = new FavoritePresetsData(); | ||
} | ||
|
||
add(preset) { | ||
const favoritePreset = this._favoritePresetsData.add(preset.fullPath); | ||
preset.lastPickDate = favoritePreset.lastPickDate; | ||
} | ||
|
||
addLastPickDate(presets) { | ||
for (let preset of presets) { | ||
let favoritePreset = this._favoritePresetsData.findPreset(preset.fullPath); | ||
|
||
if (favoritePreset) { | ||
preset.lastPickDate = favoritePreset.lastPickDate; | ||
} | ||
} | ||
} | ||
|
||
saveToStorage() { | ||
this._favoritePresetsData.saveToStorage(); | ||
} | ||
|
||
loadFromStorage() { | ||
this._favoritePresetsData.loadFromStorage(); | ||
} | ||
} | ||
|
||
|
||
let favoritePresets; // for export as singleton | ||
|
||
if (!favoritePresets) { | ||
favoritePresets = new FavoritePresetsClass(); | ||
favoritePresets.loadFromStorage(); | ||
} | ||
|
||
export { favoritePresets }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters