Files
pluriwave/lib/servicios/servicio_presets_personalizados.dart
T
FreeTLab c9fe0ad651 feat(eq): restyle equalizer screen and add custom presets
Restyle the Ecualizador settings screen to the new visual language while
keeping the equalizer at 5 bands (spike-resolved, Engram id 2498 - band
count is device-reported via just_audio's AndroidEqualizer, not app-chosen;
the approved mockup's 7 sliders would silently no-op on typical hardware).

- Restyle EcualizadorWidget in place: strip its internal title + preset
  chip row (the pushed screen's header now carries the title), add a
  habilitado parameter that greys/disables every slider when EQ is off.
  Widen PresetsEcualizadorWidget additively (personalizados param) so
  custom presets can join the chip row without a second implementation.
- Add servicio_presets_personalizados.dart (new file, own SharedPreferences
  key eq_custom_presets_v1) for custom EQ preset persistence - kept out of
  servicio_ecualizador.dart, which has an empty-git-diff success criterion
  for this change. preset_ecualizador.dart is unchanged: a custom preset is
  just a PresetEcualizador with a user-supplied name.
- Extend EstadoEcualizador with presetsPersonalizados,
  guardarPresetPersonalizado (validates non-empty name),
  eliminarPresetPersonalizado. The load is a new explicit
  cargarPresetsPersonalizados(), deliberately NOT folded into
  cargarPersistido(): that method is exercised ~30 times by
  estado_ecualizador_test.dart (protected, must stay unmodified) via Fakes
  only, with no SharedPreferences awareness in that file.
- Build out the Ecualizador screen body: base-vs-per-station explainer
  banner, a "Salida activa" row surfaced on the main screen (previously
  Advanced-only), an "Emisoras con ajuste propio" drill-down sourced from
  the existing presetsPorEmisora map, and a "Guardar como preset" action.

New coverage lives in new files rather than touching the three protected
EQ test files: ecualizador_widget_test.dart (component-level, did not
exist before this commit), servicio_presets_personalizados_test.dart, and
estado_ecualizador_presets_personalizados_test.dart. servicio_ecualizador.dart,
servicio_audio.dart and the three protected EQ test files keep an empty
git diff. Full suite: 713/713 green (2 skipped, unchanged), up from 682.

size:exception - realized 1,954 changed lines (25 files, plus this docs
update) against the 400-550 forecast: lib/ + ARB alone is ~650 lines, near
the top of the forecast band by itself since this WU also had to build out
a screen body WU3a only stubbed; the rest is 4 test files (675 lines) and
11 new ARB keys regenerating 13 lib/l10n/gen files (~546 lines) - the same
pattern every prior work unit in this branch has hit. Not splittable: WU14
reuses this unit's editor component by exact runtime type and cannot begin
until this lands as a whole.
2026-07-29 12:53:12 +02:00

89 lines
3.4 KiB
Dart

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../modelos/preset_ecualizador.dart';
import 'persistencia_tolerante.dart';
/// Persistence for USER-NAMED custom EQ presets (design ADR-5 hazard box,
/// `eq-custom-presets` spec — "Custom Preset Save").
///
/// Deliberately its OWN file and OWN SharedPreferences key
/// (`eq_custom_presets_v1`), never folded into [ServicioEcualizador]: that
/// file has an empty-`git diff` success criterion for this change, so
/// nothing custom-preset-related may be added there. A custom preset is
/// just a [PresetEcualizador] with a user-supplied `nombre` — the model
/// itself needs no change (`toJson`/`desdeJson` already exist).
class ServicioPresetsPersonalizados {
ServicioPresetsPersonalizados({SharedPreferences? prefs}) : _prefs = prefs;
static const _keyPresetsPersonalizados = 'eq_custom_presets_v1';
final SharedPreferences? _prefs;
Future<SharedPreferences> _resolverPrefs() async =>
_prefs ?? SharedPreferences.getInstance();
/// Returns every saved custom preset, in save order.
Future<List<PresetEcualizador>> listar() async {
final prefs = await _resolverPrefs();
return _leer(prefs);
}
/// Saves [preset] under its own name. A preset already saved under the
/// same name is replaced (last write wins) rather than duplicated.
Future<void> guardar(PresetEcualizador preset) async {
final prefs = await _resolverPrefs();
final actuales =
_leer(prefs)
..removeWhere((p) => p.nombre == preset.nombre)
..add(preset);
await _guardarTodos(prefs, actuales);
}
/// Removes the custom preset named [nombre], if present. A safe no-op
/// when no preset with that name exists.
Future<void> eliminar(String nombre) async {
final prefs = await _resolverPrefs();
final actuales = _leer(prefs)..removeWhere((p) => p.nombre == nombre);
await _guardarTodos(prefs, actuales);
}
/// Reads the persisted list with per-entry tolerance
/// (persistence-resilience D1/D6, same shared helper `ServicioEcualizador`
/// uses): an entry that fails to parse is skipped and logged, its
/// siblings survive. A top-level decode failure degrades to an empty
/// list (also logged) — custom presets are explicit-only user writes and
/// trivially re-creatable, so there is no flag/quarantine here, matching
/// `ServicioEcualizador`'s own documented D6 asymmetry vs. Alarms/Stations.
List<PresetEcualizador> _leer(SharedPreferences prefs) {
final raw = prefs.getString(_keyPresetsPersonalizados);
if (raw == null || raw.isEmpty) return [];
try {
final data = jsonDecode(raw) as List<dynamic>;
final resultado = parseListaTolerante<PresetEcualizador>(
data,
(entrada) => PresetEcualizador.desdeJson(entrada),
subsistema: 'presets_personalizados',
coleccion: _keyPresetsPersonalizados,
);
return resultado.validas;
} catch (e) {
registrarSaltoPersistencia(
subsistema: 'presets_personalizados',
detalle: _keyPresetsPersonalizados,
razon: e.toString(),
);
return [];
}
}
Future<void> _guardarTodos(
SharedPreferences prefs,
List<PresetEcualizador> presets,
) async {
final serializado = presets.map((p) => p.toJson()).toList();
await prefs.setString(_keyPresetsPersonalizados, jsonEncode(serializado));
}
}