Skip to content

Commit

Permalink
Refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
proninyaroslav committed Sep 9, 2024
1 parent 6ff930d commit 97eeece
Show file tree
Hide file tree
Showing 187 changed files with 4,440 additions and 3,218 deletions.
17 changes: 1 addition & 16 deletions lib/core/model/http/http_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
Expand All @@ -23,8 +23,6 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:http/http.dart' as http;
import 'package:injectable/injectable.dart';
import 'package:libretrack/core/model/request_builder.dart';
import 'package:libretrack/env.dart';
import 'package:pretty_http_logger/pretty_http_logger.dart';

part 'http_client.freezed.dart';

Expand Down Expand Up @@ -118,16 +116,3 @@ class HttpClientImpl implements HttpClient {
@override
void close() => _client.close();
}

@module
abstract class ClientModule {
@Injectable(env: [Env.prod])
http.Client get clientProd => http.Client();

@Injectable(env: [Env.dev])
http.Client get clientDev => HttpClientWithMiddleware.build(
middlewares: [
HttpLogger(logLevel: LogLevel.BODY),
],
);
}
2 changes: 1 addition & 1 deletion lib/core/notification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:image/image.dart' as image;
import 'package:injectable/injectable.dart';
import 'package:libretrack/ui/model/utils.dart';
import 'package:libretrack/ui/theme.dart';
import 'package:libretrack/ui/utils/utils.dart';
import 'package:quiver/core.dart';

import '../locale.dart';
Expand Down
37 changes: 2 additions & 35 deletions lib/core/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../env.dart';
import 'model.dart';
import 'shared_pref_migrator.dart';

Expand Down Expand Up @@ -326,40 +326,7 @@ abstract class _AppSettingsKey {
static const barcodeGeneratorType = 'pref_key_barcode_generator_type';
}

@module
abstract class SharedPreferencesModule {
@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<SharedPreferences> get prefOld async =>
SharedPreferences.getInstance();

@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<SharedPreferencesAsync> pref(SharedPreferences prefOld) async {
final pref = SharedPreferencesAsync();
final migrator = SharedPreferencesMigrator(
oldPrefs: prefOld,
newPrefs: pref,
);
await migrator.migrate();

return pref;
}

@Singleton(env: [Env.test])
@preResolve
Future<SharedPreferencesAsync> get testPref async =>
TestSharedPreferencesAsync();

@Singleton(env: [Env.test])
@preResolve
Future<SharedPreferences> get testOldPref async {
// ignore: invalid_use_of_visible_for_testing_member
SharedPreferences.setMockInitialValues({});
return SharedPreferences.getInstance();
}
}

@visibleForTesting
class TestSharedPreferencesAsync implements SharedPreferencesAsync {
final Map<String, dynamic> _map = {};

Expand Down
17 changes: 0 additions & 17 deletions lib/core/storage/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import 'dart:async';

import 'package:floor/floor.dart';
import 'package:injectable/injectable.dart';
import 'package:libretrack/core/entity/converter/converter.dart';
import 'package:libretrack/core/entity/entity.dart';
import 'package:libretrack/env.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

import 'dao/dao.dart';
Expand Down Expand Up @@ -60,21 +58,6 @@ abstract class AppDatabase extends FloorDatabase {
TrackNumberServiceDao get trackNumberServiceDao;
}

@module
abstract class AppDatabaseModule {
@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<AppDatabase> get db async => $FloorAppDatabase
.databaseBuilder('libretrack.db')
.addMigrations(migrations)
.build();

@Singleton(env: [Env.test])
@preResolve
Future<AppDatabase> get inMemoryDb async =>
$FloorAppDatabase.inMemoryDatabaseBuilder().build();
}

final migrations = [
Migration(1, 2, (db) async {
await db.transaction((txn) async {
Expand Down
14 changes: 2 additions & 12 deletions lib/core/storage/service_auth_storage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
Expand All @@ -25,8 +25,6 @@ import 'package:libretrack/core/entity/entity.dart';
import 'package:libretrack/core/platform_info.dart';
import 'package:libretrack/core/storage/database.dart';

import '../../env.dart';

abstract class ServiceAuthStorage {
Future<void> insert({
required TrackingServiceType type,
Expand Down Expand Up @@ -212,6 +210,7 @@ Future<AuthData?> _secureStorageGet(
return authData.isEmpty ? null : authData;
}

@visibleForTesting
class TestFlutterSecureStorage implements FlutterSecureStorage {
final Map<String?, String?> _keyValueMap = {};
final Map<String, List<ValueChanged<String?>>> _listeners = {};
Expand Down Expand Up @@ -344,12 +343,3 @@ class TestFlutterSecureStorage implements FlutterSecureStorage {
_keyValueMap[key] = value;
}
}

@module
abstract class FlutterSecureStorageModule {
@Injectable(env: [Env.prod, Env.dev])
FlutterSecureStorage get storage => const FlutterSecureStorage();

@Injectable(env: [Env.test])
FlutterSecureStorage get testStorage => TestFlutterSecureStorage();
}
38 changes: 38 additions & 0 deletions lib/di/app_database_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
//
// LibreTrack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibreTrack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

import 'dart:async';

import 'package:injectable/injectable.dart';
import 'package:libretrack/core/storage/database.dart';
import 'package:libretrack/env.dart';

@module
abstract class AppDatabaseModule {
@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<AppDatabase> get db async => $FloorAppDatabase
.databaseBuilder('libretrack.db')
.addMigrations(migrations)
.build();

@Singleton(env: [Env.test])
@preResolve
Future<AppDatabase> get inMemoryDb async =>
$FloorAppDatabase.inMemoryDatabaseBuilder().build();
}
35 changes: 35 additions & 0 deletions lib/di/client_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
//
// LibreTrack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibreTrack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

import 'package:http/http.dart' as http;
import 'package:injectable/injectable.dart';
import 'package:libretrack/env.dart';
import 'package:pretty_http_logger/pretty_http_logger.dart';

@module
abstract class ClientModule {
@Injectable(env: [Env.prod])
http.Client get clientProd => http.Client();

@Injectable(env: [Env.dev])
http.Client get clientDev => HttpClientWithMiddleware.build(
middlewares: [
HttpLogger(logLevel: LogLevel.BODY),
],
);
}
31 changes: 31 additions & 0 deletions lib/di/flutter_secure_storage_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
//
// LibreTrack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibreTrack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:injectable/injectable.dart';
import 'package:libretrack/core/storage/service_auth_storage.dart';
import 'package:libretrack/env.dart';

@module
abstract class FlutterSecureStorageModule {
@Injectable(env: [Env.prod, Env.dev])
FlutterSecureStorage get storage => const FlutterSecureStorage();

@Injectable(env: [Env.test])
FlutterSecureStorage get testStorage => TestFlutterSecureStorage();
}
57 changes: 57 additions & 0 deletions lib/di/shared_preferences_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
//
// LibreTrack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibreTrack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

import 'package:injectable/injectable.dart';
import 'package:libretrack/core/settings/settings.dart';
import 'package:libretrack/core/settings/shared_pref_migrator.dart';
import 'package:libretrack/env.dart';
import 'package:shared_preferences/shared_preferences.dart';

@module
abstract class SharedPreferencesModule {
@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<SharedPreferences> get prefOld async =>
SharedPreferences.getInstance();

@Singleton(env: [Env.prod, Env.dev])
@preResolve
Future<SharedPreferencesAsync> pref(SharedPreferences prefOld) async {
final pref = SharedPreferencesAsync();
final migrator = SharedPreferencesMigrator(
oldPrefs: prefOld,
newPrefs: pref,
);
await migrator.migrate();

return pref;
}

@Singleton(env: [Env.test])
@preResolve
Future<SharedPreferencesAsync> get testPref async =>
TestSharedPreferencesAsync();

@Singleton(env: [Env.test])
@preResolve
Future<SharedPreferences> get testOldPref async {
// ignore: invalid_use_of_visible_for_testing_member
SharedPreferences.setMockInitialValues({});
return SharedPreferences.getInstance();
}
}
12 changes: 8 additions & 4 deletions lib/injector.config.dart

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

4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import 'package:libretrack/core/crash_catcher/hook/flutter_crash_hook.dart';
import 'package:libretrack/core/notification_manager.dart';
import 'package:libretrack/core/settings/settings.dart';
import 'package:libretrack/platform/system_tray.dart';
import 'package:libretrack/ui/app_cubit.dart';
import 'package:libretrack/ui/model/app_cubit.dart';

import 'core/tracking_scheduler.dart';
import 'core/work_manager/work_manager.dart';
import 'env.dart';
import 'injector.dart';
import 'ui/app.dart';
import 'ui/crash_report_dialog/dialog_crash_handler.dart';
import 'ui/crash_report_dialog/model/dialog_crash_handler.dart';

final navigatorKey = GlobalKey<NavigatorState>();

Expand Down
5 changes: 3 additions & 2 deletions lib/ui/about/about.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021-2024 Yaroslav Pronin <[email protected]>
// Copyright (C) 2021 Insurgo Inc. <[email protected]>
//
// This file is part of LibreTrack.
Expand All @@ -16,5 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

export 'about_cubit.dart';
export 'about_page.dart';
export 'model/about_cubit.dart';
export 'model/about_state.dart';
Loading

0 comments on commit 97eeece

Please sign in to comment.