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 ...@@ -158,7 +158,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
if (isFinishing()) if (isFinishing())
return; return;
chatViewerAdapter.onChange(); chatViewerAdapter.updateChats();
String account = getAccount(intent); String account = getAccount(intent);
String user = getUser(intent); String user = getUser(intent);
...@@ -255,23 +255,30 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -255,23 +255,30 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
currentUser = chatByPageNumber.getUser(); currentUser = chatByPageNumber.getUser();
} }
chatViewerAdapter.onChange(); if (chatViewerAdapter.updateChats()) {
selectPage(currentAccount, currentUser, false); selectPage(currentAccount, currentUser, false);
} else {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(account, user)) {
chat.updateChat(true);
}
}
}
} }
@Override @Override
public void onContactsChanged(Collection<BaseEntity> entities) { public void onContactsChanged(Collection<BaseEntity> entities) {
chatViewerAdapter.onChange();
updateRegisteredChats(); updateRegisteredChats();
updateRegisteredRecentChatsFragments(); updateRegisteredRecentChatsFragments();
} }
@Override @Override
public void onAccountsChanged(Collection<String> accounts) { public void onAccountsChanged(Collection<String> accounts) {
chatViewerAdapter.onChange();
updateRegisteredChats(); updateRegisteredChats();
updateRegisteredRecentChatsFragments(); updateRegisteredRecentChatsFragments();
} }
...@@ -365,7 +372,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -365,7 +372,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
insertExtraText(); insertExtraText();
updateRegisteredChats(); updateRegisteredChats();
updateRegisteredRecentChatsFragments();
Fragment currentFragment = chatViewerAdapter.getCurrentFragment(); Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (currentFragment instanceof ChatViewerFragment) { if (currentFragment instanceof ChatViewerFragment) {
......
...@@ -18,7 +18,7 @@ import java.util.Comparator; ...@@ -18,7 +18,7 @@ import java.util.Comparator;
import static java.lang.Math.abs; import static java.lang.Math.abs;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter { public class ChatViewerAdapter extends FragmentStatePagerAdapter {
/** /**
* Intent sent while opening chat activity. * Intent sent while opening chat activity.
...@@ -45,7 +45,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -45,7 +45,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
if (!activeChats.contains(intent)) { if (!activeChats.contains(intent)) {
intent.updateCreationTime(); intent.updateCreationTime();
} }
onChange(); updateChats();
} }
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) { public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) {
...@@ -54,7 +54,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -54,7 +54,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats()); activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null; intent = null;
onChange(); updateChats();
} }
@Override @Override
...@@ -82,22 +82,46 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -82,22 +82,46 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser()); return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser());
} }
@Override public boolean updateChats() {
public void onChange() {
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
if (intent != null && !activeChats.contains(intent)) { ArrayList<AbstractChat> newChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
activeChats.add(intent);
if (intent != null && !newChats.contains(intent)) {
newChats.add(intent);
} }
Collections.sort(activeChats, new Comparator<AbstractChat>() { Collections.sort(newChats, new Comparator<AbstractChat>() {
@Override @Override
public int compare(AbstractChat lhs, AbstractChat rhs) { public int compare(AbstractChat lhs, AbstractChat rhs) {
return lhs.getCreationTime().compareTo(rhs.getCreationTime()); return lhs.getCreationTime().compareTo(rhs.getCreationTime());
} }
}); });
if (isChatsEquals(newChats)) {
return false;
}
activeChats = newChats;
notifyDataSetChanged(); 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) { 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