Abstract preferences into PreferenceManager class.
This commit is contained in:
parent
15822b81f4
commit
05d0804f21
4 changed files with 33 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { TwitterHider } from './hider';
|
import { TwitterHider } from './hider';
|
||||||
import { Preferences } from './interfaces/preferences';
|
import { Preferences } from './interfaces/preferences';
|
||||||
|
import { PreferencesRepository } from './preferences-repository';
|
||||||
|
|
||||||
class Background {
|
class Background {
|
||||||
static async init(): Promise<void> {
|
static async init(): Promise<void> {
|
||||||
|
@ -7,43 +8,18 @@ class Background {
|
||||||
await this.setup();
|
await this.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.listen();
|
const preferences: Preferences = await PreferencesRepository.get();
|
||||||
}
|
|
||||||
|
|
||||||
static async listen(): Promise<void> {
|
|
||||||
const preferences: Preferences = await this.getSavedPreferences();
|
|
||||||
const hider = new TwitterHider(preferences);
|
const hider = new TwitterHider(preferences);
|
||||||
|
|
||||||
hider.init();
|
hider.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
static setup(): Promise<void> {
|
private static async setup(): Promise<void> {
|
||||||
const defaults = {
|
await PreferencesRepository.set(PreferencesRepository.DefaultPreferences);
|
||||||
hideLikes: true,
|
|
||||||
hideRetweets: true,
|
|
||||||
hideReplies: true,
|
|
||||||
setup: true
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Promise(resolve => {
|
|
||||||
globalThis.chrome.storage.local.set(defaults, () => resolve());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static checkIfFirstStart(): Promise<boolean> {
|
private static async checkIfFirstStart(): Promise<boolean> {
|
||||||
return new Promise(resolve => {
|
return !(await PreferencesRepository.get());
|
||||||
globalThis.chrome.storage.local.get('setup', data => {
|
|
||||||
resolve(!data.setup);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static getSavedPreferences(): Promise<Preferences> {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
globalThis.chrome.storage.local.get(['hideLikes', 'hideReplies', 'hideRetweets'], (data: Preferences) => {
|
|
||||||
resolve(data);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,11 @@
|
||||||
<label for="hideretweets">Hide re-tweets?</label>
|
<label for="hideretweets">Hide re-tweets?</label>
|
||||||
<input type="checkbox" name="hideretweets" id="hideRetweets">
|
<input type="checkbox" name="hideretweets" id="hideRetweets">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<label for="hideretweets">Hide follower counts?</label>
|
||||||
|
<input type="checkbox" name="hidefollowers" id="hideFollowers">
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="row" id="savedChanges" style="display: none">
|
<div class="row" id="savedChanges" style="display: none">
|
||||||
<span style="color: green">Your changes have been saved. You must refresh your page before they take effect.</span>
|
<span style="color: green">Your changes have been saved. You must refresh your page before they take effect.</span>
|
||||||
|
|
22
src/preferences-repository.ts
Normal file
22
src/preferences-repository.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import { Preferences } from './interfaces/preferences';
|
||||||
|
|
||||||
|
export class PreferencesRepository {
|
||||||
|
static readonly DefaultPreferences: Preferences = {
|
||||||
|
hideLikes: true,
|
||||||
|
hideRetweets: true,
|
||||||
|
hideReplies: true
|
||||||
|
};
|
||||||
|
|
||||||
|
static get(): Promise<Preferences> {
|
||||||
|
const propertyNames: string[] = Object.keys(this.DefaultPreferences);
|
||||||
|
return new Promise(resolve => {
|
||||||
|
globalThis.chrome.storage.local.get(propertyNames, (data: Preferences) => resolve(data))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static set(preferences: Preferences): Promise<void> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
globalThis.chrome.storage.local.set(preferences, () => resolve());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue