- Schalter stehen in assets/feature_flags.json (Key -> enabled + Beschreibung) statt als Konstanten im Code - Enum Feature buendelt die stabilen Keys; FeatureFlags liest die Datei, ignoriert unbekannte Keys und nutzt fuer fehlende den Default aus dem Enum - Laden beim App-Start im AppBloc, bereitgestellt per RepositoryProvider; FeatureGate blendet Bereiche bei inaktivem Feature komplett aus - articles.credit_section = false: Abschnitt "Gutschriften" im Artikel-Step ist ausgeblendet - Bisheriges Stepper-Flag migriert (articles.credit_amount_stepper); die fuenf ungenutzten statischen Flags mit veralteten Kommentaren entfernt - Tests: Asset enthaelt alle Keys, Parsing, Defaults, FeatureGate Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
3.0 KiB
Dart
80 lines
3.0 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';
|
|
import 'package:hl_lieferservice/feature/feature_flags/feature_flags_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. Außerdem lädt er die Feature-Schalter
|
|
/// aus `assets/feature_flags.json`. 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,
|
|
FeatureFlagsRepository featureFlagsRepository =
|
|
const FeatureFlagsRepository(),
|
|
}) : _repository = repository,
|
|
_activate = activate,
|
|
_featureFlagsRepository = featureFlagsRepository,
|
|
super(const AppInitial()) {
|
|
on<AppLoadConfig>(_onLoad);
|
|
on<AppSwitchEnvironment>(_onSwitch);
|
|
}
|
|
|
|
final EnvironmentRepository _repository;
|
|
final EnvironmentActivator _activate;
|
|
final FeatureFlagsRepository _featureFlagsRepository;
|
|
|
|
Future<void> _onLoad(AppLoadConfig event, Emitter<AppState> emit) async {
|
|
emit(const AppConfigLoading());
|
|
try {
|
|
final catalog = await _repository.loadCatalog('assets/${event.path}');
|
|
final featureFlags = await _featureFlagsRepository.load();
|
|
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,
|
|
featureFlags: featureFlags,
|
|
));
|
|
} catch (e, st) {
|
|
debugPrint('AppBloc: Konfiguration nicht ladbar: $e\n$st');
|
|
emit(AppConfigLoadingFailed(
|
|
message: '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,
|
|
featureFlags: current.featureFlags,
|
|
));
|
|
}
|
|
}
|