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
|
|
|
|
2011-02-05 07:00:12 -05:00
|
|
|
Copyright 2009-2011 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/>.
|
2011-01-25 15:02:27 -05:00
|
|
|
*/
|
2010-03-05 09:56:57 -05:00
|
|
|
package org.yaaic.adapter;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2010-03-17 14:58:23 -04:00
|
|
|
import org.yaaic.R;
|
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;
|
2010-03-08 15:36:36 -05:00
|
|
|
import org.yaaic.view.MessageListView;
|
2010-03-05 13:34:05 -05:00
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.BaseAdapter;
|
2010-03-05 13:34:05 -05:00
|
|
|
import android.widget.Gallery;
|
2010-03-08 15:36:36 -05:00
|
|
|
import android.widget.ListView;
|
2011-04-15 14:39:33 -04:00
|
|
|
import android.widget.TextView;
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The adapter for the "DeckView"
|
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
|
|
|
public class DeckAdapter extends BaseAdapter
|
|
|
|
{
|
2011-05-29 20:47:18 -04:00
|
|
|
private LinkedList<ConversationInfo> conversations;
|
2010-11-18 12:52:19 -05:00
|
|
|
private MessageListView currentView;
|
|
|
|
private String currentChannel;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Create a new DeckAdapter instance
|
|
|
|
*/
|
|
|
|
public DeckAdapter()
|
|
|
|
{
|
2011-05-29 20:47:18 -04: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
|
|
|
/**
|
|
|
|
* Clear conversations
|
|
|
|
*/
|
|
|
|
public void clearConversations()
|
|
|
|
{
|
2011-05-29 20:47:18 -04: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
|
|
|
/**
|
|
|
|
* Get number of item
|
|
|
|
*/
|
2011-01-25 15:02:27 -05:00
|
|
|
@Override
|
2010-11-18 12:52:19 -05:00
|
|
|
public int getCount()
|
|
|
|
{
|
|
|
|
return conversations.size();
|
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
/**
|
|
|
|
* Get ConversationInfo on item at position
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Get item at position
|
|
|
|
*/
|
2011-01-25 15:02:27 -05:00
|
|
|
@Override
|
2010-11-18 12:52:19 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get MessageListAdapter belonging to a conversation
|
|
|
|
*
|
|
|
|
* @param position Position of the conversation in the deck
|
|
|
|
*/
|
|
|
|
public MessageListAdapter getItemAdapter(int position) {
|
|
|
|
ConversationInfo convInfo = getItemInfo(position);
|
|
|
|
if (convInfo != null) {
|
|
|
|
return convInfo.adapter;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get MessageListAdapter belonging to a conversation
|
|
|
|
*
|
|
|
|
* @param name Name of the conversation
|
|
|
|
*/
|
|
|
|
public MessageListAdapter getItemAdapter(String name) {
|
|
|
|
return getItemAdapter(getPositionByName(name));
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get id of item at position
|
|
|
|
*/
|
2011-01-25 15:02:27 -05:00
|
|
|
@Override
|
2010-11-18 12:52:19 -05:00
|
|
|
public long getItemId(int position)
|
|
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2011-01-25 15:02:27 -05:00
|
|
|
* Add an item
|
2010-11-18 12:52:19 -05:00
|
|
|
*
|
|
|
|
* @param channel Name of the channel
|
|
|
|
* @param view The view object
|
|
|
|
*/
|
|
|
|
public void addItem(Conversation conversation)
|
|
|
|
{
|
2011-05-29 20:47:18 -04:00
|
|
|
conversations.add(new ConversationInfo(conversation));
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get an item by the channel's name
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Remove an item
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
*/
|
|
|
|
public void removeItem(String target)
|
|
|
|
{
|
|
|
|
int position = getPositionByName(target);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (position != -1) {
|
|
|
|
conversations.remove(position);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Set single channel view
|
|
|
|
*
|
|
|
|
* @param switched
|
|
|
|
*/
|
|
|
|
public void setSwitched(String channel, MessageListView current)
|
|
|
|
{
|
|
|
|
currentChannel = channel;
|
|
|
|
currentView = current;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get single channel view
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public MessageListView getSwitchedView()
|
|
|
|
{
|
|
|
|
return currentView;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get name of channel (single channel view)
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getSwitchedName()
|
|
|
|
{
|
|
|
|
return currentChannel;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2011-01-25 15:02:27 -05:00
|
|
|
* Has the view been switched to single channel view?
|
2010-11-18 12:52:19 -05:00
|
|
|
*
|
|
|
|
* @return view true if view is in single channel view, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isSwitched()
|
|
|
|
{
|
|
|
|
return currentView != null;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get view at given position
|
|
|
|
*/
|
2011-01-25 15:02:27 -05:00
|
|
|
@Override
|
2010-11-18 12:52:19 -05:00
|
|
|
public View getView(int position, View convertView, ViewGroup parent)
|
|
|
|
{
|
2011-05-29 20:47:18 -04:00
|
|
|
ConversationInfo convInfo = getItemInfo(position);
|
2011-04-15 14:39:33 -04:00
|
|
|
|
|
|
|
// Market stack traces prove that sometimes we get a null converstion
|
|
|
|
// because the collection changed while a view is requested for an
|
|
|
|
// item that does not exist anymore... so we just need to reply with
|
|
|
|
// some kind of view here.
|
2011-05-29 20:47:18 -04:00
|
|
|
if (convInfo == null || convInfo.conv == null) {
|
2011-04-15 14:39:33 -04:00
|
|
|
return new TextView(parent.getContext());
|
|
|
|
}
|
|
|
|
|
2011-05-29 20:47:18 -04:00
|
|
|
if (convInfo.view != null) {
|
|
|
|
return convInfo.view;
|
|
|
|
} else {
|
|
|
|
return renderConversation(convInfo, parent);
|
|
|
|
}
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Render a conversation view (MessageListView)
|
|
|
|
*
|
|
|
|
* @param channel The conversation of the view
|
|
|
|
* @param parent The parent view (context)
|
|
|
|
* @return The rendered MessageListView
|
|
|
|
*/
|
2011-05-29 20:47:18 -04:00
|
|
|
private MessageListView renderConversation(ConversationInfo convInfo, ViewGroup parent)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
|
|
|
MessageListView list = new MessageListView(parent.getContext(), parent);
|
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
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
list.setAdapter(adapter);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
list.setDivider(null);
|
|
|
|
list.setLayoutParams(new Gallery.LayoutParams(
|
2011-03-17 20:46:40 -04:00
|
|
|
parent.getWidth()*85/100,
|
2010-11-18 12:52:19 -05:00
|
|
|
parent.getHeight()
|
|
|
|
));
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
list.setBackgroundResource(R.layout.rounded);
|
|
|
|
list.setCacheColorHint(0xee000000);
|
|
|
|
list.setPadding(5, 5, 5, 5);
|
|
|
|
list.setVerticalFadingEdgeEnabled(false);
|
|
|
|
list.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET);
|
|
|
|
list.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
|
|
|
|
list.setSelection(list.getAdapter().getCount() - 1); // scroll to bottom
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return list;
|
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|