66 lines
2.2 KiB
Dart
66 lines
2.2 KiB
Dart
import '../modelos/emisora.dart';
|
|
|
|
/// User-selectable ordering for every station list in the app.
|
|
///
|
|
/// WU6 adds [popularidad] to the Buscar "Ordenar" control (design ADR-4),
|
|
/// backed by fields the model already carries (`votes`, `clickcount`) —
|
|
/// no new API surface, no server-side `order` parameter.
|
|
enum OrdenEmisoras { nombre, calidad, popularidad }
|
|
|
|
/// Returns a sorted COPY of [emisoras] according to [orden].
|
|
List<Emisora> ordenarEmisoras(List<Emisora> emisoras, OrdenEmisoras orden) {
|
|
final ordenadas = List<Emisora>.from(emisoras);
|
|
switch (orden) {
|
|
case OrdenEmisoras.nombre:
|
|
ordenadas.sort(
|
|
(a, b) => a.nombre.toLowerCase().compareTo(b.nombre.toLowerCase()),
|
|
);
|
|
case OrdenEmisoras.calidad:
|
|
ordenadas.sort((a, b) {
|
|
final porBitrate = (b.bitrate ?? 0).compareTo(a.bitrate ?? 0);
|
|
if (porBitrate != 0) return porBitrate;
|
|
return 0;
|
|
});
|
|
case OrdenEmisoras.popularidad:
|
|
ordenadas.sort((a, b) {
|
|
final porVotos = b.votes.compareTo(a.votes);
|
|
if (porVotos != 0) return porVotos;
|
|
return b.clickcount.compareTo(a.clickcount);
|
|
});
|
|
}
|
|
return ordenadas;
|
|
}
|
|
|
|
/// Identity-memoized derived list (S4-R5).
|
|
///
|
|
/// Derived-list getters used to return a fresh copy on every read, which made
|
|
/// `context.select` rebuild on EVERY notification (lists compare by identity).
|
|
/// This memo recomputes only when one of the source [claves] changes identity,
|
|
/// so unrelated notifications (e.g. audio buffer events) stop rebuilding the
|
|
/// screens that select these lists.
|
|
class MemoLista<T> {
|
|
List<Object?>? _claves;
|
|
List<T>? _resultado;
|
|
|
|
List<T> obtener(List<Object?> claves, List<T> Function() calcular) {
|
|
final anteriores = _claves;
|
|
final resultado = _resultado;
|
|
if (anteriores != null &&
|
|
resultado != null &&
|
|
anteriores.length == claves.length) {
|
|
var iguales = true;
|
|
for (var i = 0; i < claves.length; i++) {
|
|
if (!identical(anteriores[i], claves[i])) {
|
|
iguales = false;
|
|
break;
|
|
}
|
|
}
|
|
if (iguales) return resultado;
|
|
}
|
|
final nuevo = calcular();
|
|
_claves = List<Object?>.of(claves);
|
|
_resultado = nuevo;
|
|
return nuevo;
|
|
}
|
|
}
|