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

Adapters: light refactoring.

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