Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.11.2
COCOAPODS: 1.11.3
3 changes: 3 additions & 0 deletions lib/data/api/api_config.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:injectable/injectable.dart';

@Injectable()
class ApiConfig {
final String baseUrl;

Expand Down
3 changes: 3 additions & 0 deletions lib/data/preferences/auth_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:flutter_template/data/model/auth/auth_tokens.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// This class handles the authentication state of the whole app.

@LazySingleton()
class AuthPreferences {
AuthPreferences(this._preferences);

Expand Down
3 changes: 3 additions & 0 deletions lib/data/preferences/user_shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import 'dart:convert';

import 'package:flutter_template/domain/entities/user.dart';
import 'package:flutter_template/domain/preferences/user_preferences.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// Store the current authenticated user's basic information.

@LazySingleton()
class UserSharedPreferences extends UserPreferences {
UserSharedPreferences(this._preferences);

Expand Down
3 changes: 3 additions & 0 deletions lib/data/services/http_client/dio_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:dio/dio.dart';
import 'package:flutter_template/data/services/http_client/http_client.dart';
import 'package:flutter_template/data/services/response_error.dart';
import 'package:injectable/injectable.dart';

/// Abstraction of the Dio http client class.

@Injectable(as: HttpClient)
class DioHttpClient extends HttpClient {
DioHttpClient(this._dio);

Expand Down
2 changes: 2 additions & 0 deletions lib/data/services/http_profile_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter_template/domain/services/profile_service.dart';
import 'package:injectable/injectable.dart';

@LazySingleton(as: ProfileService)
class HttpProfileService extends ProfileService {
HttpProfileService();

Expand Down
69 changes: 22 additions & 47 deletions lib/injection/data_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,31 @@ import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter_template/data/api/api_config.dart';
import 'package:flutter_template/data/interceptor/auth_interceptor.dart';
import 'package:flutter_template/data/preferences/auth_preferences.dart';
import 'package:flutter_template/data/preferences/user_shared_preferences.dart';
import 'package:flutter_template/data/services/http_client/dio_http_client.dart';
import 'package:flutter_template/data/services/http_client/http_client.dart';
import 'package:flutter_template/data/services/http_profile_service.dart';
import 'package:flutter_template/domain/preferences/user_preferences.dart';
import 'package:flutter_template/domain/services/profile_service.dart';
import 'package:flutter_template/injection/injector.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DataModule {
static Future<void> inject() async {
// Prefs
injector.registerLazySingleton<AuthPreferences>(
() => AuthPreferences(injector()),
);
injector.registerLazySingleton<UserPreferences>(
() => UserSharedPreferences(injector()),
);

// Http
injector.registerFactory<DioHttpClient>(() {
return DioHttpClient(Dio());
});

injector.registerFactory<HttpClient>(() {
return DioHttpClient(Dio());
});
@module
abstract class DataModule {
@singleton
Future<SharedPreferences> get prefs => SharedPreferences.getInstance();

injector.registerLazySingleton<Dio>(() {
final dio = Dio();
dio.httpClientAdapter = DefaultHttpClientAdapter();
dio.options.baseUrl = injector.get<ApiConfig>().apiUrl;
dio.interceptors.add(
AuthInterceptor(
httpClient: injector(),
authPreferences: injector(),
userPreferences: injector(),
refreshTokenHttpClient: injector(),
onTokenExpired: () {
// Handle log out
},
),
);

return dio;
});

// Services
injector.registerLazySingleton<ProfileService>(
() => HttpProfileService(),
Dio getDio(ApiConfig apiConfig) {
final dio = Dio();
dio.httpClientAdapter = DefaultHttpClientAdapter();
dio.options.baseUrl = apiConfig.apiUrl;
dio.interceptors.add(
AuthInterceptor(
httpClient: injector(),
authPreferences: injector(),
userPreferences: injector(),
refreshTokenHttpClient: injector(),
onTokenExpired: () {
// TODO: Handle log out
},
),
);

return dio;
}
}
11 changes: 1 addition & 10 deletions lib/injection/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import 'package:flutter_template/injection/data_module.dart';
import 'package:flutter_template/presentation/app_flavor.dart';
import 'package:flutter_template/presentation/feature/profile/profile_module.dart';
import 'package:flutter_template/presentation/routes/app_navigator.dart';
import 'package:flutter_template/presentation/routes/router.gr.dart';

import 'injector.dart';
Expand All @@ -10,13 +7,7 @@ class DependencyManager {
static Future<void> inject(AppFlavor flavor) async {
injector.registerLazySingleton<AppFlavor>(() => flavor);
injector.registerLazySingleton<AppRouter>(() => AppRouter());
injector.registerLazySingleton<AppNavigator>(
() => AppNavigator(injector.get<AppRouter>()));

// App modules
await DataModule.inject();

// Feature modules
await ProfileModule.inject();
await configureDependencies();
}
}
50 changes: 50 additions & 0 deletions lib/injection/injector.config.dart

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

10 changes: 10 additions & 0 deletions lib/injection/injector.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import 'package:flutter_template/injection/injector.config.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

GetIt injector = GetIt.instance;

@InjectableInit(
initializerName: r'$initGetIt', // default
preferRelativeImports: true, // default
asExtension: false, // default
)
Future configureDependencies() async => $initGetIt(injector);

5 changes: 3 additions & 2 deletions lib/presentation/feature/profile/profile_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_template/domain/services/profile_service.dart';
import 'package:flutter_template/extensions/extensions.dart';
import 'package:flutter_template/presentation/feature/profile/profile_state.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

@Injectable()
class ProfileCubit extends Cubit<ProfileState> {
final ProfileService profileService;

ProfileCubit({
required this.profileService,
@visibleForTesting ProfileState? state,
@factoryParam ProfileState? state,
}) : super(state ?? ProfileState.initial());

void load() async {
Expand Down
10 changes: 0 additions & 10 deletions lib/presentation/feature/profile/profile_module.dart
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
import 'package:flutter_template/injection/injector.dart';
import 'package:flutter_template/presentation/feature/profile/profile_cubit.dart';

class ProfileModule {
static Future<void> inject() async {
injector.registerFactory<ProfileCubit>(
() => ProfileCubit(profileService: injector()),
);
}
}
2 changes: 2 additions & 0 deletions lib/presentation/routes/app_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter_template/presentation/routes/router.gr.dart';
import 'package:injectable/injectable.dart';

@LazySingleton()
class AppNavigator {
final AppRouter _appRouter;

Expand Down
2 changes: 2 additions & 0 deletions lib/presentation/routes/router.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter_template/presentation/feature/home/home_screen.dart';
import 'package:flutter_template/presentation/feature/splash/splash_screen.dart';
import 'package:injectable/injectable.dart';

@MaterialAutoRouter(
routes: <AutoRoute>[
AutoRoute(page: SplashScreen, path: '/'),
AutoRoute(page: HomeScreen, path: '/home'),
],
)
@Injectable()
class $AppRouter {}
18 changes: 16 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ packages:
name: device_info_plus
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.2"
version: "3.2.3"
device_info_plus_linux:
dependency: transitive
description:
Expand All @@ -217,7 +217,7 @@ packages:
name: device_info_plus_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.2"
version: "2.2.3"
device_info_plus_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -380,6 +380,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
injectable:
dependency: "direct main"
description:
name: injectable
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
injectable_generator:
dependency: "direct dev"
description:
name: injectable_generator
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
io:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies:
# Architecture
freezed_annotation: 2.0.1
json_annotation: ^4.4.0
injectable: 1.5.3
get_it: ^7.2.0
auto_route: ^3.2.4
flutter_bloc: ^8.0.1
Expand All @@ -55,6 +56,7 @@ dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.1.8
injectable_generator: 1.5.3
freezed: 2.0.2
json_serializable: ^6.1.5
auto_route_generator: ^3.2.3
Expand Down