passo_mobile_app/lib/bloc/user/user_bloc.dart

115 lines
3.9 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:io';
2023-01-23 03:02:59 +00:00
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
2023-04-05 00:54:24 +00:00
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';
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';
import '../../utils/text_container.dart';
2023-01-23 03:02:59 +00:00
part 'user_event.dart';
part 'user_state.dart';
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;
String? _apkVersion;
2023-04-05 00:54:24 +00:00
bool save = false;
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
on<GetApkVersion>((event, emit) async {
try {
emit(SplashScreen());
2023-04-13 08:45:19 +00:00
if (_versionInfo == null) {
VersionInfo versionInfo = await AuthService.instance.getVersionInfo();
_versionInfo = versionInfo;
}
String apkVersion = await getAppVersion();
_apkVersion = apkVersion;
2023-04-05 00:54:24 +00:00
final String? saved = CREDENTIALS?.get('saved');
final String? username = CREDENTIALS?.get('username');
final String? password = CREDENTIALS?.get('password');
2023-04-11 01:27:53 +00:00
debugPrint(username);
2023-04-13 08:45:19 +00:00
debugPrint(password);
2023-04-05 00:54:24 +00:00
if (saved != null) {
save = true;
add(UserLogin(username: username, password: password));
} else {
emit(VersionLoaded(
2023-04-13 08:45:19 +00:00
versionInfo: _versionInfo,
apkVersion: _apkVersion,
username: null,
password: null));
2023-04-05 00:54:24 +00:00
}
} catch (e) {
emit(UserError(
message: e.toString(),
));
}
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-04-13 08:45:19 +00:00
emit(VersionLoaded(
versionInfo: _versionInfo,
apkVersion: _apkVersion,
username: event.username,
password: event.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 {
try {
Map<dynamic, dynamic> response = await AuthService.instance
2023-01-23 03:02:59 +00:00
.webLogin(username: event.username, password: event.password);
if (response['status'] == true) {
UserData userData = UserData.fromJson(response['data']);
emit(UserLoggedIn(
2023-04-13 08:45:19 +00:00
userData: userData,
success: true,
message: response['message'],
savedCredentials: save));
2023-04-05 00:54:24 +00:00
} else {
emit(UserLoggedIn(
2023-04-13 08:45:19 +00:00
userData: null,
success: false,
message: response['message'],
savedCredentials: save));
}
} on TimeoutException catch (_) {
emit(InternetTimeout(message: timeoutError));
} on SocketException catch (_) {
emit(InternetTimeout(message: timeoutError));
2023-01-23 03:02:59 +00:00
}
});
2023-01-23 08:23:20 +00:00
on<UuidLogin>((event, emit) async {
try {
UserData? userData = await AuthService.instance
.qrLogin(uuid: event.uuid, password: event.password);
_userData = userData;
2023-04-13 08:45:19 +00:00
emit(UserLoggedIn(userData: _userData, savedCredentials: save));
} on TimeoutException catch (_) {
emit(InternetTimeout(message: timeoutError));
} on SocketException catch (_) {
emit(InternetTimeout(message: timeoutError));
} on Error catch (_) {
emit(InvalidCredentials(message: "Invalid username or password"));
2023-01-23 08:23:20 +00:00
}
});
2023-01-23 03:02:59 +00:00
on<LoadLoggedInUser>((event, emit) {
2023-04-13 08:45:19 +00:00
emit(UserLoggedIn(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-01-23 03:02:59 +00:00
emit(UuidLoaded(uuid: result.rawContent.toString()));
}
});
}
}