Skip to content

Commit

Permalink
chore: rebase profile pic and edit bio
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash Tiwari authored and Xazin committed Feb 28, 2023
1 parent cbdff0d commit a7d1544
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 118 deletions.
39 changes: 30 additions & 9 deletions lib/application/user/profile/profile_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,45 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
await userOrFailure.fold(
(failure) => const ProfileState(
userProfile: null,
isEditing: false,
isPicEditing: false,
isBioEditing: false,
),
(userProfile) => state.copyWith(
userProfile: userProfile,
isEditing: false,
isPicEditing: false,
isBioEditing: false,
),
),
);
});

on<EditProfile>((event, emit) {
emit(state.copyWith(isEditing: true));
on<EditBio>((event, emit) {
emit(state.copyWith(isBioEditing: true));
});

on<SaveProfile>((event, emit) async {
on<EditProfilePic>((event, emit) {
emit(state.copyWith(isPicEditing: true));
});

on<SaveBio>((event, emit) async {
if (event.bio != null) {
await _profileRepository.saveProfile(bio: event.bio);
}

final userOrFailure = await _profileRepository.getUserProfile();

emit(
userOrFailure.fold(
(failure) => state.copyWith(isBioEditing: false),
(userProfile) => state.copyWith(
userProfile: userProfile,
isBioEditing: false,
),
),
);
});

on<SaveProfilePic>((event, emit) async {
final wasImageUpdated = event.image != null;
if (wasImageUpdated) {
await _avatarRepository.uploadAvatar(event.image!);
Expand All @@ -53,18 +74,18 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {

emit(
userOrFailure.fold(
(failure) => state.copyWith(isEditing: false),
(failure) => state.copyWith(isPicEditing: false),
(userProfile) => state.copyWith(
userProfile: userProfile,
isEditing: false,
isPicEditing: false,
wasProfilePictureUpdated: wasImageUpdated,
),
),
);
});

on<CancelEditProfile>((event, emit) {
emit(state.copyWith(isEditing: false));
on<CancelEditProfilePic>((event, emit) {
emit(state.copyWith(isPicEditing: false));
});
}
}
19 changes: 14 additions & 5 deletions lib/application/user/profile/profile_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ abstract class ProfileEvent {}

class GetUserProfile extends ProfileEvent {}

class EditProfile extends ProfileEvent {}
class EditBio extends ProfileEvent {}

class SaveProfile extends ProfileEvent {
class SaveBio extends ProfileEvent {
final String? bio;
SaveBio({
this.bio,
});
}

class CancelBio extends ProfileEvent {}

class EditProfilePic extends ProfileEvent {}

class SaveProfilePic extends ProfileEvent {
final File? image;

SaveProfile({
this.bio,
SaveProfilePic({
this.image,
});
}

class CancelEditProfile extends ProfileEvent {}
class CancelEditProfilePic extends ProfileEvent {}
18 changes: 12 additions & 6 deletions lib/application/user/profile/profile_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@ part of 'profile_bloc.dart';
class ProfileState extends Equatable {
const ProfileState({
required this.userProfile,
this.isEditing,
this.isPicEditing,
this.isBioEditing,
this.wasProfilePictureUpdated,
});

final UserProfile? userProfile;
final bool? isEditing;
final bool? isPicEditing;
final bool? isBioEditing;
final bool? wasProfilePictureUpdated;

factory ProfileState.initial() => const ProfileState(
userProfile: null,
isEditing: false,
isPicEditing: false,
isBioEditing: false,
wasProfilePictureUpdated: false,
);

ProfileState copyWith({
UserProfile? userProfile,
bool? isEditing,
bool? isPicEditing,
bool? isBioEditing,
bool? wasProfilePictureUpdated,
}) {
return ProfileState(
userProfile: userProfile ?? this.userProfile,
isEditing: isEditing ?? this.isEditing,
isPicEditing: isPicEditing ?? this.isPicEditing,
isBioEditing: isBioEditing ?? this.isBioEditing,
wasProfilePictureUpdated:
wasProfilePictureUpdated ?? false, // Reset state implicitly
);
}

@override
List<Object?> get props => [userProfile, isEditing, wasProfilePictureUpdated];
List<Object?> get props =>
[userProfile, isPicEditing, isBioEditing, wasProfilePictureUpdated];
}
173 changes: 77 additions & 96 deletions lib/presentation/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:ionicons/ionicons.dart';
import 'package:share_plus/share_plus.dart';

import '../../application/user/profile/profile_bloc.dart';
Expand Down Expand Up @@ -129,7 +130,7 @@ class _UserProfilePageState extends State<UserProfilePage> {
maxRadius: 50,
),
),
if (state.isEditing == true) ...[
if (state.userProfile != null) ...[
Positioned(
bottom: 0,
right: 0,
Expand All @@ -139,13 +140,34 @@ class _UserProfilePageState extends State<UserProfilePage> {
context,
onSelected: (image) {
setState(() => _image = image);
BlocProvider.of<ProfileBloc>(context)
.add(
SaveProfilePic(image: image),
);
},
),
backgroundColor: kAccentColor,
mini: true,
child: const Icon(Icons.add),
child: const Icon(
Icons.drive_file_rename_outline,
),
),
),
// Positioned(
// bottom: 0,
// left: 0,
// child: FloatingActionButton(
// onPressed: () {
// BlocProvider.of<ProfileBloc>(context)
// .add(CancelEditProfilePic());

// _image = null;
// },
// backgroundColor: kErrorColor,
// mini: true,
// child: const Icon(Icons.close),
// ),
// ),
]
],
),
Expand All @@ -162,17 +184,59 @@ class _UserProfilePageState extends State<UserProfilePage> {
),
if (state.userProfile != null) ...[
const SizedBox(height: 40),
const Text(
'About me',
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 11,
color: Color(0xFF666666),
),
textAlign: TextAlign.left,
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'About me',
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 11,
color: Color(0xFF666666),
),
textAlign: TextAlign.left,
),
TextButton(
key: const Key('save_edit_bio_button'),
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(200),
),
),
),
onPressed: () {
if (state.isBioEditing == true) {
/// TODO: Implement save profile image
BlocProvider.of<ProfileBloc>(context).add(
SaveBio(
bio: bioController.text,
),
);
} else {
context
.read<ProfileBloc>()
.add(EditBio());
}
},
child: Text(
state.isBioEditing == true
? 'Save'
: 'Edit',
style: const TextStyle(
fontSize: 11,
color: kAccentColor,
fontWeight: FontWeight.w700,
),
),
),
],
),
const SizedBox(height: 10),
if (state.isEditing == true) ...[
if (state.isBioEditing == true) ...[
//idhr bio editing start
Row(
children: [
Expanded(
Expand Down Expand Up @@ -215,8 +279,9 @@ class _UserProfilePageState extends State<UserProfilePage> {
),
const SizedBox(height: 4),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
const SizedBox(width: 16),
Text(
'Maximum 150 characters',
style: Theme.of(context)
Expand Down Expand Up @@ -265,90 +330,6 @@ class _UserProfilePageState extends State<UserProfilePage> {
],
),
),
const SizedBox(height: 40),
TextButton(
key: const Key('save_edit_button'),
style: ButtonStyle(
overlayColor: MaterialStateColor.resolveWith(
(states) => state.isEditing == true
? Colors.white.withOpacity(0.1)
: kAccentColor.withOpacity(0.1),
),
backgroundColor: state.isEditing == true
? MaterialStateProperty.all(
kAccentColor,
)
: null,
minimumSize: MaterialStateProperty.all(
const Size(double.infinity * 0.75, 52),
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(200),
side: const BorderSide(
color: kAccentColor,
),
),
),
),
onPressed: () {
if (state.isEditing == true) {
BlocProvider.of<ProfileBloc>(context).add(
SaveProfile(
bio: bioController.text,
image: _image,
),
);
} else {
context
.read<ProfileBloc>()
.add(EditProfile());
}
},
child: Text(
state.isEditing == true
? 'Save changes'
: 'Edit profile',
style: TextStyle(
color: state.isEditing == true
? Colors.white
: kAccentColor,
fontWeight: FontWeight.w700,
),
),
),
if (state.isEditing == true) ...[
const SizedBox(height: 10),
TextButton(
key: const Key('cancel_edit_button'),
style: ButtonStyle(
minimumSize: MaterialStateProperty.all(
const Size(double.infinity * 0.75, 52),
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(200),
),
),
),
onPressed: () {
BlocProvider.of<ProfileBloc>(context)
.add(CancelEditProfile());

_image = null;
bioController.value = TextEditingValue(
text: state.userProfile?.profile.bio ?? '',
);
},
child: const Text(
'Cancel',
style: TextStyle(
color: kAccentColor,
fontWeight: FontWeight.w700,
),
),
),
],
] else ...[
const SizedBox(height: 40),
PillButton(
Expand Down
4 changes: 2 additions & 2 deletions test/application/user/profile/profile_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void main() {
expect: () => const [
ProfileState(
userProfile: userProfile,
isEditing: false,
isPicEditing: false,
wasProfilePictureUpdated: false,
)
],
Expand All @@ -59,7 +59,7 @@ void main() {
expect: () => const [
ProfileState(
userProfile: null,
isEditing: false,
isPicEditing: false,
)
],
verify: (_) => verify(() => profileRepository.getUserProfile()).called(1),
Expand Down

0 comments on commit a7d1544

Please sign in to comment.