2010-03-05 09:56:57 -05:00
|
|
|
/*
|
2010-03-12 14:35:25 -05:00
|
|
|
Yaaic - Yet Another Android IRC Client
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2013-01-21 15:32:43 -05:00
|
|
|
Copyright 2009-2013 Sebastian Kaspari
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic 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 Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-21 02:56:40 -05:00
|
|
|
*/
|
2010-03-05 09:56:57 -05:00
|
|
|
package org.yaaic.adapter;
|
|
|
|
|
2012-01-21 02:56:40 -05:00
|
|
|
import java.util.HashMap;
|
2010-03-05 09:56:57 -05:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2012-07-28 05:36:50 -04:00
|
|
|
import org.yaaic.indicator.ConversationStateProvider;
|
2010-05-12 05:45:51 -04:00
|
|
|
import org.yaaic.listener.MessageClickListener;
|
2010-03-10 14:45:56 -05:00
|
|
|
import org.yaaic.model.Conversation;
|
2012-01-21 05:30:41 -05:00
|
|
|
import org.yaaic.model.Server;
|
2010-03-08 15:36:36 -05:00
|
|
|
import org.yaaic.view.MessageListView;
|
2010-03-05 13:34:05 -05:00
|
|
|
|
2012-07-28 05:36:50 -04:00
|
|
|
import android.content.Context;
|
2012-01-21 02:56:40 -05:00
|
|
|
import android.support.v4.view.PagerAdapter;
|
|
|
|
import android.support.v4.view.ViewPager;
|
2010-03-05 09:56:57 -05:00
|
|
|
import android.view.View;
|
|
|
|
|
2012-01-21 05:30:41 -05:00
|
|
|
import com.viewpagerindicator.TitlePageIndicator;
|
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Adapter for displaying a pager of conversations.
|
|
|
|
*
|
2010-03-05 09:56:57 -05:00
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
2012-07-28 05:36:50 -04:00
|
|
|
public class ConversationPagerAdapter extends PagerAdapter implements ConversationStateProvider
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2012-07-28 05:36:50 -04:00
|
|
|
public static final int COLOR_NONE = 0x0;
|
|
|
|
public static final int COLOR_DEFAULT = 0xFFDDDDDD;
|
|
|
|
public static final int COLOR_MESSAGE = 0xFF31B6E7;
|
|
|
|
public static final int COLOR_HIGHLIGHT = 0xFFFFBB00;
|
|
|
|
|
2012-01-21 05:30:41 -05:00
|
|
|
private final Server server;
|
2011-05-29 20:47:18 -04:00
|
|
|
private LinkedList<ConversationInfo> conversations;
|
2012-01-21 02:56:40 -05:00
|
|
|
private final HashMap<Integer, View> views;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2012-01-21 02:56:40 -05:00
|
|
|
/**
|
|
|
|
* Container class to remember conversation and view association.
|
|
|
|
*/
|
2011-05-29 20:47:18 -04:00
|
|
|
public class ConversationInfo {
|
|
|
|
public Conversation conv;
|
|
|
|
public MessageListAdapter adapter;
|
|
|
|
public MessageListView view;
|
|
|
|
|
|
|
|
public ConversationInfo(Conversation conv) {
|
|
|
|
this.conv = conv;
|
|
|
|
this.adapter = null;
|
|
|
|
this.view = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Create a new {@link ConversationPagerAdapter} instance.
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-07-28 05:36:50 -04:00
|
|
|
public ConversationPagerAdapter(Context context, Server server) {
|
2012-01-21 05:30:41 -05:00
|
|
|
this.server = server;
|
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
conversations = new LinkedList<ConversationInfo>();
|
2012-01-21 02:56:40 -05:00
|
|
|
views = new HashMap<Integer, View>();
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Add a conversation to the adapter.
|
|
|
|
*
|
|
|
|
* @param conversation
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
public void addConversation(Conversation conversation) {
|
|
|
|
conversations.add(new ConversationInfo(conversation));
|
|
|
|
|
|
|
|
notifyDataSetChanged();
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Remove the conversation at the given position from the adapter.
|
|
|
|
*
|
|
|
|
* @param position
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
public void removeConversation(int position) {
|
|
|
|
conversations.remove(position);
|
|
|
|
|
|
|
|
notifyDataSetChanged();
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Get position of given item.
|
2011-05-29 20:47:18 -04:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
@Override
|
|
|
|
public int getItemPosition(Object object)
|
|
|
|
{
|
|
|
|
if (views.containsKey(object)) {
|
|
|
|
return POSITION_UNCHANGED;
|
2011-05-29 20:47:18 -04:00
|
|
|
}
|
2012-01-21 02:56:40 -05:00
|
|
|
|
|
|
|
return POSITION_NONE;
|
2011-05-29 20:47:18 -04:00
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get item at position
|
|
|
|
*/
|
|
|
|
public Conversation getItem(int position)
|
|
|
|
{
|
2011-05-29 20:47:18 -04:00
|
|
|
ConversationInfo convInfo = getItemInfo(position);
|
|
|
|
if (convInfo != null) {
|
|
|
|
return convInfo.conv;
|
|
|
|
} else {
|
|
|
|
return null;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-05-29 20:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Get the adapter of the {@link MessageListView} at the given position.
|
2011-05-29 20:47:18 -04:00
|
|
|
*
|
2012-01-21 02:56:40 -05:00
|
|
|
* @param position
|
|
|
|
* @return
|
2011-05-29 20:47:18 -04:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
public MessageListAdapter getItemAdapter(int position)
|
|
|
|
{
|
2011-05-29 20:47:18 -04:00
|
|
|
ConversationInfo convInfo = getItemInfo(position);
|
|
|
|
if (convInfo != null) {
|
|
|
|
return convInfo.adapter;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Get the adapter of the {@link MessageListView} for the conversation
|
|
|
|
* with the given name.
|
2011-05-29 20:47:18 -04:00
|
|
|
*
|
2012-01-21 02:56:40 -05:00
|
|
|
* @param name
|
|
|
|
* @return
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
public MessageListAdapter getItemAdapter(String name)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
2012-01-21 02:56:40 -05:00
|
|
|
return getItemAdapter(getPositionByName(name));
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Get ConversationInfo on item at position
|
|
|
|
*
|
|
|
|
* @param position
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
private ConversationInfo getItemInfo(int position) {
|
|
|
|
if (position >= 0 && position < conversations.size()) {
|
|
|
|
return conversations.get(position);
|
|
|
|
}
|
|
|
|
return null;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get an item by the channel's name
|
2012-01-21 02:56:40 -05:00
|
|
|
*
|
2010-11-18 12:52:19 -05:00
|
|
|
* @param channel
|
|
|
|
* @return The item
|
|
|
|
*/
|
|
|
|
public int getPositionByName(String name)
|
|
|
|
{
|
|
|
|
// Optimization - cache field lookups
|
|
|
|
int mSize = conversations.size();
|
2011-05-29 20:47:18 -04:00
|
|
|
LinkedList<ConversationInfo> mItems = this.conversations;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
for (int i = 0; i < mSize; i++) {
|
2011-05-29 20:47:18 -04:00
|
|
|
if (mItems.get(i).conv.getName().equalsIgnoreCase(name)) {
|
2010-11-18 12:52:19 -05:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Remove all conversations.
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
public void clearConversations()
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
2012-01-21 02:56:40 -05:00
|
|
|
conversations = new LinkedList<ConversationInfo>();
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Get number of conversations from this adapter.
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
@Override
|
|
|
|
public int getCount()
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
2012-01-21 02:56:40 -05:00
|
|
|
return conversations.size();
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Determines whether a page View is associated with a specific key object.
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
@Override
|
|
|
|
public boolean isViewFromObject(View view, Object object)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
2012-01-21 02:56:40 -05:00
|
|
|
return view == object;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Create a view object for the conversation at the given position.
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2011-01-25 15:02:27 -05:00
|
|
|
@Override
|
2012-01-21 02:56:40 -05:00
|
|
|
public Object instantiateItem(View collection, int position) {
|
|
|
|
// ConversationInfo convInfo = getItemInfo(position);
|
|
|
|
ConversationInfo convInfo = conversations.get(position);
|
|
|
|
View view;
|
2011-04-15 14:39:33 -04:00
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
if (convInfo.view != null) {
|
2012-01-21 02:56:40 -05:00
|
|
|
view = convInfo.view;
|
2011-05-29 20:47:18 -04:00
|
|
|
} else {
|
2012-01-21 02:56:40 -05:00
|
|
|
view = renderConversation(convInfo, collection);
|
2011-05-29 20:47:18 -04:00
|
|
|
}
|
2012-01-21 02:56:40 -05:00
|
|
|
|
|
|
|
views.put(position, view);
|
|
|
|
((ViewPager) collection).addView(view);
|
|
|
|
|
|
|
|
return view;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2012-01-21 02:56:40 -05:00
|
|
|
* Render the given conversation and return the new view.
|
|
|
|
*
|
|
|
|
* @param convInfo
|
|
|
|
* @param parent
|
|
|
|
* @return
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2012-01-21 02:56:40 -05:00
|
|
|
private MessageListView renderConversation(ConversationInfo convInfo, View parent)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
2012-01-21 02:56:40 -05:00
|
|
|
MessageListView list = new MessageListView(parent.getContext());
|
2011-05-29 20:47:18 -04:00
|
|
|
convInfo.view = list;
|
2010-11-18 12:52:19 -05:00
|
|
|
list.setOnItemClickListener(MessageClickListener.getInstance());
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
MessageListAdapter adapter = convInfo.adapter;
|
2011-04-15 14:39:33 -04:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (adapter == null) {
|
2011-05-29 20:47:18 -04:00
|
|
|
adapter = new MessageListAdapter(convInfo.conv, parent.getContext());
|
|
|
|
convInfo.adapter = adapter;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2012-01-21 17:07:44 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
list.setAdapter(adapter);
|
2011-05-29 20:49:46 -04:00
|
|
|
list.setSelection(adapter.getCount() - 1); // scroll to bottom
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return list;
|
|
|
|
}
|
2012-01-21 02:56:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given view from the adapter and collection.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void destroyItem(View collection, int position, Object view) {
|
|
|
|
((ViewPager) collection).removeView((View) view);
|
|
|
|
views.remove(position);
|
|
|
|
}
|
2012-01-21 05:30:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title for the given position. Used by the {@link TitlePageIndicator}.
|
|
|
|
*/
|
|
|
|
@Override
|
2012-05-21 15:40:32 -04:00
|
|
|
public String getPageTitle(int position)
|
2012-01-21 05:30:41 -05:00
|
|
|
{
|
|
|
|
Conversation conversation = getItem(position);
|
|
|
|
|
|
|
|
if (conversation.getType() == Conversation.TYPE_SERVER) {
|
|
|
|
return server.getTitle();
|
|
|
|
} else {
|
|
|
|
return conversation.getName();
|
|
|
|
}
|
|
|
|
}
|
2012-07-28 05:36:50 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColorAt(int position)
|
|
|
|
{
|
|
|
|
Conversation conversation = getItem(position);
|
|
|
|
|
|
|
|
switch (conversation.getStatus()) {
|
|
|
|
case Conversation.STATUS_HIGHLIGHT:
|
|
|
|
return COLOR_HIGHLIGHT;
|
|
|
|
|
|
|
|
case Conversation.STATUS_MESSAGE:
|
|
|
|
return COLOR_MESSAGE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return COLOR_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the state color for all conversations lower than the given position.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getColorForLowerThan(int position)
|
|
|
|
{
|
|
|
|
int color = COLOR_NONE;
|
|
|
|
|
|
|
|
for (int i = 0; i < position; i++) {
|
|
|
|
int currentColor = getColorAt(i);
|
|
|
|
|
|
|
|
if (currentColor == COLOR_HIGHLIGHT) {
|
|
|
|
return COLOR_HIGHLIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentColor == COLOR_MESSAGE) {
|
|
|
|
color = COLOR_MESSAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the state color for all conversations greater than the given position.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getColorForGreaterThan(int position)
|
|
|
|
{
|
|
|
|
int size = conversations.size();
|
|
|
|
int color = COLOR_NONE;
|
|
|
|
|
|
|
|
for (int i = position + 1; i < size; i++) {
|
|
|
|
int currentColor = getColorAt(i);
|
|
|
|
|
|
|
|
if (currentColor == COLOR_HIGHLIGHT) {
|
|
|
|
return COLOR_HIGHLIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentColor == COLOR_MESSAGE) {
|
|
|
|
color = COLOR_MESSAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|