feat: ApiAuthService + Sincronizador reescritos para WebControl/Centinel
- ApiAuthService: login WebControl → guarda jwt + apiKey + db_tenant - Sincronizador: pull alarmas/zonas/usuarios/mensajes/banners + push pendientes - DAOs: métodos adicionales en alarmas, eventos, usuarios - Páginas actualizadas: ConectarProveedorPage, ControlAccesoPage, MensajeríaPage, MonitoreoAlarmaPage Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
4724f84b04
commit
efa4a792f4
@@ -1,8 +1,9 @@
|
||||
//agregar un boton para registrar usuarios y dale acceso por tiempo y areas
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../theme.dart';
|
||||
import '../db/dao/accesos_dao.dart';
|
||||
import '../db/dao/usuarios_dao.dart';
|
||||
import '../models/usuario.dart';
|
||||
import 'registrar_usuario_page.dart';
|
||||
|
||||
class ControlAccesoPage extends StatefulWidget {
|
||||
const ControlAccesoPage({Key? key}) : super(key: key);
|
||||
@@ -17,7 +18,9 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
bool _serverBloqueado = true;
|
||||
|
||||
final _dao = AccesosDao();
|
||||
final _usuariosDao = UsuariosDao();
|
||||
List<Map<String, dynamic>> _accesos = [];
|
||||
List<Usuario> _usuarios = [];
|
||||
bool _cargando = true;
|
||||
|
||||
@override
|
||||
@@ -29,13 +32,29 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
Future<void> _cargar() async {
|
||||
setState(() => _cargando = true);
|
||||
try {
|
||||
final lista = await _dao.getRecientesConDetalle(limit: 20);
|
||||
final results = await Future.wait([
|
||||
_dao.getRecientesConDetalle(limit: 20),
|
||||
_usuariosDao.getActivos(),
|
||||
]);
|
||||
if (!mounted) return;
|
||||
setState(() => _accesos = lista);
|
||||
setState(() {
|
||||
_accesos = results[0] as List<Map<String, dynamic>>;
|
||||
_usuarios = results[1] as List<Usuario>;
|
||||
});
|
||||
} catch (_) {}
|
||||
if (mounted) setState(() => _cargando = false);
|
||||
}
|
||||
|
||||
void _abrirRegistro() {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
fullscreenDialog: true,
|
||||
builder: (_) => const RegistrarUsuarioPage(),
|
||||
),
|
||||
).then((ok) { if (ok == true) _cargar(); });
|
||||
}
|
||||
|
||||
String _horaCorta(String iso) {
|
||||
try {
|
||||
final dt = DateTime.parse(iso).toLocal();
|
||||
@@ -53,24 +72,40 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _cargar,
|
||||
color: AppColors.primary,
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
primary: false,
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_encabezado(),
|
||||
const SizedBox(height: 32),
|
||||
_gridEntradas(),
|
||||
const SizedBox(height: 40),
|
||||
_registroAcceso(),
|
||||
const SizedBox(height: 24),
|
||||
_snapshotCamara(),
|
||||
],
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: _abrirRegistro,
|
||||
backgroundColor: AppColors.primary,
|
||||
foregroundColor: AppColors.primaryContainer,
|
||||
icon: const Icon(Icons.person_add_alt_1, size: 20),
|
||||
label: Text('Registrar Usuario',
|
||||
style: appLabel(
|
||||
size: 13,
|
||||
weight: FontWeight.bold,
|
||||
color: AppColors.primaryContainer)),
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: _cargar,
|
||||
color: AppColors.primary,
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
primary: false,
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 96),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_encabezado(),
|
||||
const SizedBox(height: 32),
|
||||
_gridEntradas(),
|
||||
const SizedBox(height: 40),
|
||||
_usuariosRegistrados(),
|
||||
const SizedBox(height: 40),
|
||||
_registroAcceso(),
|
||||
const SizedBox(height: 24),
|
||||
_snapshotCamara(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -291,6 +326,215 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
]),
|
||||
);
|
||||
|
||||
// ── Usuarios registrados ────────────────────────────────────────────────────
|
||||
Widget _usuariosRegistrados() => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text('Usuarios con Acceso',
|
||||
style: appHeadline(size: 20, weight: FontWeight.bold)),
|
||||
Text('${_usuarios.length} activo${_usuarios.length == 1 ? '' : 's'}',
|
||||
style: appLabel(size: 12, color: AppColors.onSurfaceVariant)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_cargando)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 16),
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: AppColors.primary),
|
||||
))
|
||||
else if (_usuarios.isEmpty)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceLow,
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
child: Column(children: [
|
||||
const Icon(Icons.group_outlined,
|
||||
color: AppColors.outlineVariant, size: 32),
|
||||
const SizedBox(height: 8),
|
||||
Text('Sin usuarios registrados',
|
||||
style: appLabel(
|
||||
size: 13, color: AppColors.outlineVariant)),
|
||||
Text('Usa el botón + para agregar',
|
||||
style:
|
||||
appLabel(size: 11, color: AppColors.outlineVariant)),
|
||||
]),
|
||||
)
|
||||
else
|
||||
..._usuarios.map((u) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: _cardUsuario(u),
|
||||
)),
|
||||
],
|
||||
);
|
||||
|
||||
static const _nivelLabels = ['', 'Visitante', 'Empleado', 'Supervisor', 'Admin', 'Super Admin'];
|
||||
static const _nivelColors = [
|
||||
Colors.transparent,
|
||||
AppColors.outline,
|
||||
AppColors.secondary,
|
||||
AppColors.primary,
|
||||
AppColors.tertiary,
|
||||
AppColors.error,
|
||||
];
|
||||
|
||||
Widget _cardUsuario(Usuario u) {
|
||||
final nivel = u.nivelAcceso.clamp(1, 5);
|
||||
final nColor = _nivelColors[nivel];
|
||||
final nLabel = _nivelLabels[nivel];
|
||||
final fechaFin = u.fechaFin != null
|
||||
? _formatFecha(u.fechaFin!)
|
||||
: null;
|
||||
final vencido = u.fechaFin != null &&
|
||||
DateTime.tryParse(u.fechaFin!)?.isBefore(DateTime.now()) == true;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceLow,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(
|
||||
color: vencido
|
||||
? AppColors.error.withOpacity(0.3)
|
||||
: AppColors.surfaceHighest,
|
||||
),
|
||||
),
|
||||
child: Row(children: [
|
||||
// Avatar
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: nColor.withOpacity(0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: nColor.withOpacity(0.3)),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(u.iniciales,
|
||||
style: appHeadline(
|
||||
size: 15, weight: FontWeight.bold, color: nColor)),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// Info
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(children: [
|
||||
Expanded(
|
||||
child: Text(u.nombre,
|
||||
style:
|
||||
appHeadline(size: 14, weight: FontWeight.w600)),
|
||||
),
|
||||
// Badge nivel
|
||||
Container(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: nColor.withOpacity(0.12),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text('N$nivel · $nLabel',
|
||||
style: appLabel(
|
||||
size: 10,
|
||||
weight: FontWeight.bold,
|
||||
color: nColor)),
|
||||
),
|
||||
]),
|
||||
const SizedBox(height: 4),
|
||||
Row(children: [
|
||||
Icon(Icons.calendar_today,
|
||||
size: 11, color: AppColors.onSurfaceVariant),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
fechaFin != null
|
||||
? 'Hasta $fechaFin${vencido ? ' · VENCIDO' : ''}'
|
||||
: 'Sin vencimiento',
|
||||
style: appLabel(
|
||||
size: 11,
|
||||
color: vencido
|
||||
? AppColors.error
|
||||
: AppColors.onSurfaceVariant),
|
||||
),
|
||||
if (u.areas != null && u.areas!.isNotEmpty) ...[
|
||||
const SizedBox(width: 10),
|
||||
Icon(Icons.place_outlined,
|
||||
size: 11, color: AppColors.onSurfaceVariant),
|
||||
const SizedBox(width: 3),
|
||||
Expanded(
|
||||
child: Text(
|
||||
u.areas!.replaceAll(',', ', '),
|
||||
style: appLabel(
|
||||
size: 11,
|
||||
color: AppColors.onSurfaceVariant),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
]),
|
||||
]),
|
||||
),
|
||||
// Botón desactivar
|
||||
IconButton(
|
||||
icon: const Icon(Icons.person_off_outlined,
|
||||
color: AppColors.outline, size: 18),
|
||||
tooltip: 'Desactivar acceso',
|
||||
onPressed: () => _confirmarDesactivar(u),
|
||||
padding: const EdgeInsets.all(8),
|
||||
constraints: const BoxConstraints(),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatFecha(String iso) {
|
||||
try {
|
||||
final dt = DateTime.parse(iso).toLocal();
|
||||
return '${dt.day.toString().padLeft(2, '0')}/${dt.month.toString().padLeft(2, '0')}/${dt.year}';
|
||||
} catch (_) {
|
||||
return '—';
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _confirmarDesactivar(Usuario u) async {
|
||||
final ok = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
backgroundColor: AppColors.surfaceHigh,
|
||||
title: Text('Desactivar acceso',
|
||||
style: appHeadline(
|
||||
size: 15, weight: FontWeight.w700, color: AppColors.error)),
|
||||
content: Text(
|
||||
'Se revocará el acceso de "${u.nombre}". Esta acción puede revertirse desde el servidor.',
|
||||
style: appLabel(size: 13, color: AppColors.onSurface),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: Text('Cancelar',
|
||||
style: appLabel(color: AppColors.onSurfaceVariant)),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: Text('DESACTIVAR',
|
||||
style: appLabel(
|
||||
weight: FontWeight.bold, color: AppColors.error)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (ok != true) return;
|
||||
await _usuariosDao.desactivar(u.id);
|
||||
_cargar();
|
||||
}
|
||||
|
||||
// ── Registro de acceso ──────────────────────────────────────────────────────
|
||||
Widget _registroAcceso() => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -457,12 +701,12 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
Positioned(
|
||||
left: 40,
|
||||
top: 20,
|
||||
child: _cctv_box(
|
||||
child: _cctvBox(
|
||||
80, 60, AppColors.primary.withOpacity(0.15))),
|
||||
Positioned(
|
||||
right: 60,
|
||||
top: 40,
|
||||
child: _cctv_box(
|
||||
child: _cctvBox(
|
||||
50, 40, AppColors.primary.withOpacity(0.08))),
|
||||
// Overlay oscuro inferior
|
||||
Container(
|
||||
@@ -507,7 +751,7 @@ class _ControlAccesoPageState extends State<ControlAccesoPage> {
|
||||
),
|
||||
);
|
||||
|
||||
Widget _cctv_box(double w, double h, Color color) => Container(
|
||||
Widget _cctvBox(double w, double h, Color color) => Container(
|
||||
width: w,
|
||||
height: h,
|
||||
decoration: BoxDecoration(
|
||||
|
||||
Reference in New Issue
Block a user