notificaciones al pedir servicio y al enviar mensaje de chat

This commit is contained in:
Felipe
2024-04-25 11:15:33 -05:00
parent 76be1f1f1a
commit e1049f7281
15 changed files with 411 additions and 304 deletions
+42 -118
View File
@@ -8,6 +8,7 @@ import 'package:injector/injector.dart';
import 'package:intl/intl.dart';
import 'package:prosappco/blocs/chat_bloc/chat_bloc.dart';
import 'package:prosappco/components/general_reputation.dart';
import 'package:prosappco/local_notifications/local_notifications.dart';
import 'package:service_repository/service_repository.dart';
import 'package:user_repository/user_repository.dart';
@@ -26,6 +27,7 @@ class _ChatScreenState extends State<ChatScreen> {
final FocusNode _messageFocusNode = FocusNode();
List<dynamic>? _userInfo;
DateTime? _lastNotificationTime;
@override
void initState() {
@@ -177,126 +179,8 @@ class _ChatScreenState extends State<ChatScreen> {
),
);
} else {
// Indicador de carga opcional si se desea mostrar mientras se carga la información por primera vez.
return const Center(child: CircularProgressIndicator());
}
// if (snapshot.connectionState == ConnectionState.waiting) {
// return const Center(child: CircularProgressIndicator());
// } else {
// if (snapshot.hasError) {
// return Center(
// child: Text('Error inesperado: ${snapshot.error}'),
// );
// } else {
// final userInfo = snapshot.data![0] as MyUser;
// return Container(
// padding: const EdgeInsets.symmetric(vertical: 8),
// color: const Color(0xFFD6F4FF),
// alignment: Alignment.topCenter,
// child: ListTile(
// leading: Container(
// width: 60,
// height: 60,
// decoration: BoxDecoration(
// color: Colors.grey.shade300,
// shape: BoxShape.circle,
// image: userInfo.picture == null
// ? null
// : DecorationImage(
// image: NetworkImage(
// userInfo.picture ?? ''),
// fit: BoxFit.contain,
// ),
// ),
// child: userInfo.picture == null
// ? Icon(
// CupertinoIcons.person,
// color: Colors.grey.shade400,
// size: 40,
// )
// : null,
// ),
// title: Text(
// userInfo.name ?? '',
// overflow: TextOverflow.ellipsis,
// style: const TextStyle(
// fontSize: 15,
// fontWeight: FontWeight.w600,
// ),
// ),
// subtitle: widget.service.userId ==
// FirebaseAuth.instance.currentUser!.uid
// ? GeneralReputation(
// userId: widget.service.professionalId,
// builder: (context, reputation) {
// final average = reputation.averagePro;
// final total = reputation.totalPro;
// return Row(
// children: [
// RatingBar.builder(
// initialRating:
// calculoRating(average),
// minRating: 1,
// direction: Axis.horizontal,
// allowHalfRating: true,
// itemCount: 5,
// itemSize: 25,
// maxRating: 5,
// itemBuilder: (context, _) =>
// const Icon(
// Icons.star,
// color: Color(0xFF2BA4EC),
// ),
// onRatingUpdate: (rating) {},
// ignoreGestures: true,
// ),
// const SizedBox(width: 5),
// Text(
// '${average.toStringAsFixed(1)} (${total.toString()})',
// ),
// ],
// );
// })
// : GeneralReputation(
// userId: widget.service.userId,
// builder: (context, reputation) {
// final average = reputation.average;
// final total = reputation.total;
// return Row(
// children: [
// RatingBar.builder(
// initialRating:
// calculoRating(average),
// minRating: 1,
// direction: Axis.horizontal,
// allowHalfRating: true,
// itemCount: 5,
// itemSize: 25,
// maxRating: 5,
// itemBuilder: (context, _) =>
// const Icon(
// Icons.star,
// color: Color(0xFF2BA4EC),
// ),
// onRatingUpdate: (rating) {},
// ignoreGestures: true,
// ),
// const SizedBox(width: 5),
// Text(
// '${average.toStringAsFixed(1)} (${total.toString()})',
// ),
// ],
// );
// },
// ),
// ),
// );
// }
// }
},
),
Expanded(
@@ -353,6 +237,26 @@ class _ChatScreenState extends State<ChatScreen> {
serviceId: widget.service.id!,
message: message,
));
if (_userInfo != null) {
final userInfo = _userInfo![0] as MyUser;
if (userInfo.token == null) return;
if (_lastNotificationTime == null ||
DateTime.now().difference(
_lastNotificationTime!) >
const Duration(minutes: 5)) {
LocalNotifications.sendPushNotification(
userInfo.token!,
'Nuevo mensaje',
'Tienes un nuevo mensaje de ${userInfo.name}',
);
_lastNotificationTime = DateTime.now();
}
}
_messageController.clear();
_messageFocusNode.requestFocus();
},
@@ -375,6 +279,26 @@ class _ChatScreenState extends State<ChatScreen> {
serviceId: widget.service.id!,
message: message,
));
if (_userInfo != null) {
final userInfo = _userInfo![0] as MyUser;
if (userInfo.token == null) return;
if (_lastNotificationTime == null ||
DateTime.now().difference(
_lastNotificationTime!) >
const Duration(minutes: 5)) {
LocalNotifications.sendPushNotification(
userInfo.token!,
'Nuevo mensaje',
'Tienes un nuevo mensaje de ${userInfo.name}',
);
_lastNotificationTime = DateTime.now();
}
}
_messageController.clear();
_messageFocusNode.requestFocus();
},