Resolve merge conflicts: README + ignore IDE files
This commit is contained in:
159
lib/services/local_seed_repository.dart
Normal file
159
lib/services/local_seed_repository.dart
Normal file
@@ -0,0 +1,159 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
import '../models/colony_route.dart';
|
||||
import '../models/calendar_event_entry.dart';
|
||||
import '../models/demo_profile.dart';
|
||||
import '../models/route_guide_entry.dart';
|
||||
import '../models/route_notification.dart';
|
||||
import '../models/truck_route.dart';
|
||||
|
||||
class LocalSeedRepository {
|
||||
LocalSeedRepository._();
|
||||
|
||||
static final LocalSeedRepository instance = LocalSeedRepository._();
|
||||
Future<LocalSeedData>? _cachedLoad;
|
||||
|
||||
Future<LocalSeedData> load() {
|
||||
return _cachedLoad ??= _loadInternal();
|
||||
}
|
||||
|
||||
Future<LocalSeedData> _loadInternal() async {
|
||||
try {
|
||||
final routesJson = await rootBundle.loadString('assets/json/rutas.json');
|
||||
final notificationsJson = await rootBundle.loadString('assets/json/notificaciones.json');
|
||||
final colonyRoutesJson = await rootBundle.loadString('assets/json/colonias-rutas.json');
|
||||
final profilesJson = await rootBundle.loadString('assets/json/perfiles.json');
|
||||
final calendarEventsJson = await rootBundle.loadString('assets/json/calendario.json');
|
||||
final routeGuidesJson = await rootBundle.loadString('assets/json/guia-rutas.json');
|
||||
|
||||
final routes = _decodeList(routesJson).map(TruckRoute.fromJson).toList(growable: false);
|
||||
final notifications = _decodeList(notificationsJson).map(RouteNotification.fromJson).toList(growable: false);
|
||||
final colonyRoutes = _decodeList(colonyRoutesJson).map(ColonyRoute.fromJson).toList(growable: false);
|
||||
final profiles = _decodeList(profilesJson).map(DemoProfile.fromJson).toList(growable: false);
|
||||
final calendarEvents = _decodeList(calendarEventsJson).map(CalendarEventEntry.fromJson).toList(growable: false);
|
||||
final routeGuides = _decodeList(routeGuidesJson).map(RouteGuideEntry.fromJson).toList(growable: false);
|
||||
|
||||
return LocalSeedData(
|
||||
routes: routes,
|
||||
notifications: notifications,
|
||||
colonyRoutes: colonyRoutes,
|
||||
demoProfiles: profiles,
|
||||
calendarEvents: calendarEvents,
|
||||
routeGuides: routeGuides,
|
||||
);
|
||||
} catch (_) {
|
||||
return LocalSeedData.empty();
|
||||
}
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> _decodeList(String responseBody) {
|
||||
final decoded = jsonDecode(responseBody);
|
||||
if (decoded is! List) {
|
||||
return <Map<String, dynamic>>[];
|
||||
}
|
||||
|
||||
return decoded.whereType<Map<String, dynamic>>().toList(growable: false);
|
||||
}
|
||||
}
|
||||
|
||||
class LocalSeedData {
|
||||
const LocalSeedData({
|
||||
required this.routes,
|
||||
required this.notifications,
|
||||
required this.colonyRoutes,
|
||||
required this.demoProfiles,
|
||||
required this.calendarEvents,
|
||||
required this.routeGuides,
|
||||
});
|
||||
|
||||
final List<TruckRoute> routes;
|
||||
final List<RouteNotification> notifications;
|
||||
final List<ColonyRoute> colonyRoutes;
|
||||
final List<DemoProfile> demoProfiles;
|
||||
final List<CalendarEventEntry> calendarEvents;
|
||||
final List<RouteGuideEntry> routeGuides;
|
||||
|
||||
const LocalSeedData.empty()
|
||||
: routes = const <TruckRoute>[],
|
||||
notifications = const <RouteNotification>[],
|
||||
colonyRoutes = const <ColonyRoute>[],
|
||||
demoProfiles = const <DemoProfile>[],
|
||||
calendarEvents = const <CalendarEventEntry>[],
|
||||
routeGuides = const <RouteGuideEntry>[];
|
||||
|
||||
TruckRoute? get defaultRoute => routes.isEmpty ? null : routes.first;
|
||||
|
||||
TruckRoute? routeForColonia(String? colonia) {
|
||||
if (colonia == null || colonia.trim().isEmpty) {
|
||||
return defaultRoute;
|
||||
}
|
||||
|
||||
final routeId = colonyRouteForColonia(colonia)?.routeId;
|
||||
if (routeId == null) {
|
||||
return defaultRoute;
|
||||
}
|
||||
|
||||
return routeById(routeId) ?? defaultRoute;
|
||||
}
|
||||
|
||||
ColonyRoute? colonyRouteForColonia(String colonia) {
|
||||
final normalized = colonia.trim().toLowerCase();
|
||||
for (final item in colonyRoutes) {
|
||||
if (item.colonia.trim().toLowerCase() == normalized) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
TruckRoute? routeById(String routeId) {
|
||||
for (final route in routes) {
|
||||
if (route.routeId == routeId) {
|
||||
return route;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
DemoProfile? profileForCredentials(String email, String password) {
|
||||
final normalizedEmail = email.trim().toLowerCase();
|
||||
for (final profile in demoProfiles) {
|
||||
if (profile.email.trim().toLowerCase() == normalizedEmail && profile.password == password) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
DemoProfile? profileForColonia(String? colonia) {
|
||||
if (colonia == null || colonia.trim().isEmpty) {
|
||||
return demoProfiles.isEmpty ? null : demoProfiles.first;
|
||||
}
|
||||
|
||||
final normalized = colonia.trim().toLowerCase();
|
||||
for (final profile in demoProfiles) {
|
||||
if (profile.colonia.trim().toLowerCase() == normalized) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
return demoProfiles.isEmpty ? null : demoProfiles.first;
|
||||
}
|
||||
|
||||
List<CalendarEventEntry> eventsForDay(DateTime day) {
|
||||
final normalized = DateTime(day.year, day.month, day.day);
|
||||
return calendarEvents
|
||||
.where((event) => event.date.year == normalized.year && event.date.month == normalized.month && event.date.day == normalized.day)
|
||||
.toList(growable: false);
|
||||
}
|
||||
|
||||
RouteGuideEntry? guideForRouteId(String routeId) {
|
||||
for (final guide in routeGuides) {
|
||||
if (guide.routeId == routeId) {
|
||||
return guide;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user