67 lines
2.2 KiB
Dart
67 lines
2.2 KiB
Dart
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
||
|
import 'package:bloc/bloc.dart';
|
||
|
import 'package:equatable/equatable.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 '../../utils/scanner.dart';
|
||
|
|
||
|
part 'user_event.dart';
|
||
|
part 'user_state.dart';
|
||
|
|
||
|
class UserBloc extends Bloc<UserEvent, UserState> {
|
||
|
UserData? _userData;
|
||
|
VersionInfo? _versionInfo;
|
||
|
UserBloc() : super(UserInitial()) {
|
||
|
// this event is called when opening the app to check if
|
||
|
// there is new app version
|
||
|
on<GetApkVersion>((event, emit) async {
|
||
|
try {
|
||
|
emit(SplashScreen());
|
||
|
VersionInfo versionInfo = await AuthService.instance.getVersionInfo();
|
||
|
_versionInfo = versionInfo;
|
||
|
String apkVersion = await getAppVersion();
|
||
|
emit(VersionLoaded(versionInfo: _versionInfo,apkVersion: apkVersion));
|
||
|
} catch (e) {
|
||
|
emit(UserError(
|
||
|
message: e.toString(),
|
||
|
));
|
||
|
}
|
||
|
});
|
||
|
on<LoadVersion>((event, emit) {
|
||
|
emit(VersionLoaded(versionInfo: _versionInfo));
|
||
|
});
|
||
|
on<UserLogin>((event, emit) async {
|
||
|
try {
|
||
|
UserData? userData = await AuthService.instance
|
||
|
.webLogin(username: event.username, password: event.password);
|
||
|
_userData = userData;
|
||
|
emit(UserLoggedIn(userData: _userData));
|
||
|
} catch (e) {
|
||
|
emit(UserError(message: e.toString()));
|
||
|
}
|
||
|
});
|
||
|
on<UuidLogin>((event, emit) async {
|
||
|
try {
|
||
|
UserData? userData = await AuthService.instance
|
||
|
.qrLogin(uuid: event.uuid, password: event.password);
|
||
|
_userData = userData;
|
||
|
emit(UserLoggedIn(userData: _userData));
|
||
|
} catch (e) {
|
||
|
emit(UserError(message: e.toString()));
|
||
|
}
|
||
|
});
|
||
|
on<LoadLoggedInUser>((event, emit) {
|
||
|
emit(UserLoggedIn(userData: _userData));
|
||
|
});
|
||
|
on<GetUuid>((event, emit) async {
|
||
|
ScanResult result = await QRCodeBarCodeScanner.instance.scanner();
|
||
|
if (result.rawContent.toString().isNotEmpty) {
|
||
|
emit(UuidLoaded(uuid: result.rawContent.toString()));
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|