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

Refactoring and simplification: BaseContactAdapter, BaseContactInflater,...

Refactoring and simplification: BaseContactAdapter, BaseContactInflater, ChatContactInflater, ClientContactInflater, SmoothContactAdapter and StatusContactInflater united in ContactListAdapter and GroupedContactAdapter.
parent b52b1cce
...@@ -93,7 +93,7 @@ public class ContactListFragment extends Fragment implements OnAccountChangedLis ...@@ -93,7 +93,7 @@ public class ContactListFragment extends Fragment implements OnAccountChangedLis
listView.setOnItemClickListener(this); listView.setOnItemClickListener(this);
listView.setItemsCanFocus(true); listView.setItemsCanFocus(true);
registerForContextMenu(listView); registerForContextMenu(listView);
adapter = new ContactListAdapter(getActivity(), listView, this, this); adapter = new ContactListAdapter(getActivity(), this, this);
listView.setAdapter(adapter); listView.setAdapter(adapter);
infoView = view.findViewById(R.id.info); infoView = view.findViewById(R.id.info);
connectedView = infoView.findViewById(R.id.connected); connectedView = infoView.findViewById(R.id.connected);
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.roster.AbstractContact;
import java.util.ArrayList;
import java.util.Locale;
/**
* Base adapter for the list of contacts.
*
* @author alexander.ivanov
*/
public abstract class BaseContactAdapter<Inflater extends BaseContactInflater>
extends BaseAdapter implements UpdatableAdapter, Filterable {
final Activity activity;
final Locale locale;
/**
* List of entities.
*/
final ArrayList<BaseEntity> baseEntities;
/**
* Used view inflater.
*/
final Inflater inflater;
/**
* Contact filter.
*/
ContactFilter contactFilter;
/**
* Filter string. Can be <code>null</code> if filter is disabled.
*/
String filterString;
public BaseContactAdapter(Activity activity, Inflater inflater) {
this.activity = activity;
this.locale = Locale.getDefault();
this.baseEntities = new ArrayList<>();
this.inflater = inflater;
inflater.setAdapter(this);
contactFilter = null;
filterString = null;
}
@Override
public void onChange() {
notifyDataSetChanged();
}
@Override
public int getCount() {
return baseEntities.size();
}
@Override
public Object getItem(int position) {
return baseEntities.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view;
if (convertView == null) {
view = inflater.createView(position, parent);
view.setTag(inflater.createViewHolder(position, view));
} else {
view = convertView;
}
inflater.getView(view, (AbstractContact) getItem(position));
return view;
}
@Override
public Filter getFilter() {
if (contactFilter == null) {
contactFilter = new ContactFilter();
}
return contactFilter;
}
private class ContactFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return null;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (constraint == null || constraint.length() == 0) {
filterString = null;
} else {
filterString = constraint.toString().toLowerCase(locale);
}
onChange();
}
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
/**
* Provides views and fills them with data for {@link BaseContactAdapter}.
*
* @author alexander.ivanov
*/
public abstract class BaseContactInflater {
final Activity activity;
final LayoutInflater layoutInflater;
/**
* Managed adapter.
*/
BaseAdapter adapter;
private final int[] accountColors;
public BaseContactInflater(Activity activity) {
this.activity = activity;
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
accountColors = activity.getResources().getIntArray(R.array.account_action_bar);
}
/**
* Sets managed adapter.
*
* @param adapter
*/
void setAdapter(BaseAdapter adapter) {
this.adapter = adapter;
}
/**
* Creates new view for specified position.
*
* @param position
* @param parent
* @return
*/
abstract View createView(int position, ViewGroup parent);
/**
* Creates new instance of ViewHolder.
*
* @param position
* @param view
* @return
*/
abstract ViewHolder createViewHolder(int position, View view);
/**
* Returns status text.
*
* @param abstractContact
* @return
*/
String getStatusText(AbstractContact abstractContact) {
return abstractContact.getStatusText();
}
/**
* Fills view for {@link BaseContactAdapter}.
*
* @param view view to be inflated.
* @param abstractContact contact to be shown.
*/
public void getView(View view, AbstractContact abstractContact) {
final ViewHolder viewHolder = (ViewHolder) view.getTag();
if (abstractContact.isConnected()) {
viewHolder.offlineShadow.setVisibility(View.GONE);
} else {
viewHolder.offlineShadow.setVisibility(View.VISIBLE);
}
int colorLevel = abstractContact.getColorLevel();
viewHolder.color.setImageDrawable(new ColorDrawable(accountColors[colorLevel]));
if (SettingsManager.contactsShowAvatars()) {
viewHolder.avatar.setVisibility(View.VISIBLE);
viewHolder.avatar.setImageDrawable(abstractContact.getAvatarForContactList());
} else {
viewHolder.avatar.setVisibility(View.GONE);
}
viewHolder.name.setText(abstractContact.getName());
final String statusText = getStatusText(abstractContact);
if ("".equals(statusText)) {
viewHolder.name.getLayoutParams().height = activity.getResources()
.getDimensionPixelSize(R.dimen.contact_name_height_hide_status);
viewHolder.name.setGravity(Gravity.CENTER_VERTICAL);
viewHolder.status.setVisibility(View.GONE);
} else {
viewHolder.name.getLayoutParams().height = activity.getResources()
.getDimensionPixelSize(R.dimen.contact_name_height_show_status);
viewHolder.name.setGravity(Gravity.BOTTOM);
viewHolder.status.setText(statusText);
viewHolder.status.setVisibility(View.VISIBLE);
}
}
/**
* Holder for views in contact item.
*/
static class ViewHolder {
final ImageView color;
final ImageView avatar;
final TextView name;
final TextView status;
final ImageView offlineShadow;
public ViewHolder(View view) {
color = (ImageView) view.findViewById(R.id.color);
avatar = (ImageView) view.findViewById(R.id.avatar);
name = (TextView) view.findViewById(R.id.name);
status = (TextView) view.findViewById(R.id.status);
offlineShadow = (ImageView) view.findViewById(R.id.offline_shadow);
}
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.view.View;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
/**
* Inflate view with contact's last message or status text as well as active
* chat badge.
*
* @author alexander.ivanov
*/
public class ChatContactInflater extends ClientContactInflater {
public ChatContactInflater(Activity activity) {
super(activity);
}
@Override
ViewHolder createViewHolder(int position, View view) {
return new ViewHolder(view);
}
@Override
String getStatusText(AbstractContact abstractContact) {
if (MessageManager.getInstance()
.hasActiveChat(abstractContact.getAccount(), abstractContact.getUser())) {
return MessageManager.getInstance()
.getLastText(abstractContact.getAccount(), abstractContact.getUser());
} else {
return super.getStatusText(abstractContact);
}
}
@Override
public void getView(View view, AbstractContact abstractContact) {
super.getView(view, abstractContact);
if (MessageManager.getInstance().hasActiveChat(abstractContact.getAccount(), abstractContact.getUser())) {
view.setBackgroundColor(activity.getResources().getColor(R.color.grey_50));
} else {
view.setBackgroundColor(activity.getResources().getColor(R.color.grey_300));
}
}
static class ViewHolder extends ClientContactInflater.ViewHolder {
final View panel;
public ViewHolder(View view) {
super(view);
panel = view.findViewById(R.id.panel);
}
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import com.xabber.android.data.extension.capability.ClientSoftware;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
/**
* Inflate view with contact's client software.
*
* @author alexander.ivanov
*/
public class ClientContactInflater extends StatusContactInflater {
public ClientContactInflater(Activity activity) {
super(activity);
}
@Override
ViewHolder createViewHolder(int position, View view) {
return new ClientContactInflater.ViewHolder(view);
}
@Override
public void getView(View view, AbstractContact abstractContact) {
super.getView(view, abstractContact);
ViewHolder viewHolder = (ViewHolder) view.getTag();
ClientSoftware clientSoftware = abstractContact.getClientSoftware();
if (clientSoftware == ClientSoftware.unknown)
viewHolder.clientSoftware.setVisibility(View.INVISIBLE);
else {
viewHolder.clientSoftware.setVisibility(View.VISIBLE);
viewHolder.clientSoftware.setImageLevel(clientSoftware.ordinal());
}
}
static class ViewHolder extends StatusContactInflater.ViewHolder {
final ImageView clientSoftware;
public ViewHolder(View view) {
super(view);
clientSoftware = (ImageView) view
.findViewById(R.id.client_software);
}
}
}
...@@ -16,6 +16,8 @@ package com.xabber.android.ui.adapter; ...@@ -16,6 +16,8 @@ package com.xabber.android.ui.adapter;
import android.app.Activity; import android.app.Activity;
import android.os.Handler; import android.os.Handler;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView; import android.widget.ListView;
import com.xabber.android.data.SettingsManager; import com.xabber.android.data.SettingsManager;
...@@ -47,8 +49,7 @@ import java.util.TreeMap; ...@@ -47,8 +49,7 @@ import java.util.TreeMap;
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflater, GroupManager> public class ContactListAdapter extends GroupedContactAdapter implements Runnable, Filterable {
implements Runnable {
/** /**
* Number of milliseconds between lazy refreshes. * Number of milliseconds between lazy refreshes.
...@@ -80,11 +81,21 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -80,11 +81,21 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
*/ */
private Date nextRefresh; private Date nextRefresh;
/**
* Contact filter.
*/
ContactFilter contactFilter;
/**
* Filter string. Can be <code>null</code> if filter is disabled.
*/
String filterString;
private final OnContactListChangedListener listener; private final OnContactListChangedListener listener;
public ContactListAdapter(Activity activity, ListView listView, OnContactListChangedListener listener, public ContactListAdapter(Activity activity, OnContactListChangedListener listener,
GroupedContactAdapter.OnAccountClickListener onAccountClickListener) { GroupedContactAdapter.OnAccountClickListener onAccountClickListener) {
super(activity, listView, new ChatContactInflater(activity), GroupManager.getInstance(), onAccountClickListener); super(activity, onAccountClickListener);
this.listener = listener; this.listener = listener;
handler = new Handler(); handler = new Handler();
refreshLock = new Object(); refreshLock = new Object();
...@@ -197,7 +208,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -197,7 +208,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
contacts = null; contacts = null;
for (Entry<String, AccountConfiguration> entry : accounts.entrySet()) { for (Entry<String, AccountConfiguration> entry : accounts.entrySet()) {
entry.setValue(new AccountConfiguration(entry.getKey(), entry.setValue(new AccountConfiguration(entry.getKey(),
GroupManager.IS_ACCOUNT, groupStateProvider)); GroupManager.IS_ACCOUNT, GroupManager.getInstance()));
} }
} else { } else {
if (showGroups) { if (showGroups) {
...@@ -210,7 +221,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -210,7 +221,7 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
} }
if (showActiveChats) { if (showActiveChats) {
activeChats = new GroupConfiguration(GroupManager.NO_ACCOUNT, activeChats = new GroupConfiguration(GroupManager.NO_ACCOUNT,
GroupManager.ACTIVE_CHATS, groupStateProvider); GroupManager.ACTIVE_CHATS, GroupManager.getInstance());
} else { } else {
activeChats = null; activeChats = null;
} }
...@@ -429,4 +440,32 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate ...@@ -429,4 +440,32 @@ public class ContactListAdapter extends GroupedContactAdapter<ChatContactInflate
super(account, user); super(account, user);
} }
} }
@Override
public Filter getFilter() {
if (contactFilter == null) {
contactFilter = new ContactFilter();
}
return contactFilter;
}
private class ContactFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return null;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (constraint == null || constraint.length() == 0) {
filterString = null;
} else {
filterString = constraint.toString().toLowerCase(locale);
}
onChange();
}
}
} }
...@@ -21,19 +21,23 @@ import android.graphics.drawable.ColorDrawable; ...@@ -21,19 +21,23 @@ import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.xabber.android.data.SettingsManager; import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountItem; import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager; import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.StatusMode; import com.xabber.android.data.account.StatusMode;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.avatar.AvatarManager; import com.xabber.android.data.extension.avatar.AvatarManager;
import com.xabber.android.data.extension.capability.ClientSoftware;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.roster.AbstractContact; import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.Group; import com.xabber.android.data.roster.Group;
import com.xabber.android.data.roster.GroupManager; import com.xabber.android.data.roster.GroupManager;
import com.xabber.android.data.roster.GroupStateProvider;
import com.xabber.android.data.roster.ShowOfflineMode; import com.xabber.android.data.roster.ShowOfflineMode;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
...@@ -41,6 +45,7 @@ import java.util.ArrayList; ...@@ -41,6 +45,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
/** /**
...@@ -48,8 +53,7 @@ import java.util.Map; ...@@ -48,8 +53,7 @@ import java.util.Map;
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater, StateProvider extends GroupStateProvider> public abstract class GroupedContactAdapter extends BaseAdapter implements UpdatableAdapter {
extends SmoothContactAdapter<Inflater> {
/** /**
* List of groups used if contact has no groups. * List of groups used if contact has no groups.
...@@ -82,36 +86,56 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -82,36 +86,56 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
NO_GROUP_LIST = Collections.unmodifiableCollection(groups); NO_GROUP_LIST = Collections.unmodifiableCollection(groups);
} }
/**
* Group state provider.
*/
final StateProvider groupStateProvider;
/** /**
* Layout inflater * Layout inflater
*/ */
private final LayoutInflater layoutInflater; private final LayoutInflater layoutInflater;
private final Activity activity;
private int[] accountColors; private int[] accountMainColors;
private int[] accountGroupColors;
private final int[] accountSubgroupColors; private final int[] accountSubgroupColors;
private final int activeChatsColor; private final int activeChatsColor;
private final OnAccountClickListener onAccountClickListener; private final OnAccountClickListener onAccountClickListener;
public GroupedContactAdapter(Activity activity, ListView listView, Inflater inflater, final ArrayList<BaseEntity> baseEntities = new ArrayList<>();
StateProvider groupStateProvider, OnAccountClickListener onAccountClickListener) { protected Locale locale = Locale.getDefault();
super(activity, listView, inflater);
public GroupedContactAdapter(Activity activity, OnAccountClickListener onAccountClickListener) {
this.activity = activity;
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groupStateProvider = groupStateProvider;
Resources resources = activity.getResources(); Resources resources = activity.getResources();
accountColors = resources.getIntArray(R.array.account_200); accountMainColors = resources.getIntArray(R.array.account_action_bar);
accountGroupColors = resources.getIntArray(R.array.account_200);
accountSubgroupColors = resources.getIntArray(R.array.account_50); accountSubgroupColors = resources.getIntArray(R.array.account_50);
activeChatsColor = resources.getColor(R.color.color_primary_light); activeChatsColor = resources.getColor(R.color.color_primary_light);
this.onAccountClickListener = onAccountClickListener; this.onAccountClickListener = onAccountClickListener;
} }
@Override
public void onChange() {
notifyDataSetChanged();
}
@Override
public int getCount() {
return baseEntities.size();
}
@Override
public Object getItem(int position) {
return baseEntities.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override @Override
public int getViewTypeCount() { public int getViewTypeCount() {
return TYPE_COUNT; return TYPE_COUNT;
...@@ -139,170 +163,244 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -139,170 +163,244 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
switch (getItemViewType(position)) { switch (getItemViewType(position)) {
case TYPE_CONTACT: case TYPE_CONTACT:
return super.getView(position, convertView, parent); return getContactView(position, convertView, parent);
case TYPE_GROUP: case TYPE_GROUP:
{ return getGroupView(position, convertView, parent);
final View view;
final GroupViewHolder viewHolder; case TYPE_ACCOUNT:
if (convertView == null) { return getAccountView(position, convertView, parent);
view = layoutInflater.inflate(R.layout.base_group_item, parent, false);
viewHolder = new GroupViewHolder(view); case TYPE_ACCOUNT_TOP_SEPARATOR:
view.setTag(viewHolder); return getView(convertView, parent, R.layout.account_group_item_top_separator);
} else {
view = convertView; case TYPE_ACCOUNT_BOTTOM_SEPARATOR:
viewHolder = (GroupViewHolder) view.getTag(); return getAccountBottomSeparatorView(
} getView(convertView, parent, R.layout.account_group_item_bottom_separator), position);
default:
throw new IllegalStateException();
}
}
final GroupConfiguration configuration = (GroupConfiguration) getItem(position); private View getView(View convertView, ViewGroup parent, int layoutId) {
final int level = AccountManager.getInstance().getColorLevel(configuration.getAccount()); final View view;
if (convertView == null) {
view = layoutInflater.inflate(layoutId, parent, false);
} else {
view = convertView;
}
return view;
}
final String name = GroupManager.getInstance() private View getAccountBottomSeparatorView(View view, int position) {
.getGroupName(configuration.getAccount(), configuration.getUser()); final ContactListAdapter.AccountBottomSeparator accountBottomSeparator
= (ContactListAdapter.AccountBottomSeparator) getItem(position);
final int level = AccountManager.getInstance().getColorLevel(accountBottomSeparator.getAccount());
View bottomLayer = view.findViewById(R.id.bottom_layer);
View topLayer = view.findViewById(R.id.top_layer);
viewHolder.indicator.setImageLevel(configuration.isExpanded() ? 1 : 0); bottomLayer.setBackgroundDrawable(new ColorDrawable(accountSubgroupColors[level]));
viewHolder.groupOfflineIndicator.setImageLevel(configuration.getShowOfflineMode().ordinal()); topLayer.setBackgroundDrawable(new ColorDrawable(accountSubgroupColors[level]));
int color; StatusMode statusMode = AccountManager.getInstance().getAccount(accountBottomSeparator.getAccount()).getDisplayStatusMode();
viewHolder.groupOfflineIndicator.setVisibility(View.GONE); View offlineShadowBottom = view.findViewById(R.id.offline_shadow_top);
View offlineShadowTop = view.findViewById(R.id.offline_shadow_bottom);
if (configuration.getUser().equals(GroupManager.ACTIVE_CHATS)) { if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) {
color = activeChatsColor; offlineShadowBottom.setVisibility(View.VISIBLE);
viewHolder.name.setText(name); offlineShadowTop.setVisibility(View.VISIBLE);
} else { } else {
viewHolder.name.setText(name + " (" + configuration.getOnline() offlineShadowBottom.setVisibility(View.GONE);
+ "/" + configuration.getTotal() + ")"); offlineShadowTop.setVisibility(View.GONE);
}
color = accountSubgroupColors[level]; return view;
viewHolder.groupOfflineIndicator.setVisibility(View.VISIBLE); }
StatusMode statusMode = AccountManager.getInstance().getAccount(configuration.getAccount()).getDisplayStatusMode(); private View getAccountView(int position, View convertView, ViewGroup parent) {
final View view;
final AccountViewHolder viewHolder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.account_group_item, parent, false);
if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) { viewHolder = new AccountViewHolder(view);
viewHolder.offlineShadow.setVisibility(View.VISIBLE); view.setTag(viewHolder);
} else { } else {
viewHolder.offlineShadow.setVisibility(View.GONE); view = convertView;
} viewHolder = (AccountViewHolder) view.getTag();
} }
final AccountConfiguration configuration = (AccountConfiguration) getItem(position);
view.setBackgroundDrawable(new ColorDrawable(color)); final String account = configuration.getAccount();
return view; viewHolder.statusIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAccountClickListener.onAccountClick(v, account);
} }
});
case TYPE_ACCOUNT: { final int level = AccountManager.getInstance().getColorLevel(account);
final View view; view.setBackgroundDrawable(new ColorDrawable(accountGroupColors[level]));
final AccountViewHolder viewHolder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.account_group_item, parent, false);
viewHolder = new AccountViewHolder(view);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (AccountViewHolder) view.getTag();
}
final AccountConfiguration configuration = (AccountConfiguration) getItem(position); viewHolder.jid.setText(GroupManager.getInstance().getGroupName(account, configuration.getUser()));
viewHolder.contactCounter.setText(configuration.getOnline() + "/" + configuration.getTotal());
final String account = configuration.getAccount(); AccountItem accountItem = AccountManager.getInstance().getAccount(account);
String statusText = accountItem.getStatusText().trim();
viewHolder.statusIcon.setOnClickListener(new View.OnClickListener() { if (statusText.isEmpty()) {
@Override statusText = activity.getString(accountItem.getDisplayStatusMode().getStringID());
public void onClick(View v) { }
onAccountClickListener.onAccountClick(v, account);
}
});
final int level = AccountManager.getInstance().getColorLevel(account); viewHolder.status.setText(statusText);
view.setBackgroundDrawable(new ColorDrawable(accountColors[level]));
viewHolder.jid.setText(GroupManager.getInstance().getGroupName(account, configuration.getUser())); viewHolder.avatar.setImageDrawable(AvatarManager.getInstance().getAccountAvatar(account));
viewHolder.contactCounter.setText(configuration.getOnline() + "/" + configuration.getTotal()); viewHolder.statusIcon.setImageLevel(accountItem.getDisplayStatusMode().getStatusLevel());
AccountItem accountItem = AccountManager.getInstance().getAccount(account); ShowOfflineMode showOfflineMode = configuration.getShowOfflineMode();
String statusText = accountItem.getStatusText().trim(); if (showOfflineMode == ShowOfflineMode.normal) {
if (SettingsManager.contactsShowOffline()) {
showOfflineMode = ShowOfflineMode.always;
} else {
showOfflineMode = ShowOfflineMode.never;
}
}
if (statusText.isEmpty()) { viewHolder.offlineContactsIndicator.setImageLevel(showOfflineMode.ordinal());
statusText = activity.getString(accountItem.getDisplayStatusMode().getStringID());
}
viewHolder.status.setText(statusText); StatusMode statusMode = AccountManager.getInstance().getAccount(configuration.getAccount()).getDisplayStatusMode();
viewHolder.avatar.setImageDrawable(AvatarManager.getInstance().getAccountAvatar(account)); if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) {
viewHolder.statusIcon.setImageLevel(accountItem.getDisplayStatusMode().getStatusLevel()); viewHolder.offlineShadow.setVisibility(View.VISIBLE);
} else {
viewHolder.offlineShadow.setVisibility(View.GONE);
}
ShowOfflineMode showOfflineMode = configuration.getShowOfflineMode(); return view;
if (showOfflineMode == ShowOfflineMode.normal) { }
if (SettingsManager.contactsShowOffline()) {
showOfflineMode = ShowOfflineMode.always;
} else {
showOfflineMode = ShowOfflineMode.never;
}
}
viewHolder.offlineContactsIndicator.setImageLevel(showOfflineMode.ordinal()); private View getGroupView(int position, View convertView, ViewGroup parent) {
final View view;
final GroupViewHolder viewHolder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.base_group_item, parent, false);
viewHolder = new GroupViewHolder(view);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (GroupViewHolder) view.getTag();
}
StatusMode statusMode = AccountManager.getInstance().getAccount(configuration.getAccount()).getDisplayStatusMode(); final GroupConfiguration configuration = (GroupConfiguration) getItem(position);
final int level = AccountManager.getInstance().getColorLevel(configuration.getAccount());
if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) { final String name = GroupManager.getInstance()
viewHolder.offlineShadow.setVisibility(View.VISIBLE); .getGroupName(configuration.getAccount(), configuration.getUser());
} else {
viewHolder.offlineShadow.setVisibility(View.GONE);
}
return view;
}
case TYPE_ACCOUNT_TOP_SEPARATOR: { viewHolder.indicator.setImageLevel(configuration.isExpanded() ? 1 : 0);
final View view; viewHolder.groupOfflineIndicator.setImageLevel(configuration.getShowOfflineMode().ordinal());
if (convertView == null) {
view = layoutInflater.inflate(R.layout.account_group_item_top_separator, parent, false); int color;
} else {
view = convertView;
}
return view; viewHolder.groupOfflineIndicator.setVisibility(View.GONE);
if (configuration.getUser().equals(GroupManager.ACTIVE_CHATS)) {
color = activeChatsColor;
viewHolder.name.setText(name);
} else {
viewHolder.name.setText(name + " (" + configuration.getOnline()
+ "/" + configuration.getTotal() + ")");
color = accountSubgroupColors[level];
viewHolder.groupOfflineIndicator.setVisibility(View.VISIBLE);
StatusMode statusMode = AccountManager.getInstance().getAccount(configuration.getAccount()).getDisplayStatusMode();
if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) {
viewHolder.offlineShadow.setVisibility(View.VISIBLE);
} else {
viewHolder.offlineShadow.setVisibility(View.GONE);
} }
}
case TYPE_ACCOUNT_BOTTOM_SEPARATOR: { view.setBackgroundDrawable(new ColorDrawable(color));
final View view;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.account_group_item_bottom_separator, parent, false);
} else {
view = convertView;
}
final ContactListAdapter.AccountBottomSeparator accountBottomSeparator = (ContactListAdapter.AccountBottomSeparator) getItem(position); return view;
final int level = AccountManager.getInstance().getColorLevel(accountBottomSeparator.getAccount()); }
View bottomLayer = view.findViewById(R.id.bottom_layer); private View getContactView(int position, View convertView, ViewGroup parent) {
View topLayer = view.findViewById(R.id.top_layer); final View view;
final ContactViewHolder viewHolder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.base_contact_item, parent, false);
viewHolder = new ContactViewHolder(view);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ContactViewHolder) view.getTag();
}
bottomLayer.setBackgroundDrawable(new ColorDrawable(accountSubgroupColors[level])); AbstractContact abstractContact = (AbstractContact) getItem(position);
topLayer.setBackgroundDrawable(new ColorDrawable(accountSubgroupColors[level]));
StatusMode statusMode = AccountManager.getInstance().getAccount(accountBottomSeparator.getAccount()).getDisplayStatusMode(); if (abstractContact.isConnected()) {
viewHolder.offlineShadow.setVisibility(View.GONE);
} else {
viewHolder.offlineShadow.setVisibility(View.VISIBLE);
}
View offlineShadowBottom = view.findViewById(R.id.offline_shadow_top); int colorLevel = abstractContact.getColorLevel();
View offlineShadowTop = view.findViewById(R.id.offline_shadow_bottom); viewHolder.color.setImageDrawable(new ColorDrawable(accountMainColors[colorLevel]));
if (statusMode == StatusMode.unavailable || statusMode == StatusMode.connection) { if (SettingsManager.contactsShowAvatars()) {
offlineShadowBottom.setVisibility(View.VISIBLE); viewHolder.avatar.setVisibility(View.VISIBLE);
offlineShadowTop.setVisibility(View.VISIBLE); viewHolder.avatar.setImageDrawable(abstractContact.getAvatarForContactList());
} else { } else {
offlineShadowBottom.setVisibility(View.GONE); viewHolder.avatar.setVisibility(View.GONE);
offlineShadowTop.setVisibility(View.GONE); }
}
return view; viewHolder.name.setText(abstractContact.getName());
} String statusText;
default: if (MessageManager.getInstance()
throw new IllegalStateException(); .hasActiveChat(abstractContact.getAccount(), abstractContact.getUser())) {
statusText = MessageManager.getInstance()
.getLastText(abstractContact.getAccount(), abstractContact.getUser());
} else {
statusText = abstractContact.getStatusText();
} }
statusText = statusText.trim();
if ("".equals(statusText)) {
viewHolder.status.setVisibility(View.GONE);
} else {
viewHolder.status.setText(statusText);
viewHolder.status.setVisibility(View.VISIBLE);
}
viewHolder.statusMode.setImageLevel(abstractContact.getStatusMode().getStatusLevel());
ClientSoftware clientSoftware = abstractContact.getClientSoftware();
if (clientSoftware == ClientSoftware.unknown)
viewHolder.clientSoftware.setVisibility(View.INVISIBLE);
else {
viewHolder.clientSoftware.setVisibility(View.VISIBLE);
viewHolder.clientSoftware.setImageLevel(clientSoftware.ordinal());
}
if (MessageManager.getInstance().hasActiveChat(abstractContact.getAccount(), abstractContact.getUser())) {
view.setBackgroundColor(activity.getResources().getColor(R.color.grey_50));
} else {
view.setBackgroundColor(activity.getResources().getColor(R.color.grey_300));
}
return view;
} }
/** /**
...@@ -318,7 +416,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -318,7 +416,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
return groupConfiguration; return groupConfiguration;
} }
groupConfiguration = new GroupConfiguration( groupConfiguration = new GroupConfiguration(
accountConfiguration.getAccount(), name, groupStateProvider); accountConfiguration.getAccount(), name, GroupManager.getInstance());
accountConfiguration.addGroupConfiguration(groupConfiguration); accountConfiguration.addGroupConfiguration(groupConfiguration);
return groupConfiguration; return groupConfiguration;
} }
...@@ -335,7 +433,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -335,7 +433,7 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
if (groupConfiguration != null) { if (groupConfiguration != null) {
return groupConfiguration; return groupConfiguration;
} }
groupConfiguration = new GroupConfiguration(GroupManager.NO_ACCOUNT, name, groupStateProvider); groupConfiguration = new GroupConfiguration(GroupManager.NO_ACCOUNT, name, GroupManager.getInstance());
groups.put(name, groupConfiguration); groups.put(name, groupConfiguration);
return groupConfiguration; return groupConfiguration;
} }
...@@ -489,10 +587,31 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater ...@@ -489,10 +587,31 @@ public abstract class GroupedContactAdapter<Inflater extends BaseContactInflater
* Sets whether group in specified account is expanded. * Sets whether group in specified account is expanded.
*/ */
public void setExpanded(String account, String group, boolean expanded) { public void setExpanded(String account, String group, boolean expanded) {
groupStateProvider.setExpanded(account, group, expanded); GroupManager.getInstance().setExpanded(account, group, expanded);
onChange(); onChange();
} }
static class ContactViewHolder {
final ImageView color;
final ImageView avatar;
final TextView name;
final TextView status;
final ImageView offlineShadow;
final ImageView statusMode;
final ImageView clientSoftware;
public ContactViewHolder(View view) {
color = (ImageView) view.findViewById(R.id.color);
avatar = (ImageView) view.findViewById(R.id.avatar);
name = (TextView) view.findViewById(R.id.name);
status = (TextView) view.findViewById(R.id.status);
offlineShadow = (ImageView) view.findViewById(R.id.offline_shadow);
statusMode = (ImageView) view.findViewById(R.id.status_icon);
clientSoftware = (ImageView) view.findViewById(R.id.client_software);
}
}
/** /**
* Holder for views in contact list group. * Holder for views in contact list group.
*/ */
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.widget.ListView;
/**
* Enable smooth scrollbar depend on number of entities.
*
* @param <Inflater>
* @author alexander.ivanov
*/
public abstract class SmoothContactAdapter<Inflater extends BaseContactInflater>
extends BaseContactAdapter<Inflater> {
/**
* Minimum number of item when smooth scroll bar will be disabled.
*/
private static final int SMOOTH_SCROLLBAR_LIMIT = 20;
/**
* Managed list view.
*/
ListView listView;
public SmoothContactAdapter(Activity activity, ListView listView, Inflater inflater) {
super(activity, inflater);
this.listView = listView;
}
@Override
public void onChange() {
super.onChange();
listView.setSmoothScrollbarEnabled(baseEntities.size() < SMOOTH_SCROLLBAR_LIMIT);
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
/**
* Inflate view with contact's status mode.
*
* @author alexander.ivanov
*/
public class StatusContactInflater extends BaseContactInflater {
public StatusContactInflater(Activity activity) {
super(activity);
}
@Override
View createView(int position, ViewGroup parent) {
return layoutInflater
.inflate(R.layout.base_contact_item, parent, false);
}
@Override
ViewHolder createViewHolder(int position, View view) {
return new StatusContactInflater.ViewHolder(view);
}
@Override
public void getView(View view, AbstractContact abstractContact) {
super.getView(view, abstractContact);
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.statusMode.setImageLevel(abstractContact.getStatusMode()
.getStatusLevel());
}
static class ViewHolder extends BaseContactInflater.ViewHolder {
final ImageView statusMode;
public ViewHolder(View view) {
super(view);
statusMode = (ImageView) view.findViewById(R.id.status_icon);
}
}
}
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