Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor slider update logic (#883)
Leendert de Borst16e03d4dbc9c
diff --git a/apps/mobile-app/app/(tabs)/credentials/add-edit.tsx b/apps/mobile-app/app/(tabs)/credentials/add-edit.tsxindex d8422c5e4..191734587 100644--- a/apps/mobile-app/app/(tabs)/credentials/add-edit.tsx+++ b/apps/mobile-app/app/(tabs)/credentials/add-edit.tsx@@ -673,6 +673,7 @@ export default function AddEditCredentialScreen() : React.ReactNode {initialSettings={passwordSettings}showPassword={isPasswordVisible}onShowPasswordChange={setIsPasswordVisible}+ isNewCredential={!isEditMode}/>) : (<ValidatedFormFielddiff --git a/apps/mobile-app/components/form/AdvancedPasswordField.tsx b/apps/mobile-app/components/form/AdvancedPasswordField.tsxindex 525e95ec5..97462c28b 100644--- a/apps/mobile-app/components/form/AdvancedPasswordField.tsx+++ b/apps/mobile-app/components/form/AdvancedPasswordField.tsx@@ -25,6 +25,7 @@ type AdvancedPasswordFieldProps<T extends FieldValues> = Omit<TextInputProps, 'vinitialSettings: PasswordSettings;showPassword?: boolean;onShowPasswordChange?: (show: boolean) => void;+ isNewCredential?: boolean;}/**@@ -38,6 +39,7 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, AdvainitialSettings,showPassword: controlledShowPassword,onShowPasswordChange,+ isNewCredential = false,...props}, ref) => {const colors = useColors();@@ -49,6 +51,9 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Advaconst [showSettingsModal, setShowSettingsModal] = useState(false);const [currentSettings, setCurrentSettings] = useState<PasswordSettings>(initialSettings);const [previewPassword, setPreviewPassword] = useState<string>('');+ const [displayLength, setDisplayLength] = useState<number>(initialSettings.Length);+ const [hasAutoGenerated, setHasAutoGenerated] = useState(false);+ const onChangeRef = useRef<((value: string) => void) | null>(null);// Use controlled or uncontrolled showPassword stateconst showPassword = controlledShowPassword ?? internalShowPassword;@@ -87,8 +92,27 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva*/useEffect(() => {setCurrentSettings({ ...initialSettings });+ setDisplayLength(initialSettings.Length);}, [initialSettings]);+ /**+ * Auto-generate password for new credentials after component mount.+ */+ useEffect(() => {+ if (isNewCredential && !hasAutoGenerated && currentSettings.Length > 0 && onChangeRef.current) {+ try {+ const passwordGenerator = CreatePasswordGenerator(currentSettings);+ const password = passwordGenerator.generateRandomPassword();+ onChangeRef.current(password);+ setShowPasswordState(true);+ setHasAutoGenerated(true);+ } catch (error) {+ console.error('Error auto-generating password:', error);+ setHasAutoGenerated(true);+ }+ }+ }, [isNewCredential, hasAutoGenerated, currentSettings, setShowPasswordState]);+/*** Generate a password with the given settings.*/@@ -119,16 +143,6 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva}}, []);- /**- * Handle password length change via slider.- */- const handleLengthChange = useCallback((newLength: number, onChange: (value: string) => void) => {- const roundedLength = Math.round(newLength);- const newSettings = { ...currentSettings, Length: roundedLength };- setCurrentSettings(newSettings);- generatePassword(newSettings, onChange);- }, [currentSettings, generatePassword]);-/*** Individual handlers for each switch to prevent re-renders.*/@@ -166,6 +180,8 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva* Handle opening the settings modal.*/const handleOpenSettings = useCallback(() => {+ // Focus the password input field to prevent scroll jumping+ inputRef.current?.focus();// Generate initial preview when modal opensgeneratePreview(currentSettings);setShowSettingsModal(true);@@ -307,11 +323,13 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva},slider: {height: 40,+ paddingVertical: 10, // Add padding for better touch area on Androidwidth: '100%',},sliderContainer: {marginTop: 8,paddingHorizontal: 4,+ paddingVertical: 5, // Additional padding around the slider},sliderHeader: {alignItems: 'center',@@ -337,9 +355,9 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, AdvaalignItems: 'center',backgroundColor: colors.primary,borderColor: colors.accentBorder,- color: colors.primarySurfaceText,borderRadius: 6,borderWidth: 1,+ color: colors.primarySurfaceText,flexDirection: 'row',justifyContent: 'center',padding: 6,@@ -490,6 +508,7 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Advaname={name}render={({ field: { onChange, value }, fieldState: { error } }) => {currentValue.current = value as string;+ onChangeRef.current = onChange; // Assign onChange to the refconst showClearButton = Platform.OS === 'android' && value && value.length > 0 && isFocused;return (@@ -551,7 +570,7 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva<View style={styles.sliderHeader}><ThemedText style={styles.sliderLabel}>{t('credentials.passwordLength')}</ThemedText><View style={styles.sliderValueContainer}>- <ThemedText style={styles.sliderValue}>{currentSettings.Length}</ThemedText>+ <ThemedText style={styles.sliderValue}>{displayLength}</ThemedText><TouchableOpacitystyle={styles.settingsButton}onPress={handleOpenSettings}@@ -566,7 +585,18 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, AdvaminimumValue={8}maximumValue={64}value={currentSettings.Length}- onValueChange={(value) => handleLengthChange(value, onChange)}+ onValueChange={(value) => {+ const roundedLength = Math.round(value);+ setDisplayLength(roundedLength); // Update display immediately+ const tempSettings = { ...currentSettings, Length: roundedLength };+ generatePassword(tempSettings, onChange);+ }}+ onSlidingComplete={(value) => {+ const roundedLength = Math.round(value);+ const newSettings = { ...currentSettings, Length: roundedLength };+ setCurrentSettings(newSettings);+ setDisplayLength(roundedLength); // Ensure display matches final value+ }}step={1}minimumTrackTintColor={colors.primary}maximumTrackTintColor={colors.accentBorder}