Commit 3ba35b20 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewer: chats completely updated only then needed. Better performance and...

ChatViewer: chats completely updated only then needed. Better performance and incoming message animation restored.
parent d5cdd179
......@@ -158,7 +158,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
if (isFinishing())
return;
chatViewerAdapter.onChange();
chatViewerAdapter.updateChats();
String account = getAccount(intent);
String user = getUser(intent);
......@@ -255,23 +255,30 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
currentUser = chatByPageNumber.getUser();
}
chatViewerAdapter.onChange();
if (chatViewerAdapter.updateChats()) {
selectPage(currentAccount, currentUser, false);
} else {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(account, user)) {
chat.updateChat(true);
}
}
}
selectPage(currentAccount, currentUser, false);
}
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
chatViewerAdapter.onChange();
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
chatViewerAdapter.onChange();
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
}
......@@ -365,7 +372,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
insertExtraText();
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (currentFragment instanceof ChatViewerFragment) {
......
......@@ -18,7 +18,7 @@ import java.util.Comparator;
import static java.lang.Math.abs;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter {
public class ChatViewerAdapter extends FragmentStatePagerAdapter {
/**
* Intent sent while opening chat activity.
......@@ -45,7 +45,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
if (!activeChats.contains(intent)) {
intent.updateCreationTime();
}
onChange();
updateChats();
}
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) {
......@@ -54,7 +54,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null;
onChange();
updateChats();
}
@Override
......@@ -82,22 +82,46 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser());
}
@Override
public void onChange() {
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
public boolean updateChats() {
if (intent != null && !activeChats.contains(intent)) {
activeChats.add(intent);
ArrayList<AbstractChat> newChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
if (intent != null && !newChats.contains(intent)) {
newChats.add(intent);
}
Collections.sort(activeChats, new Comparator<AbstractChat>() {
Collections.sort(newChats, new Comparator<AbstractChat>() {
@Override
public int compare(AbstractChat lhs, AbstractChat rhs) {
return lhs.getCreationTime().compareTo(rhs.getCreationTime());
}
});
if (isChatsEquals(newChats)) {
return false;
}
activeChats = newChats;
notifyDataSetChanged();
return true;
}
private boolean isChatsEquals(ArrayList<AbstractChat> newChats) {
if (newChats.size() != activeChats.size()) {
return false;
}
for (int i = 0; i < activeChats.size(); i++) {
AbstractChat oldChat = activeChats.get(i);
AbstractChat newChat = newChats.get(i);
if (!oldChat.equals(newChat.getAccount(), newChat.getUser())) {
return false;
}
}
return true;
}
public int getPageNumber(String account, String user) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment