- 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>
46 lines
1.5 KiB
Dart
46 lines
1.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import 'feature.dart';
|
|
|
|
/// Unveränderlicher Satz an Feature-Schaltern, geladen aus
|
|
/// `assets/feature_flags.json` (siehe [FeatureFlagsRepository]).
|
|
///
|
|
/// Erwartetes Format:
|
|
/// ```json
|
|
/// { "features": { "<key>": { "enabled": true, "description": "…" } } }
|
|
/// ```
|
|
/// `description` ist nur Doku für Menschen. Unbekannte Keys werden
|
|
/// ignoriert, fehlende fallen auf [Feature.defaultEnabled] zurück.
|
|
@immutable
|
|
class FeatureFlags {
|
|
const FeatureFlags(this._enabledByKey);
|
|
|
|
factory FeatureFlags.fromJson(Map<String, dynamic> json) {
|
|
final features = json['features'];
|
|
if (features is! Map<String, dynamic>) {
|
|
throw const FormatException('feature_flags.json: "features" fehlt.');
|
|
}
|
|
final knownKeys = {for (final f in Feature.values) f.key};
|
|
final enabledByKey = <String, bool>{};
|
|
features.forEach((key, value) {
|
|
final enabled = value is Map<String, dynamic> ? value['enabled'] : null;
|
|
if (enabled is! bool) {
|
|
throw FormatException(
|
|
'feature_flags.json: "$key.enabled" muss true oder false sein.',
|
|
);
|
|
}
|
|
if (!knownKeys.contains(key)) {
|
|
debugPrint('FeatureFlags: unbekannter Key "$key" wird ignoriert.');
|
|
return;
|
|
}
|
|
enabledByKey[key] = enabled;
|
|
});
|
|
return FeatureFlags(enabledByKey);
|
|
}
|
|
|
|
final Map<String, bool> _enabledByKey;
|
|
|
|
bool isEnabled(Feature feature) =>
|
|
_enabledByKey[feature.key] ?? feature.defaultEnabled;
|
|
}
|