feat: servicios WhatsApp, Bitácora WebControl, SQLite v4, UI y store listing
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:file_picker/file_picker.dart';
|
||||
import '../theme.dart';
|
||||
import '../models/usuario.dart';
|
||||
import '../db/dao/usuarios_dao.dart';
|
||||
import '../services/whatsapp_alert_service.dart';
|
||||
import '../main.dart' show LoginPage;
|
||||
import 'settings_page.dart';
|
||||
import 'seguridad_page.dart';
|
||||
@@ -123,6 +124,162 @@ class _PerfilUsuarioPageState extends State<PerfilUsuarioPage> {
|
||||
if (mounted) setState(() => _usuario = _usuario!.copyWith(fotoPath: path));
|
||||
}
|
||||
|
||||
// ── Editar teléfono para WhatsApp de alertas críticas ─────────────────────
|
||||
// Se valida mediante expresión regular el código de país internacional E.164
|
||||
Future<void> _editarTelefono() async {
|
||||
if (_usuario == null) return;
|
||||
final ctrl = TextEditingController(text: _usuario!.telefono ?? '');
|
||||
String? errorTexto;
|
||||
|
||||
// Función de validación con regex para formato internacional con código de país
|
||||
String? validarNumero(String valor) {
|
||||
final normalizado = WhatsAppAlertService.normalizarTelefono(valor);
|
||||
if (normalizado.isEmpty) {
|
||||
return null; // Permitir campo vacío para borrar el número
|
||||
}
|
||||
if (normalizado.startsWith('0')) {
|
||||
return 'No debe iniciar con 0. Incluye código de país (ej. +593...)';
|
||||
}
|
||||
if (!WhatsAppAlertService.esNumeroValido(normalizado)) {
|
||||
return 'Código de país obligatorio (ej. +5939XXXXXXXX)';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
final nuevoTelefono = await showDialog<String?>(
|
||||
context: context,
|
||||
builder: (ctx) {
|
||||
return StatefulBuilder(
|
||||
builder: (context, setDialogState) {
|
||||
return AlertDialog(
|
||||
backgroundColor: AppColors.surfaceHigh,
|
||||
title: Row(
|
||||
children: [
|
||||
const Icon(Icons.phone_android,
|
||||
color: AppColors.secondary, size: 22),
|
||||
const SizedBox(width: 8),
|
||||
Text('WhatsApp de Alertas',
|
||||
style: appHeadline(size: 16, weight: FontWeight.w700)),
|
||||
],
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Ingresa tu número con código de país para recibir alertas de intrusión, coacción y botón de pánico vía sigma-robot.',
|
||||
style:
|
||||
appLabel(size: 12, color: AppColors.onSurfaceVariant),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: ctrl,
|
||||
autofocus: true,
|
||||
keyboardType: TextInputType.phone,
|
||||
style: appHeadline(size: 15),
|
||||
onChanged: (v) {
|
||||
setDialogState(() {
|
||||
errorTexto = validarNumero(v);
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.phone_android,
|
||||
color: AppColors.secondary, size: 20),
|
||||
hintText: '+5939XXXXXXXX',
|
||||
hintStyle: appLabel(size: 13, color: AppColors.outline),
|
||||
errorText: errorTexto,
|
||||
errorMaxLines: 2,
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide:
|
||||
const BorderSide(color: AppColors.outlineVariant),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: const BorderSide(
|
||||
color: AppColors.secondary, width: 2),
|
||||
),
|
||||
filled: true,
|
||||
fillColor: AppColors.surfaceHighest,
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 14, vertical: 12),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
'Formato internacional: +[código_país][número]\nEjemplo Ecuador: +593987654321',
|
||||
style: appLabel(size: 11, color: AppColors.outline),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
if (_usuario!.telefono != null &&
|
||||
_usuario!.telefono!.isNotEmpty)
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, ''),
|
||||
child: Text('Eliminar',
|
||||
style: appLabel(size: 13, color: AppColors.error)),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, null),
|
||||
child: Text('Cancelar',
|
||||
style: appLabel(
|
||||
size: 13, color: AppColors.onSurfaceVariant)),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
final err = validarNumero(ctrl.text);
|
||||
if (err != null) {
|
||||
setDialogState(() => errorTexto = err);
|
||||
return;
|
||||
}
|
||||
final valorFinal = ctrl.text.trim().isEmpty
|
||||
? null
|
||||
: WhatsAppAlertService.normalizarTelefono(ctrl.text);
|
||||
Navigator.pop(ctx, valorFinal ?? '');
|
||||
},
|
||||
child: Text('Guardar',
|
||||
style: appLabel(
|
||||
size: 13,
|
||||
weight: FontWeight.bold,
|
||||
color: AppColors.secondary)),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
ctrl.dispose();
|
||||
if (nuevoTelefono == null) return; // Operación cancelada
|
||||
|
||||
final valorPersistir = nuevoTelefono.isEmpty ? null : nuevoTelefono;
|
||||
|
||||
final actualizado = _usuario!.copyWith(
|
||||
telefono: valorPersistir,
|
||||
sincronizado: false,
|
||||
);
|
||||
|
||||
// Persiste el nuevo número en la tabla usuarios de SQLite
|
||||
await _dao.update(actualizado);
|
||||
if (mounted) {
|
||||
setState(() => _usuario = actualizado);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
valorPersistir != null
|
||||
? 'Número de WhatsApp registrado: $valorPersistir'
|
||||
: 'Número de WhatsApp eliminado',
|
||||
style: appLabel(size: 13, color: AppColors.onSurface),
|
||||
),
|
||||
backgroundColor: AppColors.surfaceHigh,
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_cargando) {
|
||||
@@ -142,6 +299,8 @@ class _PerfilUsuarioPageState extends State<PerfilUsuarioPage> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_avatarCard(nombre, iniciales, nivel, nivelLabel),
|
||||
const SizedBox(height: 16),
|
||||
_whatsappCard(),
|
||||
const SizedBox(height: 24),
|
||||
_statsGrid(),
|
||||
const SizedBox(height: 32),
|
||||
@@ -153,6 +312,115 @@ class _PerfilUsuarioPageState extends State<PerfilUsuarioPage> {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Tarjeta de Teléfono / WhatsApp para alertas críticas ──────────────────
|
||||
Widget _whatsappCard() {
|
||||
final telefono = _usuario?.telefono;
|
||||
final tieneTelefono = telefono != null && telefono.trim().isNotEmpty;
|
||||
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: tieneTelefono
|
||||
? AppColors.secondary.withOpacity(0.35)
|
||||
: AppColors.outlineVariant,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: tieneTelefono
|
||||
? AppColors.secondary.withOpacity(0.15)
|
||||
: AppColors.surfaceHighest,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.phone_android,
|
||||
color: tieneTelefono ? AppColors.secondary : AppColors.outline,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'WHATSAPP DE ALERTAS',
|
||||
style: appLabel(
|
||||
size: 10,
|
||||
weight: FontWeight.bold,
|
||||
color: tieneTelefono
|
||||
? AppColors.secondary
|
||||
: AppColors.onSurfaceVariant,
|
||||
spacing: 1.5,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: tieneTelefono
|
||||
? AppColors.secondaryContainer
|
||||
: AppColors.surfaceLowest,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(
|
||||
tieneTelefono ? 'ACTIVO' : 'SIN CONFIGURAR',
|
||||
style: appLabel(
|
||||
size: 9,
|
||||
weight: FontWeight.bold,
|
||||
color: tieneTelefono
|
||||
? AppColors.secondary
|
||||
: AppColors.outline,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
tieneTelefono ? telefono : 'Sin número configurado',
|
||||
style: appHeadline(
|
||||
size: 14,
|
||||
weight: FontWeight.w600,
|
||||
color: tieneTelefono
|
||||
? AppColors.onSurface
|
||||
: AppColors.outline,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'Recepción de alertas de pánico y severidad 4-5',
|
||||
style: appLabel(size: 11, color: AppColors.onSurfaceVariant),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
tieneTelefono ? Icons.edit_outlined : Icons.add_circle_outline,
|
||||
color: tieneTelefono ? AppColors.secondary : AppColors.primary,
|
||||
size: 22,
|
||||
),
|
||||
onPressed: _editarTelefono,
|
||||
tooltip: 'Configurar WhatsApp de alertas',
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── Avatar + info ──────────────────────────────────────────────────────────
|
||||
Widget _avatarCard(String nombre, String iniciales, int nivel,
|
||||
String nivelLabel) =>
|
||||
|
||||
Reference in New Issue
Block a user