1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2025-02-16 15:00:14 -05:00

Single channel view on channel selection

This commit is contained in:
Sebastian Kaspari 2010-03-05 19:34:05 +01:00
parent c1f392bf08
commit ef8ddfa227
3 changed files with 205 additions and 53 deletions

View File

@ -23,9 +23,14 @@ package org.yaaic.adapter;
import java.util.HashMap;
import java.util.LinkedList;
import org.yaaic.model.Channel;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
/**
* The adapter for the "DeckView"
@ -35,22 +40,39 @@ import android.widget.BaseAdapter;
public class DeckAdapter extends BaseAdapter
{
private HashMap<String, View> map = new HashMap<String, View>();
private LinkedList<View> views = new LinkedList<View>();
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;
}
/**
* Get number of item
*/
public int getCount()
{
return views.size();
return channels.size();
}
/**
* Get item at position
*/
public View getItem(int position)
public Channel getItem(int position)
{
return views.get(position);
return channels.get(position);
}
/**
@ -67,10 +89,10 @@ public class DeckAdapter extends BaseAdapter
* @param channel Name of the channel
* @param view The view object
*/
public void addItem(String channel, View view)
public void addItem(Channel channel)
{
map.put(channel, view);
views.add(view);
channels.add(channel);
//views.add(view);
notifyDataSetChanged();
}
@ -83,7 +105,11 @@ public class DeckAdapter extends BaseAdapter
*/
public View getItemByName(String channel)
{
return map.get(channel);
if (map.containsKey(channel)) {
return map.get(channel);
}
return null;
}
/**
@ -91,18 +117,57 @@ public class DeckAdapter extends BaseAdapter
*
* @param channel
*/
public void removeItem(String channel)
public void removeItem(Channel channel)
{
channels.remove(channel);
/*
View view = map.get(channel);
views.remove(view);
map.remove(channel);
*/
notifyDataSetChanged();
}
public void updated()
/**
* Set single channel view
*
* @param switched
*/
public void setSwitched(String channel, View current)
{
//notifyDataSetChanged();
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()
{
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;
}
/**
@ -110,6 +175,43 @@ public class DeckAdapter extends BaseAdapter
*/
public View getView(int position, View convertView, ViewGroup parent)
{
return views.get(position);
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());
canvas.setText(channel.getName());
for (String message : channel.getHistory()) {
canvas.append("\n" + message);
}
canvas.setTextColor(0xff000000);
// 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;
canvas.setPadding(10, 10, 10, 10);
canvas.setBackgroundColor(0xff888888);
canvas.setLayoutParams(new Gallery.LayoutParams(w, h));
return canvas;
}
}

View File

@ -32,7 +32,8 @@ public class Channel
private static final int BUFFER_SIZE = 30;
private String name;
private LinkedList<String> messages = new LinkedList<String>();
private LinkedList<String> buffer = new LinkedList<String>();
private LinkedList<String> history = new LinkedList<String>();
/**
* Create a new channel object
@ -57,30 +58,41 @@ public class Channel
*/
public void addMessage(String message)
{
messages.addFirst(message);
buffer.addFirst(message);
history.addLast(message);
if (messages.size() > BUFFER_SIZE) {
messages.removeLast();
if (history.size() > BUFFER_SIZE) {
history.removeFirst();
}
}
/**
* Get all messages
* Get all buffered messages
*
* @return
*/
public LinkedList<String> getMessages()
public LinkedList<String> getBuffer()
{
return messages;
return buffer;
}
/**
* Get last message
* Get channel history
*
* @return
*/
public LinkedList<String> getHistory()
{
return history;
}
/**
* Get last buffered message
*
* @return
*/
public String pollMessage()
{
return messages.poll();
return buffer.poll();
}
}

View File

@ -30,18 +30,21 @@ import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TableLayout.LayoutParams;
import org.yaaic.R;
import org.yaaic.Yaaic;
@ -50,6 +53,7 @@ import org.yaaic.irc.IRCBinder;
import org.yaaic.irc.IRCService;
import org.yaaic.listener.ChannelListener;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Channel;
import org.yaaic.model.Server;
import org.yaaic.receiver.ChannelReceiver;
@ -58,7 +62,7 @@ import org.yaaic.receiver.ChannelReceiver;
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public class ServerActivity extends Activity implements ServiceConnection, ChannelListener
public class ServerActivity extends Activity implements ServiceConnection, ChannelListener, OnItemClickListener
{
public static final String TAG = "Yaaic/ServerActivity";
@ -85,10 +89,18 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann
((TextView) findViewById(R.id.title)).setText(server.getTitle());
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
deckAdapter = new DeckAdapter();
Display d = getWindowManager().getDefaultDisplay();
deckAdapter = new DeckAdapter(d.getWidth(), d.getHeight());
deck = (Gallery) findViewById(R.id.deck);
deck.setAdapter(deckAdapter);
deck.setOnItemClickListener(this);
switcher = (ViewSwitcher) findViewById(R.id.switcher);
for (Channel channel : server.getChannels()) {
onNewChannel(channel.getName());
}
}
@Override
@ -175,13 +187,17 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann
TextView canvas = (TextView) deckAdapter.getItemByName(target);
if (canvas != null) {
Log.d(TAG, "Got canvas, setting text");
canvas.append("\n" + message);
deckAdapter.notifyDataSetChanged();
Log.d(TAG, "Target: " + target + " - Switched: " + deckAdapter.getSwitchedName());
if (target.equals(deckAdapter.getSwitchedName())) {
((TextView) deckAdapter.getSwitchedView()).append("\n" + message);
}
} else {
Log.d(TAG, "No canvas found");
}
//Toast.makeText(this, "(" + target + ") " + message, Toast.LENGTH_SHORT).show();
}
/**
@ -191,34 +207,56 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann
{
Log.d(TAG, "onNewChannel() " + target);
TextView canvas = new TextView(this);
canvas.setText(target);
canvas.setTextColor(0xff000000);
deckAdapter.addItem(server.getChannel(target));
}
/**
* On Channel item clicked
*/
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
Log.d(TAG, "Selected channel: " + position);
// XXX: Refactor this crap :)
Display d = getWindowManager().getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
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;
canvas.setPadding(10, 10, 10, 10);
canvas.setBackgroundColor(0xff888888);
canvas.setLayoutParams(new Gallery.LayoutParams(w, h));
deckAdapter.addItem(target, canvas);
/*
deck.addView(child)
containers.put(target, new ChannelContainer(channel, canvas));
*/
Channel channel = deckAdapter.getItem(position);
view = deckAdapter.renderChannel(channel, switcher);
//getView(position, view, deck);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
deckAdapter.setSwitched(channel.getName(), view);
switcher.addView(view);
switcher.showNext();
}
/**
* On key down
*
* This is glue code to call onBackPressed() which
* will be automatically called by later android releases
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return false;
}
/**
* On back key pressed
*/
public void onBackPressed()
{
if (deckAdapter.isSwitched()) {
switcher.showNext();
switcher.removeView(deckAdapter.getSwitchedView());
//switcher.showNext();
deckAdapter.setSwitched(null, null);
Log.d(TAG, "Back pressed");
} else {
Log.d(TAG, "Back pressed -> FINISH");
finish();
}
}
/**