Skip to content

Commit

Permalink
Merge branch 'suesi/add-github-action' of https://github.com/suesitra…
Browse files Browse the repository at this point in the history
…n/public_chat into suesi/add-github-action
  • Loading branch information
suesitran committed Sep 29, 2024
2 parents 5e18817 + 55bc2aa commit 8683fb1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 30 deletions.
23 changes: 23 additions & 0 deletions lib/_shared/bloc/user_manager/user_manager_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:bloc/bloc.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
import 'package:public_chat/_shared/data/chat_data.dart';
import 'package:public_chat/repository/database.dart';
import 'package:public_chat/service_locator/service_locator.dart';
import 'package:public_chat/utils/bloc_extensions.dart';

part 'user_manager_state.dart';

class UserManagerCubit extends Cubit<UserManagerState> {
UserManagerCubit() : super(UserManagerInitial());

void queryUserDetail(String uid) async {
final DocumentSnapshot<UserDetail> userDetail =
await ServiceLocator.instance.get<Database>().getUser(uid);

emitSafely(UserDetailState(
uid: uid,
photoUrl: userDetail.data()?.photoUrl,
displayName: userDetail.data()?.displayName));
}
}
22 changes: 22 additions & 0 deletions lib/_shared/bloc/user_manager/user_manager_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
part of 'user_manager_cubit.dart';

abstract class UserManagerState extends Equatable {
const UserManagerState();
}

class UserManagerInitial extends UserManagerState {
@override
List<Object> get props => [];
}

final class UserDetailState extends UserManagerState {
final String uid;
final String? photoUrl;
final String? displayName;

UserDetailState(
{required this.uid, required this.photoUrl, required this.displayName});

@override
List<Object?> get props => [uid, photoUrl, displayName];
}
42 changes: 23 additions & 19 deletions lib/_shared/widgets/chat_bubble_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,30 @@ class ChatBubble extends StatelessWidget {
if (translations.isNotEmpty)
...translations.entries
.where(
(element) => element.key != 'original',
)
(element) => element.key != 'original',
)
.map(
(e) => Text.rich(
TextSpan(children: [
TextSpan(
text: '${e.key} ',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontWeight: FontWeight.bold,
color: isMine ? Colors.black87 : Colors.grey)),
TextSpan(
text: e.value,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
color: isMine ? Colors.black87 : Colors.grey),
)
]),
textAlign: isMine ? TextAlign.right : TextAlign.left,
),
)
(e) => Text.rich(
TextSpan(children: [
TextSpan(
text: '${e.key} ',
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(
fontWeight: FontWeight.bold,
color:
isMine ? Colors.black87 : Colors.grey)),
TextSpan(
text: e.value,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
color: isMine ? Colors.black87 : Colors.grey),
)
]),
textAlign: isMine ? TextAlign.right : TextAlign.left,
),
)
],
),
));
Expand Down
34 changes: 24 additions & 10 deletions lib/features/chat/ui/public_chat_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 @@ -38,16 +39,29 @@ class PublicChatScreen extends StatelessWidget {

final Message message = doc.data();

return FutureBuilder(
future: message.userDetail,
builder: (context, snapshot) {
return ChatBubble(
isMine: message.sender == user?.uid,
message: message.message,
photoUrl: snapshot.data?.photoUrl,
displayName: snapshot.data?.displayName,
translations: message.translations);
},
return BlocProvider<UserManagerCubit>(
create: (context) => UserManagerCubit()
..queryUserDetail(message.sender),
lazy: false,
child:
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: message.message,
photoUrl: photoUrl,
displayName: displayName,
translations: message.translations);
},
),
);
},
emptyBuilder: (context) => const Center(
Expand Down
2 changes: 1 addition & 1 deletion lib/repository/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class Database {
.withConverter(
fromFirestore: _userDetailFromFirestore,
toFirestore: _userDetailToFirestore)
.get();
.get(const GetOptions(source: Source.serverAndCache));
}

Stream<QuerySnapshot<UserDetail>> getUserStream() {
Expand Down

0 comments on commit 8683fb1

Please sign in to comment.