Skip to content

Commit

Permalink
Refactor code get user info of chat message
Browse files Browse the repository at this point in the history
  • Loading branch information
hapo-longnd committed Dec 6, 2024
1 parent 1da9468 commit f0b1eac
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 119 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: f6f9d8c752193d83788e2c1ea9d68279decb9a93

COCOAPODS: 1.12.1
COCOAPODS: 1.15.2
25 changes: 0 additions & 25 deletions lib/_shared/bloc/user_manager/user_manager_cubit.dart

This file was deleted.

22 changes: 0 additions & 22 deletions lib/_shared/bloc/user_manager/user_manager_state.dart

This file was deleted.

25 changes: 19 additions & 6 deletions lib/_shared/data/chat_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ import 'package:firebase_auth/firebase_auth.dart';

final class Message {
final String id;
final String sender;
final String senderId;
final String senderDisplayName;
final String? senderPhotoUrl;
final Timestamp timestamp;
final Map<String, dynamic> translations;

Message({required this.sender, required this.translations})
: id = '',
Message({
required this.senderId,
required this.senderDisplayName,
required this.senderPhotoUrl,
required this.translations,
}) : id = '',
timestamp = Timestamp.now();

Message.fromMap(this.id, Map<String, dynamic> map)
: sender = map['sender'],
: senderId = map['senderId'],
senderDisplayName = map['senderDisplayName'],
senderPhotoUrl = map['senderPhotoUrl'],
timestamp = map['time'],
translations = map['translated'] as Map<String, dynamic>? ?? {};

Map<String, dynamic> toMap() =>
{'sender': sender, 'time': timestamp, 'translated': translations};
Map<String, dynamic> toMap() => {
'senderId': senderId,
'senderDisplayName': senderDisplayName,
'senderPhotoUrl': senderPhotoUrl,
'time': timestamp,
'translated': translations
};
}

final class UserDetail {
Expand Down
7 changes: 5 additions & 2 deletions lib/features/chat/bloc/chat_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:public_chat/_shared/data/chat_data.dart';
import 'package:public_chat/repository/database.dart';
Expand Down Expand Up @@ -30,10 +31,12 @@ class ChatCubit extends Cubit<ChatState> {
toFireStore: (value, options) => value.toMap(),
);

void sendChat({required String uid, required String message}) {
void sendChat({required User user, required String message}) {
ServiceLocator.instance.get<Database>().writePublicMessage(
Message(
sender: uid,
senderId: user.uid,
senderDisplayName: user.displayName ?? '',
senderPhotoUrl: user.photoURL,
translations: {currentLanguageCodeSelected: message},
),
);
Expand Down
113 changes: 53 additions & 60 deletions lib/features/chat/ui/chat_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_ui_firestore/firebase_ui_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:public_chat/_shared/bloc/user_manager/user_manager_cubit.dart';
import 'package:public_chat/_shared/data/chat_data.dart';
import 'package:public_chat/_shared/widgets/chat_bubble_widget.dart';
import 'package:public_chat/_shared/widgets/message_box_widget.dart';
Expand Down Expand Up @@ -55,6 +54,37 @@ class _ChatScreenState extends State<ChatScreen> {
: widget.currentLanguageCode;
}

void _handleActionLogoutSuccess(ChatCubit chatCubit) {
FunctionsAlertDialog.showAlertFlushBar(
context,
Helper.getTextTranslated(
'logoutSuccessMessage',
_getCurrentLanguageCode(chatCubit),
),
true,
);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(
currentCountryCode: _getCurrentCountryCode(chatCubit),
currentLanguageCode: _getCurrentLanguageCode(chatCubit),
),
),
);
}

void _handleActionLogoutFailure(ChatCubit chatCubit) {
FunctionsAlertDialog.showAlertFlushBar(
context,
Helper.getTextTranslated(
'logoutFailMessage',
_getCurrentLanguageCode(chatCubit),
),
false,
);
}

@override
Widget build(BuildContext context) {
final chatCubit = context.read<ChatCubit>();
Expand All @@ -71,33 +101,10 @@ class _ChatScreenState extends State<ChatScreen> {
Navigator.of(context).pop();
}
if (state is LogoutSuccess && context.mounted) {
FunctionsAlertDialog.showAlertFlushBar(
context,
Helper.getTextTranslated(
'logoutSuccessMessage',
_getCurrentLanguageCode(chatCubit),
),
true,
);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(
currentCountryCode: _getCurrentCountryCode(chatCubit),
currentLanguageCode: _getCurrentLanguageCode(chatCubit),
),
),
);
_handleActionLogoutSuccess(chatCubit);
}
if (state is LogoutFailed && context.mounted) {
FunctionsAlertDialog.showAlertFlushBar(
context,
Helper.getTextTranslated(
'logoutFailMessage',
_getCurrentLanguageCode(chatCubit),
),
false,
);
_handleActionLogoutFailure(chatCubit);
}
},
child: Scaffold(
Expand All @@ -113,16 +120,7 @@ class _ChatScreenState extends State<ChatScreen> {
child: Column(
children: [
_buildListMessage(chatCubit),
MessageBox(
onSendMessage: (value) {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null || value.trim().isEmpty) {
// do nothing
return;
}
chatCubit.sendChat(uid: user.uid, message: value);
},
)
_buildMessageBox(chatCubit),
],
),
),
Expand Down Expand Up @@ -286,30 +284,12 @@ class _ChatScreenState extends State<ChatScreen> {
return const SizedBox.shrink();
}
final message = doc.data();
if (message.sender.isNotEmpty) {
context
.read<UserManagerCubit>()
.queryUserDetail(message.sender);
return BlocBuilder<UserManagerCubit, UserManagerState>(
builder: (context, state) {
String? photoUrl;
String? displayName;

if (state is UserDetailState) {
photoUrl = state.photoUrl;
displayName = state.displayName;
}

return ChatBubble(
isMine: message.sender == user?.uid,
message: chatCubit.getMessageTranslated(message),
photoUrl: photoUrl,
displayName: displayName,
);
},
);
}
return const SizedBox.shrink();
return ChatBubble(
isMine: message.senderId == user?.uid,
message: chatCubit.getMessageTranslated(message),
photoUrl: message.senderPhotoUrl,
displayName: message.senderDisplayName,
);
},
emptyBuilder: (context) => Center(
child: BlocBuilder<LanguageLoadCubit, LanguageLoadState>(
Expand Down Expand Up @@ -339,4 +319,17 @@ class _ChatScreenState extends State<ChatScreen> {
},
);
}

Widget _buildMessageBox(ChatCubit chatCubit) {
final User? user = FirebaseAuth.instance.currentUser;
return MessageBox(
onSendMessage: (value) {
if (user == null || value.trim().isEmpty) {
// do nothing
return;
}
chatCubit.sendChat(user: user, message: value);
},
);
}
}
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:public_chat/_shared/bloc/user_manager/user_manager_cubit.dart';
import 'package:public_chat/features/chat/chat.dart';
import 'package:public_chat/features/country/country.dart';
import 'package:public_chat/features/genai_setting/bloc/genai_bloc.dart';
Expand All @@ -31,7 +30,6 @@ class MainApp extends StatelessWidget {
create: (context) => LanguageLoadCubit(),
),
BlocProvider<LoginCubit>(create: (context) => LoginCubit()),
BlocProvider<UserManagerCubit>(create: (context) => UserManagerCubit()),
BlocProvider<GenaiBloc>(create: (context) => GenaiBloc()),
BlocProvider<ChatCubit>(create: (context) => ChatCubit()),
BlocProvider<CountryCubit>(create: (context) => CountryCubit()),
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/app_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:public_chat/service_locator/service_locator.dart';
extension UserPhotoUrl on Message {
Future<UserDetail?> get userDetail async {
final DocumentSnapshot<UserDetail> snapshot =
await ServiceLocator.instance.get<Database>().getUser(sender);
await ServiceLocator.instance.get<Database>().getUser(senderId);

if (!snapshot.exists) {
return null;
Expand Down

0 comments on commit f0b1eac

Please sign in to comment.