2023-02-10 02:02:35 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
2023-01-23 03:02:59 +00:00
|
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
2023-01-18 07:54:44 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
2023-04-05 00:54:24 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-08-11 08:38:26 +00:00
|
|
|
import 'package:unit2/model/login_data/user_info/assigned_area.dart';
|
|
|
|
import 'package:unit2/model/login_data/user_info/role.dart';
|
2023-01-19 05:21:12 +00:00
|
|
|
import 'package:unit2/model/login_data/user_info/user_data.dart';
|
2023-01-18 07:54:44 +00:00
|
|
|
import 'package:unit2/model/login_data/version_info.dart';
|
2023-10-06 04:43:37 +00:00
|
|
|
import 'package:unit2/model/offline/offlane_modules.dart';
|
|
|
|
import 'package:unit2/model/offline/offline_profile.dart';
|
2023-01-24 01:36:51 +00:00
|
|
|
import 'package:unit2/screens/unit2/login/functions/get_app_version.dart';
|
2023-01-18 07:54:44 +00:00
|
|
|
import 'package:unit2/sevices/login_service/auth_service.dart';
|
2023-04-05 00:54:24 +00:00
|
|
|
import 'package:unit2/utils/global.dart';
|
2023-01-23 03:02:59 +00:00
|
|
|
import '../../utils/scanner.dart';
|
2023-02-10 02:02:35 +00:00
|
|
|
import '../../utils/text_container.dart';
|
2023-01-18 07:54:44 +00:00
|
|
|
part 'user_event.dart';
|
|
|
|
part 'user_state.dart';
|
2023-10-06 04:43:37 +00:00
|
|
|
|
2023-01-18 07:54:44 +00:00
|
|
|
class UserBloc extends Bloc<UserEvent, UserState> {
|
2023-01-23 03:02:59 +00:00
|
|
|
UserData? _userData;
|
2023-01-23 08:23:20 +00:00
|
|
|
VersionInfo? _versionInfo;
|
2023-02-10 02:02:35 +00:00
|
|
|
String? _apkVersion;
|
2023-04-05 00:54:24 +00:00
|
|
|
bool save = false;
|
2023-06-06 06:54:51 +00:00
|
|
|
String? uuid;
|
2023-09-07 05:54:47 +00:00
|
|
|
String? username;
|
|
|
|
String? password;
|
2023-08-11 08:38:26 +00:00
|
|
|
List<AssignedArea> establishmentPointPersonAssignedAreas = [];
|
2023-01-18 07:54:44 +00:00
|
|
|
UserBloc() : super(UserInitial()) {
|
2023-04-13 08:45:19 +00:00
|
|
|
//// this event is called when opening the app to check if
|
|
|
|
//// there is new app version
|
2023-01-18 07:54:44 +00:00
|
|
|
on<GetApkVersion>((event, emit) async {
|
|
|
|
try {
|
|
|
|
emit(SplashScreen());
|
2023-06-06 06:54:51 +00:00
|
|
|
save = false;
|
2023-04-13 08:45:19 +00:00
|
|
|
if (_versionInfo == null) {
|
|
|
|
VersionInfo versionInfo = await AuthService.instance.getVersionInfo();
|
|
|
|
_versionInfo = versionInfo;
|
|
|
|
}
|
2023-01-24 01:36:51 +00:00
|
|
|
String apkVersion = await getAppVersion();
|
2023-02-10 02:02:35 +00:00
|
|
|
_apkVersion = apkVersion;
|
2023-04-05 00:54:24 +00:00
|
|
|
final String? saved = CREDENTIALS?.get('saved');
|
2023-10-06 04:43:37 +00:00
|
|
|
username = CREDENTIALS?.get('username');
|
|
|
|
password = CREDENTIALS?.get('password');
|
|
|
|
if (apkVersion != _versionInfo!.id) {
|
|
|
|
emit(VersionLoaded(
|
2023-09-25 03:38:00 +00:00
|
|
|
versionInfo: _versionInfo,
|
|
|
|
apkVersion: _apkVersion,
|
|
|
|
username: event.username,
|
|
|
|
password: event.password));
|
2023-10-06 04:43:37 +00:00
|
|
|
} else if (saved != null) {
|
2023-04-05 00:54:24 +00:00
|
|
|
save = true;
|
|
|
|
add(UserLogin(username: username, password: password));
|
|
|
|
} else {
|
|
|
|
emit(VersionLoaded(
|
2023-04-13 08:45:19 +00:00
|
|
|
versionInfo: _versionInfo,
|
|
|
|
apkVersion: _apkVersion,
|
2023-09-07 05:54:47 +00:00
|
|
|
username: event.username,
|
|
|
|
password: event.password));
|
2023-04-05 00:54:24 +00:00
|
|
|
}
|
2023-01-18 07:54:44 +00:00
|
|
|
} catch (e) {
|
2023-10-06 04:43:37 +00:00
|
|
|
bool? offlineAvalable = await OFFLINE!.get('offline');
|
|
|
|
if (offlineAvalable == true) {
|
|
|
|
emit(ErrorWithOfflineMode());
|
|
|
|
} else {
|
|
|
|
emit(UserError(
|
|
|
|
message: e.toString(),
|
|
|
|
));
|
|
|
|
}
|
2023-01-18 07:54:44 +00:00
|
|
|
}
|
2023-01-23 03:02:59 +00:00
|
|
|
});
|
2023-04-13 08:45:19 +00:00
|
|
|
////Loading the current version of the app
|
2023-01-23 08:23:20 +00:00
|
|
|
on<LoadVersion>((event, emit) {
|
2023-09-07 05:54:47 +00:00
|
|
|
username = event.username;
|
|
|
|
password = event.password;
|
2023-04-13 08:45:19 +00:00
|
|
|
emit(VersionLoaded(
|
|
|
|
versionInfo: _versionInfo,
|
|
|
|
apkVersion: _apkVersion,
|
2023-09-07 05:54:47 +00:00
|
|
|
username: username,
|
|
|
|
password: password));
|
2023-01-23 08:23:20 +00:00
|
|
|
});
|
2023-04-13 08:45:19 +00:00
|
|
|
////userlogin
|
2023-01-23 03:02:59 +00:00
|
|
|
on<UserLogin>((event, emit) async {
|
2023-09-07 05:54:47 +00:00
|
|
|
username = event.username;
|
|
|
|
password = event.password;
|
2023-10-06 04:43:37 +00:00
|
|
|
bool? availableOffline;
|
|
|
|
List<OfflineModules> offlineCards = [];
|
2023-01-23 03:02:59 +00:00
|
|
|
try {
|
2023-10-06 04:43:37 +00:00
|
|
|
Map<dynamic, dynamic> response = await AuthService.instance
|
|
|
|
.webLogin(username: event.username, password: event.password);
|
|
|
|
if (response['status'] == true) {
|
|
|
|
UserData userData = UserData.fromJson(response['data']);
|
|
|
|
Role? estPointPerson;
|
|
|
|
if (userData.user?.login?.user?.roles != null &&
|
|
|
|
userData.user!.login!.user!.roles!.isNotEmpty) {
|
|
|
|
userData.user!.login!.user!.roles!.forEach((element) {
|
|
|
|
if (element!.name!.toLowerCase() ==
|
|
|
|
'establishment point-person') {
|
|
|
|
estPointPerson = element;
|
|
|
|
}
|
2023-08-11 08:38:26 +00:00
|
|
|
});
|
2023-10-06 04:43:37 +00:00
|
|
|
if (estPointPerson != null &&
|
|
|
|
estPointPerson!.assignedArea!.isNotEmpty) {
|
|
|
|
estPointPerson!.assignedArea!.forEach((element) {
|
|
|
|
establishmentPointPersonAssignedAreas.add(element!);
|
|
|
|
});
|
|
|
|
}
|
2023-08-11 08:38:26 +00:00
|
|
|
}
|
2023-10-06 04:43:37 +00:00
|
|
|
////Check offline availabilty
|
|
|
|
for (var role in userData.user!.login!.user!.roles!) {
|
|
|
|
if (role!.name!.toLowerCase() == 'field surveyor') {
|
|
|
|
availableOffline = true;
|
|
|
|
OfflineModules newOfflineModule = OfflineModules(
|
|
|
|
moduleName: role.modules!.first!.name!,
|
|
|
|
object: role.modules!.first!.objects!.first!,
|
|
|
|
roleName: role.name!,
|
|
|
|
roleId: role.id!);
|
2023-08-15 06:32:21 +00:00
|
|
|
|
2023-10-06 04:43:37 +00:00
|
|
|
offlineCards.add(newOfflineModule);
|
|
|
|
OfflineProfile offlineProfile = OfflineProfile(
|
|
|
|
webuserId: userData.employeeInfo?.profile?.webuserId,
|
|
|
|
id: userData.employeeInfo?.profile?.id,
|
|
|
|
lastName: userData.employeeInfo?.profile?.lastName,
|
|
|
|
firstName: userData.employeeInfo?.profile?.lastName,
|
|
|
|
middleName: userData.employeeInfo?.profile?.middleName,
|
|
|
|
nameExtension: userData.employeeInfo?.profile?.nameExtension,
|
|
|
|
sex: userData.employeeInfo?.profile?.sex,
|
|
|
|
birthdate: userData.employeeInfo?.profile?.birthdate,
|
|
|
|
civilStatus: civilStatus,
|
|
|
|
bloodType: bloodType,
|
|
|
|
heightM: userData.employeeInfo?.profile?.heightM,
|
|
|
|
weightKg: userData.employeeInfo?.profile?.weightKg,
|
|
|
|
photoPath: userData.employeeInfo?.profile?.photoPath,
|
|
|
|
esigPath: userData.employeeInfo?.profile?.esigPath,
|
|
|
|
maidenName: userData.employeeInfo?.profile?.maidenName,
|
|
|
|
deceased: userData.employeeInfo?.profile?.deceased,
|
|
|
|
uuidQrcode: userData.employeeInfo?.profile?.uuidQrcode,
|
|
|
|
titlePrefix: userData.employeeInfo?.profile?.titlePrefix,
|
|
|
|
titleSuffix: userData.employeeInfo?.profile?.titleSuffix,
|
|
|
|
showTitleId: userData.employeeInfo?.profile?.showTitleId,
|
|
|
|
ethnicity: userData.employeeInfo?.profile?.ethnicity,
|
|
|
|
disability: userData.employeeInfo?.profile?.disability,
|
|
|
|
gender: userData.employeeInfo?.profile?.gender,
|
|
|
|
religion: userData.employeeInfo?.profile?.religion,
|
|
|
|
ip: userData.employeeInfo?.profile?.ip);
|
|
|
|
await OFFLINE!.put("offline_profile", offlineProfile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await OFFLINE!.put('modules', offlineCards);
|
|
|
|
await OFFLINE!.put('offline', availableOffline);
|
|
|
|
globalOfflineAvailable = availableOffline;
|
|
|
|
emit(UserLoggedIn(
|
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
|
|
|
userData: userData,
|
|
|
|
success: true,
|
|
|
|
message: response['message'],
|
|
|
|
savedCredentials: save));
|
|
|
|
} else {
|
|
|
|
emit(UserLoggedIn(
|
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
|
|
|
userData: null,
|
|
|
|
success: false,
|
|
|
|
message: response['message'],
|
|
|
|
savedCredentials: save));
|
|
|
|
}
|
|
|
|
} on TimeoutException catch (_) {
|
2023-03-21 04:57:38 +00:00
|
|
|
emit(InternetTimeout(message: timeoutError));
|
|
|
|
} on SocketException catch (_) {
|
2023-02-10 02:02:35 +00:00
|
|
|
emit(InternetTimeout(message: timeoutError));
|
2023-08-11 08:38:26 +00:00
|
|
|
} on Error catch (e) {
|
|
|
|
emit(LoginErrorState(message: e.toString()));
|
2023-10-06 04:43:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
emit(LoginErrorState(message: e.toString()));
|
2023-01-23 03:02:59 +00:00
|
|
|
}
|
|
|
|
});
|
2023-01-23 08:23:20 +00:00
|
|
|
on<UuidLogin>((event, emit) async {
|
|
|
|
try {
|
2023-08-11 08:38:26 +00:00
|
|
|
Map<dynamic, dynamic> response = await AuthService.instance
|
2023-01-23 08:23:20 +00:00
|
|
|
.qrLogin(uuid: event.uuid, password: event.password);
|
2023-08-11 08:38:26 +00:00
|
|
|
if (response['status'] == true) {
|
2023-06-06 06:54:51 +00:00
|
|
|
UserData userData = UserData.fromJson(response['data']);
|
|
|
|
emit(UserLoggedIn(
|
2023-08-11 08:38:26 +00:00
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
2023-06-06 06:54:51 +00:00
|
|
|
userData: userData,
|
|
|
|
success: true,
|
|
|
|
message: response['message'],
|
|
|
|
savedCredentials: save));
|
|
|
|
} else {
|
|
|
|
emit(UserLoggedIn(
|
2023-08-11 08:38:26 +00:00
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
2023-06-06 06:54:51 +00:00
|
|
|
userData: null,
|
|
|
|
success: false,
|
|
|
|
message: response['message'],
|
|
|
|
savedCredentials: save));
|
|
|
|
}
|
2023-08-11 08:38:26 +00:00
|
|
|
emit(UserLoggedIn(
|
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
|
|
|
userData: _userData,
|
|
|
|
savedCredentials: save));
|
2023-02-10 02:02:35 +00:00
|
|
|
} on TimeoutException catch (_) {
|
|
|
|
emit(InternetTimeout(message: timeoutError));
|
2023-03-21 04:57:38 +00:00
|
|
|
} on SocketException catch (_) {
|
|
|
|
emit(InternetTimeout(message: timeoutError));
|
2023-08-11 08:38:26 +00:00
|
|
|
} on Error catch (e) {
|
|
|
|
emit(LoginErrorState(message: e.toString()));
|
2023-01-23 08:23:20 +00:00
|
|
|
}
|
|
|
|
});
|
2023-01-23 03:02:59 +00:00
|
|
|
on<LoadLoggedInUser>((event, emit) {
|
2023-08-11 08:38:26 +00:00
|
|
|
emit(UserLoggedIn(
|
|
|
|
estPersonAssignedArea: establishmentPointPersonAssignedAreas,
|
|
|
|
userData: _userData,
|
|
|
|
savedCredentials: save));
|
2023-01-23 03:02:59 +00:00
|
|
|
});
|
|
|
|
on<GetUuid>((event, emit) async {
|
|
|
|
ScanResult result = await QRCodeBarCodeScanner.instance.scanner();
|
2023-01-23 05:46:09 +00:00
|
|
|
if (result.rawContent.toString().isNotEmpty) {
|
2023-06-06 06:54:51 +00:00
|
|
|
uuid = result.rawContent.toString();
|
|
|
|
emit(UuidLoaded(uuid: uuid!));
|
2023-01-23 03:02:59 +00:00
|
|
|
}
|
2023-08-11 08:38:26 +00:00
|
|
|
});
|
|
|
|
on<LoadUuid>((event, emit) {
|
|
|
|
emit(UuidLoaded(uuid: uuid!));
|
2023-01-18 07:54:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|