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>
This commit is contained in:
Dennis Nemec
2026-09-25 13:06:49 +02:00
parent c239cdfd3e
commit f832b69b9d
17 changed files with 717 additions and 72 deletions

View File

@ -1,20 +1,64 @@
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.
///
/// Vor der Backend-Migration lud dieser Bloc eine `hl_server_config.json` aus
/// assets, parste daraus eine `backendUrl` und persistierte sie ins Dateisystem.
/// Mit dem Wechsel auf das Rust-Backend kommt die URL über `BackendConfig`
/// (compile-time, siehe `data/network/backend_config.dart`); der App-Bloc
/// emittiert jetzt nur noch sofort `AppConfigLoaded`, damit die UI ihre
/// üblichen Phasen-Übergänge behält.
/// 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() : super(const AppInitial()) {
on<AppLoadConfig>((event, emit) {
emit(const AppConfigLoading());
emit(const AppConfigLoaded());
});
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));
}
}

View File

@ -1,7 +1,16 @@
abstract class AppEvents {}
/// Lädt die Server-Config aus den Assets und aktiviert die gespeicherte
/// (oder die Default-)Umgebung. [path] ist relativ zu `assets/`.
class AppLoadConfig extends AppEvents {
String path;
AppLoadConfig({required this.path});
}
}
/// Wechselt auf die Umgebung mit [environmentId] und speichert die Wahl.
class AppSwitchEnvironment extends AppEvents {
AppSwitchEnvironment({required this.environmentId});
final String environmentId;
}

View File

@ -1,10 +1,9 @@
import 'package:hl_lieferservice/data/network/backend_environment.dart';
/// Lifecycle-States des App-Bootstraps.
///
/// Die alte `LocalDocuFrameConfiguration` mit `backendUrl` ist mit der
/// Backend-Migration entfallen — die App-Konfiguration kommt jetzt aus
/// `BackendConfig` (compile-time) und nicht mehr aus einer asset-JSON.
/// Das `AppConfigLoaded`-Signal bleibt als Marker, dass der App-Bootstrap
/// abgeschlossen ist (Networking ist registriert, Token-Provider steht).
/// `AppConfigLoaded` heißt: Server-Config ist gelesen, die aktive Umgebung
/// steht fest und das Networking dafür ist im Locator registriert.
abstract class AppState {
const AppState();
}
@ -18,7 +17,13 @@ class AppConfigLoading extends AppState {
}
class AppConfigLoaded extends AppState {
const AppConfigLoaded();
const AppConfigLoaded({required this.catalog, required this.active});
/// Alle auswählbaren Umgebungen aus der Config-Datei.
final ServerCatalog catalog;
/// Die Umgebung, gegen die die App gerade spricht.
final BackendEnvironment active;
}
class AppConfigLoadingFailed extends AppState {