Skip to content

Commit

Permalink
feat: App review in the "scan card" (#4450)
Browse files Browse the repository at this point in the history
* App review in the "scan card"

* Add a keep alive to prevent many requests

* Add a missing call to super (for the KeepAlive mixin)

* Don't really understand what happened with this file 🤨
  • Loading branch information
g123k authored Aug 5, 2023
1 parent fe79c7b commit 78884a6
Show file tree
Hide file tree
Showing 48 changed files with 401 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/svg_icon_chip.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
part of '../user_preferences.dart';

/// When we add/remove entries in the [UserPreferences] class, we need to
/// add a new level and ensure we do a proper migration
class UserPreferencesMigrationTool {
const UserPreferencesMigrationTool._();

static final Iterable<UserPreferencesMigration> _versions =
<UserPreferencesMigration>[
const _UserPreferencesMigrationV1(),
const _UserPreferencesMigrationV2(),
const _UserPreferencesMigrationV3(),
];

static Future<void> onUpgrade(
UserPreferences preferences,
int? oldVersion,
int newVersion,
) async {
if (oldVersion == newVersion) {
return;
}

for (final UserPreferencesMigration migration in _versions) {
if ((oldVersion ?? 0) >= migration.version) {
continue;
}

await migration.onUpgrade(preferences, oldVersion, newVersion);
}
}
}

abstract interface class UserPreferencesMigration {
const UserPreferencesMigration();

Future<void> onUpgrade(
UserPreferences preferences,
int? oldVersion,
int newVersion,
);

int get version;
}

class _UserPreferencesMigrationV1 extends UserPreferencesMigration {
const _UserPreferencesMigrationV1();

@override
Future<void> onUpgrade(
UserPreferences preferences,
int? oldVersion,
int newVersion,
) async {
final bool? crashReporting = preferences._sharedPreferences
.getBool(UserPreferences._TAG_CRASH_REPORTS);
if (crashReporting != null) {
await preferences.setUserTracking(crashReporting);
}
}

@override
int get version => 1;
}

class _UserPreferencesMigrationV2 extends UserPreferencesMigration {
const _UserPreferencesMigrationV2();

@override
Future<void> onUpgrade(
UserPreferences preferences,
int? oldVersion,
int newVersion,
) async {
/// With version == null and 1, [_TAG_USER_GROUP] is missing
if (preferences._sharedPreferences
.getInt(UserPreferences._TAG_USER_GROUP) ==
null) {
await preferences._sharedPreferences.setInt(
UserPreferences._TAG_USER_GROUP,
Random().nextInt(10),
);
}
}

@override
int get version => 2;
}

class _UserPreferencesMigrationV3 extends UserPreferencesMigration {
const _UserPreferencesMigrationV3();

@override
Future<void> onUpgrade(
UserPreferences preferences,
int? oldVersion,
int newVersion,
) async {
if (preferences._sharedPreferences.getBool('_TAG_IS_FIRST_SCAN') ?? false) {
await preferences.incrementScanCount();
}
}

@override
int get version => 3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/themes/color_schemes.dart';

part 'package:smooth_app/data_models/preferences/migration/user_preferences_migration.dart';

/// User choice regarding the picture source.
enum UserPictureSource {
/// Always select between Gallery and Camera
Expand Down Expand Up @@ -57,9 +59,9 @@ class UserPreferences extends ChangeNotifier {
/// Whether the preferences are empty or not
static const String _TAG_INIT = 'init';

/// The current version of preferences (1)
/// The current version of preferences
static const String _TAG_VERSION = 'prefs_version';
static const int _PREFS_CURRENT_VERSION = 2;
static const int _PREFS_CURRENT_VERSION = 3;
static const String _TAG_PREFIX_IMPORTANCE = 'IMPORTANCE_AS_STRING';
static const String _TAG_CURRENT_THEME_MODE = 'currentThemeMode';
static const String _TAG_CURRENT_COLOR_SCHEME = 'currentColorScheme';
Expand All @@ -75,9 +77,6 @@ class UserPreferences extends ChangeNotifier {
static const String _TAG_USER_GROUP = '_user_group';

/// Camera preferences
// Detect if a first successful scan was achieved (condition to show the
// tagline)
static const String _TAG_IS_FIRST_SCAN = 'is_first_scan';
// Use the flash/torch with the camera
static const String _TAG_USE_FLASH_WITH_CAMERA = 'enable_flash_with_camera';
Expand All @@ -98,6 +97,8 @@ class UserPreferences extends ChangeNotifier {
static const String _TAG_IN_APP_REVIEW_ALREADY_DISPLAYED =
'inAppReviewAlreadyAsked';

static const String _TAG_NUMBER_OF_SCANS = 'numberOfScans';

Future<void> init(final ProductPreferences productPreferences) async {
await _onMigrate();

Expand All @@ -110,27 +111,11 @@ class UserPreferences extends ChangeNotifier {

/// Allow to migrate between versions
Future<void> _onMigrate() async {
final int? currentVersion = _sharedPreferences.getInt(_TAG_VERSION);
if (currentVersion == _PREFS_CURRENT_VERSION) {
return;
}

/// With version == null (or 0), [_TAG_USER_TRACKING] didn't exist
if (currentVersion == null) {
final bool? crashReporting =
_sharedPreferences.getBool(_TAG_CRASH_REPORTS);
if (crashReporting != null) {
await setUserTracking(crashReporting);
}
}

/// With version == null and 1, [_TAG_USER_GROUP] is missing
if (_sharedPreferences.getInt(_TAG_USER_GROUP) == null) {
await _sharedPreferences.setInt(
_TAG_USER_GROUP,
Random().nextInt(10),
);
}
await UserPreferencesMigrationTool.onUpgrade(
this,
_sharedPreferences.getInt(_TAG_VERSION),
_PREFS_CURRENT_VERSION,
);

await _sharedPreferences.setInt(
_TAG_VERSION,
Expand Down Expand Up @@ -232,15 +217,12 @@ class UserPreferences extends ChangeNotifier {
: OnboardingPage.values[pageIndex];
}

Future<void> setFirstScanAchieved() async {
if (isFirstScan) {
await _sharedPreferences.setBool(_TAG_IS_FIRST_SCAN, false);
notifyListeners();
}
Future<void> incrementScanCount() async {
await _sharedPreferences.setInt(_TAG_NUMBER_OF_SCANS, numberOfScans + 1);
notifyListeners();
}

bool get isFirstScan =>
_sharedPreferences.getBool(_TAG_IS_FIRST_SCAN) ?? true;
int get numberOfScans => _sharedPreferences.getInt(_TAG_NUMBER_OF_SCANS) ?? 0;

Future<void> markInAppReviewAsShown() async {
await _sharedPreferences.setBool(
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/helpers/analytics_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:matomo_tracker/matomo_tracker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/helpers/global_vars.dart';

/// Category for Matomo Events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/services.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';

/// Haptic feedback/vibrations in the app
/// Managed by a preference in the user's preferences
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_expanded_card.dart';
import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_page.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/cards/category_cards/abstract_cache.dart';
import 'package:smooth_app/cards/category_cards/svg_cache.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/extension_on_text_helper.dart';
import 'package:smooth_app/helpers/ui_helpers.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_element_card.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
Expand Down
20 changes: 8 additions & 12 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,14 @@
"@product_refreshed": {
"description": "Confirmation, that the product data refresh is done"
},
"deprecated_header": "You are using a deprecated version of the app.",
"@deprecated_header": {
"description": "Confirmation, that the user can upgrade to new version of the app"
},
"click_here": "Click here",
"@click_here": {
"description": "Confirmation click to download new version of the app"
},
"download_new_version": "Download the new version of the app",
"@download_new_version": {
"description": "Download new version of the app text"
},
"tagline_app_review":"Do you like the app?",
"tagline_app_review_button_positive":"I love it! 😍",
"tagline_app_review_button_negative":"Not really…",
"tagline_app_review_button_later":"Ask me later",
"app_review_negative_modal_title":"You don't like our app?",
"app_review_negative_modal_text":"Could you take a few seconds to tell us why?",
"app_review_negative_modal_positive_button":"Yes, absolutely!",
"app_review_negative_modal_negative_button":"No",
"could_not_refresh": "Could not refresh product",
"@could_not_refresh": {
"description": "The product data couldn't be refreshed"
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import 'package:provider/single_child_widget.dart';
import 'package:scanner_shared/scanner_shared.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:smooth_app/data_models/continuous_scan_model.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_management_provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/dao_string.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/image_crop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:image_picker/image_picker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/dao_int.dart';
import 'package:smooth_app/generic_lib/bottom_sheets/smooth_bottom_sheet.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/navigator/app_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/helpers/data_importer/smooth_app_data_importer.dart';
import 'package:smooth_app/helpers/extension_on_text_helper.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/onboarding_loader.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/app_helper.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:iso_countries/iso_countries.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/onboarding/next_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/onboarding_loader.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/pages/onboarding/onboarding_bottom_bar.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/pages/carousel_manager.dart';
import 'package:smooth_app/pages/navigator/app_navigator.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/onboarding_loader.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/app_helper.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/onboarding_data_product.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/app_helper.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/page_manager.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/pages/carousel_manager.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/widgets/tab_navigator.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/pages/preferences/user_preferences_list_tile.dart';
import 'package:smooth_app/pages/preferences/user_preferences_page.dart';
import 'package:smooth_app/themes/constant_icons.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/user_management_provider.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/buttons/smooth_simple_button.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand Down
Loading

0 comments on commit 78884a6

Please sign in to comment.