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/user_data.dart'; import 'package:unit2/model/login_data/version_info.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; 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'); final String? username = CREDENTIALS?.get('username'); final String? password = CREDENTIALS?.get('password'); if (saved != null) { save = true; add(UserLogin(username: username, password: password)); } else { emit(VersionLoaded( versionInfo: _versionInfo, apkVersion: _apkVersion, username: null, password: null)); } } catch (e) { emit(UserError( message: e.toString(), )); } }); ////Loading the current version of the app on((event, emit) { emit(VersionLoaded( versionInfo: _versionInfo, apkVersion: _apkVersion, username: event.username, password: event.password)); }); ////userlogin on((event, emit) async { try { Map response = await AuthService.instance .webLogin(username: event.username, password: event.password); if (response['status'] == true) { UserData userData = UserData.fromJson(response['data']); emit(UserLoggedIn( userData: userData, success: true, message: response['message'], savedCredentials: save)); } else { emit(UserLoggedIn( 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())); } }); 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( userData: userData, success: true, message: response['message'], savedCredentials: save)); } else { emit(UserLoggedIn( userData: null, success: false, message: response['message'], savedCredentials: save)); } emit(UserLoggedIn(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(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!)); }); } }