feat: Rename encryptedName to name in Categories table and update related logic

This commit is contained in:
2026-05-22 09:27:20 +02:00
parent f595f33f4a
commit 27e1199178
4 changed files with 245 additions and 150 deletions
+65 -10
View File
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/gestures.dart';
@@ -832,20 +833,26 @@ class _CategoryDialogState extends State<_CategoryDialog> {
Color? _selectedColor;
IconData? _selectedIcon;
int _selectedSection = 0;
bool _nameHasError = false;
bool _isSaving = false;
@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.category?.name ?? '');
_selectedColor =
widget.category != null && widget.category!.colorValue != null
? Color(widget.category!.colorValue!)
: null;
widget.category == null
? CategoryStyle.colors.first
: widget.category!.colorValue != null
? Color(widget.category!.colorValue!)
: null;
if (widget.category != null && widget.category!.iconCodePoint != null) {
_selectedIcon = CategoryStyle.icons.firstWhere(
(IconData icon) => icon.codePoint == widget.category!.iconCodePoint,
orElse: () => CategoryStyle.icons.first,
);
} else if (widget.category == null) {
_selectedIcon = CategoryStyle.icons.first;
}
}
@@ -856,12 +863,24 @@ class _CategoryDialogState extends State<_CategoryDialog> {
}
Future<void> _saveCategory() async {
if (_isSaving) {
return;
}
final String name = _controller.text.trim();
if (name.isEmpty) {
setState(() {
_nameHasError = true;
});
return;
}
try {
setState(() {
_nameHasError = false;
_isSaving = true;
});
final Category newCategory = Category(
id: widget.category?.id,
name: name,
@@ -881,16 +900,19 @@ class _CategoryDialogState extends State<_CategoryDialog> {
await widget.repository.createCategory(newCategory);
await widget.onCategoriesChanged();
try {
await widget.onRequestSync();
} catch (_) {}
if (mounted) {
await widget.onCategoryDeleted();
if (!mounted) return;
Navigator.pop(context);
}
widget.onRequestSync().catchError((_) {});
} catch (e) {
debugPrint('ERROR creating category: $e');
if (mounted) {
setState(() {
_isSaving = false;
});
}
if (mounted) {
ScaffoldMessenger.of(
context,
@@ -899,6 +921,24 @@ class _CategoryDialogState extends State<_CategoryDialog> {
}
}
Future<void> _runPostSaveCallbacks({
required Future<void> Function() onCategoriesChanged,
required Future<void> Function() onRequestSync,
required Future<void> Function() onCategoryDeleted,
}) async {
try {
await onCategoriesChanged();
} catch (_) {}
try {
await onCategoryDeleted();
} catch (_) {}
unawaited(
onRequestSync().catchError((_) {}),
);
}
Future<void> _deleteCategory() async {
final bool? confirm = await showDialog<bool>(
context: context,
@@ -954,8 +994,17 @@ class _CategoryDialogState extends State<_CategoryDialog> {
children: [
TextField(
controller: _controller,
onChanged: (_) {
if (_nameHasError) {
setState(() {
_nameHasError = false;
});
}
},
decoration: const InputDecoration(
hintText: 'Nombre de la categoría',
).copyWith(
errorText: _nameHasError ? 'El nombre es obligatorio' : null,
),
),
const SizedBox(height: 16),
@@ -1085,8 +1134,14 @@ class _CategoryDialogState extends State<_CategoryDialog> {
child: const Text('Cancelar'),
),
TextButton(
onPressed: _saveCategory,
child: Text(widget.category == null ? 'Crear' : 'Guardar'),
onPressed: _isSaving ? null : _saveCategory,
child: _isSaving
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: Text(widget.category == null ? 'Crear' : 'Guardar'),
),
],
);