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;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2010-03-05 13:34:05 -05:00
|
|
|
import org.yaaic.model.Channel;
|
2010-03-06 11:52:22 -05:00
|
|
|
import org.yaaic.model.Message;
|
2010-03-05 13:34:05 -05:00
|
|
|
|
2010-03-06 14:24:15 -05:00
|
|
|
import android.graphics.Typeface;
|
2010-03-06 11:52:22 -05:00
|
|
|
import android.text.SpannableString;
|
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;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
private HashMap<String, View> map = new HashMap<String, View>();
|
2010-03-05 13:34:05 -05:00
|
|
|
private LinkedList<Channel> channels = new LinkedList<Channel>();
|
|
|
|
private View currentView;
|
|
|
|
private String currentChannel;
|
|
|
|
|
|
|
|
private int width;
|
|
|
|
private int height;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new DeckAdapter
|
|
|
|
*
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
|
|
|
public DeckAdapter(int width, int height)
|
|
|
|
{
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get number of item
|
|
|
|
*/
|
|
|
|
public int getCount()
|
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
return channels.size();
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get item at position
|
|
|
|
*/
|
2010-03-05 13:34:05 -05:00
|
|
|
public Channel getItem(int position)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
return channels.get(position);
|
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-05 13:34:05 -05:00
|
|
|
public void addItem(Channel channel)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
channels.add(channel);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an item by the channel's name
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
* @return The item
|
|
|
|
*/
|
|
|
|
public View getItemByName(String channel)
|
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
if (map.containsKey(channel)) {
|
|
|
|
return map.get(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
*/
|
2010-03-05 13:34:05 -05:00
|
|
|
public void removeItem(Channel channel)
|
2010-03-05 09:56:57 -05:00
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
channels.remove(channel);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2010-03-05 10:25:15 -05:00
|
|
|
|
2010-03-05 13:34:05 -05:00
|
|
|
/**
|
|
|
|
* Set single channel view
|
|
|
|
*
|
|
|
|
* @param switched
|
|
|
|
*/
|
|
|
|
public void setSwitched(String channel, View current)
|
|
|
|
{
|
|
|
|
currentChannel = channel;
|
|
|
|
currentView = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get single channel view
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public View getSwitchedView()
|
|
|
|
{
|
|
|
|
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-05 09:56:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get view at given position
|
|
|
|
*/
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent)
|
|
|
|
{
|
2010-03-05 13:34:05 -05:00
|
|
|
Channel channel = getItem(position);
|
|
|
|
convertView = map.get(channel.getName());
|
|
|
|
|
|
|
|
if (convertView == null) {
|
|
|
|
convertView = renderChannel(channel, parent);
|
|
|
|
map.put(channel.getName(), convertView);
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public View renderChannel(Channel channel, ViewGroup parent)
|
|
|
|
{
|
|
|
|
TextView canvas = new TextView(parent.getContext());
|
2010-03-06 19:01:35 -05:00
|
|
|
canvas.setBackgroundColor(0xff222222);
|
|
|
|
canvas.setPadding(5, 5, 5, 5);
|
2010-03-06 14:24:15 -05:00
|
|
|
canvas.setTextSize(11);
|
|
|
|
canvas.setTypeface(Typeface.MONOSPACE);
|
2010-03-06 12:22:44 -05:00
|
|
|
canvas.setTextColor(0xffeeeeee);
|
|
|
|
|
|
|
|
SpannableString welcome = new SpannableString("Joined " + channel.getName());
|
|
|
|
canvas.setText(welcome);
|
2010-03-05 13:34:05 -05:00
|
|
|
|
2010-03-06 11:52:22 -05:00
|
|
|
for (Message message : channel.getHistory()) {
|
2010-03-06 12:59:05 -05:00
|
|
|
canvas.append(message.render(canvas.getContext()));
|
2010-03-05 13:34:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Refactor this crap :)
|
|
|
|
|
|
|
|
float fw = (float) width;
|
|
|
|
float fh = (float) height;
|
|
|
|
|
|
|
|
float vwf = fw / 100 * 80;
|
|
|
|
float vhf = fh / 100 * 80;
|
|
|
|
|
|
|
|
int w = (int) vwf;
|
|
|
|
int h = (int) vhf;
|
|
|
|
|
2010-03-06 19:01:35 -05:00
|
|
|
//channelView.setLayoutParams(new Gallery.LayoutParams(w, h));
|
2010-03-05 13:34:05 -05:00
|
|
|
canvas.setLayoutParams(new Gallery.LayoutParams(w, h));
|
|
|
|
|
|
|
|
return canvas;
|
2010-03-05 09:56:57 -05:00
|
|
|
}
|
|
|
|
}
|