From 1b5eb7c05c1d3302d400659121ee8970453bbefc Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 14 Jun 2023 16:47:32 +0000 Subject: [PATCH] improve use of haptics core --- .../app/src/cores/haptics/haptics.core.js | 33 ++++++++++++++++--- packages/app/src/cores/sound/sound.core.js | 5 --- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/app/src/cores/haptics/haptics.core.js b/packages/app/src/cores/haptics/haptics.core.js index 0b6953da..9f40d70d 100644 --- a/packages/app/src/cores/haptics/haptics.core.js +++ b/packages/app/src/cores/haptics/haptics.core.js @@ -1,5 +1,12 @@ import Core from "evite/src/core" +const vibrationPatterns = { + light: [10], + medium: [50], + heavy: [80], + error: [100, 30, 100, 30, 100], +} + export default class HapticsCore extends Core { static refName = "haptics" static namespace = "haptics" @@ -7,8 +14,12 @@ export default class HapticsCore extends Core { "settings" ] - static get isGlobalDisabled() { - return app.cores.settings.get("haptic_feedback") + static isGlobalDisabled() { + return app.cores.settings.is("haptic_feedback", false) + } + + async onInitialize() { + document.addEventListener("click", this.handleClickEvent) } public = { @@ -16,13 +27,25 @@ export default class HapticsCore extends Core { vibrate: this.vibrate.bind(this), } - vibrate(...args) { - const disabled = this.isGlobalDisabled + handleClickEvent = (event) => { + const button = event.target.closest("button") || event.target.closest(".ant-btn") + + if (button) { + this.vibrate("light") + } + } + + vibrate(pattern = "light") { + const disabled = HapticsCore.isGlobalDisabled() if (disabled) { return false } - return navigator.vibrate(...args) + if (typeof pattern === "string") { + pattern = vibrationPatterns[pattern] + } + + return navigator.vibrate(pattern) } } \ No newline at end of file diff --git a/packages/app/src/cores/sound/sound.core.js b/packages/app/src/cores/sound/sound.core.js index b6876fd2..c61d122e 100755 --- a/packages/app/src/cores/sound/sound.core.js +++ b/packages/app/src/cores/sound/sound.core.js @@ -3,7 +3,6 @@ import { Howl } from "howler" import config from "config" import axios from "axios" import store from "store" -import { Haptics, ImpactStyle } from "@capacitor/haptics" export default class SoundCore extends Core { static refName = "sound" @@ -81,10 +80,6 @@ export default class SoundCore extends Core { return this.public.useUIAudio(button.getAttribute("aria-checked") === "true" ? "component.slider_down" : "component.slider_up") } - if (app.cores.settings.get("haptic_feedback")) { - Haptics.impact({ style: ImpactStyle.Light }) - } - return this.public.useUIAudio("generic_click") } }