-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor feature flags code and fix feature flag groups
- Loading branch information
1 parent
dca0002
commit 98e1d79
Showing
8 changed files
with
173 additions
and
156 deletions.
There are no files selected for viewing
90 changes: 81 additions & 9 deletions
90
packages/uni_app/lib/controller/feature_flags/feature_flag_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,90 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:uni/controller/feature_flags/feature_flag_info.dart'; | ||
import 'package:uni/controller/feature_flags/feature_flag_state_controller.dart'; | ||
import 'package:uni/model/feature_flags/feature_flag.dart'; | ||
import 'package:uni/model/feature_flags/feature_flag_group.dart'; | ||
import 'package:uni/model/feature_flags/generic_feature_flag.dart'; | ||
|
||
class FeatureFlagController { | ||
FeatureFlagController(this.preferences); | ||
static final List<GenericFeatureFlag> _featureFlags = []; // To preserve order | ||
static final Map<String, GenericFeatureFlag> _featureFlagsMap = | ||
{}; // For fast lookup | ||
static FeatureFlagStateController? _stateController; | ||
|
||
final SharedPreferences preferences; | ||
static const _flagPrefix = '__feature_flag__'; | ||
static void parseFeatureFlagTable( | ||
List<GenericFeatureFlagInfo> featureFlagInfos, | ||
) { | ||
for (final featureFlagInfo in featureFlagInfos) { | ||
final featureFlag = featureFlagInfo is FeatureFlagInfo | ||
? _createFeatureFlag(featureFlagInfo) | ||
: _createFeatureFlagGroup(featureFlagInfo as FeatureFlagGroupInfo); | ||
|
||
String _getKey(String code) => '$_flagPrefix$code'; | ||
_featureFlags.add(featureFlag); | ||
_featureFlagsMap[featureFlag.code] = featureFlag; | ||
|
||
bool isEnabled(String code) { | ||
return preferences.getBool(_getKey(code)) ?? false; | ||
if (featureFlag is FeatureFlagGroup) { | ||
for (final subFeatureFlag in featureFlag.getFeatureFlags()) { | ||
_featureFlagsMap[subFeatureFlag.code] = subFeatureFlag; | ||
} | ||
} | ||
} | ||
} | ||
|
||
Future<void> saveEnabled(String code, {required bool enabled}) { | ||
return preferences.setBool(_getKey(code), enabled); | ||
static bool _isEnabled(String code) { | ||
if (_stateController == null) { | ||
throw Exception('FeatureFlagStateController is not initialized.'); | ||
} | ||
|
||
return _stateController!.isEnabled(code); | ||
} | ||
|
||
static Future<void> _saveEnabled(String code, {required bool enabled}) async { | ||
if (_stateController == null) { | ||
throw Exception('FeatureFlagStateController is not initialized.'); | ||
} | ||
|
||
await _stateController!.saveEnabled(code, enabled: enabled); | ||
} | ||
|
||
static FeatureFlag _createFeatureFlag(FeatureFlagInfo featureFlagInfo) { | ||
final code = featureFlagInfo.code; | ||
final getName = featureFlagInfo.getName; | ||
|
||
return FeatureFlag( | ||
code: code, | ||
getName: getName, | ||
isEnabled: () => _isEnabled(code), | ||
saveEnabled: ({required enabled}) => _saveEnabled(code, enabled: enabled), | ||
); | ||
} | ||
|
||
static GenericFeatureFlag _createFeatureFlagGroup( | ||
FeatureFlagGroupInfo featureFlagGroupInfo, | ||
) { | ||
final code = featureFlagGroupInfo.code; | ||
final getName = featureFlagGroupInfo.getName; | ||
final featureFlags = | ||
featureFlagGroupInfo.featureFlags.map(_createFeatureFlag).toList(); | ||
|
||
final featureFlagGroup = FeatureFlagGroup( | ||
code: code, | ||
getName: getName, | ||
isEnabled: () => _isEnabled(code), | ||
saveEnabled: ({required enabled}) => _saveEnabled(code, enabled: enabled), | ||
featureFlags: featureFlags, | ||
); | ||
|
||
return featureFlagGroup; | ||
} | ||
|
||
static GenericFeatureFlag? getFeatureFlag(String code) { | ||
return _featureFlagsMap[code]; | ||
} | ||
|
||
static void setStateController(FeatureFlagStateController stateController) { | ||
_stateController = stateController; | ||
} | ||
|
||
static List<GenericFeatureFlag> getFeatureFlags() { | ||
return _featureFlags; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/uni_app/lib/controller/feature_flags/feature_flag_state_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class FeatureFlagStateController { | ||
FeatureFlagStateController(this.preferences); | ||
|
||
final SharedPreferences preferences; | ||
static const _flagPrefix = '__feature_flag__'; | ||
|
||
String _getKey(String code) => '$_flagPrefix$code'; | ||
|
||
bool isEnabled(String code) { | ||
return preferences.getBool(_getKey(code)) ?? false; | ||
} | ||
|
||
Future<void> saveEnabled(String code, {required bool enabled}) { | ||
return preferences.setBool(_getKey(code), enabled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.