feat(stations): add quality filters and list ordering
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import '../modelos/emisora.dart';
|
||||
@@ -19,12 +21,14 @@ class ServicioFavoritos {
|
||||
|
||||
Future<Database> _initDb() async {
|
||||
final dbPath = await getDatabasesPath();
|
||||
await Directory(dbPath).create(recursive: true);
|
||||
final path = join(dbPath, _dbName);
|
||||
return openDatabase(
|
||||
path,
|
||||
version: _dbVersion,
|
||||
onCreate: _onCreate,
|
||||
onUpgrade: _onUpgrade,
|
||||
onOpen: _asegurarEsquema,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -50,14 +54,43 @@ class ServicioFavoritos {
|
||||
}
|
||||
|
||||
Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
|
||||
if (oldVersion < 2) {
|
||||
// v1→v2: añadir columnas de la Radio Browser API
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN codigo_pais TEXT');
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN codec TEXT');
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN bitrate INTEGER');
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN votes INTEGER NOT NULL DEFAULT 0');
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN clickcount INTEGER NOT NULL DEFAULT 0');
|
||||
await _asegurarEsquema(db);
|
||||
}
|
||||
|
||||
Future<void> _asegurarEsquema(Database db) async {
|
||||
final tablas = await db.rawQuery(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'favoritos'",
|
||||
);
|
||||
if (tablas.isEmpty) {
|
||||
await _onCreate(db, _dbVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
final columnas = await _columnas(db, 'favoritos');
|
||||
Future<void> addColumn(String nombre, String sql) async {
|
||||
if (!columnas.contains(nombre)) {
|
||||
await db.execute('ALTER TABLE favoritos ADD COLUMN $nombre $sql');
|
||||
columnas.add(nombre);
|
||||
}
|
||||
}
|
||||
|
||||
// Migración defensiva: algunas instalaciones antiguas pueden venir de
|
||||
// esquemas intermedios. No asumimos qué columna existe: la verificamos.
|
||||
await addColumn('favicon', 'TEXT');
|
||||
await addColumn('pais', 'TEXT');
|
||||
await addColumn('codigo_pais', 'TEXT');
|
||||
await addColumn('idioma', 'TEXT');
|
||||
await addColumn('tags', 'TEXT');
|
||||
await addColumn('codec', 'TEXT');
|
||||
await addColumn('bitrate', 'INTEGER');
|
||||
await addColumn('votes', 'INTEGER NOT NULL DEFAULT 0');
|
||||
await addColumn('clickcount', 'INTEGER NOT NULL DEFAULT 0');
|
||||
await addColumn('orden', 'INTEGER NOT NULL DEFAULT 0');
|
||||
}
|
||||
|
||||
Future<Set<String>> _columnas(Database db, String tabla) async {
|
||||
final info = await db.rawQuery('PRAGMA table_info($tabla)');
|
||||
return info.map((row) => row['name'] as String).toSet();
|
||||
}
|
||||
|
||||
/// Devuelve todas las emisoras favoritas ordenadas por [orden].
|
||||
|
||||
@@ -100,11 +100,13 @@ class ServicioGrabacionRadio {
|
||||
Timer? _timerAutoStop;
|
||||
String? _directorioConfigurado;
|
||||
int _maxBytes = maxBytesPorDefecto;
|
||||
File? _ultimoArchivo;
|
||||
|
||||
EstadoGrabacionRadio get estado => _estado;
|
||||
Stream<EstadoGrabacionRadio> get estadoStream => _estadoController.stream;
|
||||
String? get directorioConfigurado => _directorioConfigurado;
|
||||
int get maxBytes => _maxBytes;
|
||||
File? get ultimoArchivo => _ultimoArchivo;
|
||||
|
||||
Future<void> inicializar() async {
|
||||
try {
|
||||
@@ -244,6 +246,7 @@ class ServicioGrabacionRadio {
|
||||
}
|
||||
|
||||
Future<void> _finalizar() async {
|
||||
final archivoFinalizado = _estado.archivo;
|
||||
_timerAutoStop?.cancel();
|
||||
_timerAutoStop = null;
|
||||
await _subscripcionStream?.cancel();
|
||||
@@ -255,6 +258,9 @@ class ServicioGrabacionRadio {
|
||||
_clienteActivo?.close();
|
||||
}
|
||||
_clienteActivo = null;
|
||||
if (archivoFinalizado != null) {
|
||||
_ultimoArchivo = archivoFinalizado;
|
||||
}
|
||||
_emitir(const EstadoGrabacionRadio.inactiva());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user