refactor login status alert and message

feature/passo/PASSO-#1-Sync-data-from-device-to-postgre-and-vice-versa
PGAN-MIS 2023-03-21 12:57:38 +08:00
parent f1ed33a6c0
commit 54aa42ec21
6 changed files with 66 additions and 32 deletions

View File

@ -29,28 +29,38 @@ class UserBloc extends Bloc<UserEvent, UserState> {
_versionInfo = versionInfo; _versionInfo = versionInfo;
String apkVersion = await getAppVersion(); String apkVersion = await getAppVersion();
_apkVersion = apkVersion; _apkVersion = apkVersion;
emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion)); emit(VersionLoaded(versionInfo: _versionInfo, apkVersion: _apkVersion));
} catch (e) { } catch (e) {
emit(UserError( emit(UserError(
message: e.toString(), message: e.toString(),
)); ));
} }
}); });
//Loading the current version of the app
on<LoadVersion>((event, emit) { on<LoadVersion>((event, emit) {
emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: _apkVersion)); emit(VersionLoaded(versionInfo: _versionInfo, apkVersion: _apkVersion));
}); });
on<UserLogin>((event, emit) async { on<UserLogin>((event, emit) async {
try { try {
UserData? userData = await AuthService.instance Map<dynamic, dynamic> response = await AuthService.instance
.webLogin(username: event.username, password: event.password); .webLogin(username: event.username, password: event.password);
_userData = userData; if (response['status'] == true) {
emit(UserLoggedIn(userData: _userData)); UserData userData = UserData.fromJson(response['data']);
} on TimeoutException catch (_) { 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)); emit(InternetTimeout(message: timeoutError));
}on SocketException catch (_){
emit(InternetTimeout(message:timeoutError));
}on Error catch(_){
emit(InvalidCredentials(message: "Invalid username or password"));
} }
}); });
on<UuidLogin>((event, emit) async { on<UuidLogin>((event, emit) async {
@ -61,9 +71,9 @@ class UserBloc extends Bloc<UserEvent, UserState> {
emit(UserLoggedIn(userData: _userData)); emit(UserLoggedIn(userData: _userData));
} on TimeoutException catch (_) { } on TimeoutException catch (_) {
emit(InternetTimeout(message: timeoutError)); emit(InternetTimeout(message: timeoutError));
}on SocketException catch (_){ } on SocketException catch (_) {
emit(InternetTimeout(message:timeoutError)); emit(InternetTimeout(message: timeoutError));
}on Error catch(_){ } on Error catch (_) {
emit(InvalidCredentials(message: "Invalid username or password")); emit(InvalidCredentials(message: "Invalid username or password"));
} }
}); });

View File

@ -32,7 +32,9 @@ class UserError extends UserState {
} }
class UserLoggedIn extends UserState{ class UserLoggedIn extends UserState{
final UserData? userData; final UserData? userData;
UserLoggedIn({this.userData}); final String? message;
final bool? success;
UserLoggedIn({this.userData,this.message,this.success});
} }
class VersionLoaded extends UserState { class VersionLoaded extends UserState {

View File

@ -5,6 +5,7 @@ import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart'; import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:unit2/screens/unit2/login/components/update_required.dart'; import 'package:unit2/screens/unit2/login/components/update_required.dart';
@ -42,19 +43,23 @@ class _UniT2LoginState extends State<UniT2Login> {
backgroundColor: Colors.black87, backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white), indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
child: BlocConsumer<UserBloc, UserState>(listener: (context, state) { child: BlocConsumer<UserBloc, UserState>(listener: (context, state) {
if (state is UserLoggedIn || if (state is UserLoggedIn || state is UuidLoaded) {
state is UuidLoaded ||
state is UserError ||
state is InternetTimeout) {
final progress = ProgressHUD.of(context); final progress = ProgressHUD.of(context);
progress!.dismiss(); progress!.dismiss();
Navigator.pushReplacementNamed(context, '/module-screen');
} }
if (state is InvalidCredentials) { if (state is UserLoggedIn) {
final progress = ProgressHUD.of(context); if (state.success == true) {
progress!.dismiss(); Fluttertoast.showToast(msg: state.message!,toastLength: Toast.LENGTH_LONG,gravity: ToastGravity.CENTER);
errorAlert(context, "Error Login", state.message, () {}); Navigator.pushReplacementNamed(context, '/module-screen');
context.read<UserBloc>().add(LoadVersion()); } else {
final progress = ProgressHUD.of(context);
progress!.dismiss();
errorAlert(context, "Error Login", state.message, () {
context.read<UserBloc>().add(LoadVersion());
Navigator.of(context).pop();
});
}
} }
}, builder: (context, state) { }, builder: (context, state) {
if (state is VersionLoaded) { if (state is VersionLoaded) {
@ -223,7 +228,8 @@ class _UniT2LoginState extends State<UniT2Login> {
BlocProvider.of<UserBloc>(context) BlocProvider.of<UserBloc>(context)
.add(UserLogin( .add(UserLogin(
username: "rjvincentlopeplopez", username:
"rjvincentlopeplopez",
password: "shesthequ33n", password: "shesthequ33n",
// username: _formKey // username: _formKey
// .currentState! // .currentState!

View File

@ -33,23 +33,27 @@ class AuthService {
return versionInfo; return versionInfo;
} }
Future<UserData> webLogin({String? username, String? password})async{ Future<Map<dynamic,dynamic>> webLogin({String? username, String? password})async{
Map <String,String> body ={'username':username!,'password':password!}; Map <String,String> body ={'username':username!,'password':password!};
Map<String, String> baseHeaders = { Map<String, String> baseHeaders = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}; };
Map<dynamic,dynamic> responseStatus = {};
String path = Url.instance.authentication(); String path = Url.instance.authentication();
UserData? userData; UserData? userData;
try{ try{
http.Response response = await Request.instance.postRequest(path: path,param: {},headers: baseHeaders,body: body); http.Response response = await Request.instance.postRequest(path: path,param: {},headers: baseHeaders,body: body);
if(response.statusCode == 200){
Map data = jsonDecode(response.body); Map data = jsonDecode(response.body);
userData = UserData.fromJson(data['data']);
} responseStatus = data;
return responseStatus;
}catch(e){ }catch(e){
throw (e.toString()); throw (e.toString());
} }
return userData!;
} }
Future<UserData> qrLogin({String? uuid, String? password})async{ Future<UserData> qrLogin({String? uuid, String? password})async{
Map <String,String> body ={'uuid':uuid!,'password':password!}; Map <String,String> body ={'uuid':uuid!,'password':password!};

View File

@ -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")), ) btnOk: SizedBox(height: 50,child: ElevatedButton(style: mainBtnStyle(success2, Colors.transparent, success), onPressed: func, child: const Text("OK")), )
).show(); ).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();
}

View File

@ -3,8 +3,8 @@ class Url {
static Url get instance => _instance; static Url get instance => _instance;
String host() { String host() {
return '192.168.10.221:3003'; // return '192.168.10.221:3003';
// return 'agusandelnorte.gov.ph'; return 'agusandelnorte.gov.ph';
// return "192.168.10.219:3000"; // return "192.168.10.219:3000";
// return "devweb.agusandelnorte.gov.ph"; // return "devweb.agusandelnorte.gov.ph";
// return 'devapi.agusandelnorte.gov.ph:3004'; // return 'devapi.agusandelnorte.gov.ph:3004';