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

ChatListController how to update when stream update? #17

Closed
lucasjinreal opened this issue Apr 15, 2021 · 9 comments
Closed

ChatListController how to update when stream update? #17

lucasjinreal opened this issue Apr 15, 2021 · 9 comments

Comments

@lucasjinreal
Copy link

Hi, to clarify my question, I'll post code in case being missunderstand.

Now I am using this way to add list to _mode.controller:

widget.chatBloc.chatWithMembers.listen((event) {
      gLogger.i("chatWithMembers: $event");
      // _model.controller.notifyChanges();
      _model.controller.addAll(event);
    });

This works on first init load which addAll list. But when I update the list order in stream, (only update order, not insert or add any items). the stream will update right? But there addAll will add all again. But I just need update. So that the list will add twice.

My question is, currently ChatListController only have a addAll function, how to update the order anyway? If it have .clear() then I can just clear and addAll, but currently not same as List in dart. What should users do in this siutation?

@jonasN5
Copy link
Owner

jonasN5 commented Apr 15, 2021

In my opinion there are 3 usual cases:

  1. You need to load more items. In this case, since you load older items, you can just use addAll
  2. A new chat has been created. Since it's the newest activity, you can just use insert(0)
  3. A chat received a new message, in which case you'll just use updateById. You're supposed to know which chat has been modified.

In any other case, you can use clear and then addAll, just like you suggested. I do this too when the user authenticates for the first time and synchronizes chat messages with the backend.

@lucasjinreal
Copy link
Author

lucasjinreal commented Apr 16, 2021

@themadmrj thank u for your reply. I rethinked a little this morning.

  1. addAll can be used in incremental add, this can be used in MessageChatListContrller when I got a new message just add new once, this has no problem;
  2. But what if I am not insert not incremental add, I just want update order of exist List. Say we want make the 2th item to the first, I need remove that item, and then insert to first, this seems can be easily done in dart List but not in ChatListController (another reason is that not very good to operate ChatListController inside bloc)

Overall, what's the standared way to do in this situation:

  1. Not inremental adding, just reorder ChatListController;

Also, I agreed that clear then addAll not a very efficient way. In case for adding, better add incremental. But reorder don't know how to solve.

Btw, it's the possible to add a top widget inside ChatList widget? Sometimes we need add some top bar along with the list such as facebook Messenger.

image

is that possible add a searchBar or add button along with the list?

@lucasjinreal
Copy link
Author

@themadmrj Correct me if am wrong, I don't find clear in ChatListController at the moment.

@jonasN5
Copy link
Owner

jonasN5 commented Apr 16, 2021

I don't understand when or why you would randomly change the order, if not when a new message is received?

As for your widget, which is what you asked in your previous question, you need a header (or footer at the bottom of the list). This feature does not currently exist. If you want it, feel free to make a pull request. I will not implement it myself, because I don't really see the point of a header. In my opinion, a search bar makes more sense when it's static, always visible at the top, and does not scroll with the list.

You have access to the items, so you can call items.clear()

@lucasjinreal
Copy link
Author

@themadmrj thanks for your reply. I am not randomly change order. In my case, I have a List of ChatWithMembers, which is my chats page, when I enter the second chat to detail, I talked to someone, I should update ChatWithMembers order so that the second chat I just talked goes on top of list when I return, is it make sense?
I am just want update that order .

Not sure which way is the proper way to do it.

SearchBar can be always visible, but what about some other buttom row, such as Messenger you have your frequent chat friends in the top row? Maybe you have a better way to do it, I just make a feature suggestion.

@jonasN5
Copy link
Owner

jonasN5 commented Apr 16, 2021

@themadmrj thanks for your reply. I am not randomly change order. In my case, I have a List of ChatWithMembers, which is my chats page, when I enter the second chat to detail, I talked to someone, I should update ChatWithMembers order so that the second chat I just talked goes on top of list when I return, is it make sense?
I am just want update that order .

Yes, but that's exactly when you should use updateById to update the ChatWithMembers object and push its position to 0.

SearchBar can be always visible, but what about some other buttom row, such as Messenger you have your frequent chat friends in the top row? Maybe you have a better way to do it, I just make a feature suggestion.

No, if you want exactly the same thing, like I said, you need a header. Feel free to make a pull request. The documentation about how to proceed is very rich. I'll review it once you've submitted it and integrate it in the repo.

@lucasjinreal
Copy link
Author

@themadmrj I'll try send u a PR. it's that OK to make a header widget as params in ChatList? Or if ChatList can receive shrinkWrap=true in ChatListStyle params, we can set NeverScrollPhsyics and make whole list static, so that we can wrap ChatList in a child of ListView while whole also can scroll along with it's parent

@jonasN5
Copy link
Owner

jonasN5 commented Apr 18, 2021

Don't use shrinkWrap=true since performance is worse. Yes, if the header does not depend on the list items, a Widget passed as a parameter is fine, otherwise a builder is better.

@jonasN5
Copy link
Owner

jonasN5 commented Apr 18, 2021

You're supposed to call controller.dispose() yourself, as mentioned in the docs. At this point, you can save the information somewhere.

@jonasN5 jonasN5 closed this as completed Apr 18, 2021
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

2 participants