Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #833 - dev mode #834

Merged
merged 6 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class UserPreferences extends ChangeNotifier {
static const String _TAG_LAST_VISITED_ONBOARDING_PAGE =
'lastVisitedOnboardingPage';
static const String _TAG_PREFIX_FLAG = 'FLAG_PREFIX_';
static const String _TAG_DEV_MODE = 'devMode';

Future<void> init(final ProductPreferences productPreferences) async {
if (_sharedPreferences.getBool(_TAG_INIT) != null) {
Expand Down Expand Up @@ -91,4 +92,9 @@ class UserPreferences extends ChangeNotifier {

bool? getFlag(final String key) =>
_sharedPreferences.getBool(_getFlagTag(key));

set devMode(final int value) =>
_sharedPreferences.setInt(_TAG_DEV_MODE, value);

int get devMode => _sharedPreferences.getInt(_TAG_DEV_MODE) ?? 0;
Copy link
Member

@M123-dev M123-dev Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency please either all get/set or all methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Too bad the dart setter syntax does not work with async.

Copy link
Member

@M123-dev M123-dev Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a bool instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course a bool would do the job in a first approach, but in my experience I noticed that sometimes we're stuck with a bool when we want to upgrade the app to something more elaborate. An int is safer.

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/cupertino.dart';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/themes/theme_provider.dart';
import 'package:smooth_ui_library/widgets/smooth_card.dart';
import 'package:smooth_ui_library/widgets/smooth_text_form_field.dart';
Expand All @@ -14,6 +16,8 @@ class ForgotPasswordPage extends StatefulWidget {
}

class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
int _devModeCounter = 0;

static Color _textFieldBackgroundColor = const Color.fromARGB(
255,
240,
Expand Down Expand Up @@ -65,6 +69,7 @@ class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
final ThemeData theme = Theme.of(context);
final ThemeProvider themeProvider = context.watch<ThemeProvider>();
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final UserPreferences userPreferences = context.watch<UserPreferences>();
final Size size = MediaQuery.of(context).size;

// Needs to be changed
Expand Down Expand Up @@ -136,6 +141,30 @@ class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
],
validator: (String? value) {
if (value == null || value.isEmpty) {
_devModeCounter++;
if (_devModeCounter >= 10) {
if (userPreferences.devMode == 0) {
showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Ready for the dev mode?'),
actions: <Widget>[
TextButton(
child: Text(appLocalizations.yes),
onPressed: () {
userPreferences.devMode = 1;
Navigator.pop(context);
},
),
TextButton(
child: Text(appLocalizations.no),
onPressed: () => Navigator.pop(context),
)
],
),
);
}
}
return appLocalizations.enter_some_text;
}
return null;
Expand Down
68 changes: 68 additions & 0 deletions packages/smooth_app/lib/pages/user_preferences_dev_mode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/pages/abstract_user_preferences.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';

/// Collapsed/expanded display of "dev mode" for the preferences page.
///
/// The dev mode is triggered this way:
/// * go to the "forgotten password" page
/// * click 10 times on the action button (in French "Changer le mot de passe")
/// * you'll see a dialog; obviously click "yes"
/// * go to the preferences page
/// * expand/collapse any item
/// * then you'll see the dev mode in red
class UserPreferencesDevMode extends AbstractUserPreferences {
UserPreferencesDevMode({
required final Function(Function()) setState,
required final BuildContext context,
required final UserPreferences userPreferences,
required final AppLocalizations appLocalizations,
required final ThemeData themeData,
}) : super(
setState: setState,
context: context,
userPreferences: userPreferences,
appLocalizations: appLocalizations,
themeData: themeData,
);

@override
bool isCollapsedByDefault() => true;

@override
String getPreferenceFlagKey() => 'devMode';

@override
Widget getTitle() => Container(
color: Colors.red,
child: Text(
'DEV MODE',
style: themeData.textTheme.headline2!.copyWith(color: Colors.white),
),
);

@override
Widget? getSubtitle() => null;

@override
List<Widget> getBody() => <Widget>[
ListTile(
title: const Text('Remove dev mode'),
onTap: () async {
userPreferences.devMode = 0;
setState(() {});
},
),
ListTile(
title: const Text('restart onboarding'),
subtitle: const Text('then you have to restart flutter'),
onTap: () async {
userPreferences
.setLastVisitedOnboardingPage(OnboardingPage.NOT_STARTED);
setState(() {});
},
),
];
}
12 changes: 12 additions & 0 deletions packages/smooth_app/lib/pages/user_preferences_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/pages/abstract_user_preferences.dart';
import 'package:smooth_app/pages/user_preferences_dev_mode.dart';
import 'package:smooth_app/pages/user_preferences_food.dart';
import 'package:smooth_app/pages/user_preferences_profile.dart';
import 'package:smooth_app/pages/user_preferences_settings.dart';
Expand Down Expand Up @@ -52,6 +53,17 @@ class _UserPreferencesPageState extends State<UserPreferencesPage> {
themeData: themeData,
),
];
if (userPreferences.devMode > 0) {
items.add(
UserPreferencesDevMode(
setState: setState,
context: context,
userPreferences: userPreferences,
appLocalizations: appLocalizations,
themeData: themeData,
),
);
}
final List<Widget> children = <Widget>[];
for (final AbstractUserPreferences abstractUserPreferences in items) {
children.addAll(abstractUserPreferences.getContent());
Expand Down