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 { Preferences } from './interfaces/preferences';
|
||||
import { PreferencesRepository } from './preferences-repository';
|
||||
|
||||
class Background {
|
||||
static async init(): Promise<void> {
|
||||
|
@ -7,43 +8,18 @@ class Background {
|
|||
await this.setup();
|
||||
}
|
||||
|
||||
this.listen();
|
||||
}
|
||||
|
||||
static async listen(): Promise<void> {
|
||||
const preferences: Preferences = await this.getSavedPreferences();
|
||||
const preferences: Preferences = await PreferencesRepository.get();
|
||||
const hider = new TwitterHider(preferences);
|
||||
|
||||
hider.init();
|
||||
}
|
||||
|
||||
static setup(): Promise<void> {
|
||||
const defaults = {
|
||||
hideLikes: true,
|
||||
hideRetweets: true,
|
||||
hideReplies: true,
|
||||
setup: true
|
||||
};
|
||||
|
||||
return new Promise(resolve => {
|
||||
globalThis.chrome.storage.local.set(defaults, () => resolve());
|
||||
});
|
||||
private static async setup(): Promise<void> {
|
||||
await PreferencesRepository.set(PreferencesRepository.DefaultPreferences);
|
||||
}
|
||||
|
||||
static checkIfFirstStart(): Promise<boolean> {
|
||||
return new Promise(resolve => {
|
||||
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);
|
||||
});
|
||||
})
|
||||
private static async checkIfFirstStart(): Promise<boolean> {
|
||||
return !(await PreferencesRepository.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,11 @@
|
|||
<label for="hideretweets">Hide re-tweets?</label>
|
||||
<input type="checkbox" name="hideretweets" id="hideRetweets">
|
||||
</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">
|
||||
<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