Skip to content

Commit

Permalink
Implement ExperimentalFeatureWrapper; Light refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Mar 20, 2024
1 parent ecdc2cd commit 218b55e
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 22 deletions.
5 changes: 4 additions & 1 deletion uni/lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class MessageLookup extends MessageLookupByLibrary {
"expired_password":
MessageLookupByLibrary.simpleMessage("Your password has expired"),
"failed_login": MessageLookupByLibrary.simpleMessage("Login failed"),
"feature_flags": MessageLookupByLibrary.simpleMessage("Feature flags"),
"feature_flags":
MessageLookupByLibrary.simpleMessage("Experimental features"),
"fee_date":
MessageLookupByLibrary.simpleMessage("Deadline for next fee:"),
"fee_notification":
Expand All @@ -153,6 +154,8 @@ class MessageLookup extends MessageLookupByLibrary {
"language": MessageLookupByLibrary.simpleMessage("Language"),
"last_refresh_time": m0,
"last_timestamp": m1,
"library_modules":
MessageLookupByLibrary.simpleMessage("Library modules"),
"library_occupation":
MessageLookupByLibrary.simpleMessage("Library Occupation"),
"load_error": MessageLookupByLibrary.simpleMessage(
Expand Down
4 changes: 3 additions & 1 deletion uni/lib/generated/intl/messages_pt_PT.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("A tua palavra-passe expirou"),
"failed_login": MessageLookupByLibrary.simpleMessage("O login falhou"),
"feature_flags":
MessageLookupByLibrary.simpleMessage("Sinalizadores de recursos"),
MessageLookupByLibrary.simpleMessage("Features experimentais"),
"fee_date": MessageLookupByLibrary.simpleMessage(
"Data limite próxima prestação:"),
"fee_notification":
Expand All @@ -154,6 +154,8 @@ class MessageLookup extends MessageLookupByLibrary {
"language": MessageLookupByLibrary.simpleMessage("Idioma"),
"last_refresh_time": m0,
"last_timestamp": m1,
"library_modules":
MessageLookupByLibrary.simpleMessage("Módulos da biblioteca"),
"library_occupation":
MessageLookupByLibrary.simpleMessage("Ocupação da Biblioteca"),
"load_error": MessageLookupByLibrary.simpleMessage(
Expand Down
14 changes: 12 additions & 2 deletions uni/lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion uni/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,7 @@
"try_again": "Try again",
"@try_again": {},
"feature_flags": "Experimental features",
"@feature_flags": {}
"@feature_flags": {},
"library_modules": "Library modules",
"@library_modules": {}
}
4 changes: 3 additions & 1 deletion uni/lib/l10n/intl_pt_PT.arb
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,7 @@
"try_again": "Tentar de novo",
"@try_again": {},
"feature_flags": "Features experimentais",
"@feature_flags": {}
"@feature_flags": {},
"library_modules": "Módulos da biblioteca",
"@library_modules": {}
}
46 changes: 46 additions & 0 deletions uni/lib/view/common_widgets/experimental_feature_wrapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni/controller/local_storage/enabled_feature_controller.dart';

class ExperimentalFeatureWrapper extends StatefulWidget {
const ExperimentalFeatureWrapper({
required this.featureCode,
required this.child,
super.key,
});

final String featureCode;
final Widget child;

@override
State<StatefulWidget> createState() => ExperimentalFeatureWrapperState();
}

class ExperimentalFeatureWrapperState
extends State<ExperimentalFeatureWrapper> {
late Future<SharedPreferences> _preferences;

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

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _preferences,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}

final enabledFeatureController =
EnabledFeatureController(snapshot.data!);
return enabledFeatureController.existsFeature(widget.featureCode)
? widget.child
: Container();
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FeatureFlagsDialogState extends State<FeatureFlagsDialog> {
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: const Text('Library modules'),
title: Text(S.of(context).library_modules),
trailing: LibraryModulesSwitch(
enabledFeatureController: enabledFeatureController,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:flutter/material.dart';
import 'package:uni/controller/local_storage/enabled_feature_controller.dart';

abstract class GenericSwitch extends StatefulWidget {
const GenericSwitch({super.key});
abstract class FeatureSwitch extends StatefulWidget {
const FeatureSwitch({required this.enabledFeatureController, super.key});

final EnabledFeatureController enabledFeatureController;

@override
GenericSwitchState createState();
FeatureSwitchState createState();
}

abstract class GenericSwitchState<T extends GenericSwitch> extends State<T> {
GenericSwitchState();
abstract class FeatureSwitchState<T extends FeatureSwitch> extends State<T> {
FeatureSwitchState();

@override
void initState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import 'package:uni/controller/local_storage/enabled_feature_controller.dart';
import 'package:uni/view/settings/widgets/generic_switch.dart';
import 'package:uni/view/settings/widgets/feature_flags/feature_switch.dart';

class LibraryModulesSwitch extends GenericSwitch {
class LibraryModulesSwitch extends FeatureSwitch {
const LibraryModulesSwitch({
required this.enabledFeatureController,
required super.enabledFeatureController,
super.key,
});

final EnabledFeatureController enabledFeatureController;

@override
GenericSwitchState<GenericSwitch> createState() =>
FeatureSwitchState<FeatureSwitch> createState() =>
LibraryModulesSwitchState();
}

class LibraryModulesSwitchState
extends GenericSwitchState<LibraryModulesSwitch> {
extends FeatureSwitchState<LibraryModulesSwitch> {
static const String _featureCode = 'library_modules';

@override
bool initializeValue() {
return widget.enabledFeatureController.existsFeature('library_cards');
return widget.enabledFeatureController.existsFeature(_featureCode);
}

@override
Future<void> storeValue({required bool value}) async {
await widget.enabledFeatureController.addFeature('library_cards');
if (value) {
await widget.enabledFeatureController.addFeature(_featureCode);
} else {
await widget.enabledFeatureController.removeFeature(_featureCode);
}
}
}

0 comments on commit 218b55e

Please sign in to comment.