feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:krow/core/application/di/injectable.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_event.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_state.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/menu_tree.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/profile_repository.dart';
|
||||
|
||||
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
final menuTree = MenuTree();
|
||||
|
||||
ProfileBloc()
|
||||
: super(ProfileState(
|
||||
menu: MenuRoutItem(),
|
||||
roles: const [],
|
||||
)) {
|
||||
on<ProfileEventInit>(_onInit);
|
||||
on<ProfileEventSelectMenu>(_onSelectMenu);
|
||||
on<ProfileSwitchAvailability>(_onSwitchAvailability);
|
||||
}
|
||||
|
||||
void _onInit(ProfileEventInit event, emit) async {
|
||||
var repo = getIt<ProfileRepository>();
|
||||
var menu = menuTree.buildMenuTree();
|
||||
emit(state.copyWith(
|
||||
menu: menu,
|
||||
));
|
||||
|
||||
emit(state.copyWith(
|
||||
roles: (await repo.getCachedProfileRoles())
|
||||
?.map(mapRoleToRoleState)
|
||||
.toList()));
|
||||
|
||||
await for (var staff in repo.getUserProfile()) {
|
||||
emit(state.copyWith(
|
||||
staff: staff,
|
||||
));
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
roles: (await repo.getUserProfileRoles())
|
||||
.map(mapRoleToRoleState)
|
||||
.toList()));
|
||||
}
|
||||
|
||||
RoleState mapRoleToRoleState(e) {
|
||||
return RoleState(
|
||||
name: e.skill!.name, experience: e.experience!, level: e.level!);
|
||||
}
|
||||
|
||||
void _onSelectMenu(ProfileEventSelectMenu event, emit) {
|
||||
emit(state.copyWith(menu: event.item));
|
||||
}
|
||||
|
||||
void _onSwitchAvailability(ProfileSwitchAvailability event, emit) {
|
||||
emit(state.copyWith(isAvailableNow: event.available));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_state.dart';
|
||||
|
||||
@immutable
|
||||
sealed class ProfileEvent {}
|
||||
|
||||
class ProfileEventInit extends ProfileEvent {}
|
||||
|
||||
class ProfileEventSelectMenu extends ProfileEvent {
|
||||
ProfileEventSelectMenu({required this.item});
|
||||
|
||||
final MenuRoutItem item;
|
||||
}
|
||||
|
||||
class ProfileSwitchAvailability extends ProfileEvent {
|
||||
ProfileSwitchAvailability({required this.available});
|
||||
|
||||
final bool available;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:krow/core/data/enums/staff_skill_enums.dart';
|
||||
import 'package:krow/core/data/models/staff/staff.dart';
|
||||
|
||||
@immutable
|
||||
class ProfileState {
|
||||
final List<RoleState> roles;
|
||||
final Staff? staff;
|
||||
final MenuRoutItem menu;
|
||||
final bool isAvailableNow;
|
||||
|
||||
const ProfileState({
|
||||
required this.menu,
|
||||
this.staff,
|
||||
this.roles = const [],
|
||||
this.isAvailableNow = false,
|
||||
});
|
||||
|
||||
ProfileState copyWith({
|
||||
MenuRoutItem? menu,
|
||||
List<RoleState>? roles,
|
||||
Staff? staff,
|
||||
bool? isAvailableNow,
|
||||
int? testField,
|
||||
}) {
|
||||
return ProfileState(
|
||||
menu: menu ?? this.menu,
|
||||
roles: roles ?? this.roles,
|
||||
staff: staff ?? this.staff,
|
||||
isAvailableNow: isAvailableNow ?? this.isAvailableNow,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//mock
|
||||
class RoleState {
|
||||
final String name;
|
||||
final int experience;
|
||||
final StaffSkillLevel level;
|
||||
|
||||
RoleState(
|
||||
{required this.name, required this.experience, required this.level});
|
||||
}
|
||||
|
||||
class MenuRoutItem {
|
||||
final String? title;
|
||||
final SvgPicture? icon;
|
||||
final bool showBadge;
|
||||
final PageRouteInfo? route;
|
||||
final VoidCallback? onTap;
|
||||
MenuRoutItem? parent;
|
||||
|
||||
List<MenuRoutItem> children;
|
||||
|
||||
MenuRoutItem({
|
||||
this.title,
|
||||
this.parent,
|
||||
this.icon,
|
||||
this.onTap,
|
||||
this.showBadge = false,
|
||||
this.route,
|
||||
}) : children = [];
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:krow/core/application/routing/routes.gr.dart';
|
||||
import 'package:krow/core/presentation/gen/assets.gen.dart';
|
||||
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_state.dart';
|
||||
import 'package:krow/features/profile/role_kit/domain/staff_role_kit_repository_impl.dart';
|
||||
|
||||
class MenuTree {
|
||||
final MenuRoutItem root;
|
||||
final MenuRoutItem profileSettings;
|
||||
final MenuRoutItem personalInfo;
|
||||
final MenuRoutItem bankAccount;
|
||||
final MenuRoutItem workSettings;
|
||||
final MenuRoutItem workingArea;
|
||||
final MenuRoutItem schedule;
|
||||
final MenuRoutItem verificationCenter;
|
||||
final MenuRoutItem certification;
|
||||
final MenuRoutItem livePhoto;
|
||||
final MenuRoutItem wagesForm;
|
||||
final MenuRoutItem equipment;
|
||||
final MenuRoutItem uniform;
|
||||
final MenuRoutItem employeeResources;
|
||||
final MenuRoutItem training;
|
||||
final MenuRoutItem benefits;
|
||||
final MenuRoutItem helpSupport;
|
||||
final MenuRoutItem faq;
|
||||
final MenuRoutItem termsConditions;
|
||||
final MenuRoutItem contactSupport;
|
||||
|
||||
MenuTree()
|
||||
: root = MenuRoutItem(),
|
||||
profileSettings = MenuRoutItem(
|
||||
title: 'account_settings',
|
||||
icon: Assets.images.userProfile.menu.profileCircle.svg(),
|
||||
),
|
||||
personalInfo = MenuRoutItem(
|
||||
title: 'profile_settings',
|
||||
icon: Assets.images.userProfile.menu.userEdit.svg(),
|
||||
route: const ProfileSettingsFlowRoute(),
|
||||
),
|
||||
bankAccount = MenuRoutItem(
|
||||
title: 'bank_account',
|
||||
icon: Assets.images.userProfile.menu.emptyWallet.svg(),
|
||||
route: const BankAccountFlowRoute(),
|
||||
),
|
||||
workSettings = MenuRoutItem(
|
||||
title: 'work_settings',
|
||||
showBadge: true,
|
||||
icon: Assets.images.userProfile.menu.briefcase.svg(),
|
||||
),
|
||||
workingArea = MenuRoutItem(
|
||||
title: 'working_area',
|
||||
icon: Assets.images.userProfile.menu.map.svg(),
|
||||
route: WorkingAreaRoute(),
|
||||
),
|
||||
schedule = MenuRoutItem(
|
||||
title: 'schedule',
|
||||
icon: Assets.images.userProfile.menu.calendar.svg(),
|
||||
route: ScheduleRoute(),
|
||||
),
|
||||
verificationCenter = MenuRoutItem(
|
||||
title: 'verification_center',
|
||||
showBadge: true,
|
||||
icon: Assets.images.userProfile.menu.shieldTick.svg(),
|
||||
),
|
||||
certification = MenuRoutItem(
|
||||
title: 'certification',
|
||||
icon: Assets.images.userProfile.menu.medalStar.svg(),
|
||||
route: const CertificatesRoute(),
|
||||
),
|
||||
livePhoto = MenuRoutItem(
|
||||
title: 'live_photo',
|
||||
icon: Assets.images.userProfile.menu.gallery.svg(),
|
||||
route: const LivePhotoRoute(),
|
||||
),
|
||||
wagesForm = MenuRoutItem(
|
||||
title: 'wages_form',
|
||||
icon: Assets.images.userProfile.menu.note.svg(),
|
||||
route: const WagesFormsFlowRoute(),
|
||||
),
|
||||
equipment = MenuRoutItem(
|
||||
title: 'equipment',
|
||||
icon: Assets.images.userProfile.menu.pot.svg(),
|
||||
route: RoleKitFlowRoute(roleKitType: RoleKitType.equipment),
|
||||
),
|
||||
uniform = MenuRoutItem(
|
||||
title: 'uniform',
|
||||
icon: Assets.images.userProfile.menu.chef.svg(),
|
||||
route: RoleKitFlowRoute(roleKitType: RoleKitType.uniform),
|
||||
),
|
||||
employeeResources = MenuRoutItem(
|
||||
title: 'employee_resources',
|
||||
icon: Assets.images.userProfile.menu.star.svg(),
|
||||
),
|
||||
training = MenuRoutItem(
|
||||
title: 'training',
|
||||
icon: Assets.images.userProfile.menu.teacher.svg(),
|
||||
),
|
||||
benefits = MenuRoutItem(
|
||||
title: 'benefits',
|
||||
icon: Assets.images.userProfile.menu.star.svg(),
|
||||
route: const BenefitsRoute(),
|
||||
),
|
||||
helpSupport = MenuRoutItem(
|
||||
title: 'help_support',
|
||||
icon: Assets.images.userProfile.menu.message.svg(),
|
||||
),
|
||||
faq = MenuRoutItem(
|
||||
title: 'faq',
|
||||
icon: Assets.images.userProfile.menu.helpCircle.svg(),
|
||||
route: const FaqRoute(),
|
||||
),
|
||||
termsConditions = MenuRoutItem(
|
||||
title: 'terms_conditions',
|
||||
icon: Assets.images.userProfile.menu.securitySafe.svg(),
|
||||
),
|
||||
contactSupport = MenuRoutItem(
|
||||
title: 'contact_support',
|
||||
icon: Assets.images.userProfile.menu.headphone.svg(),
|
||||
route: const SupportRoute(),
|
||||
);
|
||||
|
||||
MenuRoutItem buildMenuTree() {
|
||||
profileSettings.children.addAll(
|
||||
[
|
||||
personalInfo..parent = profileSettings,
|
||||
bankAccount..parent = profileSettings
|
||||
],
|
||||
);
|
||||
workSettings.children.addAll(
|
||||
[workingArea..parent = workSettings, schedule..parent = workSettings],
|
||||
);
|
||||
verificationCenter.children.addAll(
|
||||
[
|
||||
certification..parent = verificationCenter,
|
||||
livePhoto..parent = verificationCenter,
|
||||
equipment..parent = verificationCenter,
|
||||
uniform..parent = verificationCenter
|
||||
],
|
||||
);
|
||||
employeeResources.children.addAll(
|
||||
[
|
||||
if (kDebugMode) benefits..parent = employeeResources
|
||||
],
|
||||
);
|
||||
helpSupport.children.addAll(
|
||||
[
|
||||
faq..parent = helpSupport,
|
||||
contactSupport..parent = helpSupport
|
||||
],
|
||||
);
|
||||
|
||||
root.children.addAll(
|
||||
[
|
||||
profileSettings..parent = root,
|
||||
workSettings..parent = root,
|
||||
verificationCenter..parent = root,
|
||||
employeeResources..parent = root,
|
||||
helpSupport..parent = root,
|
||||
],
|
||||
);
|
||||
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:krow/core/data/models/staff/staff.dart';
|
||||
import 'package:krow/core/data/models/staff_role.dart';
|
||||
|
||||
abstract class ProfileRepository {
|
||||
|
||||
|
||||
Stream<Staff> getUserProfile();
|
||||
|
||||
Future<List<StaffRole>?> getCachedProfileRoles();
|
||||
|
||||
Future<void> cacheProfileRoles(List<StaffRole> roles);
|
||||
|
||||
Future<List<StaffRole>> getUserProfileRoles();
|
||||
}
|
||||
Reference in New Issue
Block a user