Yaaic/application/src/org/yaaic/adapter/DeckAdapter.java

241 lines
5.9 KiB
Java
Raw Normal View History

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;
import org.yaaic.listener.MessageClickListener;
import org.yaaic.model.Conversation;
import org.yaaic.view.MessageListView;
2010-03-05 09:56:57 -05:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ListView;
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
{
2010-11-18 12:52:19 -05:00
private LinkedList<Conversation> conversations;
private MessageListView currentView;
private String currentChannel;
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
/**
* Create a new DeckAdapter instance
*/
public DeckAdapter()
{
conversations = new LinkedList<Conversation>();
}
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
/**
* Clear conversations
*/
public void clearConversations()
{
conversations = new LinkedList<Conversation>();
}
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
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)
{
if (position >= 0 && position < conversations.size()) {
return conversations.get(position);
}
return null;
}
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)
{
conversations.add(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();
LinkedList<Conversation> 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++) {
if (mItems.get(i).getName().equalsIgnoreCase(name)) {
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)
{
Conversation conversation = getItem(position);
// 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.
if (conversation == null) {
return new TextView(parent.getContext());
}
2010-11-18 12:52:19 -05:00
return renderConversation(conversation, parent);
}
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
*/
public MessageListView renderConversation(Conversation conversation, ViewGroup parent)
{
MessageListView list = new MessageListView(parent.getContext(), parent);
list.setOnItemClickListener(MessageClickListener.getInstance());
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
MessageListAdapter adapter = conversation.getMessageListAdapter();
2010-11-18 12:52:19 -05:00
if (adapter == null) {
adapter = new MessageListAdapter(conversation, parent.getContext());
conversation.setMessageListAdapter(adapter);
}
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(
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
}