From 8372e65b94ff51846070204ed394227a54bc644f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 15:42:04 +0200 Subject: [PATCH 01/10] cleanup useSyncTask --- src/app/settings/pages/EmailPage.vue | 4 +-- src/app/shared/index.ts | 2 +- src/app/shared/tasks/runSyncTask.ts | 49 ------------------------- src/app/shared/tasks/useSyncTask.ts | 54 ++++++++++++++++++++++++++++ src/init/app/initTasks.ts | 4 +-- 5 files changed, 58 insertions(+), 55 deletions(-) delete mode 100644 src/app/shared/tasks/runSyncTask.ts create mode 100644 src/app/shared/tasks/useSyncTask.ts diff --git a/src/app/settings/pages/EmailPage.vue b/src/app/settings/pages/EmailPage.vue index 24295b7e..fed0d717 100644 --- a/src/app/settings/pages/EmailPage.vue +++ b/src/app/settings/pages/EmailPage.vue @@ -77,7 +77,7 @@ diff --git a/src/app/shared/composables/useSync.ts b/src/app/shared/composables/useSync.ts index 002228b3..89edbfc4 100644 --- a/src/app/shared/composables/useSync.ts +++ b/src/app/shared/composables/useSync.ts @@ -6,7 +6,6 @@ import { useEnv } from './useEnv' import { useAuthentication } from './useAuthentication' import { useApplication } from './useApp' import { useLogger } from './useLogger' -import { useEmitter } from './useEmitter' /* -------------------------------------------------------------------------- */ /* Interface */ @@ -34,7 +33,6 @@ export function useSync() { const auth = useAuthentication() const env = useEnv() const log = useLogger('sync') - const emitter = useEmitter() /* -------------------------------------------------------------------------- */ @@ -72,7 +70,6 @@ export function useSync() { currentTime: currentTime }) settings.syncAt = currentTime - emitter.emit('syncCompleted') } catch (e) { log.error('Syncing failed...', e) throw new Error('Syncing failed: ' + JSON.stringify(e)) diff --git a/src/app/statistics/index.ts b/src/app/statistics/index.ts index 9aa95da6..3e021a7b 100644 --- a/src/app/statistics/index.ts +++ b/src/app/statistics/index.ts @@ -1,2 +1,2 @@ export * from './stores/useStatisticsStore' -export * from './tasks/runUpdateStatisticsTask' \ No newline at end of file +export * from './tasks/useUpdateStatisticsTask' \ No newline at end of file diff --git a/src/app/statistics/tasks/runUpdateStatisticsTask.ts b/src/app/statistics/tasks/useUpdateStatisticsTask.ts similarity index 75% rename from src/app/statistics/tasks/runUpdateStatisticsTask.ts rename to src/app/statistics/tasks/useUpdateStatisticsTask.ts index d6287b27..8cee24e2 100644 --- a/src/app/statistics/tasks/runUpdateStatisticsTask.ts +++ b/src/app/statistics/tasks/useUpdateStatisticsTask.ts @@ -9,7 +9,7 @@ import { useSettingsStore } from '@/app/settings' /** * Updates statistics in the background. */ -export function runUpdateStatisticsTask() { +export function useUpdateStatisticsTask() { /* -------------------------------------------------------------------------- */ /* Dependencies */ /* -------------------------------------------------------------------------- */ @@ -30,21 +30,23 @@ export function runUpdateStatisticsTask() { /* -------------------------------------------------------------------------- */ - /* Triggers */ + /* Actions */ /* -------------------------------------------------------------------------- */ - watch([ - app.now, - app.currentContextName, - isActive, - syncAt - ], onUpdateStatistics) - - emitter.on('commandExecuted', async (e) => { - if (e instanceof ReviewCardReviewed) { await onUpdateStatistics() } - if (e instanceof InboxCardMemorized) { await onUpdateStatistics() } - if (e instanceof AddVerseToInboxDeck) { await onUpdateStatistics() } - }) + async function run() { + watch([ + app.now, + app.currentContextName, + isActive, + syncAt + ], onUpdateStatistics) + + emitter.on('commandExecuted', async (e) => { + if (e instanceof ReviewCardReviewed) { await onUpdateStatistics() } + if (e instanceof InboxCardMemorized) { await onUpdateStatistics() } + if (e instanceof AddVerseToInboxDeck) { await onUpdateStatistics() } + }) + } /* -------------------------------------------------------------------------- */ @@ -69,6 +71,13 @@ export function runUpdateStatisticsTask() { return result } + + /* -------------------------------------------------------------------------- */ + /* Interface */ + /* -------------------------------------------------------------------------- */ + + return { run } + } diff --git a/src/app/welcome/pages/WelcomePage.vue b/src/app/welcome/pages/WelcomePage.vue index 59e20818..98ebcbd7 100644 --- a/src/app/welcome/pages/WelcomePage.vue +++ b/src/app/welcome/pages/WelcomePage.vue @@ -78,14 +78,13 @@ import { logoApple, logoGoogle, mail, people } from 'ionicons/icons' import { Capacitor } from '@capacitor/core' import { useLoadLibraryIntoMemory, useSyncLibraryTask } from '@/app/library' import { useSettingsStore } from '@/app/settings' -import { DarkImage, go, useAuthentication, useApplication, useEmitter } from '@/app/shared' +import { DarkImage, go, useAuthentication, useApplication } from '@/app/shared' /* -------------------------------------------------------------------------- */ /* Dependencies */ /* -------------------------------------------------------------------------- */ -const emitter = useEmitter() const application = useApplication() const isDark = usePreferredDark() const libraryDatabase = inject('verses') @@ -124,7 +123,6 @@ async function onOpened() { async function onSignIn(strategy: string) { try { await auth.authenticate(strategy) - emitter.emit('signedIn') router.replace(go('library')) } catch (e) { const alert = await alertController.create({ diff --git a/src/init/app/initTasks.ts b/src/init/app/initTasks.ts index a5f7f120..0e5b9640 100644 --- a/src/init/app/initTasks.ts +++ b/src/init/app/initTasks.ts @@ -1,6 +1,6 @@ import { useSettingsPersistenceTask } from '@/app/settings' import { runEnterFullscreenMode, runHideStatusBar, useScheduleNotifications, useSyncTask, useUpdateAppBadge, useResetMemorizationTime, useAppStatePersistenceTask } from '@/app/shared' -import { runUpdateStatisticsTask } from '@/app/statistics' +import { useUpdateStatisticsTask } from '@/app/statistics' import { runTutorialPersistenceTask, runTutorialRestoreTask } from '@/app/tutorial' @@ -8,12 +8,12 @@ export async function initTasks() { await useSettingsPersistenceTask().run() await useAppStatePersistenceTask().run() await useSyncTask().run() + await useUpdateStatisticsTask().run() await useScheduleNotifications().run() await useUpdateAppBadge().run() await useResetMemorizationTime().run() - runUpdateStatisticsTask() runTutorialPersistenceTask() await runTutorialRestoreTask() runEnterFullscreenMode() From 30f822817742c76b7595d645b1405af4185b2560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 16:41:10 +0200 Subject: [PATCH 05/10] remove emitter --- package-lock.json | 6 --- package.json | 1 - src/app/shared/composables/useEmitter.ts | 37 ------------------- src/app/shared/index.ts | 1 - .../tasks/useUpdateStatisticsTask.ts | 27 +++++++------- src/init/app/initCommands.ts | 16 -------- src/init/index.ts | 2 - 7 files changed, 14 insertions(+), 76 deletions(-) delete mode 100644 src/app/shared/composables/useEmitter.ts delete mode 100644 src/init/app/initCommands.ts diff --git a/package-lock.json b/package-lock.json index bc9e08ca..c4c59ecf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,7 +41,6 @@ "cordova-plugin-email-composer": "^0.10.1", "cordova-plugin-ionic": "5.5.3", "cordova-sqlite-storage": "^6.1.0", - "eventemitter2": "^6.4.9", "ionicons": "^7.1.0", "jwt-decode": "^3.1.2", "pinia": "^2.0.33", @@ -8249,11 +8248,6 @@ "node": ">=6" } }, - "node_modules/eventemitter2": { - "version": "6.4.9", - "resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz", - "integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg==" - }, "node_modules/eventemitter3": { "version": "4.0.7", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", diff --git a/package.json b/package.json index a0192170..e3088874 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,6 @@ "cordova-plugin-email-composer": "^0.10.1", "cordova-plugin-ionic": "5.5.3", "cordova-sqlite-storage": "^6.1.0", - "eventemitter2": "^6.4.9", "ionicons": "^7.1.0", "jwt-decode": "^3.1.2", "pinia": "^2.0.33", diff --git a/src/app/shared/composables/useEmitter.ts b/src/app/shared/composables/useEmitter.ts deleted file mode 100644 index 59fe28d2..00000000 --- a/src/app/shared/composables/useEmitter.ts +++ /dev/null @@ -1,37 +0,0 @@ -import EventEmitter2 from 'eventemitter2' -import { useLogger } from './useLogger' - - -const emitter = new EventEmitter2() - - -export function useEmitter() { - - /* -------------------------------------------------------------------------- */ - /* Dependencies */ - /* -------------------------------------------------------------------------- */ - - const logger = useLogger('emitter') - - - /* -------------------------------------------------------------------------- */ - /* Methods */ - /* -------------------------------------------------------------------------- */ - - function emit(event: string, ...args: any[]) { - logger.debug(`Emitting ${event}`) - emitter.emit(event, ...args) - } - - function on(event: string, listener: (...args: any[]) => void) { - logger.debug(`Registering listener for ${event}`) - emitter.on(event, listener) - } - - - /* -------------------------------------------------------------------------- */ - /* Interface */ - /* -------------------------------------------------------------------------- */ - - return { emit, on } -} \ No newline at end of file diff --git a/src/app/shared/index.ts b/src/app/shared/index.ts index be2c627f..4e7d38ac 100644 --- a/src/app/shared/index.ts +++ b/src/app/shared/index.ts @@ -8,7 +8,6 @@ export * from './composables/useApp' export * from './composables/useDeviceStorage' export * from './composables/useAuthentication' export * from './composables/useSync' -export * from './composables/useEmitter' export * from './composables/useTestId' export * from './composables/useClearCache' export * from './composables/useAppVersion' diff --git a/src/app/statistics/tasks/useUpdateStatisticsTask.ts b/src/app/statistics/tasks/useUpdateStatisticsTask.ts index 8cee24e2..5853a20a 100644 --- a/src/app/statistics/tasks/useUpdateStatisticsTask.ts +++ b/src/app/statistics/tasks/useUpdateStatisticsTask.ts @@ -2,7 +2,7 @@ import { AddVerseToInboxDeck, InboxCardMemorized, ReviewCardReviewed } from '@ak import { watch } from 'vue' import { storeToRefs } from 'pinia' import { useStatisticsStore } from '@/app/statistics' -import { useAppStateStore, useApplication, useEmitter } from '@/app/shared' +import { useAppStateStore, useApplication } from '@/app/shared' import { useSettingsStore } from '@/app/settings' @@ -15,7 +15,6 @@ export function useUpdateStatisticsTask() { /* -------------------------------------------------------------------------- */ const app = useApplication() - const emitter = useEmitter() const statisticsStore = useStatisticsStore() const appStateStore = useAppStateStore() const settingsStore = useSettingsStore() @@ -39,13 +38,9 @@ export function useUpdateStatisticsTask() { app.currentContextName, isActive, syncAt - ], onUpdateStatistics) + ], updateStatistics) - emitter.on('commandExecuted', async (e) => { - if (e instanceof ReviewCardReviewed) { await onUpdateStatistics() } - if (e instanceof InboxCardMemorized) { await onUpdateStatistics() } - if (e instanceof AddVerseToInboxDeck) { await onUpdateStatistics() } - }) + app.instance().commandExecuted.subscribe(onCommandExecuted) } @@ -53,11 +48,10 @@ export function useUpdateStatisticsTask() { /* Handlers */ /* -------------------------------------------------------------------------- */ - async function onUpdateStatistics() { - const date = nextDays(1, app.application.timeMachine.today) - statisticsStore.cardsCountDueToTomorrow = (await app.application.reviewDeck.dueToCards(date)).length - statisticsStore.cardsInReview = (await app.application.reviewDeck.dueToCards(app.application.timeMachine.now)).length - statisticsStore.cardsInInbox = (await app.application.inboxDeck.cards()).length + async function onCommandExecuted(e) { + if (e instanceof ReviewCardReviewed) { await updateStatistics() } + if (e instanceof InboxCardMemorized) { await updateStatistics() } + if (e instanceof AddVerseToInboxDeck) { await updateStatistics() } } @@ -65,6 +59,13 @@ export function useUpdateStatisticsTask() { /* Helpers */ /* -------------------------------------------------------------------------- */ + async function updateStatistics() { + const date = nextDays(1, app.application.timeMachine.today) + statisticsStore.cardsCountDueToTomorrow = (await app.application.reviewDeck.dueToCards(date)).length + statisticsStore.cardsInReview = (await app.application.reviewDeck.dueToCards(app.application.timeMachine.now)).length + statisticsStore.cardsInInbox = (await app.application.inboxDeck.cards()).length + } + function nextDays(days: number, date?: Date) { const result = date ? new Date(date) : new Date() result.setDate(result.getDate()+days) diff --git a/src/init/app/initCommands.ts b/src/init/app/initCommands.ts deleted file mode 100644 index eaae0f67..00000000 --- a/src/init/app/initCommands.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Application } from '@akdasa-studios/shlokas-core' - -import { useEmitter } from '@/app/shared' -import { InitArgs, InitResult } from '../initialization' - - -export async function initCommands( - { get }: InitArgs -): Promise { - const emitter = useEmitter() - - const shlokas = get('app') - shlokas.commandExecuted.subscribe((cmd: any) => emitter.emit('commandExecuted', cmd)) - shlokas.commandReverted.subscribe((cmd: any) => emitter.emit('commandExecuted', cmd)) - return {} -} diff --git a/src/init/index.ts b/src/init/index.ts index 04f9c16d..6c3d8af1 100644 --- a/src/init/index.ts +++ b/src/init/index.ts @@ -11,7 +11,6 @@ import { initShlokasApp } from './infrastructure/initShlokasApp' import { initAuth } from './infrastructure/initAuth' // stage 1: logic -import { initCommands } from './app/initCommands' import { initTasks } from './app/initTasks' import { initParams } from './app/initParams' import { initStaticData } from './app/initStaticData' @@ -34,7 +33,6 @@ export const appInitStages = [ initTasks, initLocale, initParams, - initCommands, initMigrations, initStaticData, initRoutes From 5ed1bef8993e3522f571928f764674921ba37a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 16:51:19 +0200 Subject: [PATCH 06/10] fix --- src/app/statistics/tasks/useUpdateStatisticsTask.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/statistics/tasks/useUpdateStatisticsTask.ts b/src/app/statistics/tasks/useUpdateStatisticsTask.ts index 5853a20a..feb0b820 100644 --- a/src/app/statistics/tasks/useUpdateStatisticsTask.ts +++ b/src/app/statistics/tasks/useUpdateStatisticsTask.ts @@ -48,7 +48,7 @@ export function useUpdateStatisticsTask() { /* Handlers */ /* -------------------------------------------------------------------------- */ - async function onCommandExecuted(e) { + async function onCommandExecuted(e: any) { if (e instanceof ReviewCardReviewed) { await updateStatistics() } if (e instanceof InboxCardMemorized) { await updateStatistics() } if (e instanceof AddVerseToInboxDeck) { await updateStatistics() } From 54587b2d7e3fc640cf4b6a75cf9579bc55cf8a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 16:52:02 +0200 Subject: [PATCH 07/10] refactoring --- src/app/shared/index.ts | 4 +-- .../shared/tasks/useResetMemorizationTime.ts | 34 +++++++++++++++---- ...cations.ts => useScheduleNotifications.ts} | 0 ...UpdateAppBadge.ts => useUpdateAppBadge.ts} | 0 src/init/app/initTasks.ts | 1 - 5 files changed, 30 insertions(+), 9 deletions(-) rename src/app/shared/tasks/{runScheduleNotifications.ts => useScheduleNotifications.ts} (100%) rename src/app/shared/tasks/{runUpdateAppBadge.ts => useUpdateAppBadge.ts} (100%) diff --git a/src/app/shared/index.ts b/src/app/shared/index.ts index 4e7d38ac..7c46fbca 100644 --- a/src/app/shared/index.ts +++ b/src/app/shared/index.ts @@ -18,8 +18,8 @@ export * from './composables/useScreenOrientation' export * from './tasks/useSyncTask' export * from './tasks/runEnterFullscreenMode' export * from './tasks/runHideStatusBar' -export * from './tasks/runScheduleNotifications' -export * from './tasks/runUpdateAppBadge' +export * from './tasks/useScheduleNotifications' +export * from './tasks/useUpdateAppBadge' export * from './tasks/useResetMemorizationTime' export * from './tasks/useAppStatePersistenceTask' diff --git a/src/app/shared/tasks/useResetMemorizationTime.ts b/src/app/shared/tasks/useResetMemorizationTime.ts index 659c7e7f..53c87e69 100644 --- a/src/app/shared/tasks/useResetMemorizationTime.ts +++ b/src/app/shared/tasks/useResetMemorizationTime.ts @@ -1,4 +1,6 @@ -import { useAppStateStore } from '@/app/shared' +import { storeToRefs } from 'pinia' +import { watch } from 'vue' +import { useAppStateStore, useApplication } from '@/app/shared' export function useResetMemorizationTime() { @@ -6,7 +8,15 @@ export function useResetMemorizationTime() { /* Dependencies */ /* -------------------------------------------------------------------------- */ - const appState = useAppStateStore() + const app = useApplication() + const appStateStore = useAppStateStore() + + + /* -------------------------------------------------------------------------- */ + /* State */ + /* -------------------------------------------------------------------------- */ + + const { isActive } = storeToRefs(appStateStore) /* -------------------------------------------------------------------------- */ @@ -14,13 +24,25 @@ export function useResetMemorizationTime() { /* -------------------------------------------------------------------------- */ async function run() { - const lastResetAt = appState.memorizationTimeResetAt - const now = new Date() + watch([ + isActive, app.now + ], reset) + reset() + } + + + /* -------------------------------------------------------------------------- */ + /* Helpers */ + /* -------------------------------------------------------------------------- */ + + async function reset() { + const lastResetAt = appStateStore.memorizationTimeResetAt + const now = app.now.value const today = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}` if (lastResetAt !== today) { - appState.memorizationTimeSpend = 0 - appState.memorizationTimeResetAt = today + appStateStore.memorizationTimeSpend = 0 + appStateStore.memorizationTimeResetAt = today } } diff --git a/src/app/shared/tasks/runScheduleNotifications.ts b/src/app/shared/tasks/useScheduleNotifications.ts similarity index 100% rename from src/app/shared/tasks/runScheduleNotifications.ts rename to src/app/shared/tasks/useScheduleNotifications.ts diff --git a/src/app/shared/tasks/runUpdateAppBadge.ts b/src/app/shared/tasks/useUpdateAppBadge.ts similarity index 100% rename from src/app/shared/tasks/runUpdateAppBadge.ts rename to src/app/shared/tasks/useUpdateAppBadge.ts diff --git a/src/init/app/initTasks.ts b/src/init/app/initTasks.ts index 0e5b9640..28d7f161 100644 --- a/src/init/app/initTasks.ts +++ b/src/init/app/initTasks.ts @@ -9,7 +9,6 @@ export async function initTasks() { await useAppStatePersistenceTask().run() await useSyncTask().run() await useUpdateStatisticsTask().run() - await useScheduleNotifications().run() await useUpdateAppBadge().run() await useResetMemorizationTime().run() From 99d2065427e7f1ee70228826d6e0dd6b04d54051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 17:39:35 +0200 Subject: [PATCH 08/10] useTutorialPersistenceTask --- .../tutorial/components/TutorialPlayer.vue | 2 +- src/app/tutorial/index.ts | 3 +- .../tutorial/tasks/runTutorialRestoreTask.ts | 62 ------------------- src/app/tutorial/tasks/shared.ts | 2 - ...eTask.ts => useTutorialPersistenceTask.ts} | 49 +++++++++++---- src/init/app/initTasks.ts | 5 +- 6 files changed, 42 insertions(+), 81 deletions(-) delete mode 100644 src/app/tutorial/tasks/runTutorialRestoreTask.ts delete mode 100644 src/app/tutorial/tasks/shared.ts rename src/app/tutorial/tasks/{runTutorialPersistenceTask.ts => useTutorialPersistenceTask.ts} (50%) diff --git a/src/app/tutorial/components/TutorialPlayer.vue b/src/app/tutorial/components/TutorialPlayer.vue index 0ee30854..6fa818d0 100644 --- a/src/app/tutorial/components/TutorialPlayer.vue +++ b/src/app/tutorial/components/TutorialPlayer.vue @@ -207,7 +207,7 @@ const tutorialSteps: TutorialStep[] = [ { id: TutorialSteps.ConfigNotificationTimeUserSets, onEnter: async () => { - isNotificationTimePickerOpen.value = true + setTimeout(() => { isMemorizationTimePickerOpen.value = true }, 10) }, }, diff --git a/src/app/tutorial/index.ts b/src/app/tutorial/index.ts index d7d4c47e..a6daa82c 100644 --- a/src/app/tutorial/index.ts +++ b/src/app/tutorial/index.ts @@ -10,5 +10,4 @@ export * from './stores/useTutorialStore' export * from './models/TutorialStep' // tasks: -export * from './tasks/runTutorialPersistenceTask' -export * from './tasks/runTutorialRestoreTask' \ No newline at end of file +export * from './tasks/useTutorialPersistenceTask' \ No newline at end of file diff --git a/src/app/tutorial/tasks/runTutorialRestoreTask.ts b/src/app/tutorial/tasks/runTutorialRestoreTask.ts deleted file mode 100644 index ba47baae..00000000 --- a/src/app/tutorial/tasks/runTutorialRestoreTask.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { watch } from 'vue' -import { storeToRefs } from 'pinia' -import { useDeviceStore, useApplication, useLogger } from '@/app/shared' -import { useTutorialStore } from '@/app/tutorial' -import { CURRENT_STEP_KEY, DATE_KEY } from './shared' - - -/** - * Restores tutorial state from device storage once application context is changed to tutorial. - * - * Data restored: - * - Current tutorial step - * - Tutorial date - */ -export async function runTutorialRestoreTask() { - - /* -------------------------------------------------------------------------- */ - /* Dependencies */ - /* -------------------------------------------------------------------------- */ - - const logger = useLogger('tutorial:restore') - const application = useApplication() - const tutorialStore = useTutorialStore() - const deviceStore = useDeviceStore() - - /* -------------------------------------------------------------------------- */ - /* Watch */ - /* -------------------------------------------------------------------------- */ - - watch(application.currentContextName, onApplicationContextChanged) - - - /* -------------------------------------------------------------------------- */ - /* Init state */ - /* -------------------------------------------------------------------------- */ - - const { currentStep } = storeToRefs(tutorialStore) - currentStep.value = (await deviceStore.get(CURRENT_STEP_KEY)) || 0 - logger.debug(`Started with ${currentStep.value} step`) - if (tutorialStore.isStarted && !tutorialStore.isCompleted) { - logger.debug('Switching to tutorial context') - application.switchContextTo('tutorial') - } - - - /* -------------------------------------------------------------------------- */ - /* Handlers */ - /* -------------------------------------------------------------------------- */ - - async function onApplicationContextChanged(context: string) { - if (context !== 'tutorial') { return } - - logger.debug('Restoring tutorial state') - const tutorialDate = await deviceStore.get(DATE_KEY) - if (tutorialDate) { - logger.debug('Restored date', tutorialDate) - application.setTime(new Date(tutorialDate)) - } else { - logger.debug('No tutorial date saved') - } - } -} diff --git a/src/app/tutorial/tasks/shared.ts b/src/app/tutorial/tasks/shared.ts deleted file mode 100644 index 843b8886..00000000 --- a/src/app/tutorial/tasks/shared.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const CURRENT_STEP_KEY = 'tutorial:step' -export const DATE_KEY = 'tutorial:date' \ No newline at end of file diff --git a/src/app/tutorial/tasks/runTutorialPersistenceTask.ts b/src/app/tutorial/tasks/useTutorialPersistenceTask.ts similarity index 50% rename from src/app/tutorial/tasks/runTutorialPersistenceTask.ts rename to src/app/tutorial/tasks/useTutorialPersistenceTask.ts index 81a050bf..287f0dfd 100644 --- a/src/app/tutorial/tasks/runTutorialPersistenceTask.ts +++ b/src/app/tutorial/tasks/useTutorialPersistenceTask.ts @@ -2,7 +2,6 @@ import { storeToRefs } from 'pinia' import { watch } from 'vue' import { useApplication, useDeviceStore, useLogger } from '@/app/shared' import { useTutorialStore } from '@/app/tutorial' -import { CURRENT_STEP_KEY, DATE_KEY } from './shared' /** @@ -12,12 +11,15 @@ import { CURRENT_STEP_KEY, DATE_KEY } from './shared' * - Current tutorial step * - Tutorial date */ -export function runTutorialPersistenceTask() { +export function useTutorialPersistenceTask() { /* -------------------------------------------------------------------------- */ /* Dependencies */ /* -------------------------------------------------------------------------- */ + const CURRENT_STEP_KEY = 'tutorial:currentStep' + const DATE_KEY = 'tutorial:date' + const logger = useLogger('tutorial:persistence') const application = useApplication() const tutorialStore = useTutorialStore() @@ -31,26 +33,51 @@ export function runTutorialPersistenceTask() { /* -------------------------------------------------------------------------- */ - /* Triggers */ + /* Actions */ /* -------------------------------------------------------------------------- */ - watch(currentStep, onTutorialStepChanged) - watch(application.now, onDateChanged) + async function run() { + await load() + + watch([currentStep, application.now], onStateChanged) + } /* -------------------------------------------------------------------------- */ /* Handlers */ /* -------------------------------------------------------------------------- */ - function onTutorialStepChanged(value: number) { - logger.debug(`Saving step ${value}`) - deviceStore.set(CURRENT_STEP_KEY, value) + async function onStateChanged() { + await save() } - function onDateChanged(value: Date) { + + /* -------------------------------------------------------------------------- */ + /* Helpers */ + /* -------------------------------------------------------------------------- */ + + async function save() { + logger.debug('Saving tutorial state') + deviceStore.set(CURRENT_STEP_KEY, currentStep.value) if (application.currentContextName.value === 'tutorial') { - logger.debug(`Saving date ${value}`) - deviceStore.set(DATE_KEY, value.getTime()) + deviceStore.set(DATE_KEY, application.now.value.getTime()) } } + + async function load() { + currentStep.value = (await deviceStore.get(CURRENT_STEP_KEY)) || 0 + const tutorialDate = await deviceStore.get(DATE_KEY) + + if (tutorialStore.isStarted && !tutorialStore.isCompleted) { + logger.debug(`Tutorial resored ${currentStep.value} step and ${tutorialDate} date`) + application.switchContextTo('tutorial') + if (tutorialDate) { application.setTime(new Date(tutorialDate)) } + } + } + + /* -------------------------------------------------------------------------- */ + /* Interface */ + /* -------------------------------------------------------------------------- */ + + return { run } } diff --git a/src/init/app/initTasks.ts b/src/init/app/initTasks.ts index 28d7f161..dee263d9 100644 --- a/src/init/app/initTasks.ts +++ b/src/init/app/initTasks.ts @@ -1,7 +1,7 @@ import { useSettingsPersistenceTask } from '@/app/settings' import { runEnterFullscreenMode, runHideStatusBar, useScheduleNotifications, useSyncTask, useUpdateAppBadge, useResetMemorizationTime, useAppStatePersistenceTask } from '@/app/shared' import { useUpdateStatisticsTask } from '@/app/statistics' -import { runTutorialPersistenceTask, runTutorialRestoreTask } from '@/app/tutorial' +import { useTutorialPersistenceTask, } from '@/app/tutorial' export async function initTasks() { @@ -12,9 +12,8 @@ export async function initTasks() { await useScheduleNotifications().run() await useUpdateAppBadge().run() await useResetMemorizationTime().run() + await useTutorialPersistenceTask().run() - runTutorialPersistenceTask() - await runTutorialRestoreTask() runEnterFullscreenMode() runHideStatusBar() } \ No newline at end of file From 4fa2c5932487103b6d5c60a7904f80907536bedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 17:56:05 +0200 Subject: [PATCH 09/10] tasks refactoring --- src/app/decks/shared/index.ts | 1 + .../shared/tasks/useEnterFullscreenMode.ts} | 31 ++++++++++---- src/app/shared/index.ts | 2 - src/app/shared/tasks/runHideStatusBar.ts | 42 ------------------- .../tasks/useAppStatePersistenceTask.ts | 1 + src/init/app/initTasks.ts | 7 ++-- 6 files changed, 27 insertions(+), 57 deletions(-) rename src/app/{shared/tasks/runEnterFullscreenMode.ts => decks/shared/tasks/useEnterFullscreenMode.ts} (69%) delete mode 100644 src/app/shared/tasks/runHideStatusBar.ts diff --git a/src/app/decks/shared/index.ts b/src/app/decks/shared/index.ts index 8e2d3cd2..0e0f3dde 100644 --- a/src/app/decks/shared/index.ts +++ b/src/app/decks/shared/index.ts @@ -1,5 +1,6 @@ export * from './composables/useLibraryCache' export * from './composables/useIndexedList' +export * from './tasks/useEnterFullscreenMode' export { default as VerseTextLines } from './components/VerseTextLines.vue' export { default as StackedFlipCardsDeck } from './components/StackedFlipCardsDeck.vue' diff --git a/src/app/shared/tasks/runEnterFullscreenMode.ts b/src/app/decks/shared/tasks/useEnterFullscreenMode.ts similarity index 69% rename from src/app/shared/tasks/runEnterFullscreenMode.ts rename to src/app/decks/shared/tasks/useEnterFullscreenMode.ts index 2f41dfd1..5cc8ac5b 100644 --- a/src/app/shared/tasks/runEnterFullscreenMode.ts +++ b/src/app/decks/shared/tasks/useEnterFullscreenMode.ts @@ -1,14 +1,14 @@ import { watch } from 'vue' import { storeToRefs } from 'pinia' +import { StatusBar } from '@capacitor/status-bar' +import { Capacitor } from '@capacitor/core' +import { useScreenOrientation , useAppStateStore } from '@/app/shared' import { useSettingsStore } from '@/app/settings' -import { useScreenOrientation } from '../composables/useScreenOrientation' -import { useAppStateStore } from '../stores/appStateStore' - /** * Enter fullscreen mode when in landscape mode and hide controls is enabled. */ -export function runEnterFullscreenMode() { +export function useEnterFullscreenMode() { /* -------------------------------------------------------------------------- */ /* Dependencies */ /* -------------------------------------------------------------------------- */ @@ -27,13 +27,15 @@ export function runEnterFullscreenMode() { /* -------------------------------------------------------------------------- */ - /* Init */ + /* Actions */ /* -------------------------------------------------------------------------- */ - watch( - [screenOrientation.isPortrait, routeName], - onChanged - ) + async function run() { + watch( + [screenOrientation.isPortrait, routeName], + onChanged + ) + } /* -------------------------------------------------------------------------- */ @@ -45,5 +47,16 @@ export function runEnterFullscreenMode() { const isFullscreenEligible = fullscrenEligibleRoutes.includes(routeName.value) const hideControls = hideControlsInLandscapeMode.value appStateStore.fullscreen = isLandscape && isFullscreenEligible && hideControls + + if (Capacitor.getPlatform() !== 'web') { + if (appStateStore.fullscreen) { await StatusBar.hide() } else { await StatusBar.show() } + } } + + + /* -------------------------------------------------------------------------- */ + /* Interface */ + /* -------------------------------------------------------------------------- */ + + return { run } } \ No newline at end of file diff --git a/src/app/shared/index.ts b/src/app/shared/index.ts index 7c46fbca..5718c06b 100644 --- a/src/app/shared/index.ts +++ b/src/app/shared/index.ts @@ -16,8 +16,6 @@ export * from './composables/useScreenOrientation' // tasks: export * from './tasks/useSyncTask' -export * from './tasks/runEnterFullscreenMode' -export * from './tasks/runHideStatusBar' export * from './tasks/useScheduleNotifications' export * from './tasks/useUpdateAppBadge' export * from './tasks/useResetMemorizationTime' diff --git a/src/app/shared/tasks/runHideStatusBar.ts b/src/app/shared/tasks/runHideStatusBar.ts deleted file mode 100644 index 2a30cf57..00000000 --- a/src/app/shared/tasks/runHideStatusBar.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { watch } from 'vue' -import { StatusBar } from '@capacitor/status-bar' -import { storeToRefs } from 'pinia' -import { Capacitor } from '@capacitor/core' -import { useAppStateStore } from '../stores/appStateStore' - -/** - * Hide status bar when fullscreen is true, otherwise show it. - */ -export function runHideStatusBar() { - // Only run on native platforms - if (Capacitor.getPlatform() === 'web') { return } - - /* -------------------------------------------------------------------------- */ - /* Dependencies */ - /* -------------------------------------------------------------------------- */ - - const appStateStore = useAppStateStore() - - - /* -------------------------------------------------------------------------- */ - /* State */ - /* -------------------------------------------------------------------------- */ - - const { fullscreen } = storeToRefs(appStateStore) - - - /* -------------------------------------------------------------------------- */ - /* Init */ - /* -------------------------------------------------------------------------- */ - - watch(fullscreen, onChanged) - - - /* -------------------------------------------------------------------------- */ - /* Handlers */ - /* -------------------------------------------------------------------------- */ - - async function onChanged(value: boolean) { - if (value) { await StatusBar.hide() } else { await StatusBar.show() } - } -} \ No newline at end of file diff --git a/src/app/shared/tasks/useAppStatePersistenceTask.ts b/src/app/shared/tasks/useAppStatePersistenceTask.ts index b5f4101a..2c08b905 100644 --- a/src/app/shared/tasks/useAppStatePersistenceTask.ts +++ b/src/app/shared/tasks/useAppStatePersistenceTask.ts @@ -43,6 +43,7 @@ export function useAppStatePersistenceTask() { /* -------------------------------------------------------------------------- */ async function onAppStateChanged() { + if (isActive.value) { return } logger.debug('Saving app state') for (const key of settingsKeys) { // @ts-ignore diff --git a/src/init/app/initTasks.ts b/src/init/app/initTasks.ts index dee263d9..3c228e29 100644 --- a/src/init/app/initTasks.ts +++ b/src/init/app/initTasks.ts @@ -1,5 +1,6 @@ import { useSettingsPersistenceTask } from '@/app/settings' -import { runEnterFullscreenMode, runHideStatusBar, useScheduleNotifications, useSyncTask, useUpdateAppBadge, useResetMemorizationTime, useAppStatePersistenceTask } from '@/app/shared' +import { useScheduleNotifications, useSyncTask, useUpdateAppBadge, useResetMemorizationTime, useAppStatePersistenceTask } from '@/app/shared' +import { useEnterFullscreenMode } from '@/app/decks/shared' import { useUpdateStatisticsTask } from '@/app/statistics' import { useTutorialPersistenceTask, } from '@/app/tutorial' @@ -13,7 +14,5 @@ export async function initTasks() { await useUpdateAppBadge().run() await useResetMemorizationTime().run() await useTutorialPersistenceTask().run() - - runEnterFullscreenMode() - runHideStatusBar() + await useEnterFullscreenMode().run() } \ No newline at end of file From e1106584860fa02c6791d038b0b3f5fe52900672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Sat, 17 Jun 2023 18:10:00 +0200 Subject: [PATCH 10/10] fix --- src/app/tutorial/tasks/useTutorialPersistenceTask.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/tutorial/tasks/useTutorialPersistenceTask.ts b/src/app/tutorial/tasks/useTutorialPersistenceTask.ts index 287f0dfd..18c055d0 100644 --- a/src/app/tutorial/tasks/useTutorialPersistenceTask.ts +++ b/src/app/tutorial/tasks/useTutorialPersistenceTask.ts @@ -17,7 +17,7 @@ export function useTutorialPersistenceTask() { /* Dependencies */ /* -------------------------------------------------------------------------- */ - const CURRENT_STEP_KEY = 'tutorial:currentStep' + const CURRENT_STEP_KEY = 'tutorial:step' const DATE_KEY = 'tutorial:date' const logger = useLogger('tutorial:persistence')