From b09981a62ae0954369be6ee95279be1cc90f92ec Mon Sep 17 00:00:00 2001 From: Sol Mendiola <57235692+SolMendiola@users.noreply.github.com> Date: Wed, 18 Jan 2023 17:23:44 -0300 Subject: [PATCH] feat: Added clean data (#68) * Added clean data * Resolve comments * Resolve comments --- ios/Podfile.lock | 11 ++--- lib/core/common/config.dart | 15 +++---- .../store/secure_storage_cached_source.dart | 13 +++--- lib/core/di/di_provider.dart | 18 +++------ lib/core/di/di_repository_module.dart | 7 +--- lib/core/di/di_utils_module.dart | 27 ++++++++++++- lib/core/repository/session_repository.dart | 5 ++- .../local_source/session_local_source.dart | 6 +-- .../shared_preferences_provider.dart | 40 +++++++++++++++++++ lib/main.dart | 1 - pubspec.lock | 15 ++----- pubspec.yaml | 1 + 12 files changed, 105 insertions(+), 54 deletions(-) create mode 100644 lib/core/source/providers/shared_preferences_provider.dart diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 11421ed..0e20281 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -66,8 +66,9 @@ PODS: - path_provider_ios (0.0.1): - Flutter - PromisesObjC (2.1.1) - - shared_preferences_ios (0.0.1): + - shared_preferences_foundation (0.0.1): - Flutter + - FlutterMacOS - sqflite (0.0.2): - Flutter - FMDB (>= 2.7.5) @@ -87,7 +88,7 @@ DEPENDENCIES: - flutter_web_browser (from `.symlinks/plugins/flutter_web_browser/ios`) - package_info_plus (from `.symlinks/plugins/package_info_plus/ios`) - path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`) - - shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`) + - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/ios`) - sqflite (from `.symlinks/plugins/sqflite/ios`) - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) - webview_flutter_wkwebview (from `.symlinks/plugins/webview_flutter_wkwebview/ios`) @@ -127,8 +128,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/package_info_plus/ios" path_provider_ios: :path: ".symlinks/plugins/path_provider_ios/ios" - shared_preferences_ios: - :path: ".symlinks/plugins/shared_preferences_ios/ios" + shared_preferences_foundation: + :path: ".symlinks/plugins/shared_preferences_foundation/ios" sqflite: :path: ".symlinks/plugins/sqflite/ios" url_launcher_ios: @@ -158,7 +159,7 @@ SPEC CHECKSUMS: package_info_plus: 6c92f08e1f853dc01228d6f553146438dafcd14e path_provider_ios: 14f3d2fd28c4fdb42f44e0f751d12861c43cee02 PromisesObjC: ab77feca74fa2823e7af4249b8326368e61014cb - shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad + shared_preferences_foundation: 297b3ebca31b34ec92be11acd7fb0ba932c822ca sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904 url_launcher_ios: 839c58cdb4279282219f5e248c3321761ff3c4de webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f diff --git a/lib/core/common/config.dart b/lib/core/common/config.dart index 2494abd..b4ede7c 100644 --- a/lib/core/common/config.dart +++ b/lib/core/common/config.dart @@ -28,11 +28,18 @@ extension EnviromentPath on Environments { } abstract class Config { + static const debugMode = kDebugMode; + static const firebaseEnabled = !debugMode; + static const analyticsEnabled = !debugMode; + static const crashlyticsEnabled = !debugMode; + static bool bugseeEnabled = !debugMode && _environment == Environments.dev; + static const String _environmentFolder = 'environments'; static final num maxDatabaseIntValue = pow(2, 32) - 1; static const Duration durationAnimation = Duration(milliseconds: 150); static const Duration splashMinDuration = Duration(milliseconds: 300); static const String appEmail = 'fluttips@xmartlabs.com'; + static Uri imagesTipsRepository = Uri.parse('https://www.github.com/vandadnp/flutter-tips-and-tricks'); static Uri widgetOfTheWeekLink = Uri.parse( @@ -45,13 +52,7 @@ abstract class Config { Uri.parse('https://www.instagram.com/xmartlabs/?hl=es'); static Uri xmartlabsTwitter = Uri.parse('https://twitter.com/xmartlabs'); - static const debugMode = kDebugMode; - - static const firebaseEnabled = !debugMode; - static const analyticsEnabled = !debugMode; - static const crashlyticsEnabled = !debugMode; - - static bool bugseeEnabled = !debugMode && _environment == Environments.dev; + static const databaseName = 'database.db'; static const apiBaseUrl = 'https://api.github.com/repos/vandadnp/flutter-tips-and-tricks'; diff --git a/lib/core/common/store/secure_storage_cached_source.dart b/lib/core/common/store/secure_storage_cached_source.dart index 0f8a2f2..f421eb4 100644 --- a/lib/core/common/store/secure_storage_cached_source.dart +++ b/lib/core/common/store/secure_storage_cached_source.dart @@ -1,17 +1,18 @@ import 'dart:async'; import 'package:flutter/widgets.dart'; -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:fluttips/core/source/providers/shared_preferences_provider.dart'; import 'package:stock/stock.dart'; -class SecuredStorageSourceOfTruth extends CachedSourceOfTruth { - final FlutterSecureStorage _secureStorage; +class SharedPreferencesSourceOfTruth + extends CachedSourceOfTruth { + final LocalSharedPreferencesStorage _storage; - SecuredStorageSourceOfTruth(this._secureStorage); + SharedPreferencesSourceOfTruth(this._storage); @override Stream reader(String key) async* { - final stringValue = await _secureStorage.read(key: key); + final stringValue = await _storage.read(key: key); setCachedValue(key, stringValue); yield* super.reader(key); } @@ -20,6 +21,6 @@ class SecuredStorageSourceOfTruth extends CachedSourceOfTruth { @protected Future write(String key, String? value) async { await super.write(key, value); - await _secureStorage.write(key: key, value: value); + await _storage.write(key: key, value: value); } } diff --git a/lib/core/di/di_provider.dart b/lib/core/di/di_provider.dart index fd3aa2f..f1793df 100644 --- a/lib/core/di/di_provider.dart +++ b/lib/core/di/di_provider.dart @@ -3,21 +3,15 @@ import 'package:fluttips/core/di/di_utils_module.dart'; import 'package:get_it/get_it.dart'; abstract class DiProvider { - static bool _initialized = false; + static GetIt get _instance => GetIt.instance; - static GetIt get _instance { - final instance = GetIt.instance; - if (!_initialized) { - RepositoryDiModule().setupModule(instance); - UtilsDiModule().setupModule(instance); - _initialized = true; - } - - return instance; + static Future init() async { + await UtilsDiModule().setupProviders(_instance); + RepositoryDiModule().setupModule(_instance); + UtilsDiModule().setupModule(_instance); + await _instance.allReady(); } - static Future init() => _instance.allReady(); - static T get({ String? instanceName, dynamic param1, diff --git a/lib/core/di/di_repository_module.dart b/lib/core/di/di_repository_module.dart index fbedad4..1bbcb97 100644 --- a/lib/core/di/di_repository_module.dart +++ b/lib/core/di/di_repository_module.dart @@ -1,4 +1,3 @@ -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:fluttips/core/repository/session_repository.dart'; import 'package:fluttips/core/repository/tip_repository.dart'; import 'package:fluttips/core/source/common/http_service.dart'; @@ -25,15 +24,11 @@ class RepositoryDiModule { extension _GetItUseCaseDiModuleExtensions on GetIt { void _setupProvidersAndUtils() { - registerLazySingleton(FlutterSecureStorage.new); registerLazySingleton(() => HttpServiceDio([])); - registerSingletonAsync( - () => $FloorAppDatabase.databaseBuilder('database.db').build(), - ); } void _setupRepositories() { - registerLazySingleton(() => SessionRepository()); + registerLazySingleton(() => SessionRepository(get())); registerLazySingleton(() => TipRepository(get(), get())); } diff --git a/lib/core/di/di_utils_module.dart b/lib/core/di/di_utils_module.dart index 1ad23e1..a5a7d91 100644 --- a/lib/core/di/di_utils_module.dart +++ b/lib/core/di/di_utils_module.dart @@ -1,5 +1,10 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:fluttips/core/source/database.dart'; +import 'package:fluttips/core/source/providers/shared_preferences_provider.dart'; import 'package:fluttips/ui/app_router.dart'; import 'package:get_it/get_it.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:fluttips/core/common/config.dart'; class UtilsDiModule { UtilsDiModule._privateConstructor(); @@ -11,10 +16,30 @@ class UtilsDiModule { void setupModule(GetIt locator) { locator._setupModule(); } + + Future setupProviders(GetIt locator) { + locator._setupProviders(); + return locator.allReady(); + } } -extension _GetItUseCaseDiModuleExtensions on GetIt { +extension _GetItUtilsDiModuleExtensions on GetIt { void _setupModule() { registerSingleton(AppRouter()); } + + void _setupProviders() { + registerSingletonAsync( + () => $FloorAppDatabase.databaseBuilder(Config.databaseName).build(), + ); + registerLazySingleton(FlutterSecureStorage.new); + registerSingletonAsync(() => SharedPreferences.getInstance()); + + registerSingletonAsync( + () async => LocalSharedPreferencesStorage( + get(), + await getAsync(), + ).init(), + ); + } } diff --git a/lib/core/repository/session_repository.dart b/lib/core/repository/session_repository.dart index 0e3da35..c6471b1 100644 --- a/lib/core/repository/session_repository.dart +++ b/lib/core/repository/session_repository.dart @@ -1,11 +1,12 @@ import 'dart:async'; -import 'package:fluttips/core/di/di_provider.dart'; import 'package:fluttips/core/model/onboarding_status.dart'; import 'package:fluttips/core/source/local_source/session_local_source.dart'; class SessionRepository { - final SessionLocalSource _sessionLocalSource = DiProvider.get(); + final SessionLocalSource _sessionLocalSource; + + SessionRepository(this._sessionLocalSource); Stream getSessionStatus() => _sessionLocalSource .getSessionStatus() diff --git a/lib/core/source/local_source/session_local_source.dart b/lib/core/source/local_source/session_local_source.dart index c97b985..098f610 100644 --- a/lib/core/source/local_source/session_local_source.dart +++ b/lib/core/source/local_source/session_local_source.dart @@ -1,7 +1,7 @@ -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:fluttips/core/common/helper/enum_helpers.dart'; import 'package:fluttips/core/common/store/secure_storage_cached_source.dart'; import 'package:fluttips/core/model/onboarding_status.dart'; +import 'package:fluttips/core/source/providers/shared_preferences_provider.dart'; import 'package:stock/stock.dart'; class SessionLocalSource { @@ -10,8 +10,8 @@ class SessionLocalSource { late SourceOfTruth _userOnboardedStorage; - SessionLocalSource(FlutterSecureStorage storage) { - final secureStorage = SecuredStorageSourceOfTruth(storage); + SessionLocalSource(LocalSharedPreferencesStorage storage) { + final secureStorage = SharedPreferencesSourceOfTruth(storage); _userOnboardedStorage = secureStorage.mapToUsingMapper(_AppSessionStatusStockTypeMapper()); } diff --git a/lib/core/source/providers/shared_preferences_provider.dart b/lib/core/source/providers/shared_preferences_provider.dart new file mode 100644 index 0000000..ea9159e --- /dev/null +++ b/lib/core/source/providers/shared_preferences_provider.dart @@ -0,0 +1,40 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class LocalSharedPreferencesStorage { + static const String _keyPrefix = 'SharedPreferencesProvider.'; + static const String _alreadyCleanDataKey = '${_keyPrefix}alreadyCleanData'; + + final FlutterSecureStorage _flutterSecureStorage; + + final SharedPreferences _sharedPreferences; + + LocalSharedPreferencesStorage( + this._flutterSecureStorage, + this._sharedPreferences, + ); + + Future init() async { + await _clearSecureStorageOnReinstall(); + return this; + } + + Future _clearSecureStorageOnReinstall() async { + if (!(_sharedPreferences.getBool(_alreadyCleanDataKey) ?? false)) { + await _flutterSecureStorage.deleteAll(); + await _sharedPreferences.setBool(_alreadyCleanDataKey, true); + } + } + + Future read({required String key}) => + _flutterSecureStorage.read(key: key); + + Future write({required String key, required String? value}) => + _flutterSecureStorage.write(key: key, value: value); + + Future delete({required String key}) => + _flutterSecureStorage.delete(key: key); + + Future containsKey({required String key}) => + _flutterSecureStorage.containsKey(key: key); +} diff --git a/lib/main.dart b/lib/main.dart index 349a317..42ed054 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -10,7 +10,6 @@ import 'package:fluttips/core/common/logger.dart'; import 'package:fluttips/core/di/di_provider.dart'; import 'package:fluttips/firebase_options.dart'; import 'package:fluttips/ui/main/main_screen.dart'; - import 'package:bugsee_flutter/bugsee.dart'; Future main() async { diff --git a/pubspec.lock b/pubspec.lock index 4226181..aeb633c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -995,12 +995,12 @@ packages: source: hosted version: "3.5.0" shared_preferences: - dependency: transitive + dependency: "direct main" description: name: shared_preferences url: "https://pub.dartlang.org" source: hosted - version: "2.0.15" + version: "2.0.16" shared_preferences_android: dependency: transitive description: @@ -1008,10 +1008,10 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.14" - shared_preferences_ios: + shared_preferences_foundation: dependency: transitive description: - name: shared_preferences_ios + name: shared_preferences_foundation url: "https://pub.dartlang.org" source: hosted version: "2.1.1" @@ -1022,13 +1022,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" - shared_preferences_macos: - dependency: transitive - description: - name: shared_preferences_macos - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.4" shared_preferences_platform_interface: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 22a61b1..da14559 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -48,6 +48,7 @@ dependencies: package_info_plus: 3.0.2 photo_view: 0.14.0 rxdart: 0.27.7 + shared_preferences: 2.0.16 sqflite: 2.2.0+3 stack_trace: 1.10.0 stock: 1.0.1