150 lines
4.9 KiB
Dart
150 lines
4.9 KiB
Dart
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<StewardRootLogic>();
|
|
RxInt selectedInformationType = 0.obs;
|
|
Rxn<Jalali> birthDate = Rxn<Jalali>();
|
|
|
|
Rx<Resource<UserProfile>> userProfile = Rx<Resource<UserProfile>>(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<IranProvinceCityModel> cites = <IranProvinceCityModel>[].obs;
|
|
Rxn<IranProvinceCityModel> selectedProvince = Rxn();
|
|
Rxn<IranProvinceCityModel> selectedCity = Rxn();
|
|
|
|
GlobalKey<FormState> formKey = GlobalKey();
|
|
ImagePicker imagePicker = ImagePicker();
|
|
Rxn<XFile> selectedImage = Rxn<XFile>();
|
|
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();
|
|
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<void> getUserProfile() async {
|
|
userProfile.value = Resource.loading();
|
|
await safeCall<UserProfile?>(
|
|
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<void> getCites() async {
|
|
await safeCall(
|
|
call: () =>
|
|
rootLogic.chickenRepository.getCity(provinceName: selectedProvince.value?.name ?? ''),
|
|
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,
|
|
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<void> 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,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void clearPasswordForm() {
|
|
oldPasswordController.clear();
|
|
newPasswordController.clear();
|
|
retryNewPasswordController.clear();
|
|
}
|
|
}
|