Skip to content

Commit

Permalink
feat: Added clean data (#68)
Browse files Browse the repository at this point in the history
* Added clean data

* Resolve comments

* Resolve comments
  • Loading branch information
SolMendiola authored Jan 18, 2023
1 parent 2abf934 commit b09981a
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 54 deletions.
11 changes: 6 additions & 5 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions lib/core/common/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';

static Uri imagesTipsRepository =
Uri.parse('https://www.github.com/vandadnp/flutter-tips-and-tricks');
static Uri widgetOfTheWeekLink = Uri.parse(
Expand All @@ -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';
Expand Down
13 changes: 7 additions & 6 deletions lib/core/common/store/secure_storage_cached_source.dart
Original file line number Diff line number Diff line change
@@ -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<String, String> {
final FlutterSecureStorage _secureStorage;
class SharedPreferencesSourceOfTruth
extends CachedSourceOfTruth<String, String> {
final LocalSharedPreferencesStorage _storage;

SecuredStorageSourceOfTruth(this._secureStorage);
SharedPreferencesSourceOfTruth(this._storage);

@override
Stream<String?> 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);
}
Expand All @@ -20,6 +21,6 @@ class SecuredStorageSourceOfTruth extends CachedSourceOfTruth<String, String> {
@protected
Future<void> 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);
}
}
18 changes: 6 additions & 12 deletions lib/core/di/di_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> init() async {
await UtilsDiModule().setupProviders(_instance);
RepositoryDiModule().setupModule(_instance);
UtilsDiModule().setupModule(_instance);
await _instance.allReady();
}

static Future<void> init() => _instance.allReady();

static T get<T extends Object>({
String? instanceName,
dynamic param1,
Expand Down
7 changes: 1 addition & 6 deletions lib/core/di/di_repository_module.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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()));
}

Expand Down
27 changes: 26 additions & 1 deletion lib/core/di/di_utils_module.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -11,10 +16,30 @@ class UtilsDiModule {
void setupModule(GetIt locator) {
locator._setupModule();
}

Future<void> 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<SharedPreferences>(),
).init(),
);
}
}
5 changes: 3 additions & 2 deletions lib/core/repository/session_repository.dart
Original file line number Diff line number Diff line change
@@ -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<AppSessionStatus> getSessionStatus() => _sessionLocalSource
.getSessionStatus()
Expand Down
6 changes: 3 additions & 3 deletions lib/core/source/local_source/session_local_source.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,8 +10,8 @@ class SessionLocalSource {

late SourceOfTruth<String, AppSessionStatus> _userOnboardedStorage;

SessionLocalSource(FlutterSecureStorage storage) {
final secureStorage = SecuredStorageSourceOfTruth(storage);
SessionLocalSource(LocalSharedPreferencesStorage storage) {
final secureStorage = SharedPreferencesSourceOfTruth(storage);
_userOnboardedStorage =
secureStorage.mapToUsingMapper(_AppSessionStatusStockTypeMapper());
}
Expand Down
40 changes: 40 additions & 0 deletions lib/core/source/providers/shared_preferences_provider.dart
Original file line number Diff line number Diff line change
@@ -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<LocalSharedPreferencesStorage> init() async {
await _clearSecureStorageOnReinstall();
return this;
}

Future<void> _clearSecureStorageOnReinstall() async {
if (!(_sharedPreferences.getBool(_alreadyCleanDataKey) ?? false)) {
await _flutterSecureStorage.deleteAll();
await _sharedPreferences.setBool(_alreadyCleanDataKey, true);
}
}

Future<String?> read({required String key}) =>
_flutterSecureStorage.read(key: key);

Future<void> write({required String key, required String? value}) =>
_flutterSecureStorage.write(key: key, value: value);

Future<void> delete({required String key}) =>
_flutterSecureStorage.delete(key: key);

Future<void> containsKey({required String key}) =>
_flutterSecureStorage.containsKey(key: key);
}
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> main() async {
Expand Down
15 changes: 4 additions & 11 deletions pubspec.lock

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

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b09981a

Please sign in to comment.