From cef2472aae7ac3c0004c79973b72fe49bb209ec5 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Wed, 6 Sep 2023 20:06:46 +0500 Subject: [PATCH] mobile: handle state when app entered background to take some action like pick a file/image etc. --- apps/mobile/app/hooks/use-app-events.js | 6 ++++++ apps/mobile/app/screens/editor/tiptap/picker.js | 4 ++++ apps/mobile/app/screens/editor/tiptap/use-editor.ts | 1 + apps/mobile/app/screens/editor/wrapper.js | 1 + apps/mobile/app/stores/use-setting-store.ts | 10 +++++++++- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/mobile/app/hooks/use-app-events.js b/apps/mobile/app/hooks/use-app-events.js index 23e9df912..94438d99e 100644 --- a/apps/mobile/app/hooks/use-app-events.js +++ b/apps/mobile/app/hooks/use-app-events.js @@ -534,6 +534,12 @@ export const useAppEvents = () => { if (refValues.current?.isReconnecting || !refValues.current?.isUserReady) return; + if (useSettingStore.getState().appDidEnterBackgroundForAction) { + useSettingStore.getState().setAppDidEnterBackgroundForAction(false); + console.log("AppDidEnterForegroundAfterAction"); + return; + } + if (SettingsService.get().sessionExpired) { refValues.current.isReconnecting = false; return; diff --git a/apps/mobile/app/screens/editor/tiptap/picker.js b/apps/mobile/app/screens/editor/tiptap/picker.js index 0f74301b5..ba8b987a9 100644 --- a/apps/mobile/app/screens/editor/tiptap/picker.js +++ b/apps/mobile/app/screens/editor/tiptap/picker.js @@ -34,6 +34,7 @@ import PremiumService from "../../../services/premium"; import { FILE_SIZE_LIMIT, IMAGE_SIZE_LIMIT } from "../../../utils/constants"; import { eCloseSheet } from "../../../utils/events"; import { editorController, editorState } from "./utils"; +import { useSettingStore } from "../../../stores/use-setting-store"; const showEncryptionSheet = (file) => { presentSheet({ @@ -62,6 +63,7 @@ const file = async (fileOptions) => { let file; try { + useSettingStore.getState().setAppDidEnterBackgroundForAction(true); file = await DocumentPicker.pick(options); } catch (e) { return; @@ -135,6 +137,7 @@ const file = async (fileOptions) => { const camera = async (options) => { try { await db.attachments.generateKey(); + useSettingStore.getState().setAppDidEnterBackgroundForAction(true); launchCamera( { includeBase64: true, @@ -156,6 +159,7 @@ const camera = async (options) => { const gallery = async (options) => { try { await db.attachments.generateKey(); + useSettingStore.getState().setAppDidEnterBackgroundForAction(true); launchImageLibrary( { includeBase64: true, diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts index 5cd901a54..9c1926af1 100644 --- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts +++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts @@ -141,6 +141,7 @@ export const useEditor = ( clearTimeout(timers.current["loading-images"]); sessionHistoryId.current = undefined; saveCount.current = 0; + lock.current = false; useEditorStore.getState().setReadonly(false); resetContent && postMessage(EditorEvents.title, ""); lastContentChangeTime.current = 0; diff --git a/apps/mobile/app/screens/editor/wrapper.js b/apps/mobile/app/screens/editor/wrapper.js index a3632084e..1bf0ea77e 100644 --- a/apps/mobile/app/screens/editor/wrapper.js +++ b/apps/mobile/app/screens/editor/wrapper.js @@ -55,6 +55,7 @@ export const EditorWrapper = ({ width }) => { prevState.current = state; return; } + if (useSettingStore.getState().appDidEnterBackgroundForAction) return; if (state === "active") { editorController.current.onReady(); editorController.current.overlay(false); diff --git a/apps/mobile/app/stores/use-setting-store.ts b/apps/mobile/app/stores/use-setting-store.ts index e165d35bc..af6398f1a 100644 --- a/apps/mobile/app/stores/use-setting-store.ts +++ b/apps/mobile/app/stores/use-setting-store.ts @@ -103,6 +103,8 @@ export interface SettingStore extends State { sheetKeyboardHandler: boolean; requestBiometrics: boolean; setRequestBiometrics: (requestBiometrics: boolean) => void; + appDidEnterBackgroundForAction: boolean; + setAppDidEnterBackgroundForAction: (value: boolean) => void; insets: Insets; setInsets: (insets: Insets) => void; timeFormat: string; @@ -155,7 +157,7 @@ export const defaultSettings: SettingStore["settings"] = { darkTheme: ThemeDark }; -export const useSettingStore = create((set) => ({ +export const useSettingStore = create((set, get) => ({ settings: { ...defaultSettings }, sheetKeyboardHandler: true, fullscreen: false, @@ -174,6 +176,12 @@ export const useSettingStore = create((set) => ({ setInsets: (insets) => set({ insets }), timeFormat: "12-hour", dateFormat: "DD-MM-YYYY", + setAppDidEnterBackgroundForAction: (value: boolean) => { + set({ + appDidEnterBackgroundForAction: value + }); + }, + appDidEnterBackgroundForAction: false, insets: initialWindowMetrics?.insets ? initialWindowMetrics.insets : { top: 0, right: 0, left: 0, bottom: 0 }