From 34c31491131584e1782b9b976c12cd232e68c4be Mon Sep 17 00:00:00 2001 From: Nils Reichardt Date: Sat, 8 Apr 2023 02:09:16 +0200 Subject: [PATCH] Add option to clear app cache (#571) ## Description Users can clear the app cache with the "ClearCache" activation code. https://user-images.githubusercontent.com/24459435/230214220-9c523a51-3693-4f4f-91c8-28fb5b4d372c.mov ## Related Tickets Closes #570 --- .../src/bloc/enter_activation_code_bloc.dart | 38 ++++++++++++++++--- .../enter_activation_code_bloc_factory.dart | 4 ++ .../enter_activation_code_result_dialog.dart | 2 +- .../enter_activation_code_text_field.dart | 2 +- app/lib/blocs/sharezone_bloc_providers.dart | 1 + 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/lib/activation_code/src/bloc/enter_activation_code_bloc.dart b/app/lib/activation_code/src/bloc/enter_activation_code_bloc.dart index 672e33ef8..4a8a33df1 100644 --- a/app/lib/activation_code/src/bloc/enter_activation_code_bloc.dart +++ b/app/lib/activation_code/src/bloc/enter_activation_code_bloc.dart @@ -10,9 +10,14 @@ import 'package:analytics/analytics.dart'; import 'package:app_functions/sharezone_app_functions.dart'; import 'package:bloc_base/bloc_base.dart'; import 'package:crash_analytics/crash_analytics.dart'; +import 'package:feature_discovery/feature_discovery.dart'; +import 'package:flutter/material.dart'; +import 'package:key_value_store/key_value_store.dart'; import 'package:rxdart/rxdart.dart'; +import 'package:sharezone/blackboard/details/blackboard_details.dart'; import 'package:sharezone/sharezone_plus/subscription_service/subscription_flag.dart'; import 'package:sharezone_common/helper_functions.dart'; + import '../models/enter_activation_code_result.dart'; import 'enter_activation_code_activator.dart'; @@ -23,6 +28,7 @@ class EnterActivationCodeBloc extends BlocBase { final _enterActivationCodeSubject = BehaviorSubject(); final SubscriptionEnabledFlag subscriptionEnabledFlag; + final KeyValueStore keyValueStore; String _lastEnteredValue; @@ -31,6 +37,7 @@ class EnterActivationCodeBloc extends BlocBase { this.crashAnalytics, this.appFunctions, this.subscriptionEnabledFlag, + this.keyValueStore, ) { _changeEnterActivationCodeResult(NoDataEnterActivationCodeResult()); } @@ -44,9 +51,9 @@ class EnterActivationCodeBloc extends BlocBase { Function(EnterActivationCodeResult) get _changeEnterActivationCodeResult => _enterActivationCodeSubject.sink.add; - Future retry() async { + Future retry(BuildContext context) async { if (_lastEnteredValue != null) { - return _enterValue(_lastEnteredValue); + return _enterValue(_lastEnteredValue, context); } } @@ -60,15 +67,15 @@ class EnterActivationCodeBloc extends BlocBase { _lastEnteredValue = currentText; } - Future submit() async { - _enterValue(_lastEnteredValue); + Future submit(BuildContext context) async { + _enterValue(_lastEnteredValue, context); } bool get isValidActivationCodeID { return _lastEnteredValue != null && _lastEnteredValue.trim().isNotEmpty; } - Future _enterValue(String enteredValue) async { + Future _enterValue(String enteredValue, BuildContext context) async { if (isEmptyOrNull(enteredValue)) return; _lastEnteredValue = enteredValue; @@ -83,12 +90,33 @@ class EnterActivationCodeBloc extends BlocBase { return; } + if (_lastEnteredValue.trim().toLowerCase() == 'clearcache') { + await _clearCache(context); + return; + } + _changeEnterActivationCodeResult(LoadingEnterActivationCodeResult()); final enterActivationCodeResult = await _runAppFunction(enteredValue); _changeEnterActivationCodeResult(enterActivationCodeResult); } + Future _clearCache(BuildContext context) async { + await Future.wait([ + keyValueStore.clear(), + FeatureDiscovery.clearPreferences(context, [ + blackboardItemReadByUsersListFeatureDiscoveryStepId, + ]), + ]); + + _changeEnterActivationCodeResult( + SuccessfullEnterActivationCodeResult( + 'clear', + 'Cache geleert. Möglicherweise ist ein App-Neustart notwendig, um die Änderungen zu sehen.', + ), + ); + } + Future _runAppFunction(String value) { return enterActivationCodeFunction.activateCode(value); } diff --git a/app/lib/activation_code/src/bloc/enter_activation_code_bloc_factory.dart b/app/lib/activation_code/src/bloc/enter_activation_code_bloc_factory.dart index cbbfcc26e..adaa01115 100644 --- a/app/lib/activation_code/src/bloc/enter_activation_code_bloc_factory.dart +++ b/app/lib/activation_code/src/bloc/enter_activation_code_bloc_factory.dart @@ -10,6 +10,7 @@ import 'package:analytics/analytics.dart'; import 'package:app_functions/sharezone_app_functions.dart'; import 'package:bloc_base/bloc_base.dart'; import 'package:crash_analytics/crash_analytics.dart'; +import 'package:key_value_store/key_value_store.dart'; import 'package:meta/meta.dart'; import 'package:sharezone/activation_code/src/bloc/enter_activation_code_bloc.dart'; @@ -20,12 +21,14 @@ class EnterActivationCodeBlocFactory extends BlocBase { final Analytics analytics; final SharezoneAppFunctions appFunctions; final SubscriptionEnabledFlag subscriptionEnabledFlag; + final KeyValueStore keyValueStore; EnterActivationCodeBlocFactory({ @required this.analytics, @required this.crashAnalytics, @required this.appFunctions, @required this.subscriptionEnabledFlag, + @required this.keyValueStore, }); EnterActivationCodeBloc createBloc() { @@ -34,6 +37,7 @@ class EnterActivationCodeBlocFactory extends BlocBase { crashAnalytics, appFunctions, subscriptionEnabledFlag, + keyValueStore, ); } diff --git a/app/lib/activation_code/src/enter_activation_code_result_dialog.dart b/app/lib/activation_code/src/enter_activation_code_result_dialog.dart index b3bb18c78..63eb7f7ca 100644 --- a/app/lib/activation_code/src/enter_activation_code_result_dialog.dart +++ b/app/lib/activation_code/src/enter_activation_code_result_dialog.dart @@ -48,7 +48,7 @@ class EnterActivationCodeResultDialog { actions: [ ActionItem( title: "Nochmal versuchen", - onSelect: () => enterActivationCodeBloc.retry(), + onSelect: () => enterActivationCodeBloc.retry(context), ), ], ); diff --git a/app/lib/activation_code/src/widgets/enter_activation_code_text_field.dart b/app/lib/activation_code/src/widgets/enter_activation_code_text_field.dart index 4b34adec2..1baf4790b 100644 --- a/app/lib/activation_code/src/widgets/enter_activation_code_text_field.dart +++ b/app/lib/activation_code/src/widgets/enter_activation_code_text_field.dart @@ -69,7 +69,7 @@ class EnterActivationCodeTextField extends StatelessWidget ) async { hideKeyboard(context: context); if (bloc.isValidActivationCodeID) { - bloc.submit(); + bloc.submit(context); final dialog = EnterActivationCodeResultDialog(bloc); dialog.show(context); } diff --git a/app/lib/blocs/sharezone_bloc_providers.dart b/app/lib/blocs/sharezone_bloc_providers.dart index a91640c3e..251d81523 100644 --- a/app/lib/blocs/sharezone_bloc_providers.dart +++ b/app/lib/blocs/sharezone_bloc_providers.dart @@ -407,6 +407,7 @@ class _SharezoneBlocProvidersState extends State { analytics: analytics, appFunctions: api.references.functions, subscriptionEnabledFlag: subscriptionEnabledFlag, + keyValueStore: widget.blocDependencies.keyValueStore, ), ), BlocProvider(