2010-03-05 09:56:57 -05:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
|
|
|
Copyright 2009 Sebastian Kaspari
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
package org.yaaic.adapter;
|
|
|
|
|
2010-03-11 17:41:31 -05:00
|
|
|
import java.util.Collections;
|
2010-03-05 09:56:57 -05:00
|
|
|
import java.util.LinkedList;
|
2010-03-11 17:41:31 -05:00
|
|
|
import java.util.List;
|
2010-03-05 09:56:57 -05:00
|
|
|
|
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;
|
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-03-08 18:30:17 -05:00
|
|
|
public static final String TAG = "Yaaic/DeckAdapter";
|
|
|
|
|
2010-03-11 17:41:31 -05:00
|
|
|
private List<Conversation> conversations;
|
2010-03-08 15:44:42 -05:00
|
|
|
private MessageListView currentView;
|
2010-03-05 13:34:05 -05:00
|
|
|
private String currentChannel;
|
|
|
|
|
2010-03-11 17:41:31 -05:00
|
|
|
/**
|
|
|
|
* Create a new DeckAdapter instance
|
|
|
|
*/
|
|
|
|
public DeckAdapter()
|
|
|
|
{
|
|
|
|
conversations = Collections.synchronizedList(new LinkedList<Conversation>());
|
|
|
|
}
|
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
/**
|
|
|
|
* Get number of item
|
|
|
|
*/
|
|
|
|
public int getCount()
|
|
|
|
{
|
2010-03-10 14:45:56 -05:00
|
|
|
return conversations.size();
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get item at position
|
|
|
|
*/
|
2010-03-10 14:45:56 -05:00
|
|
|
public Conversation getItem(int position)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-10 14:45:56 -05:00
|
|
|
if (position >= 0 && position < conversations.size()) {
|
|
|
|
return conversations.get(position);
|
2010-03-08 18:30:17 -05:00
|
|
|
}
|
|
|
|
return null;
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get id of item at position
|
|
|
|
*/
|
|
|
|
public long getItemId(int position)
|
|
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an item
|
|
|
|
*
|
|
|
|
* @param channel Name of the channel
|
|
|
|
* @param view The view object
|
|
|
|
*/
|
2010-03-10 14:45:56 -05:00
|
|
|
public void addItem(Conversation conversation)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-10 14:45:56 -05:00
|
|
|
conversations.add(conversation);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an item by the channel's name
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
* @return The item
|
|
|
|
*/
|
2010-03-08 19:57:22 -05:00
|
|
|
public int getPositionByName(String name)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-10 14:45:56 -05:00
|
|
|
for (int i = 0; i < conversations.size(); i++) {
|
|
|
|
if (conversations.get(i).getName().equals(name)) {
|
2010-03-08 19:57:22 -05:00
|
|
|
return i;
|
|
|
|
}
|
2010-03-05 13:34:05 -05:00
|
|
|
}
|
|
|
|
|
2010-03-08 19:57:22 -05:00
|
|
|
return -1;
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
*/
|
2010-03-10 19:10:21 -05:00
|
|
|
public void removeItem(String target)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-10 19:10:21 -05:00
|
|
|
int position = getPositionByName(target);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2010-03-10 19:10:21 -05:00
|
|
|
if (position != -1) {
|
|
|
|
conversations.remove(position);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
2010-03-05 10:25:15 -05:00
|
|
|
|
2010-03-05 13:34:05 -05:00
|
|
|
/**
|
|
|
|
* Set single channel view
|
|
|
|
*
|
|
|
|
* @param switched
|
|
|
|
*/
|
2010-03-08 15:44:42 -05:00
|
|
|
public void setSwitched(String channel, MessageListView current)
|
2010-03-05 13:34:05 -05:00
|
|
|
{
|
|
|
|
currentChannel = channel;
|
|
|
|
currentView = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get single channel view
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2010-03-08 15:44:42 -05:00
|
|
|
public MessageListView getSwitchedView()
|
2010-03-05 13:34:05 -05:00
|
|
|
{
|
|
|
|
return currentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get name of channel (single channel view)
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getSwitchedName()
|
2010-03-05 10:25:15 -05:00
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
return currentChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has the view been switched to single channel view?
|
|
|
|
*
|
|
|
|
* @return view true if view is in single channel view, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isSwitched()
|
|
|
|
{
|
|
|
|
return currentView != null;
|
2010-03-05 10:25:15 -05:00
|
|
|
}
|
2010-03-08 19:57:22 -05:00
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
/**
|
|
|
|
* Get view at given position
|
|
|
|
*/
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent)
|
|
|
|
{
|
2010-03-10 14:45:56 -05:00
|
|
|
Conversation conversation = getItem(position);
|
|
|
|
return renderConversation(conversation, parent);
|
2010-03-05 13:34:05 -05:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:36:36 -05:00
|
|
|
/**
|
2010-03-10 14:45:56 -05:00
|
|
|
* Render a conversation view (MessageListView)
|
2010-03-08 15:36:36 -05:00
|
|
|
*
|
2010-03-10 14:45:56 -05:00
|
|
|
* @param channel The conversation of the view
|
2010-03-08 15:36:36 -05:00
|
|
|
* @param parent The parent view (context)
|
|
|
|
* @return The rendered MessageListView
|
|
|
|
*/
|
2010-03-10 14:45:56 -05:00
|
|
|
public MessageListView renderConversation(Conversation conversation, ViewGroup parent)
|
2010-03-05 13:34:05 -05:00
|
|
|
{
|
2010-03-08 15:36:36 -05:00
|
|
|
MessageListView list = new MessageListView(parent.getContext());
|
2010-03-10 14:45:56 -05:00
|
|
|
list.setAdapter(new MessageListAdapter(conversation, parent.getContext()));
|
2010-03-08 15:36:36 -05:00
|
|
|
|
2010-03-08 15:54:20 -05:00
|
|
|
list.setDivider(null);
|
2010-03-08 17:56:14 -05:00
|
|
|
list.setLayoutParams(new Gallery.LayoutParams(
|
|
|
|
parent.getWidth() / 100 * 85,
|
|
|
|
parent.getHeight() / 100 * 95
|
|
|
|
));
|
2010-03-08 15:36:36 -05:00
|
|
|
list.setBackgroundColor(0xff222222);
|
|
|
|
list.setPadding(5, 5, 5, 5);
|
|
|
|
list.setScrollContainer(false);
|
2010-03-08 19:57:22 -05:00
|
|
|
list.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
|
2010-03-08 16:32:49 -05:00
|
|
|
list.setSelection(list.getAdapter().getCount() - 1); // scroll to bottom
|
2010-03-08 15:36:36 -05:00
|
|
|
|
|
|
|
return list;
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
}
|