feat: add SQLite data layer, local auth and security settings
- Add offline-first SQLite architecture with DatabaseHelper singleton, 12 CREATE TABLE statements and full seed data on first launch - Add 10 models (fromMap/toMap) and 12 DAOs with CRUD + getPendientes() - Add ApiAuthService (JWT via SharedPreferences) and Sincronizador skeleton - Move login auth from SQLite to SharedPreferences with SHA-256 PIN hash; default PIN 123456, platform-independent (no native plugin required) - Add seguridad_page.dart: change PIN form (current → new → confirm) - Add Cerrar Sesión with confirmation dialog and full stack clear - Redesign settings_page.dart with AppColors dark theme; add Sistema section with "Inicializar base de datos" and "Inicializar sincronización" - Fix Linux desktop: sqflite_common_ffi with platform detection in DatabaseHelper - Fix ScrollController conflict on Linux: primary: false on all IndexedStack pages - Update plan_proyecto.md with session 2–3 progress and auth roadmap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
8f13d57839
commit
1184ab5fe2
@@ -0,0 +1,49 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/acceso.dart';
|
||||
|
||||
class AccesosDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Acceso>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('accesos', orderBy: 'timestamp DESC');
|
||||
return rows.map(Acceso.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Acceso>> getRecientes({int limit = 20}) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'accesos',
|
||||
orderBy: 'timestamp DESC',
|
||||
limit: limit,
|
||||
);
|
||||
return rows.map(Acceso.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Acceso>> getByPuerta(int puertaId) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'accesos',
|
||||
where: 'puerta_id = ?',
|
||||
whereArgs: [puertaId],
|
||||
orderBy: 'timestamp DESC',
|
||||
);
|
||||
return rows.map(Acceso.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<int> insert(Acceso a) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('accesos', a.toMap());
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('accesos', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Acceso>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('accesos', where: 'sincronizado = 0');
|
||||
return rows.map(Acceso.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/alarma.dart';
|
||||
|
||||
class AlarmasDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Alarma>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('alarmas', orderBy: 'timestamp DESC');
|
||||
return rows.map(Alarma.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Alarma>> getActivas() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'alarmas',
|
||||
where: 'estado = ?',
|
||||
whereArgs: ['activa'],
|
||||
orderBy: 'severidad DESC, timestamp DESC',
|
||||
);
|
||||
return rows.map(Alarma.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Alarma>> getByZona(int zonaId) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'alarmas',
|
||||
where: 'zona_id = ?',
|
||||
whereArgs: [zonaId],
|
||||
orderBy: 'timestamp DESC',
|
||||
);
|
||||
return rows.map(Alarma.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Alarma?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('alarmas', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Alarma.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Alarma a) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('alarmas', a.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(Alarma a) async {
|
||||
final db = await _db.db;
|
||||
return db.update('alarmas', a.toMap(), where: 'id = ?', whereArgs: [a.id]);
|
||||
}
|
||||
|
||||
Future<int> resolver(int id, int usuarioId, {String? notas}) async {
|
||||
final db = await _db.db;
|
||||
return db.update(
|
||||
'alarmas',
|
||||
{
|
||||
'estado': 'resuelta',
|
||||
'resuelto_por': usuarioId,
|
||||
'notas': notas,
|
||||
'sincronizado': 0,
|
||||
},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> silenciar(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.update(
|
||||
'alarmas',
|
||||
{'estado': 'silenciada', 'sincronizado': 0},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('alarmas', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Alarma>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('alarmas', where: 'sincronizado = 0');
|
||||
return rows.map(Alarma.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<int> contarActivas() async {
|
||||
final db = await _db.db;
|
||||
final result = await db.rawQuery(
|
||||
"SELECT COUNT(*) as c FROM alarmas WHERE estado = 'activa'",
|
||||
);
|
||||
return result.first['c'] as int;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/banner_item.dart';
|
||||
|
||||
class BannersDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<BannerItem>> getActivos() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'banners',
|
||||
where: 'activo = 1',
|
||||
orderBy: 'orden ASC',
|
||||
);
|
||||
return rows.map(BannerItem.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<BannerItem?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('banners', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : BannerItem.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(BannerItem b) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('banners', b.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(BannerItem b) async {
|
||||
final db = await _db.db;
|
||||
return db.update('banners', b.toMap(), where: 'id = ?', whereArgs: [b.id]);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('banners', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<BannerItem>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('banners', where: 'sincronizado = 0');
|
||||
return rows.map(BannerItem.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import '../database_helper.dart';
|
||||
|
||||
class ConfiguracionDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<String?> get(String clave) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'configuracion',
|
||||
where: 'clave = ?',
|
||||
whereArgs: [clave],
|
||||
);
|
||||
return rows.isEmpty ? null : rows.first['valor'] as String?;
|
||||
}
|
||||
|
||||
Future<void> set(String clave, String valor) async {
|
||||
final db = await _db.db;
|
||||
await db.insert(
|
||||
'configuracion',
|
||||
{'clave': clave, 'valor': valor},
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> delete(String clave) async {
|
||||
final db = await _db.db;
|
||||
await db.delete('configuracion', where: 'clave = ?', whereArgs: [clave]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/evento.dart';
|
||||
|
||||
class EventosDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Evento>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('eventos', orderBy: 'timestamp DESC');
|
||||
return rows.map(Evento.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Evento>> getRecientes({int limit = 10}) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'eventos',
|
||||
orderBy: 'timestamp DESC',
|
||||
limit: limit,
|
||||
);
|
||||
return rows.map(Evento.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Evento?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('eventos', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Evento.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Evento e) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('eventos', e.toMap());
|
||||
}
|
||||
|
||||
Future<int> marcarLeido(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.update(
|
||||
'eventos',
|
||||
{'leido': 1, 'sincronizado': 0},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('eventos', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Evento>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('eventos', where: 'sincronizado = 0');
|
||||
return rows.map(Evento.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<int> contarNoLeidos() async {
|
||||
final db = await _db.db;
|
||||
final result = await db.rawQuery(
|
||||
'SELECT COUNT(*) as c FROM eventos WHERE leido = 0',
|
||||
);
|
||||
return result.first['c'] as int;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/mensaje.dart';
|
||||
|
||||
class MensajesDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Mensaje>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('mensajes', orderBy: 'timestamp ASC');
|
||||
return rows.map(Mensaje.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<int> insert(Mensaje m) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('mensajes', m.toMap());
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('mensajes', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Mensaje>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('mensajes', where: 'sincronizado = 0');
|
||||
return rows.map(Mensaje.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/nodo.dart';
|
||||
|
||||
class NodosDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Nodo>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('nodos', orderBy: 'nombre ASC');
|
||||
return rows.map(Nodo.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<List<Nodo>> getByZona(int zonaId) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('nodos', where: 'zona_id = ?', whereArgs: [zonaId]);
|
||||
return rows.map(Nodo.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Nodo?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('nodos', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Nodo.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Nodo n) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('nodos', n.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(Nodo n) async {
|
||||
final db = await _db.db;
|
||||
return db.update('nodos', n.toMap(), where: 'id = ?', whereArgs: [n.id]);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('nodos', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Nodo>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('nodos', where: 'sincronizado = 0');
|
||||
return rows.map(Nodo.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/puerta.dart';
|
||||
|
||||
class PuertasDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Puerta>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('puertas', orderBy: 'nombre ASC');
|
||||
return rows.map(Puerta.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Puerta?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('puertas', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Puerta.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Puerta p) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('puertas', p.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(Puerta p) async {
|
||||
final db = await _db.db;
|
||||
return db.update('puertas', p.toMap(), where: 'id = ?', whereArgs: [p.id]);
|
||||
}
|
||||
|
||||
Future<int> setBloqueada(int id, bool bloqueada) async {
|
||||
final db = await _db.db;
|
||||
return db.update(
|
||||
'puertas',
|
||||
{'bloqueada': bloqueada ? 1 : 0, 'sincronizado': 0},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('puertas', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Puerta>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('puertas', where: 'sincronizado = 0');
|
||||
return rows.map(Puerta.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/sesion.dart';
|
||||
|
||||
class SesionesDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Sesion>> getByUsuario(int usuarioId) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'sesiones',
|
||||
where: 'usuario_id = ?',
|
||||
whereArgs: [usuarioId],
|
||||
orderBy: 'timestamp_inicio DESC',
|
||||
);
|
||||
return rows.map(Sesion.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Sesion?> getSesionActiva(int usuarioId) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'sesiones',
|
||||
where: 'usuario_id = ? AND timestamp_fin IS NULL',
|
||||
whereArgs: [usuarioId],
|
||||
limit: 1,
|
||||
);
|
||||
return rows.isEmpty ? null : Sesion.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> iniciar(int usuarioId, {String? dispositivo}) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('sesiones', {
|
||||
'usuario_id': usuarioId,
|
||||
'timestamp_inicio': DateTime.now().toIso8601String(),
|
||||
'dispositivo': dispositivo,
|
||||
});
|
||||
}
|
||||
|
||||
Future<int> cerrar(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.update(
|
||||
'sesiones',
|
||||
{'timestamp_fin': DateTime.now().toIso8601String()},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../database_helper.dart';
|
||||
|
||||
class SyncLogDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<int> registrar({
|
||||
required String tipo,
|
||||
required String estado,
|
||||
String? detalle,
|
||||
}) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('sync_log', {
|
||||
'timestamp': DateTime.now().toIso8601String(),
|
||||
'tipo': tipo,
|
||||
'estado': estado,
|
||||
'detalle': detalle,
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getRecientes({int limit = 20}) async {
|
||||
final db = await _db.db;
|
||||
return db.query(
|
||||
'sync_log',
|
||||
orderBy: 'timestamp DESC',
|
||||
limit: limit,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/usuario.dart';
|
||||
|
||||
class UsuariosDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Usuario>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('usuarios', orderBy: 'nombre ASC');
|
||||
return rows.map(Usuario.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Usuario?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('usuarios', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Usuario.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<Usuario?> getByPinHash(String pinHash) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query(
|
||||
'usuarios',
|
||||
where: 'pin_hash = ? AND activo = 1',
|
||||
whereArgs: [pinHash],
|
||||
);
|
||||
return rows.isEmpty ? null : Usuario.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Usuario u) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('usuarios', u.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(Usuario u) async {
|
||||
final db = await _db.db;
|
||||
return db.update('usuarios', u.toMap(), where: 'id = ?', whereArgs: [u.id]);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('usuarios', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Usuario>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('usuarios', where: 'sincronizado = 0');
|
||||
return rows.map(Usuario.fromMap).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import '../database_helper.dart';
|
||||
import '../../models/zona.dart';
|
||||
|
||||
class ZonasDao {
|
||||
final _db = DatabaseHelper.instance;
|
||||
|
||||
Future<List<Zona>> getAll() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('zonas', orderBy: 'codigo ASC');
|
||||
return rows.map(Zona.fromMap).toList();
|
||||
}
|
||||
|
||||
Future<Zona?> getById(int id) async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('zonas', where: 'id = ?', whereArgs: [id]);
|
||||
return rows.isEmpty ? null : Zona.fromMap(rows.first);
|
||||
}
|
||||
|
||||
Future<int> insert(Zona z) async {
|
||||
final db = await _db.db;
|
||||
return db.insert('zonas', z.toMap());
|
||||
}
|
||||
|
||||
Future<int> update(Zona z) async {
|
||||
final db = await _db.db;
|
||||
return db.update('zonas', z.toMap(), where: 'id = ?', whereArgs: [z.id]);
|
||||
}
|
||||
|
||||
Future<int> delete(int id) async {
|
||||
final db = await _db.db;
|
||||
return db.delete('zonas', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<List<Zona>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('zonas', where: 'sincronizado = 0');
|
||||
return rows.map(Zona.fromMap).toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user