refactor: enhance AuthLogic to handle user roles and improve navigation based on role selection; format code for better readability in ProfilePage

This commit is contained in:
2025-12-30 09:16:07 +03:30
parent 9e2e7845c8
commit 71123c3520
2 changed files with 186 additions and 59 deletions

View File

@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/common/dio_error_handler.dart';
import 'package:rasadyar_chicken/data/common/fa_user_role.dart';
import 'package:rasadyar_chicken/data/di/chicken_di.dart';
import 'package:rasadyar_chicken/features/common/data/model/response/user_info/user_info_model.dart';
import 'package:rasadyar_chicken/features/common/data/model/response/user_profile_model/user_profile_model.dart';
@@ -123,7 +124,6 @@ class AuthLogic extends GetxController with GetTickerProviderStateMixin {
},
),
onSuccess: (result) async {
var tmpRoles = result?.role?.where((element) {
final allowedRoles = {
'poultryscience',
@@ -142,7 +142,7 @@ class AuthLogic extends GetxController with GetTickerProviderStateMixin {
return allowedRoles.contains(lowerElement);
}).toList();
if (tmpRoles != null && tmpRoles.isNotEmpty ) {
if (tmpRoles != null && tmpRoles.isNotEmpty) {
await gService.saveSelectedModule(_module);
await tokenStorageService.saveModule(_module);
await tokenStorageService.saveAccessToken(
@@ -169,8 +169,19 @@ class AuthLogic extends GetxController with GetTickerProviderStateMixin {
"device_name": deviceName.value,
},
);
if (tmpRoles.length == 1) {
var tmpRoute = getFaUserRoleWithOnTap(tmpRoles.first).values.first;
await gService.saveRole(Module.chicken, tmpRoles.first);
await gService.saveRoute(
Module.chicken,
tmpRoute ?? CommonRoutes.role,
);
Get.offAndToNamed(tmpRoute ?? CommonRoutes.role);
} else {
Get.offAndToNamed(CommonRoutes.role);
}
} else {
Get.snackbar(
'با احترام ',

View File

@@ -32,7 +32,11 @@ class ProfilePage extends GetView<ProfileLogic> {
return Container(
width: 128.w,
height: 128.h,
child: Center(child: CupertinoActivityIndicator(color: AppColor.greenNormal)),
child: Center(
child: CupertinoActivityIndicator(
color: AppColor.greenNormal,
),
),
);
}
@@ -57,7 +61,9 @@ class ProfilePage extends GetView<ProfileLogic> {
child: data.value.data?.image != null
? CircleAvatar(
radius: 64.w,
backgroundImage: NetworkImage(data.value.data!.image!),
backgroundImage: NetworkImage(
data.value.data!.image!,
),
)
: Icon(Icons.person, size: 64.w),
),
@@ -129,16 +135,25 @@ class ProfilePage extends GetView<ProfileLogic> {
}, controller.userProfile),
GestureDetector(
onTap: () {
Get.bottomSheet(changePasswordBottomSheet(), isScrollControlled: true);
Get.bottomSheet(
changePasswordBottomSheet(),
isScrollControlled: true,
);
},
child: Container(
height: 47.h,
margin: EdgeInsets.symmetric(horizontal: 8, vertical: 8.h),
padding: EdgeInsets.symmetric(horizontal: 11.h, vertical: 8.h),
padding: EdgeInsets.symmetric(
horizontal: 11.h,
vertical: 8.h,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(width: 1, color: const Color(0xFFD6D6D6)),
border: Border.all(
width: 1,
color: const Color(0xFFD6D6D6),
),
),
child: Row(
spacing: 6,
@@ -146,12 +161,17 @@ class ProfilePage extends GetView<ProfileLogic> {
Assets.vec.lockSvg.svg(
width: 24.w,
height: 24.h,
colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
colorFilter: ColorFilter.mode(
AppColor.blueNormal,
BlendMode.srcIn,
),
),
Text(
'تغییر رمز عبور',
textAlign: TextAlign.center,
style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
style: AppFonts.yekan14.copyWith(
color: AppColor.blueNormal,
),
),
],
),
@@ -159,16 +179,25 @@ class ProfilePage extends GetView<ProfileLogic> {
),
GestureDetector(
onTap: () {
Get.bottomSheet(exitBottomSheet(), isScrollControlled: true);
Get.bottomSheet(
exitBottomSheet(),
isScrollControlled: true,
);
},
child: Container(
height: 47.h,
margin: EdgeInsets.symmetric(horizontal: 8),
padding: EdgeInsets.symmetric(horizontal: 11.h, vertical: 8.h),
padding: EdgeInsets.symmetric(
horizontal: 11.h,
vertical: 8.h,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(width: 1, color: const Color(0xFFD6D6D6)),
border: Border.all(
width: 1,
color: const Color(0xFFD6D6D6),
),
),
child: Row(
spacing: 6,
@@ -176,12 +205,17 @@ class ProfilePage extends GetView<ProfileLogic> {
Assets.vec.logoutSvg.svg(
width: 24.w,
height: 24.h,
colorFilter: ColorFilter.mode(AppColor.redNormal, BlendMode.srcIn),
colorFilter: ColorFilter.mode(
AppColor.redNormal,
BlendMode.srcIn,
),
),
Text(
'خروج',
textAlign: TextAlign.center,
style: AppFonts.yekan14.copyWith(color: AppColor.redNormal),
style: AppFonts.yekan14.copyWith(
color: AppColor.redNormal,
),
),
],
),
@@ -219,7 +253,10 @@ class ProfilePage extends GetView<ProfileLogic> {
child: ObxValue(
(val) => Container(
height: val.value ? 320.h : 47.h,
margin: EdgeInsets.symmetric(horizontal: 8, vertical: val.value ? 8 : 0),
margin: EdgeInsets.symmetric(
horizontal: 8,
vertical: val.value ? 8 : 0,
),
padding: EdgeInsets.symmetric(horizontal: 11.h, vertical: 8.h),
decoration: BoxDecoration(
color: Colors.white,
@@ -244,14 +281,20 @@ class ProfilePage extends GetView<ProfileLogic> {
child: Assets.vec.editSvg.svg(
width: 24.w,
height: 24.h,
colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
colorFilter: ColorFilter.mode(
AppColor.blueNormal,
BlendMode.srcIn,
),
),
),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 8.0,
),
child: Column(
children: [
itemList(
@@ -278,7 +321,10 @@ class ProfilePage extends GetView<ProfileLogic> {
),
itemList(
title: 'تاریخ تولد',
content: item.birthday?.toJalali.formatCompactDate() ?? 'نامشخص',
content:
item.birthday?.toJalali
.formatCompactDate() ??
'نامشخص',
icon: Assets.vec.calendarSvg.path,
),
//todo
@@ -299,7 +345,12 @@ class ProfilePage extends GetView<ProfileLogic> {
)
: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [Icon(CupertinoIcons.chevron_down, color: AppColor.iconColor)],
children: [
Icon(
CupertinoIcons.chevron_down,
color: AppColor.iconColor,
),
],
),
),
controller.isUserInformationOpen,
@@ -340,7 +391,10 @@ class ProfilePage extends GetView<ProfileLogic> {
child: ObxValue(
(val) => Container(
height: val.value ? 320.h : 47.h,
margin: EdgeInsets.symmetric(horizontal: 8, vertical: val.value ? 12 : 0),
margin: EdgeInsets.symmetric(
horizontal: 8,
vertical: val.value ? 12 : 0,
),
padding: EdgeInsets.symmetric(horizontal: 11.h),
decoration: BoxDecoration(
color: Colors.white,
@@ -365,14 +419,20 @@ class ProfilePage extends GetView<ProfileLogic> {
child: Assets.vec.editSvg.svg(
width: 24.w,
height: 24.h,
colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
colorFilter: ColorFilter.mode(
AppColor.blueNormal,
BlendMode.srcIn,
),
),
),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 8.0,
),
child: Column(
spacing: 2,
children: [
@@ -389,7 +449,8 @@ class ProfilePage extends GetView<ProfileLogic> {
),
itemList(
title: 'شماره ثبت',
content: item.unitRegistrationNumber ?? 'نامشخص',
content:
item.unitRegistrationNumber ?? 'نامشخص',
visible: item.unitName != null,
),
@@ -414,7 +475,10 @@ class ProfilePage extends GetView<ProfileLogic> {
content: item.city ?? 'نامشخص',
visible: item.unitName != null,
),
itemList(title: 'آدرس', content: item.unitAddress ?? 'نامشخص'),
itemList(
title: 'آدرس',
content: item.unitAddress ?? 'نامشخص',
),
],
),
),
@@ -422,7 +486,12 @@ class ProfilePage extends GetView<ProfileLogic> {
)
: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [Icon(CupertinoIcons.chevron_down, color: AppColor.iconColor)],
children: [
Icon(
CupertinoIcons.chevron_down,
color: AppColor.iconColor,
),
],
),
),
controller.isUnitInformationOpen,
@@ -480,12 +549,21 @@ class ProfilePage extends GetView<ProfileLogic> {
child: SvgGenImage.vec(icon).svg(
width: 20.w,
height: 20.h,
colorFilter: ColorFilter.mode(AppColor.textColor, BlendMode.srcIn),
colorFilter: ColorFilter.mode(
AppColor.textColor,
BlendMode.srcIn,
),
),
Text(title, style: AppFonts.yekan14.copyWith(color: AppColor.textColor)),
),
Text(
title,
style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
),
Spacer(),
Text(content, style: AppFonts.yekan14.copyWith(color: AppColor.textColor)),
Text(
content,
style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
),
],
),
),
@@ -512,7 +590,9 @@ class ProfilePage extends GetView<ProfileLogic> {
padding: EdgeInsets.all(6),
decoration: ShapeDecoration(
color: cardColor ?? AppColor.blueLight,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Container(
padding: EdgeInsets.all(4),
@@ -523,7 +603,8 @@ class ProfilePage extends GetView<ProfileLogic> {
child: SvgGenImage.vec(icon).svg(
width: 40.w,
height: 40.h,
colorFilter: color ?? ColorFilter.mode(Colors.white, BlendMode.srcIn),
colorFilter:
color ?? ColorFilter.mode(Colors.white, BlendMode.srcIn),
),
),
),
@@ -547,7 +628,9 @@ class ProfilePage extends GetView<ProfileLogic> {
children: [
Text(
'ویرایش اطلاعات هویتی',
style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
style: AppFonts.yekan16Bold.copyWith(
color: AppColor.darkGreyDarkHover,
),
),
Container(
@@ -619,7 +702,9 @@ class ProfilePage extends GetView<ProfileLogic> {
children: [
Text(
'عکس پروفایل',
style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal),
style: AppFonts.yekan16Bold.copyWith(
color: AppColor.blueNormal,
),
),
ObxValue((data) {
return Container(
@@ -628,17 +713,29 @@ class ProfilePage extends GetView<ProfileLogic> {
decoration: BoxDecoration(
color: AppColor.lightGreyNormal,
borderRadius: BorderRadius.circular(8),
border: Border.all(width: 1, color: AppColor.blackLight),
border: Border.all(
width: 1,
color: AppColor.blackLight,
),
),
child: Center(
child: data.value == null
? Padding(
padding: const EdgeInsets.fromLTRB(30, 10, 10, 30),
padding: const EdgeInsets.fromLTRB(
30,
10,
10,
30,
),
child: Image.network(
controller.userProfile.value.data?.image ?? '',
controller.userProfile.value.data?.image ??
'',
),
)
: Image.file(File(data.value!.path), fit: BoxFit.cover),
: Image.file(
File(data.value!.path),
fit: BoxFit.cover,
),
),
);
}, controller.selectedImage),
@@ -651,9 +748,13 @@ class ProfilePage extends GetView<ProfileLogic> {
text: 'گالری',
width: 150.w,
height: 40.h,
textStyle: AppFonts.yekan20.copyWith(color: Colors.white),
textStyle: AppFonts.yekan20.copyWith(
color: Colors.white,
),
onPressed: () async {
controller.selectedImage.value = await controller.imagePicker.pickImage(
controller.selectedImage.value = await controller
.imagePicker
.pickImage(
source: ImageSource.gallery,
imageQuality: 60,
maxWidth: 1080,
@@ -666,9 +767,13 @@ class ProfilePage extends GetView<ProfileLogic> {
text: 'دوربین',
width: 150.w,
height: 40.h,
textStyle: AppFonts.yekan20.copyWith(color: AppColor.blueNormal),
textStyle: AppFonts.yekan20.copyWith(
color: AppColor.blueNormal,
),
onPressed: () async {
controller.selectedImage.value = await controller.imagePicker.pickImage(
controller.selectedImage.value = await controller
.imagePicker
.pickImage(
source: ImageSource.camera,
imageQuality: 60,
maxWidth: 1080,
@@ -724,7 +829,9 @@ class ProfilePage extends GetView<ProfileLogic> {
children: [
Text(
'تغییر رمز عبور',
style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
style: AppFonts.yekan16Bold.copyWith(
color: AppColor.darkGreyDarkHover,
),
),
SizedBox(),
RTextField(
@@ -736,7 +843,8 @@ class ProfilePage extends GetView<ProfileLogic> {
validator: (value) {
if (value == null || value.isEmpty) {
return 'رمز عبور را وارد کنید';
} else if (controller.userProfile.value.data?.password != value) {
} else if (controller.userProfile.value.data?.password !=
value) {
return 'رمز عبور صحیح نیست';
}
return null;
@@ -820,7 +928,10 @@ class ProfilePage extends GetView<ProfileLogic> {
child: Column(
spacing: 8,
children: [
Text('خروج', style: AppFonts.yekan16Bold.copyWith(color: AppColor.error)),
Text(
'خروج',
style: AppFonts.yekan16Bold.copyWith(color: AppColor.error),
),
SizedBox(),
Text(
'آیا مطمئن هستید که می‌خواهید از حساب کاربری خود خارج شوید؟',
@@ -840,7 +951,9 @@ class ProfilePage extends GetView<ProfileLogic> {
backgroundColor: AppColor.error,
onPressed: () async {
await Future.wait([
controller.tokenService.deleteModuleTokens(Module.chicken),
controller.tokenService.deleteModuleTokens(
Module.chicken,
),
controller.gService.clearSelectedModule(),
]).then((value) async {
await removeChickenDI();
@@ -884,10 +997,13 @@ class ProfilePage extends GetView<ProfileLogic> {
children: List.generate(item?.length ?? 0, (index) {
Map tmpRole = getFaUserRoleWithOnTap(item?[index]);
return CustomChip(
isSelected: controller.gService.getRoute(Module.chicken) == tmpRole.values.first,
isSelected:
controller.gService.getRoute(Module.chicken) ==
tmpRole.values.first,
title: tmpRole.keys.first,
index: index,
onTap: (int p1) {
controller.changeUserRole(tmpRole.values.first);
},
);