Skip to content

Commit

Permalink
Add toggle to disable swipe events, change store provider to always r…
Browse files Browse the repository at this point in the history
…eturn requested key and not record
  • Loading branch information
OctoNezd committed Jan 5, 2024
1 parent bd7b37d commit 04d3d56
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/extensionPreferences/baseStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface PrefStore {
get: (key: string) => Promise<Record<string, unknown>>;
get: (key: string) => Promise<unknown>;
set: (key: string, value: unknown) => Promise<void>;
}
4 changes: 2 additions & 2 deletions src/extensionPreferences/userScriptStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export default class UserScriptStore {
set(key: string, value: unknown) {
return GM.setValue(key, value);
}
get(key: string) {
return GM.getValue(key);
async get(key: string) {
return (await GM.getValue(key))[key];
}
name = "gmStore";
}
4 changes: 2 additions & 2 deletions src/extensionPreferences/webExtensionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default class WebExtStore {
keyValuePair[key] = value;
return browser.storage.sync.set(keyValuePair);
}
get(key: string) {
return browser.storage.sync.get(key);
async get(key: string) {
return (await browser.storage.sync.get(key))[key];
}
name = "webExtStore";
}
4 changes: 2 additions & 2 deletions src/features/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ export class SettingToggle extends SettingOption {
}
async loadPD() {
const prefData = await store.get(this.settingId)
const value = !!prefData[this.settingId]
const value = !!prefData
this.element.querySelector("input")!.checked = value;
this.callback(value);
}
async toggleSetting() {
const prefData = await store.get(this.settingId)
const old_value = prefData[this.settingId]
const old_value = prefData
const new_value = !old_value
await store.set(this.settingId, new_value);
console.log("updating", this.settingId, old_value, new_value);
Expand Down
12 changes: 10 additions & 2 deletions src/features/sidebar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "swiped-events";
import buildSubredditSidebar from "./subredditSidebar";
import buildUserSidebar from "./userSidebar";
import "./css/prefs.css";
import { store } from "../../extensionPreferences";

const eventListeners: { [id: string]: ((event: Event) => void)[] } = {
toggleUser: [],
Expand Down Expand Up @@ -40,7 +41,7 @@ function swipeWrapper(callback: ((event: Event) => void)): ((event: Event) => vo
}
return wrapped;
}

const swipeDisabledKey = "swipeDisabled"
export default class Sidebar extends OLFeature {
moduleName = "Sidebar";
moduleId = "sidebar";
Expand All @@ -59,6 +60,7 @@ export default class Sidebar extends OLFeature {
})
);
}
this.settingOptions.push(new SettingToggle("Disable swipe events", "Reload is required to apply", swipeDisabledKey, (toggle) => {}))
await this.setupSubredditSidebar();
await this.setupUserSidebar();
this.setSidebarEvents();
Expand All @@ -78,11 +80,17 @@ export default class Sidebar extends OLFeature {
this.userToggle = user.activeToggle;
}

setSidebarEvents() {
async setSidebarEvents() {
console.log("Setting up sidebar events, handlers:", this.subToggle, this.userToggle)
setEventListener("toggleSub", this.subToggle);
setEventListener("toggleUser", this.userToggle);
document.body.dataset.swipeUnit = "vw"
// @ts-ignore
const swipeDisabled = await store.get(swipeDisabledKey)
console.log("Swipe disabled?", swipeDisabled)
if ( swipeDisabled !== undefined && swipeDisabled) {
return
}
setEventListener("swiped-right", swipeWrapper((event) => {
if (this.subSide && this.subSide.classList.contains("active")) {
this.subToggle();
Expand Down
2 changes: 1 addition & 1 deletion src/features/themeSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class WhiteTheme extends OLFeature {
async init () {
this.settingOptions.push(
new SettingToggle("White theme", "Enables white theme instead of dark.", whiteThemeKey, async (theme) => {
const { systemTheme } = await store.get(systemThemeKey)
const systemTheme = await store.get(systemThemeKey)
if (systemTheme !== undefined && systemTheme) {
console.log("Systemtheme key is set, not enabling white theme")
return
Expand Down
4 changes: 2 additions & 2 deletions src/header/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async function addSubSidebarButton(header: HTMLDivElement) {
btn.classList.add("material-symbols-outlined");
btn.onclick = () => {
const evt = new Event("toggleSub");
document.dispatchEvent(evt);
document.body.dispatchEvent(evt);
};
header.querySelector(".aux-buttons")!.appendChild(btn);
}
Expand All @@ -114,7 +114,7 @@ function addUserSidebarButton(header: HTMLDivElement) {
btn.id = "user-sidebar-open";
btn.onclick = () => {
const evt = new Event("toggleUser");
document.dispatchEvent(evt);
document.body.dispatchEvent(evt);
};
header.prepend(btn);
}
Expand Down

0 comments on commit 04d3d56

Please sign in to comment.