Commit 926f4ca9 authored by Alexander Ivanov's avatar Alexander Ivanov

Move contact list suggestions to the fragment.

parent ff6d0d8f
......@@ -10,22 +10,32 @@ import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.xabber.android.data.Application;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.CommonState;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.account.StatusMode;
import com.xabber.android.data.connection.ConnectionManager;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.message.OnChatChangedListener;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.AccountConfiguration;
import com.xabber.android.ui.adapter.ContactListAdapter;
import com.xabber.android.ui.adapter.ContactListAdapter.OnContactListChangedListener;
import com.xabber.android.ui.adapter.ContactListState;
import com.xabber.android.ui.adapter.GroupConfiguration;
import com.xabber.android.ui.adapter.UpdatableAdapter;
import com.xabber.android.ui.helper.ContextMenuHelper;
......@@ -33,11 +43,43 @@ import com.xabber.androiddev.R;
public class ContactListFragment extends Fragment implements
OnAccountChangedListener, OnContactChangedListener,
OnChatChangedListener, OnItemClickListener {
OnChatChangedListener, OnItemClickListener,
OnContactListChangedListener {
private ContactListAdapter adapter;
private ListView listView;
/**
* View with information shown on empty contact list.
*/
private View infoView;
/**
* Image view with connected icon.
*/
private View connectedView;
/**
* Image view with disconnected icon.
*/
private View disconnectedView;
/**
* View with help text.
*/
private TextView textView;
/**
* Button to apply help text.
*/
private Button buttonView;
/**
* Animation for disconnected view.
*/
private Animation animation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......@@ -47,9 +89,15 @@ public class ContactListFragment extends Fragment implements
listView.setOnItemClickListener(this);
listView.setItemsCanFocus(true);
registerForContextMenu(listView);
adapter = new ContactListAdapter(getActivity(), listView,
view.findViewById(R.id.info));
adapter = new ContactListAdapter(getActivity(), listView, this);
listView.setAdapter(adapter);
infoView = view.findViewById(R.id.info);
connectedView = infoView.findViewById(R.id.connected);
disconnectedView = infoView.findViewById(R.id.disconnected);
textView = (TextView) infoView.findViewById(R.id.text);
buttonView = (Button) infoView.findViewById(R.id.button);
animation = AnimationUtils.loadAnimation(getActivity(),
R.anim.connection);
return view;
}
......@@ -122,6 +170,142 @@ public class ContactListFragment extends Fragment implements
adapter.refreshRequest();
}
@Override
public void onContactListChanged(CommonState commonState,
boolean hasContacts, boolean hasVisibleContacts,
boolean isFilterEnabled) {
if (hasVisibleContacts) {
infoView.setVisibility(View.GONE);
disconnectedView.clearAnimation();
return;
}
infoView.setVisibility(View.VISIBLE);
final int text;
final int button;
final ContactListState state;
final OnClickListener listener;
if (isFilterEnabled) {
if (commonState == CommonState.online)
state = ContactListState.online;
else if (commonState == CommonState.roster
|| commonState == CommonState.connecting)
state = ContactListState.connecting;
else
state = ContactListState.offline;
text = R.string.application_state_no_online;
button = 0;
listener = null;
} else if (hasContacts) {
state = ContactListState.online;
text = R.string.application_state_no_online;
button = R.string.application_action_no_online;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
SettingsManager.setContactsShowOffline(true);
adapter.onChange();
}
};
} else if (commonState == CommonState.online) {
state = ContactListState.online;
text = R.string.application_state_no_contacts;
button = R.string.application_action_no_contacts;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(ContactAdd.createIntent(getActivity()));
}
};
} else if (commonState == CommonState.roster) {
state = ContactListState.connecting;
text = R.string.application_state_roster;
button = 0;
listener = null;
} else if (commonState == CommonState.connecting) {
state = ContactListState.connecting;
text = R.string.application_state_connecting;
button = 0;
listener = null;
} else if (commonState == CommonState.waiting) {
state = ContactListState.offline;
text = R.string.application_state_waiting;
button = R.string.application_action_waiting;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
ConnectionManager.getInstance().updateConnections(true);
}
};
} else if (commonState == CommonState.offline) {
state = ContactListState.offline;
text = R.string.application_state_offline;
button = R.string.application_action_offline;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
AccountManager.getInstance().setStatus(
StatusMode.available, null);
}
};
} else if (commonState == CommonState.disabled) {
state = ContactListState.offline;
text = R.string.application_state_disabled;
button = R.string.application_action_disabled;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(AccountList.createIntent(getActivity()));
}
};
} else if (commonState == CommonState.empty) {
state = ContactListState.offline;
text = R.string.application_state_empty;
button = R.string.application_action_empty;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(AccountAdd.createIntent(getActivity()));
}
};
} else {
throw new IllegalStateException();
}
if (state == ContactListState.offline) {
connectedView.setVisibility(View.INVISIBLE);
disconnectedView.setVisibility(View.VISIBLE);
disconnectedView.clearAnimation();
} else if (state == ContactListState.connecting) {
connectedView.setVisibility(View.VISIBLE);
disconnectedView.setVisibility(View.VISIBLE);
if (disconnectedView.getAnimation() == null)
disconnectedView.startAnimation(animation);
} else if (state == ContactListState.online) {
connectedView.setVisibility(View.VISIBLE);
disconnectedView.setVisibility(View.INVISIBLE);
disconnectedView.clearAnimation();
}
textView.setText(text);
if (button == 0) {
buttonView.setVisibility(View.GONE);
} else {
buttonView.setVisibility(View.VISIBLE);
buttonView.setText(button);
}
buttonView.setOnClickListener(listener);
}
/**
* Force stop contact list updates before pause or application close.
*/
......
......@@ -24,19 +24,11 @@ import java.util.TreeMap;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.CommonState;
import com.xabber.android.data.account.StatusMode;
import com.xabber.android.data.connection.ConnectionManager;
import com.xabber.android.data.extension.muc.RoomChat;
import com.xabber.android.data.extension.muc.RoomContact;
import com.xabber.android.data.message.AbstractChat;
......@@ -46,10 +38,6 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.GroupManager;
import com.xabber.android.data.roster.RosterContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.AccountAdd;
import com.xabber.android.ui.AccountList;
import com.xabber.android.ui.ContactAdd;
import com.xabber.androiddev.R;
/**
* Adapter for contact list in the main activity.
......@@ -66,36 +54,6 @@ public class ContactListAdapter extends
*/
private static final long REFRESH_INTERVAL = 1000;
/**
* View with information shown on empty contact list.
*/
private final View infoView;
/**
* Image view with connected icon.
*/
private View connectedView;
/**
* Image view with disconnected icon.
*/
private View disconnectedView;
/**
* View with help text.
*/
private TextView textView;
/**
* Button to apply help text.
*/
private Button buttonView;
/**
* Animation for disconnected view.
*/
private Animation animation;
/**
* Handler for deferred refresh.
*/
......@@ -121,25 +79,13 @@ public class ContactListAdapter extends
*/
private Date nextRefresh;
private final OnContactListChangedListener listener;
public ContactListAdapter(Activity activity, ListView listView,
View infoView) {
OnContactListChangedListener listener) {
super(activity, listView, new ChatContactInflater(activity),
GroupManager.getInstance());
this.infoView = infoView;
if (infoView != null) {
connectedView = infoView.findViewById(R.id.connected);
disconnectedView = infoView.findViewById(R.id.disconnected);
textView = (TextView) infoView.findViewById(R.id.text);
buttonView = (Button) infoView.findViewById(R.id.button);
animation = AnimationUtils.loadAnimation(activity,
R.anim.connection);
} else {
connectedView = null;
disconnectedView = null;
textView = null;
buttonView = null;
animation = null;
}
this.listener = listener;
handler = new Handler();
refreshLock = new Object();
refreshRequested = false;
......@@ -228,12 +174,12 @@ public class ContactListAdapter extends
/**
* Whether there is at least one contact.
*/
boolean hasContact = false;
boolean hasContacts = false;
/**
* Whether there is at least one visible contact.
*/
boolean hasVisible = false;
boolean hasVisibleContacts = false;
for (String account : AccountManager.getInstance().getAccounts())
accounts.put(account, null);
......@@ -282,7 +228,7 @@ public class ContactListAdapter extends
for (RosterContact rosterContact : rosterContacts) {
if (!rosterContact.isEnabled())
continue;
hasContact = true;
hasContacts = true;
final boolean online = rosterContact.getStatusMode().isOnline();
final String account = rosterContact.getAccount();
final TreeMap<String, AbstractChat> users = abstractChats
......@@ -295,7 +241,7 @@ public class ContactListAdapter extends
if (showActiveChats && abstractChat != null
&& abstractChat.isActive()) {
activeChats.setNotEmpty();
hasVisible = true;
hasVisibleContacts = true;
if (activeChats.isExpanded())
activeChats.addAbstractContact(rosterContact);
activeChats.increment(online);
......@@ -306,7 +252,7 @@ public class ContactListAdapter extends
continue;
if (addContact(rosterContact, online, accounts, groups,
contacts, showAccounts, showGroups, showOffline))
hasVisible = true;
hasVisibleContacts = true;
}
for (TreeMap<String, AbstractChat> users : abstractChats.values())
for (AbstractChat abstractChat : users.values()) {
......@@ -318,7 +264,7 @@ public class ContactListAdapter extends
abstractContact = new ChatContact(abstractChat);
if (showActiveChats && abstractChat.isActive()) {
activeChats.setNotEmpty();
hasVisible = true;
hasVisibleContacts = true;
if (activeChats.isExpanded())
activeChats.addAbstractContact(abstractContact);
activeChats.increment(false);
......@@ -338,14 +284,14 @@ public class ContactListAdapter extends
group = GroupManager.NO_GROUP;
online = false;
}
hasVisible = true;
hasVisibleContacts = true;
addContact(abstractContact, group, online, accounts,
groups, contacts, showAccounts, showGroups);
}
// Remove empty groups, sort and apply structure.
baseEntities.clear();
if (hasVisible) {
if (hasVisibleContacts) {
if (showActiveChats) {
if (!activeChats.isEmpty()) {
if (showAccounts || showGroups)
......@@ -425,147 +371,12 @@ public class ContactListAdapter extends
Collections.sort(baseEntities, comparator);
this.baseEntities.clear();
this.baseEntities.addAll(baseEntities);
hasVisible = baseEntities.size() > 0;
}
if (infoView != null) {
if (hasVisible) {
infoView.setVisibility(View.GONE);
disconnectedView.clearAnimation();
} else {
infoView.setVisibility(View.VISIBLE);
final int text;
final int button;
final ContactListState state;
final OnClickListener listener;
if (filterString != null) {
if (commonState == CommonState.online)
state = ContactListState.online;
else if (commonState == CommonState.roster
|| commonState == CommonState.connecting)
state = ContactListState.connecting;
else
state = ContactListState.offline;
text = R.string.application_state_no_online;
button = 0;
listener = null;
} else if (hasContact) {
state = ContactListState.online;
text = R.string.application_state_no_online;
button = R.string.application_action_no_online;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
SettingsManager.setContactsShowOffline(true);
onChange();
}
};
} else if (commonState == CommonState.online) {
state = ContactListState.online;
text = R.string.application_state_no_contacts;
button = R.string.application_action_no_contacts;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
activity.startActivity(ContactAdd
.createIntent(activity));
}
};
} else if (commonState == CommonState.roster) {
state = ContactListState.connecting;
text = R.string.application_state_roster;
button = 0;
listener = null;
} else if (commonState == CommonState.connecting) {
state = ContactListState.connecting;
text = R.string.application_state_connecting;
button = 0;
listener = null;
} else if (commonState == CommonState.waiting) {
state = ContactListState.offline;
text = R.string.application_state_waiting;
button = R.string.application_action_waiting;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
ConnectionManager.getInstance().updateConnections(
true);
}
};
} else if (commonState == CommonState.offline) {
state = ContactListState.offline;
text = R.string.application_state_offline;
button = R.string.application_action_offline;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
AccountManager.getInstance().setStatus(
StatusMode.available, null);
}
};
} else if (commonState == CommonState.disabled) {
state = ContactListState.offline;
text = R.string.application_state_disabled;
button = R.string.application_action_disabled;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
activity.startActivity(AccountList
.createIntent(activity));
}
};
} else if (commonState == CommonState.empty) {
state = ContactListState.offline;
text = R.string.application_state_empty;
button = R.string.application_action_empty;
listener = new OnClickListener() {
@Override
public void onClick(View view) {
activity.startActivity(AccountAdd
.createIntent(activity));
}
};
} else {
throw new IllegalStateException();
}
if (state == ContactListState.offline) {
connectedView.setVisibility(View.INVISIBLE);
disconnectedView.setVisibility(View.VISIBLE);
disconnectedView.clearAnimation();
} else if (state == ContactListState.connecting) {
connectedView.setVisibility(View.VISIBLE);
disconnectedView.setVisibility(View.VISIBLE);
if (disconnectedView.getAnimation() == null)
disconnectedView.startAnimation(animation);
} else if (state == ContactListState.online) {
connectedView.setVisibility(View.VISIBLE);
disconnectedView.setVisibility(View.INVISIBLE);
disconnectedView.clearAnimation();
}
textView.setText(text);
if (button == 0) {
buttonView.setVisibility(View.GONE);
} else {
buttonView.setVisibility(View.VISIBLE);
buttonView.setText(button);
}
buttonView.setOnClickListener(listener);
}
hasVisibleContacts = baseEntities.size() > 0;
}
super.onChange();
listener.onContactListChanged(commonState, hasContacts, hasVisibleContacts,
filterString != null);
synchronized (refreshLock) {
nextRefresh = new Date(new Date().getTime() + REFRESH_INTERVAL);
......@@ -581,4 +392,17 @@ public class ContactListAdapter extends
onChange();
}
/**
* Listener for contact list appearance changes.
*
* @author alexander.ivanov
*
*/
public interface OnContactListChangedListener {
void onContactListChanged(CommonState commonState, boolean hasContacts,
boolean hasVisibleContacts, boolean isFilterEnabled);
}
}
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