File tree Expand file tree Collapse file tree 9 files changed +63
-172
lines changed
lib/presentation/ui/pages Expand file tree Collapse file tree 9 files changed +63
-172
lines changed Original file line number Diff line number Diff line change 11import 'package:app/main/init.dart' ;
2- import 'package:domain/services/auth_service .dart' ;
2+ import 'package:domain/bloc/auth/auth_cubit .dart' ;
33import 'package:flutter/material.dart' ;
44import 'package:app/presentation/ui/custom/app_theme_switch.dart' ;
55
66class HomeView extends StatelessWidget {
7- AuthService get _authService => getIt ();
7+ /// Given this is a global cubit, we can access it directly from getIt
8+ /// other wise use context.read<AuthCubit>() to read the Cubit under that context
9+ AuthCubit get _authCubit => getIt ();
810
911 const HomeView ({super .key});
1012
@@ -14,7 +16,7 @@ class HomeView extends StatelessWidget {
1416 appBar: AppBar (
1517 actions: [
1618 IconButton (
17- onPressed: () => _authService. onLogout (),
19+ onPressed: () => _authCubit. logOut (),
1820 icon: const Icon (Icons .logout),
1921 ),
2022 const AppThemeSwitch (),
Original file line number Diff line number Diff line change @@ -5,18 +5,17 @@ import 'package:app/presentation/ui/custom/app_theme_switch.dart';
55import 'package:app/presentation/ui/custom/loading_screen.dart' ;
66import 'package:common/core/resource.dart' ;
77import 'package:domain/bloc/auth/auth_cubit.dart' ;
8- import 'package:domain/services/auth_service.dart' ;
98import 'package:flutter/foundation.dart' ;
109import 'package:flutter/material.dart' ;
1110import 'package:flutter_bloc/flutter_bloc.dart' ;
1211
1312import '../../custom/environment_selector.dart' ;
1413
1514class LoginPage extends StatelessWidget {
16- AuthService get _authService => getIt ();
17-
1815 const LoginPage ({super .key});
1916
17+ AuthCubit get _authCubit => getIt ();
18+
2019 @override
2120 Widget build (BuildContext context) {
2221 return Stack (
@@ -29,14 +28,15 @@ class LoginPage extends StatelessWidget {
2928 child: Column (
3029 mainAxisAlignment: MainAxisAlignment .center,
3130 children: [
31+ const _Loading (),
3232 const AppThemeSwitch (),
3333 SizedBox (height: spacing.m),
3434 SizedBox (
3535 width: double .maxFinite,
3636 child: ElevatedButton (
3737 child: const Text ('Login' ),
3838 onPressed: () {
39- _authService. logInWithCredentials (
39+ _authCubit. login (
4040 'Rootstrap' ,
4141 '12345678' ,
4242 );
@@ -51,7 +51,6 @@ class LoginPage extends StatelessWidget {
5151 ),
5252 ),
5353 ),
54- const _Loading (),
5554 ],
5655 );
5756 }
Original file line number Diff line number Diff line change 11import 'package:app/main/init.dart' ;
2- import 'package:domain/services/auth_service .dart' ;
2+ import 'package:domain/bloc/auth/auth_cubit .dart' ;
33import 'package:flutter/material.dart' ;
44
55class SplashPage extends StatefulWidget {
@@ -10,12 +10,19 @@ class SplashPage extends StatefulWidget {
1010}
1111
1212class _SplashPageState extends State <SplashPage > {
13- AuthService get _authService => getIt ();
13+ /// Given this is a global cubit, we can access it directly from getIt
14+ /// other wise use context.read<AuthCubit>() to read the Cubit under that context
15+ AuthCubit get _authCubit => getIt ();
1416
1517 @override
1618 void initState () {
1719 super .initState ();
18- _authService.onValidate ();
20+
21+ /// Add post frame callback to avoid calling bloc methods during build
22+ WidgetsBinding .instance.addPostFrameCallback ((_) async {
23+ await Future .delayed (const Duration (seconds: 1 ));
24+ _authCubit.onValidate ();
25+ });
1926 }
2027
2128 @override
Original file line number Diff line number Diff line change @@ -5,6 +5,9 @@ packages:
55 - modules/*
66
77scripts :
8+ run:web :
9+ exec : cd app && flutter run -t lib/main.dart --dart-define-from-file=env/.dev -d chrome
10+ description : Run the app in development mode for web.
811 lint:all :
912 run : melos run analyze && melos run format
1013 description : Run all static analysis checks.
Original file line number Diff line number Diff line change 11import 'package:common/core/resource.dart' ;
2+ import 'package:common/core/result_type.dart' ;
23import 'package:domain/bloc/base_cubit.dart' ;
34import 'package:domain/bloc/auth/auth_state.dart' ;
5+ import 'package:domain/services/auth_service.dart' ;
46
57class AuthCubit extends BaseCubit <AuthState > {
6- AuthCubit () : super (RSuccess (data: AuthStateUnknown ()));
8+ final AuthService _authService;
9+ AuthCubit (this ._authService) : super (RSuccess (data: AuthStateUnknown ()));
10+
11+ Future login (String username, String password) async {
12+ isLoading ();
13+ final authResult =
14+ await _authService.logInWithCredentials (username, password);
15+
16+ authResult.map (
17+ success: (_) => isLogin (),
18+ error: (failure) {
19+ isError (failure);
20+ return failure;
21+ },
22+ );
23+ }
24+
25+ Future <void > onValidate () async {
26+ if (_authService.isLoggedIn ()) {
27+ isLogin ();
28+ } else {
29+ isLogOut ();
30+ }
31+ }
32+
33+ Future <void > logOut () async {
34+ await _authService.onLogout ();
35+ isLogOut ();
36+ }
737
838 void isLogin () => isSuccess (AuthStateAuthenticated ());
939
Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ import 'package:get_it/get_it.dart';
55
66class DomainInit {
77 static Future <void > initialize (GetIt getIt) async {
8- //Cubits
8+ //Services (with dependencies to repositories)
9+ getIt.registerLazySingleton (() => AuthService (getIt ()));
10+
11+ //Global Cubits
912 getIt.registerSingleton (AppCubit (getIt ()));
10- getIt.registerSingleton (AuthCubit ());
11-
12- //Services
13- getIt.registerLazySingleton (() => AuthService (getIt (), getIt ()));
13+ getIt.registerSingleton (AuthCubit (getIt ()));
1414 }
1515}
Original file line number Diff line number Diff line change 11import 'package:common/core/result_type.dart' ;
2- import 'package:domain/bloc/auth/auth_cubit.dart' ;
32import 'package:domain/repositories/auth_repository.dart' ;
43
54class AuthService {
65 final AuthRepository _authRepository;
7- final AuthCubit _sessionCubit;
86
9- AuthService (this ._authRepository, this ._sessionCubit );
7+ AuthService (this ._authRepository);
108
11- Future <void > logInWithCredentials (String username, String password) async {
12- _sessionCubit.isLoading ();
13- final result = await _authRepository.login (username, password);
14- switch (result) {
15- case TSuccess <void > _:
16- _sessionCubit.isLogin ();
17- case TError <void > _:
18- _sessionCubit.isError (result.error);
19- }
20- }
9+ Future <ResultType <void >> logInWithCredentials (
10+ String username, String password) =>
11+ _authRepository.login (username, password);
2112
22- void onValidate () {
23- if (_authRepository.isLoggedIn ()) {
24- _sessionCubit.isLogin ();
25- } else {
26- _sessionCubit.isLogOut ();
27- }
28- }
13+ bool isLoggedIn () => _authRepository.isLoggedIn ();
2914
3015 Future <void > onLogout () async {
3116 await _authRepository.logout ();
32- _sessionCubit.isLogOut ();
3317 }
3418}
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments