Commit 227cdda6 authored by Grigory Fedorov's avatar Grigory Fedorov

Adapters: light refactoring.

parent 251955a4
...@@ -31,7 +31,7 @@ public class AccountConfiguration extends GroupConfiguration { ...@@ -31,7 +31,7 @@ public class AccountConfiguration extends GroupConfiguration {
public AccountConfiguration(String account, String user, public AccountConfiguration(String account, String user,
GroupStateProvider groupStateProvider) { GroupStateProvider groupStateProvider) {
super(account, user, groupStateProvider); super(account, user, groupStateProvider);
groups = new TreeMap<String, GroupConfiguration>(); groups = new TreeMap<>();
} }
/** /**
......
...@@ -37,6 +37,8 @@ import java.util.Collection; ...@@ -37,6 +37,8 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -138,31 +140,22 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -138,31 +140,22 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
final CommonState commonState = AccountManager.getInstance().getCommonState(); final CommonState commonState = AccountManager.getInstance().getCommonState();
final String selectedAccount = AccountManager.getInstance().getSelectedAccount(); final String selectedAccount = AccountManager.getInstance().getSelectedAccount();
/**
* Accounts.
*/
final TreeMap<String, AccountConfiguration> accounts = new TreeMap<>();
/** /**
* Groups. * Groups.
*/ */
final TreeMap<String, GroupConfiguration> groups; final Map<String, GroupConfiguration> groups;
/** /**
* Contacts. * Contacts.
*/ */
final ArrayList<AbstractContact> contacts; final List<AbstractContact> contacts;
/** /**
* List of active chats. * List of active chats.
*/ */
final GroupConfiguration activeChats; final GroupConfiguration activeChats;
/**
* List of rooms and active chats grouped by users inside accounts.
*/
final TreeMap<String, TreeMap<String, AbstractChat>> abstractChats = new TreeMap<>();
/** /**
* Whether there is at least one contact. * Whether there is at least one contact.
*/ */
...@@ -173,15 +166,22 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -173,15 +166,22 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
*/ */
boolean hasVisibleContacts = false; boolean hasVisibleContacts = false;
final Map<String, AccountConfiguration> accounts = new TreeMap<>();
for (String account : AccountManager.getInstance().getAccounts()) { for (String account : AccountManager.getInstance().getAccounts()) {
accounts.put(account, null); accounts.put(account, null);
} }
/**
* List of rooms and active chats grouped by users inside accounts.
*/
final Map<String, Map<String, AbstractChat>> abstractChats = new TreeMap<>();
for (AbstractChat abstractChat : MessageManager.getInstance().getChats()) { for (AbstractChat abstractChat : MessageManager.getInstance().getChats()) {
if ((abstractChat instanceof RoomChat || abstractChat.isActive()) if ((abstractChat instanceof RoomChat || abstractChat.isActive())
&& accounts.containsKey(abstractChat.getAccount())) { && accounts.containsKey(abstractChat.getAccount())) {
final String account = abstractChat.getAccount(); final String account = abstractChat.getAccount();
TreeMap<String, AbstractChat> users = abstractChats.get(account); Map<String, AbstractChat> users = abstractChats.get(account);
if (users == null) { if (users == null) {
users = new TreeMap<>(); users = new TreeMap<>();
abstractChats.put(account, users); abstractChats.put(account, users);
...@@ -223,7 +223,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -223,7 +223,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
hasContacts = true; hasContacts = true;
final boolean online = rosterContact.getStatusMode().isOnline(); final boolean online = rosterContact.getStatusMode().isOnline();
final String account = rosterContact.getAccount(); final String account = rosterContact.getAccount();
final TreeMap<String, AbstractChat> users = abstractChats.get(account); final Map<String, AbstractChat> users = abstractChats.get(account);
final AbstractChat abstractChat; final AbstractChat abstractChat;
if (users == null) { if (users == null) {
abstractChat = null; abstractChat = null;
...@@ -249,7 +249,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -249,7 +249,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
hasVisibleContacts = true; hasVisibleContacts = true;
} }
} }
for (TreeMap<String, AbstractChat> users : abstractChats.values()) { for (Map<String, AbstractChat> users : abstractChats.values()) {
for (AbstractChat abstractChat : users.values()) { for (AbstractChat abstractChat : users.values()) {
final AbstractContact abstractContact; final AbstractContact abstractContact;
if (abstractChat instanceof RoomChat) { if (abstractChat instanceof RoomChat) {
...@@ -333,36 +333,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -333,36 +333,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
} }
} }
} else { // Search } else { // Search
final ArrayList<AbstractContact> baseEntities = new ArrayList<>(); final ArrayList<AbstractContact> baseEntities = getSearchResults(rosterContacts, comparator, abstractChats);
// Build structure.
for (RosterContact rosterContact : rosterContacts) {
if (!rosterContact.isEnabled()) {
continue;
}
final String account = rosterContact.getAccount();
final TreeMap<String, AbstractChat> users = abstractChats.get(account);
if (users != null) {
users.remove(rosterContact.getUser());
}
if (rosterContact.getName().toLowerCase(locale).contains(filterString)) {
baseEntities.add(rosterContact);
}
}
for (TreeMap<String, AbstractChat> users : abstractChats.values()) {
for (AbstractChat abstractChat : users.values()) {
final AbstractContact abstractContact;
if (abstractChat instanceof RoomChat) {
abstractContact = new RoomContact((RoomChat) abstractChat);
} else {
abstractContact = new ChatContact(abstractChat);
}
if (abstractContact.getName().toLowerCase(locale).contains(filterString)) {
baseEntities.add(abstractContact);
}
}
}
Collections.sort(baseEntities, comparator);
this.baseEntities.clear(); this.baseEntities.clear();
this.baseEntities.addAll(baseEntities); this.baseEntities.addAll(baseEntities);
hasVisibleContacts = baseEntities.size() > 0; hasVisibleContacts = baseEntities.size() > 0;
...@@ -382,6 +353,42 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -382,6 +353,42 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
} }
} }
private ArrayList<AbstractContact> getSearchResults(Collection<RosterContact> rosterContacts,
Comparator<AbstractContact> comparator,
Map<String, Map<String, AbstractChat>> abstractChats) {
final ArrayList<AbstractContact> baseEntities = new ArrayList<>();
// Build structure.
for (RosterContact rosterContact : rosterContacts) {
if (!rosterContact.isEnabled()) {
continue;
}
final String account = rosterContact.getAccount();
final Map<String, AbstractChat> users = abstractChats.get(account);
if (users != null) {
users.remove(rosterContact.getUser());
}
if (rosterContact.getName().toLowerCase(locale).contains(filterString)) {
baseEntities.add(rosterContact);
}
}
for (Map<String, AbstractChat> users : abstractChats.values()) {
for (AbstractChat abstractChat : users.values()) {
final AbstractContact abstractContact;
if (abstractChat instanceof RoomChat) {
abstractContact = new RoomContact((RoomChat) abstractChat);
} else {
abstractContact = new ChatContact(abstractChat);
}
if (abstractContact.getName().toLowerCase(locale).contains(filterString)) {
baseEntities.add(abstractContact);
}
}
}
Collections.sort(baseEntities, comparator);
return baseEntities;
}
@Override @Override
public void run() { public void run() {
onChange(); onChange();
......
...@@ -63,7 +63,7 @@ public class GroupConfiguration extends BaseEntity { ...@@ -63,7 +63,7 @@ public class GroupConfiguration extends BaseEntity {
public GroupConfiguration(String account, String group, public GroupConfiguration(String account, String group,
GroupStateProvider groupStateProvider) { GroupStateProvider groupStateProvider) {
super(account, group); super(account, group);
abstractContacts = new ArrayList<AbstractContact>(); abstractContacts = new ArrayList<>();
expanded = groupStateProvider.isExpanded(account, group); expanded = groupStateProvider.isExpanded(account, group);
showOfflineMode = groupStateProvider.getShowOfflineMode(account, group); showOfflineMode = groupStateProvider.getShowOfflineMode(account, group);
empty = true; empty = true;
...@@ -105,8 +105,9 @@ public class GroupConfiguration extends BaseEntity { ...@@ -105,8 +105,9 @@ public class GroupConfiguration extends BaseEntity {
*/ */
public void increment(boolean online) { public void increment(boolean online) {
this.total++; this.total++;
if (online) if (online) {
this.online++; this.online++;
}
} }
/** /**
...@@ -157,31 +158,41 @@ public class GroupConfiguration extends BaseEntity { ...@@ -157,31 +158,41 @@ public class GroupConfiguration extends BaseEntity {
int result = account.compareTo(another.getAccount()); int result = account.compareTo(another.getAccount());
if (result != 0) { if (result != 0) {
if (user.compareTo(another.getUser()) != 0) { if (user.compareTo(another.getUser()) != 0) {
if (user == GroupManager.ACTIVE_CHATS) if (user.equals(GroupManager.ACTIVE_CHATS)) {
return -1; return -1;
if (anotherUser == GroupManager.ACTIVE_CHATS) }
if (anotherUser.equals(GroupManager.ACTIVE_CHATS)) {
return 1; return 1;
}
} }
return result; return result;
} }
result = user.compareTo(anotherUser); result = user.compareTo(anotherUser);
if (result != 0) { if (result != 0) {
if (user == GroupManager.ACTIVE_CHATS) if (user.equals(GroupManager.ACTIVE_CHATS)) {
return -1; return -1;
if (anotherUser == GroupManager.ACTIVE_CHATS) }
if (anotherUser.equals(GroupManager.ACTIVE_CHATS)) {
return 1; return 1;
if (user == GroupManager.IS_ACCOUNT) }
if (user.equals(GroupManager.IS_ACCOUNT)) {
return -1; return -1;
if (anotherUser == GroupManager.IS_ACCOUNT) }
if (anotherUser.equals(GroupManager.IS_ACCOUNT)) {
return 1; return 1;
if (user == GroupManager.NO_GROUP) }
if (user.equals(GroupManager.NO_GROUP)) {
return -1; return -1;
if (anotherUser == GroupManager.NO_GROUP) }
if (anotherUser.equals(GroupManager.NO_GROUP)) {
return 1; return 1;
if (user == GroupManager.IS_ROOM) }
if (user.equals(GroupManager.IS_ROOM)) {
return -1; return -1;
if (anotherUser == GroupManager.IS_ROOM) }
if (anotherUser.equals(GroupManager.IS_ROOM)) {
return 1; return 1;
}
return result; return result;
} }
return 0; return 0;
......
...@@ -39,7 +39,8 @@ import com.xabber.androiddev.R; ...@@ -39,7 +39,8 @@ import com.xabber.androiddev.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.TreeMap; import java.util.List;
import java.util.Map;
/** /**
* Provide grouping implementation for the list of contacts. * Provide grouping implementation for the list of contacts.
...@@ -245,8 +246,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -245,8 +246,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
* @param name * @param name
* @return * @return
*/ */
protected GroupConfiguration getGroupConfiguration( protected GroupConfiguration getGroupConfiguration(AccountConfiguration accountConfiguration, String name) {
AccountConfiguration accountConfiguration, String name) {
GroupConfiguration groupConfiguration = accountConfiguration.getGroupConfiguration(name); GroupConfiguration groupConfiguration = accountConfiguration.getGroupConfiguration(name);
if (groupConfiguration != null) { if (groupConfiguration != null) {
return groupConfiguration; return groupConfiguration;
...@@ -264,8 +264,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -264,8 +264,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
* @param name * @param name
* @return * @return
*/ */
protected GroupConfiguration getGroupConfiguration( protected GroupConfiguration getGroupConfiguration(Map<String, GroupConfiguration> groups, String name) {
TreeMap<String, GroupConfiguration> groups, String name) {
GroupConfiguration groupConfiguration = groups.get(name); GroupConfiguration groupConfiguration = groups.get(name);
if (groupConfiguration != null) { if (groupConfiguration != null) {
return groupConfiguration; return groupConfiguration;
...@@ -288,8 +287,8 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -288,8 +287,8 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
* @param showGroups * @param showGroups
*/ */
protected void addContact(AbstractContact abstractContact, String group, boolean online, protected void addContact(AbstractContact abstractContact, String group, boolean online,
TreeMap<String, AccountConfiguration> accounts, TreeMap<String, GroupConfiguration> groups, Map<String, AccountConfiguration> accounts, Map<String, GroupConfiguration> groups,
ArrayList<AbstractContact> contacts, boolean showAccounts, boolean showGroups) { List<AbstractContact> contacts, boolean showAccounts, boolean showGroups) {
if (showAccounts) { if (showAccounts) {
final String account = abstractContact.getAccount(); final String account = abstractContact.getAccount();
final AccountConfiguration accountConfiguration; final AccountConfiguration accountConfiguration;
...@@ -341,9 +340,9 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -341,9 +340,9 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
* @return whether contact is visible. * @return whether contact is visible.
*/ */
protected boolean addContact(AbstractContact abstractContact, protected boolean addContact(AbstractContact abstractContact,
boolean online, TreeMap<String, AccountConfiguration> accounts, boolean online, Map<String, AccountConfiguration> accounts,
TreeMap<String, GroupConfiguration> groups, Map<String, GroupConfiguration> groups,
ArrayList<AbstractContact> contacts, boolean showAccounts, List<AbstractContact> contacts, boolean showAccounts,
boolean showGroups, boolean showOffline) { boolean showGroups, boolean showOffline) {
boolean hasVisible = false; boolean hasVisible = false;
if (showAccounts) { if (showAccounts) {
......
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