Skip to content

Commit

Permalink
feat(frontend): add dependencies injection configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
minikin committed Jan 22, 2024
1 parent 820ea53 commit 2752beb
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 62 deletions.
6 changes: 6 additions & 0 deletions catalyst_voices/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ PODS:
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- url_launcher_ios (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)

EXTERNAL SOURCES:
Flutter:
Expand All @@ -23,12 +26,15 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/integration_test/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
url_launcher_ios:
:path: ".symlinks/plugins/url_launcher_ios/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_secure_storage: 23fc622d89d073675f2eaa109381aefbcf5a49be
integration_test: 13825b8a9334a850581300559b8839134b124670
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
url_launcher_ios: bf5ce03e0e2088bad9cc378ea97fa0ed5b49673b

PODFILE CHECKSUM: ff9ae414ffbc80ad6f9d2058e299051a15f6eca7

Expand Down
31 changes: 21 additions & 10 deletions catalyst_voices/lib/app/view/app_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
import 'package:go_router/go_router.dart';

const _restorationScopeId = 'rootVoices';

final class AppContent extends StatelessWidget {
const AppContent({super.key});

List<LocalizationsDelegate<dynamic>> get _localizationsDelegates {
return const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
];
}

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return BlocListener<AuthenticationBloc, AuthenticationState>(
listener: (context, state) {},
child: MaterialApp.router(
restorationScopeId: 'rootVoices',
localizationsDelegates: const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
],
restorationScopeId: _restorationScopeId,
localizationsDelegates: _localizationsDelegates,
supportedLocales: VoicesLocalizations.supportedLocales,
localeListResolutionCallback: basicLocaleListResolution,
routerConfig: AppRouter.init(
authenticationBloc: context.read<AuthenticationBloc>(),
),
title: 'Catalyst Voices',
routerConfig: _routeConfig(context),
title: l10n.homeScreenText,
theme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
bottomNavigationBarTheme: const BottomNavigationBarThemeData(
type: BottomNavigationBarType.fixed,
Expand All @@ -34,4 +39,10 @@ final class AppContent extends StatelessWidget {
),
);
}

GoRouter _routeConfig(BuildContext context) {
return AppRouter.init(
authenticationBloc: context.read<AuthenticationBloc>(),
);
}
}
56 changes: 24 additions & 32 deletions catalyst_voices/lib/app/view/app_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ignore_for_file: discarded_futures

import 'package:catalyst_voices/app/view/app_content.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_repositories/catalyst_voices_repositories.dart';
import 'package:catalyst_voices_services/catalyst_voices_services.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Expand All @@ -13,47 +13,39 @@ final class App extends StatefulWidget {
}

final class _AppState extends State<App> {
late final AuthenticationRepository _authenticationRepository;
late final CredentialsStorageRepository _credentialsStorageRepository;
late final Future<void> _initFuture;

@override
Widget build(BuildContext context) {
return MultiRepositoryProvider(
providers: [
RepositoryProvider.value(
value: _authenticationRepository,
),
],
child: BlocProvider(
create: (_) => AuthenticationBloc(
authenticationRepository: _authenticationRepository,
),
child: const AppContent(),
),
return FutureBuilder<void>(
future: _initFuture,
builder: (context, snapshot) {
return MultiBlocProvider(
providers: _multiBlocProviders(),
child: const AppContent(),
);
},
);
}

@override
Future<void> dispose() async {
await _authenticationRepository.dispose();

super.dispose();
}

@override
void initState() {
super.initState();

_configureRepositories();
_initFuture = _init();
}

void _configureRepositories() {
_credentialsStorageRepository = CredentialsStorageRepository(
secureStorageService: SecureStorageService(),
);
Future<void> _init() async {
await Dependency.instance.init();
}

_authenticationRepository = AuthenticationRepository(
credentialsStorageRepository: _credentialsStorageRepository,
);
List<BlocProvider> _multiBlocProviders() {
return [
BlocProvider<AuthenticationBloc>(
create: (_) => Dependency.instance.get<AuthenticationBloc>(),
),
BlocProvider<LoginBloc>(
create: (_) => Dependency.instance.get<LoginBloc>(),
),
];
}
}
16 changes: 1 addition & 15 deletions catalyst_voices/lib/pages/login/login_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import 'package:catalyst_voices/pages/login/login.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_repositories/catalyst_voices_repositories.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

final class LoginPage extends StatelessWidget {
static const loginPage = Key('LoginInPage');
Expand All @@ -11,17 +8,6 @@ final class LoginPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocProvider(
key: loginPage,
create: (context) {
return LoginBloc(
authenticationRepository:
RepositoryProvider.of<AuthenticationRepository>(
context,
),
);
},
child: const LoginForm(),
);
return const LoginForm();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'authentication/authentication.dart';
export 'dependency/dependency.dart';
export 'login/login.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_blocs/src/dependency/dependency_provider.dart';
import 'package:catalyst_voices_repositories/catalyst_voices_repositories.dart';
import 'package:catalyst_voices_services/catalyst_voices_services.dart';

final class Dependency extends DependencyProvider {
static final Dependency instance = Dependency._();

Dependency._();

Future<void> init() async {
_registerServices();
_registerRepositories();
_registerBlocsWithDependencies();
}

void _registerBlocsWithDependencies() {
this
..registerSingleton<AuthenticationBloc>(
AuthenticationBloc(
authenticationRepository: get(),
),
)
..registerLazySingleton<LoginBloc>(
() => LoginBloc(
authenticationRepository: get(),
),
);
}

void _registerRepositories() {
this
..registerSingleton<CredentialsStorageRepository>(
CredentialsStorageRepository(secureStorageService: get()),
)
..registerSingleton<AuthenticationRepository>(
AuthenticationRepository(credentialsStorageRepository: get()),
);
}

void _registerServices() {
registerSingleton<SecureStorageService>(
SecureStorageService(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';

abstract class DependencyProvider {
static final getIt = GetIt.instance;

static Future<void> get reset => getIt.reset();

@protected
Future<void> allReady() {
return getIt.allReady();
}

T get<T extends Object>() => getIt.get<T>();

Future<T> getAsync<T extends Object>() => getIt.getAsync<T>();

T getWithParam<T extends Object, P extends Object>({P? param}) {
return getIt.get<T>(param1: param);
}

@protected
void registerFactory<T extends Object>(ValueGetter<T> factoryFunc) {
getIt.registerFactory(factoryFunc);
}

@protected
void registerLazySingleton<T extends Object>(
ValueGetter<T> factoryFunc, {
DisposingFunc<T>? dispose,
}) {
getIt.registerLazySingleton(
factoryFunc,
dispose: dispose,
);
}

@protected
void registerSingleton<T extends Object>(T instance) {
getIt.registerSingleton(instance);
}

@protected
void registerSingletonAsync<T extends Object>(
ValueGetter<Future<T>> factoryFunc, {
Iterable<Type>? dependsOn,
}) {
getIt.registerSingletonAsync(
factoryFunc,
dependsOn: dependsOn,
);
}

@protected
void registerSingletonWithDependencies<T extends Object>(
FactoryFunc<T> factoryFunc, {
required List<Type> dependsOn,
}) {
getIt.registerSingletonWithDependencies(
factoryFunc,
dependsOn: dependsOn,
);
}
}
3 changes: 3 additions & 0 deletions catalyst_voices/packages/catalyst_voices_blocs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ dependencies:
path: ../catalyst_voices_models
catalyst_voices_repositories:
path: ../catalyst_voices_repositories
catalyst_voices_services:
path: ../catalyst_voices_services
catalyst_voices_view_models:
path: ../catalyst_voices_view_models
collection: ^1.17.1
equatable: ^2.0.5
flutter:
sdk: flutter
formz: ^0.6.1
get_it: ^7.6.7
meta: ^1.10.0
result_type: ^0.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:catalyst_voices_repositories/catalyst_voices_repositories.dart';

final class AuthenticationRepository {
final CredentialsStorageRepository credentialsStorageRepository;
final _controller = StreamController<AuthenticationStatus>();
final _streamController = StreamController<AuthenticationStatus>();

AuthenticationRepository({required this.credentialsStorageRepository});

Expand All @@ -22,10 +22,10 @@ final class AuthenticationRepository {
yield AuthenticationStatus.unknown;
}

yield* _controller.stream;
yield* _streamController.stream;
}

Future<void> dispose() async => _controller.close();
Future<void> dispose() async => _streamController.close();

Future<SessionData?> getSessionData() async {
try {
Expand All @@ -43,7 +43,7 @@ final class AuthenticationRepository {

void logOut() {
credentialsStorageRepository.clearSessionData;
_controller.add(AuthenticationStatus.unauthenticated);
_streamController.add(AuthenticationStatus.unauthenticated);
}

Future<void> signIn({
Expand All @@ -60,7 +60,7 @@ final class AuthenticationRepository {
// TODO(minikin): remove this delay after implementing real auth flow.
await Future.delayed(
const Duration(milliseconds: 300),
() => _controller.add(AuthenticationStatus.authenticated),
() => _streamController.add(AuthenticationStatus.authenticated),
);
}
}

0 comments on commit 2752beb

Please sign in to comment.