Files
rasadyar_application/packages/chicken/lib/presentation/pages/common/profile/logic.dart

182 lines
5.8 KiB
Dart
Raw Normal View History

2025-07-08 16:06:10 +03:30
import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/di/chicken_di.dart';
2025-07-08 16:06:10 +03:30
import 'package:rasadyar_chicken/data/models/request/change_password/change_password_request_model.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/user_profile/user_profile.dart';
import 'package:rasadyar_chicken/data/repositories/chicken/chicken_repository.dart';
2025-07-08 16:06:10 +03:30
import 'package:rasadyar_core/core.dart';
class ProfileLogic extends GetxController {
ChickenRepository chickenRepository = diChicken.get<ChickenRepository>();
GService gService = Get.find<GService>();
2025-09-13 16:18:30 +03:30
TokenStorageService tokenService = Get.find<TokenStorageService>();
2025-07-08 16:06:10 +03:30
RxInt selectedInformationType = 0.obs;
Rxn<Jalali> birthDate = Rxn<Jalali>();
2025-10-15 14:26:37 +03:30
Rx<Resource<UserProfile>> userProfile = Rx<Resource<UserProfile>>(
Resource.loading(),
);
Rx<Resource<UserLocalModel>> userLocal = Rx<Resource<UserLocalModel>>(
Resource.loading(),
);
2025-07-08 16:06:10 +03:30
TextEditingController nameController = TextEditingController();
TextEditingController lastNameController = TextEditingController();
TextEditingController nationalCodeController = TextEditingController();
TextEditingController nationalIdController = TextEditingController();
TextEditingController birthdayController = TextEditingController();
TextEditingController oldPasswordController = TextEditingController();
TextEditingController newPasswordController = TextEditingController();
TextEditingController retryNewPasswordController = TextEditingController();
RxList<IranProvinceCityModel> cites = <IranProvinceCityModel>[].obs;
Rxn<IranProvinceCityModel> selectedProvince = Rxn();
Rxn<IranProvinceCityModel> selectedCity = Rxn();
GlobalKey<FormState> formKey = GlobalKey();
2025-07-21 16:05:49 +03:30
ImagePicker imagePicker = ImagePicker();
Rxn<XFile> selectedImage = Rxn<XFile>();
final RxnString _base64Image = RxnString();
2025-07-21 16:05:49 +03:30
RxBool isOnLoading = false.obs;
2025-10-15 14:26:37 +03:30
RxBool isUserInformationOpen = true.obs;
RxBool isUnitInformationOpen = false.obs;
2025-07-21 16:05:49 +03:30
@override
void onInit() {
super.onInit();
ever(selectedImage, (data) async {
if (data?.path != null) {
_base64Image.value = await convertImageToBase64(data!.path);
}
});
}
2025-07-08 16:06:10 +03:30
@override
void onReady() {
super.onReady();
getUserProfile();
2025-09-13 16:18:30 +03:30
getUserRole();
2025-07-08 16:06:10 +03:30
selectedProvince.listen((p0) => getCites());
userProfile.listen((data) {
nameController.text = data.data?.firstName ?? '';
lastNameController.text = data.data?.lastName ?? '';
nationalCodeController.text = data.data?.nationalCode ?? '';
nationalIdController.text = data.data?.nationalId ?? '';
2025-10-15 14:26:37 +03:30
birthdayController.text =
data.data?.birthday?.toJalali.formatCompactDate() ?? '';
2025-07-08 16:06:10 +03:30
birthDate.value = data.data?.birthday?.toJalali;
selectedProvince.value = IranProvinceCityModel(
name: data.data?.province ?? '',
id: data.data?.provinceNumber ?? 0,
);
selectedCity.value = IranProvinceCityModel(
name: data.data?.city ?? '',
id: data.data?.cityNumber ?? 0,
);
});
}
Future<void> getUserProfile() async {
userProfile.value = Resource.loading();
await safeCall<UserProfile?>(
2025-10-15 14:26:37 +03:30
call: () async => await chickenRepository.getUserProfile(
token: tokenService.accessToken.value!,
),
2025-07-08 16:06:10 +03:30
onSuccess: (result) {
if (result != null) {
userProfile.value = Resource.success(result);
}
},
onError: (error, stackTrace) {},
);
}
Future<void> getCites() async {
await safeCall(
2025-10-15 14:26:37 +03:30
call: () => chickenRepository.getCity(
provinceName: selectedProvince.value?.name ?? '',
),
2025-07-08 16:06:10 +03:30
onSuccess: (result) {
if (result != null && result.isNotEmpty) {
cites.value = result;
}
},
);
}
Future<void> updateUserProfile() async {
UserProfile userProfile = UserProfile(
firstName: nameController.text,
lastName: lastNameController.text,
nationalCode: nationalCodeController.text,
nationalId: nationalIdController.text,
2025-10-15 14:26:37 +03:30
birthday: birthDate.value
?.toDateTime()
.formattedDashedGregorian
.toString(),
2025-07-21 16:05:49 +03:30
image: _base64Image.value,
2025-07-08 16:06:10 +03:30
personType: 'self',
type: 'self_profile',
);
2025-07-21 16:05:49 +03:30
isOnLoading.value = true;
2025-07-08 16:06:10 +03:30
await safeCall(
call: () async => await chickenRepository.updateUserProfile(
token: tokenService.accessToken.value!,
2025-07-08 16:06:10 +03:30
userProfile: userProfile,
),
2025-07-21 16:05:49 +03:30
onSuccess: (result) {
isOnLoading.value = false;
},
onError: (error, stackTrace) {
isOnLoading.value = false;
},
2025-07-08 16:06:10 +03:30
);
}
Future<void> updatePassword() async {
if (formKey.currentState?.validate() ?? false) {
ChangePasswordRequestModel model = ChangePasswordRequestModel(
username: userProfile.value.data?.mobile,
password: newPasswordController.text,
);
await safeCall(
call: () async => await chickenRepository.updatePassword(
token: tokenService.accessToken.value!,
2025-07-08 16:06:10 +03:30
model: model,
),
);
}
}
2025-09-13 16:18:30 +03:30
Future<void> getUserRole() async {
userLocal.value = Resource.loading();
await safeCall<UserLocalModel?>(
call: () async => tokenService.getUserLocal(Module.chicken),
2025-09-13 16:18:30 +03:30
onSuccess: (result) {
if (result != null) {
userLocal.value = Resource.success(result);
}
},
onError: (error, stackTrace) {},
);
}
2025-07-08 16:06:10 +03:30
void clearPasswordForm() {
oldPasswordController.clear();
newPasswordController.clear();
retryNewPasswordController.clear();
}
2025-09-13 16:18:30 +03:30
Future<void> changeUserRole(String newRole) async {
dLog(newRole);
await gService.saveRoute(Module.chicken, newRole);
2025-09-13 16:18:30 +03:30
Get.offAllNamed(newRole);
}
2025-07-08 16:06:10 +03:30
}