- 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>
266 lines
9.5 KiB
Dart
266 lines
9.5 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import '../theme.dart';
|
|
import '../db/database_helper.dart';
|
|
import '../services/sincronizador.dart';
|
|
|
|
class SettingsPage extends StatefulWidget {
|
|
const SettingsPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SettingsPage> createState() => _SettingsPageState();
|
|
}
|
|
|
|
class _SettingsPageState extends State<SettingsPage> {
|
|
bool _notificaciones = true;
|
|
bool _sonido = true;
|
|
bool _vibracion = true;
|
|
|
|
bool _iniciandoBd = false;
|
|
bool _sincronizando = false;
|
|
|
|
// ── Build ──────────────────────────────────────────────────────────────────
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: AppColors.surfaceLowest,
|
|
appBar: AppBar(
|
|
backgroundColor: AppColors.surfaceLow,
|
|
elevation: 0,
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back_ios_new,
|
|
color: AppColors.onSurface, size: 18),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
title: Text('CONFIGURACIÓN',
|
|
style: appHeadline(size: 16, weight: FontWeight.w800,
|
|
color: AppColors.primary, spacing: 2.0)),
|
|
),
|
|
body: ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
children: [
|
|
// ── Notificaciones ───────────────────────────────────────────────
|
|
_sectionHeader('NOTIFICACIONES'),
|
|
_switchItem(
|
|
icon: Icons.notifications_active,
|
|
titulo: 'Notificaciones',
|
|
subtitulo: 'Activar alertas del sistema',
|
|
value: _notificaciones,
|
|
onChanged: (v) => setState(() => _notificaciones = v),
|
|
),
|
|
_divider(),
|
|
_switchItem(
|
|
icon: Icons.volume_up,
|
|
titulo: 'Sonido',
|
|
subtitulo: 'Reproducir sonido al activar alarma',
|
|
value: _sonido,
|
|
onChanged: (v) => setState(() => _sonido = v),
|
|
),
|
|
_divider(),
|
|
_switchItem(
|
|
icon: Icons.vibration,
|
|
titulo: 'Vibración',
|
|
subtitulo: 'Vibrar al activar alarma',
|
|
value: _vibracion,
|
|
onChanged: (v) => setState(() => _vibracion = v),
|
|
),
|
|
|
|
// ── Sistema ──────────────────────────────────────────────────────
|
|
_sectionHeader('SISTEMA'),
|
|
_accionItem(
|
|
icon: Icons.storage,
|
|
iconColor: AppColors.secondary,
|
|
titulo: 'Inicializar base de datos',
|
|
subtitulo: 'Borra y recrea la BD con datos de seed',
|
|
cargando: _iniciandoBd,
|
|
onTap: _confirmarInicializarBd,
|
|
),
|
|
_divider(),
|
|
_accionItem(
|
|
icon: Icons.sync,
|
|
iconColor: AppColors.primary,
|
|
titulo: 'Inicializar sincronización',
|
|
subtitulo: 'Sincroniza datos locales con el servidor',
|
|
cargando: _sincronizando,
|
|
onTap: _iniciarSincronizacion,
|
|
),
|
|
|
|
const SizedBox(height: 32),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Inicializar BD ─────────────────────────────────────────────────────────
|
|
Future<void> _confirmarInicializarBd() async {
|
|
final confirmar = await showDialog<bool>(
|
|
context: context,
|
|
builder: (_) => AlertDialog(
|
|
backgroundColor: AppColors.surfaceHigh,
|
|
title: Text('Inicializar base de datos',
|
|
style: appHeadline(size: 16, weight: FontWeight.w700)),
|
|
content: Text(
|
|
'Se borrarán todos los datos locales y se recreará la base de datos '
|
|
'con los datos de seed.\n\nEsta acción no se puede deshacer.',
|
|
style: appLabel(size: 13),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: Text('Cancelar',
|
|
style: appLabel(size: 13,
|
|
color: AppColors.onSurfaceVariant)),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text('Inicializar',
|
|
style: appLabel(size: 13,
|
|
weight: FontWeight.bold, color: AppColors.error)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmar != true) return;
|
|
setState(() => _iniciandoBd = true);
|
|
|
|
try {
|
|
// Borra la BD existente
|
|
if (Platform.isLinux || Platform.isWindows) {
|
|
final dbPath = p.join(Directory.current.path, 'sentinel_one.db');
|
|
await databaseFactoryFfi.deleteDatabase(dbPath);
|
|
} else {
|
|
final dbPath = p.join(await getDatabasesPath(), 'sentinel_one.db');
|
|
await deleteDatabase(dbPath);
|
|
}
|
|
|
|
// Fuerza la reinicialización en el próximo acceso
|
|
DatabaseHelper.instance.resetInstance();
|
|
|
|
// Abre la BD para ejecutar el seed inmediatamente
|
|
await DatabaseHelper.instance.db;
|
|
|
|
if (!mounted) return;
|
|
_snack('Base de datos inicializada correctamente', esError: false);
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
_snack('Error al inicializar: $e');
|
|
} finally {
|
|
if (mounted) setState(() => _iniciandoBd = false);
|
|
}
|
|
}
|
|
|
|
// ── Sincronización ─────────────────────────────────────────────────────────
|
|
Future<void> _iniciarSincronizacion() async {
|
|
setState(() => _sincronizando = true);
|
|
try {
|
|
await Sincronizador.instance.sincronizar();
|
|
if (!mounted) return;
|
|
_snack('Sincronización completada', esError: false);
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
_snack('Error en sincronización: $e');
|
|
} finally {
|
|
if (mounted) setState(() => _sincronizando = false);
|
|
}
|
|
}
|
|
|
|
// ── Helpers de UI ──────────────────────────────────────────────────────────
|
|
void _snack(String msg, {bool esError = true}) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(msg),
|
|
backgroundColor: esError ? AppColors.error : AppColors.primary,
|
|
));
|
|
}
|
|
|
|
Widget _sectionHeader(String label) => Padding(
|
|
padding: const EdgeInsets.fromLTRB(24, 24, 24, 8),
|
|
child: Text(label,
|
|
style: appLabel(size: 10, weight: FontWeight.bold,
|
|
color: AppColors.onSurfaceVariant, spacing: 2.0)),
|
|
);
|
|
|
|
Widget _divider() => const Divider(
|
|
color: AppColors.outlineVariant, height: 1,
|
|
indent: 24, endIndent: 24);
|
|
|
|
Widget _switchItem({
|
|
required IconData icon,
|
|
required String titulo,
|
|
required String subtitulo,
|
|
required bool value,
|
|
required ValueChanged<bool> onChanged,
|
|
}) =>
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 4),
|
|
child: Row(children: [
|
|
Container(
|
|
width: 40, height: 40,
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceHighest,
|
|
borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(icon, color: AppColors.primary, size: 20),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(titulo, style: appHeadline(size: 14)),
|
|
Text(subtitulo, style: appLabel(size: 12)),
|
|
],
|
|
)),
|
|
Switch(
|
|
value: value,
|
|
onChanged: onChanged,
|
|
activeColor: AppColors.primary,
|
|
activeTrackColor: AppColors.primaryContainer,
|
|
inactiveThumbColor: AppColors.outlineVariant,
|
|
inactiveTrackColor: AppColors.surfaceHighest,
|
|
),
|
|
]),
|
|
);
|
|
|
|
Widget _accionItem({
|
|
required IconData icon,
|
|
required Color iconColor,
|
|
required String titulo,
|
|
required String subtitulo,
|
|
required bool cargando,
|
|
required VoidCallback onTap,
|
|
}) =>
|
|
InkWell(
|
|
onTap: cargando ? null : onTap,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
|
|
child: Row(children: [
|
|
Container(
|
|
width: 40, height: 40,
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceHighest,
|
|
borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(icon, color: iconColor, size: 20),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(titulo, style: appHeadline(size: 14)),
|
|
Text(subtitulo, style: appLabel(size: 12)),
|
|
],
|
|
)),
|
|
if (cargando)
|
|
const SizedBox(
|
|
width: 20, height: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2, color: AppColors.primary))
|
|
else
|
|
const Icon(Icons.chevron_right,
|
|
color: AppColors.outlineVariant, size: 20),
|
|
]),
|
|
),
|
|
);
|
|
}
|