Files
Holzleitner-Lieferservice-App/lib/bloc/app_bloc.dart
Dennis Nemec f832b69b9d feat(umgebung): Server-Umgebung im Login-Screen waehlbar (Produktion/Test)
- Umgebungen (Name, API-URL, Keycloak-Issuer) kommen aus
  assets/hl_server_config.json statt hartcodiertem BackendConfig.prod;
  Testumgebung 192.168.1.9:3001 ist dort hinterlegt
- AppBloc laedt die Config, aktiviert die gespeicherte Wahl (Fallback
  Default) und registriert das Networking; Wechsel persistiert per
  SharedPreferences und registriert neu
- Bloc-Baum ist pro Umgebung gekeyt: beim Wechsel entstehen alle Blocs
  mit dem neuen API-Client neu
- Login-Screen: Zahnrad oben rechts oeffnet Einstellungen mit Umgebungs-
  Cards (Name + IP:Port); Hinweis auf aktive Umgebung unter dem Button
- Fahrzeugauswahl pro Umgebung getrennt (eigene DB = eigene Car-IDs)
- Tests fuer Config-Parsing, Auswahl-Fallback, Card-Wechsel, Re-Registrierung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 13:06:49 +02:00

65 lines
2.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/bloc/app_events.dart';
import 'package:hl_lieferservice/bloc/app_states.dart';
import 'package:hl_lieferservice/data/network/backend_config.dart';
import 'package:hl_lieferservice/data/network/backend_environment.dart';
import 'package:hl_lieferservice/data/network/environment_repository.dart';
/// Aktiviert das Networking für eine Umgebung (Registrierung im Locator).
typedef EnvironmentActivator = void Function(BackendConfig config);
/// App-Bootstrap-Bloc.
///
/// Liest die Server-Umgebungen aus `assets/hl_server_config.json`,
/// bestimmt die aktive Umgebung (gespeicherte Wahl, sonst Default) und
/// registriert dafür das Networking. Ein Umgebungswechsel läuft über
/// denselben Weg; die UI baut ihren Bloc-Baum pro Umgebung neu auf.
class AppBloc extends Bloc<AppEvents, AppState> {
AppBloc({
required EnvironmentRepository repository,
required EnvironmentActivator activate,
}) : _repository = repository,
_activate = activate,
super(const AppInitial()) {
on<AppLoadConfig>(_onLoad);
on<AppSwitchEnvironment>(_onSwitch);
}
final EnvironmentRepository _repository;
final EnvironmentActivator _activate;
Future<void> _onLoad(AppLoadConfig event, Emitter<AppState> emit) async {
emit(const AppConfigLoading());
try {
final catalog = await _repository.loadCatalog('assets/${event.path}');
final storedId = await _repository.loadSelectedId();
// Unbekannte ID (Umgebung aus der Config entfernt) → Default.
final active = catalog.byId(storedId) ?? catalog.defaultEnvironment;
_activate(catalog.configFor(active));
emit(AppConfigLoaded(catalog: catalog, active: active));
} catch (e, st) {
debugPrint('AppBloc: Server-Config nicht ladbar: $e\n$st');
emit(AppConfigLoadingFailed(
message: 'Server-Konfiguration konnte nicht geladen werden.\n$e',
));
}
}
Future<void> _onSwitch(
AppSwitchEnvironment event,
Emitter<AppState> emit,
) async {
final current = state;
if (current is! AppConfigLoaded) return;
if (current.active.id == event.environmentId) return;
final BackendEnvironment? next = current.catalog.byId(event.environmentId);
if (next == null) return;
await _repository.saveSelectedId(next.id);
_activate(current.catalog.configFor(next));
emit(AppConfigLoaded(catalog: current.catalog, active: next));
}
}