Skip to content

Commit

Permalink
mobile: add verification prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Sep 4, 2023
1 parent 11f3931 commit 9a31c07
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
5 changes: 3 additions & 2 deletions apps/mobile/app/screens/settings/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export async function verifyUser(
negativeText: closeText || "Cancel",
positivePress: async (value) => {
try {
let verified = await db.user.verifyPassword(value);
const user = await db.user.getUser();
let verified = !user ? true : await db.user.verifyPassword(value);
if (verified) {
sleep(300).then(async () => {
await onsuccess();
Expand All @@ -58,7 +59,7 @@ export async function verifyUser(
}
} catch (e) {
ToastEvent.show({
heading: "Failed to backup data",
heading: "Failed to verify",
message: e.message,
type: "error",
context: "global"
Expand Down
15 changes: 13 additions & 2 deletions apps/mobile/app/screens/settings/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import PremiumService from "../../../services/premium";
import { useThemeColors } from "@notesnook/theme";
import { SIZE } from "../../../utils/size";
import { sleep } from "../../../utils/time";
import { verifyUser } from "../functions";
import { Dialog } from "../../../components/dialog";

interface PickerOptions<T> {
getValue: () => T;
Expand All @@ -37,6 +39,7 @@ interface PickerOptions<T> {
options: T[];
premium?: boolean;
onCheckOptionIsPremium?: (item: T) => boolean;
requiresVerification?: () => boolean;
}

export function SettingsPicker<T>({
Expand All @@ -47,7 +50,8 @@ export function SettingsPicker<T>({
options,
getItemKey,
premium,
onCheckOptionIsPremium = () => true
onCheckOptionIsPremium = () => true,
requiresVerification = () => false
}: PickerOptions<T>) {
const { colors } = useThemeColors("contextMenu");
const menuRef = useRef<any>();
Expand Down Expand Up @@ -118,11 +122,18 @@ export function SettingsPicker<T>({
</PressableButton>
}
>
<Dialog context="local" />
{options.map((item) => (
<MenuItem
key={getItemKey(item)}
onPress={async () => {
onChange(item);
if (requiresVerification?.()) {
verifyUser("local", () => {
onChange(item);
});
} else {
onChange(item);
}
}}
style={{
backgroundColor: compareValue(currentValue, item)
Expand Down
7 changes: 7 additions & 0 deletions apps/mobile/app/screens/settings/picker/pickers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { createSettingsPicker } from ".";
import { getFontById, getFonts } from "@notesnook/editor/dist/utils/font";
import { DATE_FORMATS, TIME_FORMATS } from "@notesnook/core/dist/common";
import dayjs from "dayjs";
import { useUserStore } from "../../../stores/use-user-store";

export const FontPicker = createSettingsPicker({
getValue: () => useSettingStore.getState().settings.defaultFontFamily,
Expand Down Expand Up @@ -127,6 +128,12 @@ export const BackupReminderPicker = createSettingsPicker({
options: ["useroff", "daily", "weekly", "monthly"],
compareValue: (current, item) => current === item,
premium: true,
requiresVerification: () => {
return (
!useSettingStore.getState().settings.encryptedBackup &&
useUserStore.getState().user
);
},
onCheckOptionIsPremium: (item) => {
return item !== "useroff";
}
Expand Down
21 changes: 16 additions & 5 deletions apps/mobile/app/screens/settings/settings-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,11 @@ export const settingsGroups: SettingSection[] = [
description: "Create a backup of your data",
modifer: async () => {
const user = useUserStore.getState().user;
if (!user) {
if (!user || SettingsService.getProperty("encryptedBackup")) {
await BackupService.run(true);
return;
}

verifyUser(null, () => BackupService.run(true));
}
},
Expand Down Expand Up @@ -869,9 +870,17 @@ export const settingsGroups: SettingSection[] = [
});
return;
}
SettingsService.set({
encryptedBackup: !settings.encryptedBackup
});
if (settings.encryptedBackup) {
await verifyUser(null, () => {
SettingsService.set({
encryptedBackup: false
});
});
} else {
SettingsService.set({
encryptedBackup: true
});
}
}
}
]
Expand Down Expand Up @@ -901,7 +910,9 @@ export const settingsGroups: SettingSection[] = [
description:
"Export all notes as pdf, markdown, html or text in a single zip file",
modifer: () => {
ExportNotesSheet.present(undefined, true);
verifyUser(null, () => {
ExportNotesSheet.present(undefined, true);
});
}
}
]
Expand Down

0 comments on commit 9a31c07

Please sign in to comment.