feat: Refactor code structure and optimize performance across multiple modules

This commit is contained in:
Achintha Isuru
2025-11-17 23:29:28 -05:00
parent 831570f2e0
commit a64cbd9edf
1508 changed files with 105319 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import 'package:auto_route/auto_route.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/data/enums/state_status.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/scroll_layout_helper.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_loading_overlay.dart';
import 'package:krow/features/profile/domain/bloc/personal_info_bloc.dart';
import 'package:krow/features/profile/presentation/widgets/personal_info_form.dart';
@RoutePage()
class PersonalInfoScreen extends StatelessWidget {
const PersonalInfoScreen({
super.key,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: KwAppBar(
titleText: 'Edit Profile',
),
body: ScrollLayoutHelper(
upperWidget: const PersonalInfoForm(),
lowerWidget: BlocConsumer<PersonalInfoBloc, PersonalInfoState>(
buildWhen: (previous, current) =>
previous.status != current.status ||
previous.isFilled != current.isFilled,
listenWhen: (previous, current) => previous.status != current.status,
listener: (context, state) {
if (state.status == StateStatus.success) {}
if(state.deleted){
FirebaseAuth.instance.signOut();
context.router.replace(const SplashRoute());
}
},
builder: (context, state) {
return Column(
children: [
KwLoadingOverlay(
shouldShowLoading: state.status == StateStatus.loading,
child: KwButton.primary(
label: 'Save edits',
height: 52,
disabled: !state.isFilled,
onPressed: () {
context
.read<PersonalInfoBloc>()
.add(SaveProfileChanges());
},
),
),
Gap(12),
KwButton.outlinedPrimary(
label: 'Delete Account', onPressed: () {
_showDeleteAccDialog(context);
})
.copyWith(color: AppColors.statusError)
],
);
},
),
),
);
}
void _showDeleteAccDialog(BuildContext context) {
KwDialog.show(
context: context,
icon: Assets.images.icons.alertTriangle,
state: KwDialogState.negative,
title: 'Are you sure you want to delete account?',
message:
'Deleting your account is permanent and cannot be undone. You will lose all your data, including saved preferences, history, and any content associated with your account',
primaryButtonLabel: 'Delete Account',
secondaryButtonLabel: 'Cancel',
onPrimaryButtonPressed: (dialogContext) {
Navigator.pop(dialogContext);
BlocProvider.of<PersonalInfoBloc>(context)
.add(DeactivateProfile());
});
}
}

View File

@@ -0,0 +1,177 @@
import 'package:auto_route/auto_route.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/profile/domain/bloc/personal_info_bloc.dart';
@RoutePage()
class ProfilePreviewScreen extends StatelessWidget {
const ProfilePreviewScreen({super.key});
@override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
color: AppColors.primaryBlue,
child: Assets.images.bgPattern.svg(
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
),
Scaffold(
backgroundColor: Colors.transparent,
appBar: KwAppBar(
contentColor: AppColors.grayWhite,
titleText: 'Profile',
centerTitle: false,
backgroundColor: Colors.transparent,
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(12),
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
decoration: BoxDecoration(
color: AppColors.bgProfileCard,
borderRadius: BorderRadius.circular(12),
),
child: BlocBuilder<PersonalInfoBloc, PersonalInfoState>(
builder: (context, state) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildUserPhoto(state.profileImageUrl, '${state.firstName} ${state.lastName}'),
const Gap(16),
_buildUserInfo(context, state),
const Gap(24),
KwButton.accent(
label: 'Edit Profile',
onPressed: () {
context.router.push(const PersonalInfoRoute());
}),
const Gap(12),
KwButton.outlinedPrimary(
label: 'Log Out',
onPressed: ()async {
await FirebaseAuth.instance.signOut();
context.router.replace(const SplashRoute());
}).copyWith(color: AppColors.tintDarkRed),
const Gap(12),
],
);
},
),
),
),
),
],
);
}
Container _buildUserPhoto(String? imageUrl, String? userName) {
return Container(
width: 174,
height: 174,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: AppColors.darkBgActiveButtonState,
),
child: imageUrl == null || imageUrl.isEmpty
? Center(
child: Text(
getInitials(userName),
style: AppTextStyles.headingH1.copyWith(
color: Colors.white,
),
),
)
: ClipOval(
child: Image.network(
imageUrl,
fit: BoxFit.cover,
width: 174,
height: 174,
),
),
);
}
Column _buildUserInfo(BuildContext context, PersonalInfoState state) {
return Column(
children: [
Text(
'${state.firstName} ${state.lastName}',
style: AppTextStyles.headingH3.copyWith(color: Colors.white),
textAlign: TextAlign.center,
),
if (state.email.isNotEmpty) ...[
const Gap(8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Assets.images.icons.userProfile.sms.svg(
colorFilter: const ColorFilter.mode(
AppColors.tintDropDownButton,
BlendMode.srcIn,
),
),
const Gap(4),
Text(
state.email,
style: AppTextStyles.bodyMediumReg
.copyWith(color: AppColors.tintDropDownButton),
)
],
),
],
if (state.phoneNumber.isNotEmpty) ...[
const Gap(8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Assets.images.icons.userProfile.call.svg(
colorFilter: const ColorFilter.mode(
AppColors.tintDropDownButton,
BlendMode.srcIn,
),
),
const Gap(4),
Text(
state.phoneNumber,
style: AppTextStyles.bodyMediumReg
.copyWith(color: AppColors.tintDropDownButton),
)
],
),
]
],
);
}
String getInitials(String? name) {
if (name == null || name.isEmpty) return '';
if(name.trim().isEmpty) {
return '';
}
List<String> nameParts = name.split(' ');
if(nameParts.first.isEmpty) {
return '';
}
if (nameParts.length == 1) {
return nameParts[0].substring(0, 1).toUpperCase();
}
return (nameParts[0][0] + nameParts[1][0]).toUpperCase();
}
}

View File

@@ -0,0 +1,140 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/data/enums/state_status.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/widgets/profile_icon.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_phone_input.dart';
import 'package:krow/features/profile/domain/bloc/personal_info_bloc.dart';
class PersonalInfoForm extends StatefulWidget {
const PersonalInfoForm({
super.key,
});
@override
State<PersonalInfoForm> createState() => _PersonalInfoFormState();
}
class _PersonalInfoFormState extends State<PersonalInfoForm> {
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
final _phoneController = TextEditingController();
// final _emailController = TextEditingController();
late final _bloc = context.read<PersonalInfoBloc>();
void _syncControllersWithState(PersonalInfoState state) {
_firstNameController.text = state.firstName;
_lastNameController.text = state.lastName;
// _emailController.text = state.email;
_phoneController.text = state.phoneNumber;
}
@override
void initState() {
super.initState();
_syncControllersWithState(_bloc.state);
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Gap(16),
BlocBuilder<PersonalInfoBloc, PersonalInfoState>(
buildWhen: (previous, current) =>
previous.profileImageUrl != current.profileImageUrl,
builder: (context, state) {
return ProfileIcon(
diameter: 96,
onChange: (imagePath) {
_bloc.add(UpdateProfileImage(imagePath));
},
imagePath: state.profileImagePath,
imageUrl: state.profileImageUrl,
imageQuality: 0,
);
},
),
const Gap(24),
BlocConsumer<PersonalInfoBloc, PersonalInfoState>(
buildWhen: (previous, current) => previous.status != current.status,
listenWhen: (previous, current) =>
previous.isUpdateReceived != current.isUpdateReceived,
listener: (_, state) {
if (!state.isUpdateReceived) return;
_syncControllersWithState(state);
},
builder: (context, state) {
final bool isValidationFailed = state.status == StateStatus.error;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Name', style: AppTextStyles.headingH3),
const Gap(12),
KwTextInput(
title: 'First Name',
hintText: 'Enter your first name',
controller: _firstNameController,
keyboardType: TextInputType.name,
onChanged: (firstName) {
_bloc.add(UpdateFirstName(firstName));
},
),
const Gap(8),
KwTextInput(
title: 'Last Name',
hintText: 'Enter your last name',
controller: _lastNameController,
keyboardType: TextInputType.name,
onChanged: (lastName) {
_bloc.add(UpdateLastName(lastName));
},
),
// const Gap(8),
// KwTextInput(
// title: 'Middle Name (optional)',
// hintText: 'Enter your middle name',
// controller: _middleNameController,
// keyboardType: TextInputType.name,
// onChanged: (middleName) {
// _bloc.add(UpdateMiddleName(middleName));
// },
// ),
const Gap(24),
const Text('Phone Number', style: AppTextStyles.headingH3),
const Gap(12),
KwPhoneInput(
controller: _phoneController,
onChanged: (phoneNumber) {
_bloc.add(UpdatePhoneNumber(phoneNumber));
},
),
// const Gap(24),
// const Text('Email', style: AppTextStyles.headingH3),
// const Gap(12),
// KwTextInput(
// hintText: 'email@website.com',
// controller: _emailController,
// keyboardType: TextInputType.emailAddress,
// showError: isValidationFailed &&
// state.emailValidationError.isNotEmpty,
// helperText: state.emailValidationError,
// onChanged: (email) {
// _bloc.add(UpdateEmail(email));
// },
// ),
],
);
},
),
const Gap(40),
],
);
}
}