import 'dart:async'; import 'dart:io'; import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; import 'package:unit2/model/login_data/user_info/assigned_area.dart'; import 'package:unit2/model/login_data/user_info/role.dart'; import 'package:unit2/model/login_data/user_info/user_data.dart'; import 'package:unit2/model/login_data/version_info.dart'; import 'package:unit2/model/offline/offlane_modules.dart'; import 'package:unit2/model/offline/offline_profile.dart'; import 'package:unit2/screens/unit2/login/functions/get_app_version.dart'; import 'package:unit2/sevices/login_service/auth_service.dart'; import 'package:unit2/utils/global.dart'; import '../../utils/scanner.dart'; import '../../utils/text_container.dart'; part 'user_event.dart'; part 'user_state.dart'; class UserBloc extends Bloc { UserData? _userData; VersionInfo? _versionInfo; String? _apkVersion; bool save = false; String? uuid; String? username; String? password; List establishmentPointPersonAssignedAreas = []; UserBloc() : super(UserInitial()) { //// this event is called when opening the app to check if //// there is new app version on((event, emit) async { try { emit(SplashScreen()); save = false; if (_versionInfo == null) { VersionInfo versionInfo = await AuthService.instance.getVersionInfo(); _versionInfo = versionInfo; } String apkVersion = await getAppVersion(); _apkVersion = apkVersion; final String? saved = CREDENTIALS?.get('saved'); username = CREDENTIALS?.get('username'); password = CREDENTIALS?.get('password'); if (apkVersion != _versionInfo!.id) { emit(VersionLoaded( versionInfo: _versionInfo, apkVersion: _apkVersion, username: event.username, password: event.password)); } else if (saved != null) { save = true; add(UserLogin(username: username, password: password)); } else { emit(VersionLoaded( versionInfo: _versionInfo, apkVersion: _apkVersion, username: event.username, password: event.password)); } } catch (e) { bool? offlineAvalable = await OFFLINE!.get('offline'); if (offlineAvalable == true) { emit(ErrorWithOfflineMode()); } else { emit(UserError( message: e.toString(), )); } } }); ////Loading the current version of the app on((event, emit) { username = event.username; password = event.password; emit(VersionLoaded( versionInfo: _versionInfo, apkVersion: _apkVersion, username: username, password: password)); }); ////userlogin on((event, emit) async { username = event.username; password = event.password; bool? availableOffline; List offlineCards = []; try { Map 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; } }); if (estPointPerson != null && estPointPerson!.assignedArea!.isNotEmpty) { estPointPerson!.assignedArea!.forEach((element) { establishmentPointPersonAssignedAreas.add(element!); }); } } ////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!); 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 (_) { emit(InternetTimeout(message: timeoutError)); } on SocketException catch (_) { emit(InternetTimeout(message: timeoutError)); } on Error catch (e) { emit(LoginErrorState(message: e.toString())); } catch (e) { emit(LoginErrorState(message: e.toString())); } }); on((event, emit) async { try { Map response = await AuthService.instance .qrLogin(uuid: event.uuid, password: event.password); if (response['status'] == true) { UserData userData = UserData.fromJson(response['data']); 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)); } emit(UserLoggedIn( estPersonAssignedArea: establishmentPointPersonAssignedAreas, userData: _userData, savedCredentials: save)); } on TimeoutException catch (_) { emit(InternetTimeout(message: timeoutError)); } on SocketException catch (_) { emit(InternetTimeout(message: timeoutError)); } on Error catch (e) { emit(LoginErrorState(message: e.toString())); } }); on((event, emit) { emit(UserLoggedIn( estPersonAssignedArea: establishmentPointPersonAssignedAreas, userData: _userData, savedCredentials: save)); }); on((event, emit) async { ScanResult result = await QRCodeBarCodeScanner.instance.scanner(); if (result.rawContent.toString().isNotEmpty) { uuid = result.rawContent.toString(); emit(UuidLoaded(uuid: uuid!)); } }); on((event, emit) { emit(UuidLoaded(uuid: uuid!)); }); } }