From 54aa42ec21d6de89910f842ebafeed8e3a58061f Mon Sep 17 00:00:00 2001 From: PGAN-MIS Date: Tue, 21 Mar 2023 12:57:38 +0800 Subject: [PATCH] refactor login status alert and message --- lib/bloc/user/user_bloc.dart | 36 +++++++++++++-------- lib/bloc/user/user_state.dart | 4 ++- lib/screens/unit2/login/login.dart | 28 +++++++++------- lib/sevices/login_service/auth_service.dart | 14 +++++--- lib/utils/alerts.dart | 12 +++++++ lib/utils/urls.dart | 4 +-- 6 files changed, 66 insertions(+), 32 deletions(-) diff --git a/lib/bloc/user/user_bloc.dart b/lib/bloc/user/user_bloc.dart index 104b7dd..1042352 100644 --- a/lib/bloc/user/user_bloc.dart +++ b/lib/bloc/user/user_bloc.dart @@ -29,28 +29,38 @@ class UserBloc extends Bloc { _versionInfo = versionInfo; String apkVersion = await getAppVersion(); _apkVersion = apkVersion; - emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion)); + emit(VersionLoaded(versionInfo: _versionInfo, apkVersion: _apkVersion)); } catch (e) { emit(UserError( message: e.toString(), )); } }); +//Loading the current version of the app on((event, emit) { - emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion)); + emit(VersionLoaded(versionInfo: _versionInfo, apkVersion: _apkVersion)); }); + on((event, emit) async { try { - UserData? userData = await AuthService.instance + Map response = await AuthService.instance .webLogin(username: event.username, password: event.password); - _userData = userData; - emit(UserLoggedIn(userData: _userData)); - } on TimeoutException catch (_) { + if (response['status'] == true) { + UserData userData = UserData.fromJson(response['data']); + emit(UserLoggedIn( + userData: userData, + success: true, + message: response['message'])); + }else{ + emit(UserLoggedIn( + userData: null, + success: false, + message: response['message'])); + } + } on TimeoutException catch (_) { + emit(InternetTimeout(message: timeoutError)); + } on SocketException catch (_) { emit(InternetTimeout(message: timeoutError)); - }on SocketException catch (_){ - emit(InternetTimeout(message:timeoutError)); - }on Error catch(_){ - emit(InvalidCredentials(message: "Invalid username or password")); } }); on((event, emit) async { @@ -61,9 +71,9 @@ class UserBloc extends Bloc { emit(UserLoggedIn(userData: _userData)); } on TimeoutException catch (_) { emit(InternetTimeout(message: timeoutError)); - }on SocketException catch (_){ - emit(InternetTimeout(message:timeoutError)); - }on Error catch(_){ + } on SocketException catch (_) { + emit(InternetTimeout(message: timeoutError)); + } on Error catch (_) { emit(InvalidCredentials(message: "Invalid username or password")); } }); diff --git a/lib/bloc/user/user_state.dart b/lib/bloc/user/user_state.dart index 372f052..26d815f 100644 --- a/lib/bloc/user/user_state.dart +++ b/lib/bloc/user/user_state.dart @@ -32,7 +32,9 @@ class UserError extends UserState { } class UserLoggedIn extends UserState{ final UserData? userData; - UserLoggedIn({this.userData}); + final String? message; + final bool? success; + UserLoggedIn({this.userData,this.message,this.success}); } class VersionLoaded extends UserState { diff --git a/lib/screens/unit2/login/login.dart b/lib/screens/unit2/login/login.dart index d59d3d7..b5dc6b5 100644 --- a/lib/screens/unit2/login/login.dart +++ b/lib/screens/unit2/login/login.dart @@ -5,6 +5,7 @@ import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:fluttericon/font_awesome5_icons.dart'; +import 'package:fluttertoast/fluttertoast.dart'; import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:flutter_progress_hud/flutter_progress_hud.dart'; import 'package:unit2/screens/unit2/login/components/update_required.dart'; @@ -42,19 +43,23 @@ class _UniT2LoginState extends State { backgroundColor: Colors.black87, indicatorWidget: const SpinKitFadingCircle(color: Colors.white), child: BlocConsumer(listener: (context, state) { - if (state is UserLoggedIn || - state is UuidLoaded || - state is UserError || - state is InternetTimeout) { + if (state is UserLoggedIn || state is UuidLoaded) { final progress = ProgressHUD.of(context); progress!.dismiss(); - Navigator.pushReplacementNamed(context, '/module-screen'); + } - if (state is InvalidCredentials) { - final progress = ProgressHUD.of(context); - progress!.dismiss(); - errorAlert(context, "Error Login", state.message, () {}); - context.read().add(LoadVersion()); + if (state is UserLoggedIn) { + if (state.success == true) { + Fluttertoast.showToast(msg: state.message!,toastLength: Toast.LENGTH_LONG,gravity: ToastGravity.CENTER); + Navigator.pushReplacementNamed(context, '/module-screen'); + } else { + final progress = ProgressHUD.of(context); + progress!.dismiss(); + errorAlert(context, "Error Login", state.message, () { + context.read().add(LoadVersion()); + Navigator.of(context).pop(); + }); + } } }, builder: (context, state) { if (state is VersionLoaded) { @@ -223,7 +228,8 @@ class _UniT2LoginState extends State { BlocProvider.of(context) .add(UserLogin( - username: "rjvincentlopeplopez", + username: + "rjvincentlopeplopez", password: "shesthequ33n", // username: _formKey // .currentState! diff --git a/lib/sevices/login_service/auth_service.dart b/lib/sevices/login_service/auth_service.dart index 477abe8..497e8f4 100644 --- a/lib/sevices/login_service/auth_service.dart +++ b/lib/sevices/login_service/auth_service.dart @@ -33,23 +33,27 @@ class AuthService { return versionInfo; } - Future webLogin({String? username, String? password})async{ + Future> webLogin({String? username, String? password})async{ Map body ={'username':username!,'password':password!}; Map baseHeaders = { 'Content-Type': 'application/json' }; + Map responseStatus = {}; String path = Url.instance.authentication(); UserData? userData; try{ http.Response response = await Request.instance.postRequest(path: path,param: {},headers: baseHeaders,body: body); - if(response.statusCode == 200){ + Map data = jsonDecode(response.body); - userData = UserData.fromJson(data['data']); - } + + responseStatus = data; + + + return responseStatus; }catch(e){ throw (e.toString()); } - return userData!; + } Future qrLogin({String? uuid, String? password})async{ Map body ={'uuid':uuid!,'password':password!}; diff --git a/lib/utils/alerts.dart b/lib/utils/alerts.dart index 3517fb7..a3e6f4e 100644 --- a/lib/utils/alerts.dart +++ b/lib/utils/alerts.dart @@ -61,3 +61,15 @@ successAlert(context, title, description,Function() func) { btnOk: SizedBox(height: 50,child: ElevatedButton(style: mainBtnStyle(success2, Colors.transparent, success), onPressed: func, child: const Text("OK")), ) ).show(); } +okAlert(context,title,description){ + AwesomeDialog( + width: blockSizeHorizontal * 90, + context: context, + dialogType: DialogType.error, + animType: AnimType.scale, + headerAnimationLoop: false, + title: title, + desc: description, + btnOkOnPress: () {}, + ).show(); +} diff --git a/lib/utils/urls.dart b/lib/utils/urls.dart index bff5d0e..e04f450 100644 --- a/lib/utils/urls.dart +++ b/lib/utils/urls.dart @@ -3,8 +3,8 @@ class Url { static Url get instance => _instance; String host() { - return '192.168.10.221:3003'; - // return 'agusandelnorte.gov.ph'; + // return '192.168.10.221:3003'; + return 'agusandelnorte.gov.ph'; // return "192.168.10.219:3000"; // return "devweb.agusandelnorte.gov.ph"; // return 'devapi.agusandelnorte.gov.ph:3004';