78 lines
2.4 KiB
Dart
78 lines
2.4 KiB
Dart
import '../../../api/connect/rmv/rmv_models.dart';
|
|
import '../../../state/app/modules/settings/bloc/settings_cubit.dart';
|
|
import '../../../storage/rmv_settings.dart';
|
|
|
|
/// Thin wrapper around the global [SettingsCubit] that keeps the RMV
|
|
/// favorites/recents bookkeeping out of every view. All mutations go through
|
|
/// here so that the cubit's write/emit cycle and the recent-list trimming
|
|
/// stay consistent.
|
|
class RmvFavoritesController {
|
|
final SettingsCubit _settings;
|
|
|
|
RmvFavoritesController(this._settings);
|
|
|
|
RmvSettings get _rmv => _settings.val().rmvSettings;
|
|
|
|
bool isFavorite(StopLocation stop) =>
|
|
_rmv.favoriteStations.any((s) => s.id == stop.id);
|
|
|
|
void toggleFavorite(StopLocation stop) {
|
|
if (isFavorite(stop)) {
|
|
removeFavorite(stop);
|
|
} else {
|
|
addFavorite(stop);
|
|
}
|
|
}
|
|
|
|
void addFavorite(StopLocation stop) {
|
|
final mutable = _settings.val(write: true).rmvSettings;
|
|
if (mutable.favoriteStations.any((s) => s.id == stop.id)) return;
|
|
mutable.favoriteStations = [...mutable.favoriteStations, stop];
|
|
}
|
|
|
|
void removeFavorite(StopLocation stop) {
|
|
final mutable = _settings.val(write: true).rmvSettings;
|
|
mutable.favoriteStations = mutable.favoriteStations
|
|
.where((s) => s.id != stop.id)
|
|
.toList();
|
|
}
|
|
|
|
void addRecent(StopLocation stop) {
|
|
final mutable = _settings.val(write: true).rmvSettings;
|
|
final filtered =
|
|
mutable.recentStations.where((s) => s.id != stop.id).toList();
|
|
filtered.insert(0, stop);
|
|
if (filtered.length > RmvSettings.maxRecents) {
|
|
filtered.removeRange(RmvSettings.maxRecents, filtered.length);
|
|
}
|
|
mutable.recentStations = filtered;
|
|
}
|
|
|
|
void clearRecents() {
|
|
_settings.val(write: true).rmvSettings.recentStations = const [];
|
|
}
|
|
|
|
void addRecentTrip(StopLocation from, StopLocation to) {
|
|
final mutable = _settings.val(write: true).rmvSettings;
|
|
final filtered = mutable.recentTripQueries
|
|
.where((q) => q.from.id != from.id || q.to.id != to.id)
|
|
.toList();
|
|
filtered.insert(
|
|
0,
|
|
RecentTripQuery(
|
|
from: from,
|
|
to: to,
|
|
timestampMs: DateTime.now().millisecondsSinceEpoch,
|
|
),
|
|
);
|
|
if (filtered.length > RmvSettings.maxRecents) {
|
|
filtered.removeRange(RmvSettings.maxRecents, filtered.length);
|
|
}
|
|
mutable.recentTripQueries = filtered;
|
|
}
|
|
|
|
void clearRecentTrips() {
|
|
_settings.val(write: true).rmvSettings.recentTripQueries = const [];
|
|
}
|
|
}
|