Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get users messages #34

Open
harunB10 opened this issue Jan 20, 2020 · 16 comments
Open

Get users messages #34

harunB10 opened this issue Jan 20, 2020 · 16 comments

Comments

@harunB10
Copy link

Is it possible from this data model to get a list of my previous chats with last message shown. Like the one in Messenger or WhatsApp after you login...

Right now you save the document in collection like idFrom - idTo ... But I am struggling to query it for a user with uid.

@Nikualla1
Copy link

Nikualla1 commented Apr 6, 2020

Sure!
At the main.dart add this widget inside buildItem.

  Widget getLastMessage(String userId, String peerId) {
    String groupChatId = '';

    if (userId.hashCode <= peerId.hashCode) {
      groupChatId = '$userId-$peerId';
    } else {
      groupChatId = '$peerId-$userId';
    }

    return StreamBuilder(
      stream: _firestore
      .collection('messages')
      .document(groupChatId)
      .collection(groupChatId)
      .orderBy('timestamp', descending: true)
      .limit(1)
      .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text('Loading...');
        } else {
          var documentsData = snapshot.data.documents;
          if (documentsData[0]['type'] == 1) {
           return Text('Image message');
          } else {
           return Text(documentsData[0]['content']);
          }
        }
      }
    );
  }

@harunB10
Copy link
Author

harunB10 commented Apr 6, 2020

But if I don't know apeerId?

I contacted person A, B and C. I need an overview of last chats, something like this

https://i.stack.imgur.com/OliD7.jpg

@Nikualla1
Copy link

So basically you are mapping over the list of users you have chatted. So you have avatar url, user name & id. In this case while mapping items call the Widget getLastMessage with the peerId of current item id.
Hopefully I was clear! :)

@harunB10
Copy link
Author

harunB10 commented Apr 6, 2020

If I understood correctly, then in this case I have to save that list of users I've chatted with (in DB, or cache)?

@Nikualla1
Copy link

Yes!
If you are coming from this project example as I said you should call the Widget getLastMessage at the main.dart inside buildItem

@akiligedeon
Copy link

Whenever a chat is initiated, please check if its the first chat between the current user and the peer, if its the case, save a new instance of that chat with time reference in a firestore table called chats, also save the last message there. when the app loads, take a chat list from that table.in this case, the last message will be stored there. if this is not the first chat, just update the time and the last message sent. this was my approach

@harshraj32
Copy link

harshraj32 commented May 18, 2020

Yes!
If you are coming from this project example as I said you should call the Widget getLastMessage at the main.dart inside buildItem

i added the code snippet to my code and this is the error i got
rere

instead of the actual message this is what i got

@ramneekkashyap9
Copy link

@akiligedeon I referred your idea and did a similar thing, but one thing I am not getting is that when the user list will be retrieved with the last message, if the last message is a image, how should it be dealt with?

@MaheshPeri19
Copy link

MaheshPeri19 commented Aug 18, 2020

Hi,

Good one. I am using this project example. If current user or peer user get new message, it is coming as push notification in notification tray. Along with push notification, i want to show new message count in chat list screen as badge like whatsapp also.

Attached sample image. How can i acheive this. Please help me on this.

whatsapp_badgecount

@Baksman
Copy link

Baksman commented Aug 18, 2020 via email

@MaheshPeri19
Copy link

Ok. Thanks for your reply. Any other suggestion without using rx dart package ? As i am not aware of Rxdart.

@Baksman
Copy link

Baksman commented Aug 18, 2020

https://m.youtube.com/watch?v=B4ZFVBm-Ugs&t=2s
https://m.youtube.com/watch?v=-7EaYUIRBmc&t=6s

@Baksman
Copy link

Baksman commented Aug 18, 2020

watch these two videos ,one deal with combine latest the othere deals with stream

@MaheshPeri19
Copy link

Thank you. i will watch.

@mohamedlouati
Copy link

mohamedlouati commented Aug 19, 2020 via email

@gbbest15
Copy link

gbbest15 commented Jul 7, 2021

@Baksman i am trying to achieve something like that whatsapp like the image the guy share above but i have watch the video and i dont no how to go about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants