-
-
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 7c39cce
Showing
2 changed files
with
139 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,114 @@ | ||
|
||
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) ? 1 : -1); | ||
} | ||
|
||
_purgeOldPresets() { | ||
this._favoritePresetsList.splice(s_maxFavoritePresetsCount, this._favoritePresetsList.length); | ||
} | ||
|
||
loadFromStorage() { | ||
this._favoritePresetsList = []; | ||
const obj = ConfigStorage.get(s_favoritePresetsListConfigStorageName); | ||
|
||
if (obj.FavoritePresetsList) { | ||
this._favoritePresetsList = obj[s_favoritePresetsListConfigStorageName]; | ||
} | ||
} | ||
|
||
saveToStorage() { | ||
ConfigStorage.set({s_favoritePresetsListConfigStorageName: this._favoritePresetsList}); | ||
} | ||
|
||
presetPicked(presetPath) { | ||
let preset = this.findPreset(presetPath); | ||
|
||
if (null === preset) { | ||
preset = new FavoritePreset(presetPath); | ||
this._favoritePresetsList.push(preset); | ||
} | ||
|
||
preset.lastPickDate = Date.now(); | ||
this._sort(); | ||
this._purgeOldPresets(); | ||
|
||
return preset; | ||
} | ||
|
||
findPresetIndex(presetPath) { | ||
for (let i = 0; i < this._favoritePresetsList.length; i++) { | ||
if (this._favoritePresetsList[i].presetPath === presetPath) { | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
findPreset(presetPath) { | ||
const index = this.findPresetIndex(presetPath); | ||
|
||
if (index >= 0) { | ||
return this._favoritePresetsList[index]; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
|
||
class FavoritePresetsClass { | ||
constructor() { | ||
this._favoritePresetsData = new FavoritePresetsData(); | ||
} | ||
|
||
presetPicked(preset) { | ||
const favoritePreset = this._favoritePresetsData.presetPicked(preset.fullPath); | ||
preset.lastPickDate = favoritePreset.lastPickDate; | ||
} | ||
|
||
addLastPickDate(presets) { | ||
for (let i = 0; i < presets.length; i++) { | ||
let favoritePreset = this._favoritePresetsData.findPreset(presets[i].fullPath); | ||
|
||
if (favoritePreset) { | ||
presets[i].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