Skip to content

Commit

Permalink
message send recieve
Browse files Browse the repository at this point in the history
  • Loading branch information
rudramistry001 committed Feb 7, 2024
1 parent 8b81753 commit d8ea50b
Show file tree
Hide file tree
Showing 9 changed files with 540 additions and 182 deletions.
49 changes: 45 additions & 4 deletions login/lib/api/apis.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// ignore_for_file: avoid_print

import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:login/model/chat_user_model.dart';
import 'package:login/model/messages.dart';

class APIs {
// for authentication
Expand Down Expand Up @@ -73,7 +76,6 @@ class APIs {
});
}

//for updating the profile picture
// update profile picture of user
static Future<void> updateProfilePicture(File file) async {
//getting image file extension
Expand All @@ -98,11 +100,50 @@ class APIs {
.update({'image': me.Image});
}

// CHAT SCREEN RELATED APIS
///************** Chat Screen Related APIs **************
// chats (collection) --> conversation_id (doc) --> messages (collection) --> message (doc)

// useful for getting conversation id
static String getConversationID(String id) => user.uid.hashCode <= id.hashCode
? '${user.uid}_$id'
: '${id}_${user.uid}';

static Stream<QuerySnapshot<Map<String, dynamic>>> getAllMessages(
ChatUser user) {
return firestore.collection('messages').snapshots();
return firestore
.collection('chats/${getConversationID(user.Id)}/messages/')
.orderBy('sent', descending: false)
.snapshots();
}

static getUserInfo(ChatUser user) {}
// for sending message
static Future<void> sendMessage(
ChatUser chatUser,
String msg,
) async {
//message sending time (also used as id)
final time = DateTime.now().millisecondsSinceEpoch.toString();

//message to send
final Message message = Message(
toId: chatUser.Id,
msg: msg,
read: '',
type: Type.text,
fromId: user.uid,
sent: time);

final ref = firestore
.collection('chats/${getConversationID(chatUser.Id)}/messages/');
await ref.doc(time).set(message.toJson());
}

//update read status of message
static Future<void> updateMessageReadStatus(Message message) async {
firestore
.collection('chats/${getConversationID(message.fromId)}/messages/')
.doc(message.sent)
.update({'read': DateTime.now().millisecondsSinceEpoch.toString()});
}
}
10 changes: 10 additions & 0 deletions login/lib/dialogs/my_date_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/material.dart';

class MyDateUtil {
// for getting formatted time from milliSecondsSinceEpochs String
static String getFormattedTime(
{required BuildContext context, required String time}) {
final date = DateTime.fromMillisecondsSinceEpoch(int.parse(time));
return TimeOfDay.fromDateTime(date).format(context);
}
}
39 changes: 39 additions & 0 deletions login/lib/model/messages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Message {
Message({
required this.toId,
required this.msg,
required this.read,
required this.type,
required this.fromId,
required this.sent,
});

late final String toId;
late final String msg;
late final String read;
late final String fromId;
late final String sent;
late final Type type;

Message.fromJson(Map<String, dynamic> json) {
toId = json['toId'].toString();
msg = json['msg'].toString();
read = json['read'].toString();
type = json['type'].toString() == Type.image.name ? Type.image : Type.text;
fromId = json['fromId'].toString();
sent = json['sent'].toString();
}

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['toId'] = toId;
data['msg'] = msg;
data['read'] = read;
data['type'] = type.name;
data['fromId'] = fromId;
data['sent'] = sent;
return data;
}
}

enum Type { text, image }
Loading

0 comments on commit d8ea50b

Please sign in to comment.