import 'package:flutter/material.dart'; 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/presentation/pages/steward/root/logic.dart'; import 'package:rasadyar_core/core.dart'; class ProfileLogic extends GetxController { StewardRootLogic rootLogic = Get.find(); GService gService = Get.find(); TokenStorageService tokenService = Get.find(); RxInt selectedInformationType = 0.obs; Rxn birthDate = Rxn(); Rx> userProfile = Rx>(Resource.loading()); Rx> userLocal = Rx>(Resource.loading()); 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 cites = [].obs; Rxn selectedProvince = Rxn(); Rxn selectedCity = Rxn(); GlobalKey formKey = GlobalKey(); ImagePicker imagePicker = ImagePicker(); Rxn selectedImage = Rxn(); RxnString _base64Image = RxnString(); RxBool isOnLoading = false.obs; @override void onInit() { super.onInit(); ever(selectedImage, (data) async { if (data?.path != null) { _base64Image.value = await convertImageToBase64(data!.path); } }); } @override void onReady() { super.onReady(); getUserProfile(); getUserRole(); 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 ?? ''; birthdayController.text = data.data?.birthday?.toJalali.formatCompactDate() ?? ''; 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, ); }); } @override void onClose() { super.onClose(); } Future getUserProfile() async { userProfile.value = Resource.loading(); await safeCall( call: () async => await rootLogic.chickenRepository.getUserProfile( token: rootLogic.tokenService.accessToken.value!, ), onSuccess: (result) { if (result != null) { userProfile.value = Resource.success(result); } }, onError: (error, stackTrace) {}, ); } Future getCites() async { await safeCall( call: () => rootLogic.chickenRepository.getCity(provinceName: selectedProvince.value?.name ?? ''), onSuccess: (result) { if (result != null && result.isNotEmpty) { cites.value = result; } }, ); } Future updateUserProfile() async { UserProfile userProfile = UserProfile( firstName: nameController.text, lastName: lastNameController.text, nationalCode: nationalCodeController.text, nationalId: nationalIdController.text, birthday: birthDate.value?.toDateTime().formattedDashedGregorian.toString(), image: _base64Image.value, personType: 'self', type: 'self_profile', ); isOnLoading.value = true; await safeCall( call: () async => await rootLogic.chickenRepository.updateUserProfile( token: rootLogic.tokenService.accessToken.value!, userProfile: userProfile, ), onSuccess: (result) { isOnLoading.value = false; }, onError: (error, stackTrace) { isOnLoading.value = false; }, ); } Future updatePassword() async { if (formKey.currentState?.validate() ?? false) { ChangePasswordRequestModel model = ChangePasswordRequestModel( username: userProfile.value.data?.mobile, password: newPasswordController.text, ); await safeCall( call: () async => await rootLogic.chickenRepository.updatePassword( token: rootLogic.tokenService.accessToken.value!, model: model, ), ); } } Future getUserRole() async { userLocal.value = Resource.loading(); await safeCall( call: () async => rootLogic.tokenService.getUserLocal(Module.chicken), onSuccess: (result) { if (result != null) { userLocal.value = Resource.success(result); } }, onError: (error, stackTrace) {}, ); } void clearPasswordForm() { oldPasswordController.clear(); newPasswordController.clear(); retryNewPasswordController.clear(); } Future changeUserRole(String newRole) async { await gService.saveSelectedRole(Module.chicken, newRole); Get.offAllNamed(newRole); } }