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:flutter/material.dart';
import 'package:rasadyar_chicken/data/common/dio_error_handler.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/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_info/user_info_model.dart';
import 'package:rasadyar_chicken/features/common/data/model/response/user_profile_model/user_profile_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 { onSuccess: (result) async {
var tmpRoles = result?.role?.where((element) { var tmpRoles = result?.role?.where((element) {
final allowedRoles = { final allowedRoles = {
'poultryscience', 'poultryscience',
@@ -141,9 +141,9 @@ class AuthLogic extends GetxController with GetTickerProviderStateMixin {
final lowerElement = element.toString().toLowerCase().trim(); final lowerElement = element.toString().toLowerCase().trim();
return allowedRoles.contains(lowerElement); return allowedRoles.contains(lowerElement);
}).toList(); }).toList();
if (tmpRoles != null && tmpRoles.isNotEmpty ) { if (tmpRoles != null && tmpRoles.isNotEmpty) {
await gService.saveSelectedModule(_module); await gService.saveSelectedModule(_module);
await tokenStorageService.saveModule(_module); await tokenStorageService.saveModule(_module);
await tokenStorageService.saveAccessToken( await tokenStorageService.saveAccessToken(
_module, _module,
@@ -169,8 +169,19 @@ class AuthLogic extends GetxController with GetTickerProviderStateMixin {
"device_name": deviceName.value, "device_name": deviceName.value,
}, },
); );
if (tmpRoles.length == 1) {
var tmpRoute = getFaUserRoleWithOnTap(tmpRoles.first).values.first;
Get.offAndToNamed(CommonRoutes.role); 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 { } else {
Get.snackbar( Get.snackbar(
'با احترام ', 'با احترام ',

View File

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