Houshan-Basa/lib/ui/screens/splash/cubit/user_info_cubit.dart

62 lines
1.8 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hoshan/data/model/ai/credit_model.dart';
import 'package:hoshan/data/model/auth/user_info_model.dart';
import 'package:hoshan/data/repository/auth_repository.dart';
part 'user_info_state.dart';
class UserInfoCubit extends Cubit<UserInfoState> {
UserInfoCubit() : super(UserInfoInitial());
static UserInfoModel userInfoModel = UserInfoModel();
Future<void> getUserInfo() async {
if (state is UserInfoLoading) return;
emit(UserInfoLoading());
try {
final response = await AuthRepository.getUserInfo();
userInfoModel = response;
userInfoModel.login = true;
emit(UserInfoSuccess());
} on DioException catch (e) {
if (e.type == DioExceptionType.connectionError ||
e.type == DioExceptionType.connectionTimeout) {
emit(UserInfoConnectionError());
} else if (e.response != null &&
e.response!.statusCode != null &&
e.response!.statusCode! >= 500) {
emit(UserInfoServerFail());
} else {
emit(UserInfoFail());
}
if (kDebugMode) {
print("Dio Error is : $e");
}
}
}
void changeUser(UserInfoModel newUserInfoModel) {
userInfoModel = newUserInfoModel;
emit(UserInfoSuccess());
}
void changeCredit(CreditModel newUserInfoModel) {
final user = userInfoModel;
if (newUserInfoModel.credit != null) {
user.credit = newUserInfoModel.credit;
}
if (newUserInfoModel.freeCredit != null) {
user.freeCredit = newUserInfoModel.freeCredit;
}
userInfoModel = user;
emit(UserInfoSuccess());
}
void clearUser() {
userInfoModel = UserInfoModel();
emit(UserInfoSuccess());
}
}