Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: enable save button only if keys are edited

Ammar Ahmed3216c33fe697
diff --git a/apps/mobile/app/components/ui/input/form-input.tsx b/apps/mobile/app/components/ui/input/form-input.tsx
index 7d510ea0b..3e109734a 100644
--- a/apps/mobile/app/components/ui/input/form-input.tsx
+++ b/apps/mobile/app/components/ui/input/form-input.tsx
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { useThemeColors } from "@notesnook/theme";
import React, { RefObject, useEffect, useMemo, useState } from "react";
import {
ColorValue,
@@ -29,12 +30,11 @@ import {
} from "react-native";
import isEmail from "validator/lib/isEmail";
import isURL from "validator/lib/isURL";
-import { useThemeColors } from "@notesnook/theme";
-import { defaultBorderRadius, AppFontSize } from "../../../utils/size";
+import { AppFontSize, defaultBorderRadius } from "../../../utils/size";
import { DefaultAppStyles } from "../../../utils/styles";
+import AppIcon from "../AppIcon";
import { IconButton } from "../icon-button";
import Paragraph from "../typography/paragraph";
-import AppIcon from "../AppIcon";
export type FormValues = Record<string, string>;
export type FormErrors = Partial<Record<string, string>>;
@@ -58,10 +58,12 @@ export interface FormRef {
validateField: (name: string) => string | undefined;
validate: () => boolean;
subscribe: (listener: () => void) => () => void;
+ subscribeChange: (listener: () => void) => () => void;
}
export function createFormRef(initialValues: FormValues = {}): FormRef {
const listeners = new Set<() => void>();
+ const changeListeners = new Set<() => void>();
const values: FormValues = { ...initialValues };
const errors: FormErrors = {};
const schema: ValidationSchema = {};
@@ -70,6 +72,10 @@ export function createFormRef(initialValues: FormValues = {}): FormRef {
listeners.forEach((listener) => listener());
};
+ const notifyChange = () => {
+ changeListeners.forEach((listener) => listener());
+ };
+
return {
values,
errors,
@@ -79,6 +85,7 @@ export function createFormRef(initialValues: FormValues = {}): FormRef {
delete errors[name];
notify();
}
+ notifyChange();
},
getValue(name) {
return values[name] ?? "";
@@ -100,6 +107,7 @@ export function createFormRef(initialValues: FormValues = {}): FormRef {
clearErrors() {
Object.keys(errors).forEach((key) => delete errors[key]);
notify();
+ notifyChange();
},
registerField(name, validators = []) {
schema[name] = validators;
@@ -139,6 +147,12 @@ export function createFormRef(initialValues: FormValues = {}): FormRef {
return () => {
listeners.delete(listener);
};
+ },
+ subscribeChange(listener) {
+ changeListeners.add(listener);
+ return () => {
+ changeListeners.delete(listener);
+ };
}
};
}
diff --git a/apps/mobile/app/components/ui/pressable/index.tsx b/apps/mobile/app/components/ui/pressable/index.tsx
index f43da699c..568fd285f 100644
--- a/apps/mobile/app/components/ui/pressable/index.tsx
+++ b/apps/mobile/app/components/ui/pressable/index.tsx
@@ -268,8 +268,8 @@ export const Pressable = ({
const opacity = customOpacity
? customOpacity
: type === "accent"
- ? 1
- : colorOpacity;
+ ? 1
+ : colorOpacity;
const alpha = customAlpha ? customAlpha : isDark ? 0.03 : -0.03;
const { fontScale } = useWindowDimensions();
const growFactor = 1 + (fontScale - 1) / 8;
@@ -325,7 +325,10 @@ export const Pressable = ({
ref={fwdRef}
disabled={disabled}
hitSlop={hitSlop}
- onPress={onPress}
+ onPress={(e) => {
+ if (disabled) return;
+ onPress?.(e);
+ }}
onLongPress={onLongPress}
style={getStyle}
>
diff --git a/apps/mobile/app/screens/settings/manage-inbox-keys.tsx b/apps/mobile/app/screens/settings/manage-inbox-keys.tsx
index 543f51ed2..64dc8e6be 100644
--- a/apps/mobile/app/screens/settings/manage-inbox-keys.tsx
+++ b/apps/mobile/app/screens/settings/manage-inbox-keys.tsx
@@ -189,6 +189,7 @@ const ManageInboxKeys = () => {
})
);
const [formVersion, setFormVersion] = useState(0);
+ const [hasChanged, setHasChanged] = useState(false);
useEffect(() => {
if (!inboxKeys) return;
@@ -198,6 +199,21 @@ const ManageInboxKeys = () => {
setFormVersion((prev) => prev + 1);
}, [inboxKeys]);
+ useEffect(() => {
+ const unsub = formRef.current?.subscribeChange(() => {
+ const values = formRef.current?.getValues();
+ if (
+ inboxKeys?.publicKey !== values.publicKey ||
+ inboxKeys.privateKey !== values.privateKey
+ ) {
+ setHasChanged(true);
+ } else {
+ setHasChanged(false);
+ }
+ });
+ return unsub;
+ });
+
return (
<ScrollView
contentContainerStyle={{
@@ -232,17 +248,20 @@ const ManageInboxKeys = () => {
/>
<Button
title={strings.save()}
+ disabled={!hasChanged}
type="accent"
width={"100%"}
onPress={async () => {
try {
- if (!formRef.current.validate()) return;
-
const keysEdited: SerializedKeyPair = {
publicKey: formRef.current.getValue("publicKey"),
privateKey: formRef.current.getValue("privateKey")
};
+ if (!keysEdited.privateKey || !keysEdited.publicKey) {
+ if (!formRef.current.validate()) return;
+ }
+
const result = await Storage.validatePGPKeyPair(keysEdited);
if (!result.isValid) {