Skip to content

Commit

Permalink
Refactor feature flag dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Nov 11, 2024
1 parent 4d45056 commit 911b2b1
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FeatureFlagController {
return preferences.getBool(_getKey(code)) ?? false;
}

void saveEnabled(String code, {required bool enabled}) {
preferences.setBool(_getKey(code), enabled);
Future<void> saveEnabled(String code, {required bool enabled}) {
return preferences.setBool(_getKey(code), enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class FeatureFlagTable {
static FeatureFlagController? _controller;

static List<GenericFeatureFlag> _toFeatureFlags(
List<GenericFeatureFlagInfo> featureFlagInfos) {
List<GenericFeatureFlagInfo> featureFlagInfos,
) {
final featureFlags = <GenericFeatureFlag>[];

for (final featureFlagInfo in featureFlagInfos) {
Expand All @@ -35,7 +36,8 @@ class FeatureFlagTable {
}

static Map<String, GenericFeatureFlag> _toFeatureFlagsMap(
List<GenericFeatureFlag> featureFlags) {
List<GenericFeatureFlag> featureFlags,
) {
final featureFlagsMap = <String, GenericFeatureFlag>{};

for (final featureFlag in featureFlags) {
Expand All @@ -53,12 +55,12 @@ class FeatureFlagTable {
return _controller!.isEnabled(code);
}

static void _saveEnabled(String code, {required bool enabled}) {
static Future<void> _saveEnabled(String code, {required bool enabled}) async {
if (_controller == null) {
throw Exception('FeatureFlagController is not initialized.');
}

_controller!.saveEnabled(code, enabled: enabled);
await _controller!.saveEnabled(code, enabled: enabled);
}

static FeatureFlag _createFeatureFlag(FeatureFlagInfo featureFlagInfo) {
Expand All @@ -74,7 +76,8 @@ class FeatureFlagTable {
}

static GenericFeatureFlag _createFeatureFlagGroup(
FeatureFlagGroupInfo featureFlagGroupInfo) {
FeatureFlagGroupInfo featureFlagGroupInfo,
) {
final code = featureFlagGroupInfo.code;
final getName = featureFlagGroupInfo.getName;
final featureFlags =
Expand All @@ -84,12 +87,7 @@ class FeatureFlagTable {
code: code,
getName: getName,
isEnabled: () => _isEnabled(code),
saveEnabled: ({required enabled}) {
_saveEnabled(code, enabled: enabled);
for (final featureFlag in featureFlags) {
featureFlag.enabled = enabled;
}
},
saveEnabled: ({required enabled}) => _saveEnabled(code, enabled: enabled),
featureFlags: featureFlags,
);

Expand Down
21 changes: 0 additions & 21 deletions packages/uni_app/lib/model/feature_flag.dart

This file was deleted.

11 changes: 5 additions & 6 deletions packages/uni_app/lib/model/feature_flags/feature_flag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FeatureFlag extends GenericFeatureFlag {
required this.code,
required String Function(BuildContext) getName,
required bool Function() isEnabled,
required void Function({required bool enabled}) saveEnabled,
required Future<void> Function({required bool enabled}) saveEnabled,
}) : _getName = getName,
_isEnabled = isEnabled,
_saveEnabled = saveEnabled;
Expand All @@ -15,16 +15,15 @@ class FeatureFlag extends GenericFeatureFlag {
final String code;
final String Function(BuildContext) _getName;
final bool Function() _isEnabled;
final void Function({required bool enabled}) _saveEnabled;
final Future<void> Function({required bool enabled}) _saveEnabled;

@override
String getName(BuildContext context) => _getName(context);

@override
bool get enabled => _isEnabled();
bool isEnabled() => _isEnabled();

@override
set enabled(bool enabled) {
_saveEnabled(enabled: enabled);
}
Future<void> setEnabled({required bool enabled}) =>
_saveEnabled(enabled: enabled);
}
12 changes: 6 additions & 6 deletions packages/uni_app/lib/model/feature_flags/feature_flag_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class FeatureFlagGroup extends GenericFeatureFlag {
required this.code,
required String Function(BuildContext) getName,
required bool Function() isEnabled,
required void Function({required bool enabled}) saveEnabled,
required Future<void> Function({required bool enabled}) saveEnabled,
required List<FeatureFlag> featureFlags,
}) : _getName = getName,
_isEnabled = isEnabled,
Expand All @@ -18,21 +18,21 @@ class FeatureFlagGroup extends GenericFeatureFlag {
final String code;
final String Function(BuildContext) _getName;
final bool Function() _isEnabled;
final void Function({required bool enabled}) _saveEnabled;
final Future<void> Function({required bool enabled}) _saveEnabled;
final List<FeatureFlag> _featureFlags;

@override
String getName(BuildContext context) => _getName(context);

@override
bool get enabled => _isEnabled();
bool isEnabled() => _isEnabled();

@override
set enabled(bool enabled) {
_saveEnabled(enabled: enabled);
Future<void> setEnabled({required bool enabled}) async {
await _saveEnabled(enabled: enabled);

for (final featureFlag in _featureFlags) {
featureFlag.enabled = enabled;
await featureFlag.setEnabled(enabled: enabled);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import 'package:flutter/material.dart';
abstract class GenericFeatureFlag {
String get code;
String getName(BuildContext context);
bool get enabled;
set enabled(bool enabled);
bool isEnabled();
Future<void> setEnabled({required bool enabled});
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni/controller/feature_flags/feature_flag_table.dart';

import 'package:uni/generated/l10n.dart';
import 'package:uni/view/settings/widgets/feature_flags/feature_switch_tile.dart';

class FeatureFlagsDialog extends StatefulWidget {
const FeatureFlagsDialog({super.key});
Expand All @@ -12,32 +12,29 @@ class FeatureFlagsDialog extends StatefulWidget {
}

class FeatureFlagsDialogState extends State<FeatureFlagsDialog> {
late Future<SharedPreferences> _preferences;

@override
void initState() {
super.initState();
_preferences = SharedPreferences.getInstance();
}

void refresh() {
setState(() {});
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(S.of(context).feature_flags),
content: FutureBuilder<SharedPreferences>(
future: _preferences,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}

return Column(
mainAxisSize: MainAxisSize.min,
children: FeatureFlagTable.getFeatureFlags()
.map((featureFlag) => Text(featureFlag.getName(context)))
.toList(),
);
},
content: Column(
mainAxisSize: MainAxisSize.min,
children: FeatureFlagTable.getFeatureFlags()
.map(
(featureFlag) => FeatureSwitchTile(
featureFlag: featureFlag,
refreshDialog: refresh,
),
)
.toList(),
),
);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:uni/model/feature_flags/generic_feature_flag.dart';

class FeatureSwitchTile extends StatelessWidget {
const FeatureSwitchTile({
required this.featureFlag,
required this.refreshDialog,
super.key,
});

final GenericFeatureFlag featureFlag;
final void Function() refreshDialog;

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(featureFlag.getName(context)),
trailing: Switch.adaptive(
value: featureFlag.isEnabled(),
onChanged: (value) async {
await featureFlag.setEnabled(enabled: value);
refreshDialog();
},
),
);
}
}

0 comments on commit 911b2b1

Please sign in to comment.