Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command: /clear Clear the history of the current conversation #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/org/yaaic/adapter/ConversationPagerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public void removeConversation(int position) {
notifyDataSetChanged();
}

/**
* Update the history of the conversation at the given position from the adapter.
*
* @param position
*/
public void updateConversation(int position) {
ConversationInfo convInfo = conversations.get(position);
convInfo.adapter.update(convInfo.conv);
}

/**
* Get position of given item.
*/
Expand Down
59 changes: 36 additions & 23 deletions app/src/main/java/org/yaaic/adapter/MessageListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,11 @@ public class MessageListAdapter extends BaseAdapter
*/
public MessageListAdapter(Conversation conversation, Context context)
{
LinkedList<TextView> messages = new LinkedList<TextView>();

// Render channel name as first message in channel
if (conversation.getType() != Conversation.TYPE_SERVER) {
Message header = new Message(conversation.getName());
header.setColor(Message.COLOR_RED);
messages.add(header.renderTextView(context));
}

// Optimization - cache field lookups
LinkedList<Message> mHistory = conversation.getHistory();
int mSize = mHistory.size();

for (int i = 0; i < mSize; i++) {
messages.add(mHistory.get(i).renderTextView(context));
}
this.context = context;
this.messages = new LinkedList<TextView>();

// XXX: We don't want to clear the buffer, we want to add only
// buffered messages that are not already added (history)
conversation.clearBuffer();
update(conversation);

this.messages = messages;
this.context = context;
historySize = conversation.getHistorySize();
}

Expand All @@ -87,7 +69,7 @@ public void addMessage(Message message)
messages.add(message.renderTextView(context));

if (messages.size() > historySize) {
messages.remove(0);
messages.remove(1);
}

notifyDataSetChanged();
Expand All @@ -108,7 +90,7 @@ public void addBulkMessages(LinkedList<Message> messages)
mMessages.add(messages.get(i).renderTextView(mContext));

if (mMessages.size() > historySize) {
mMessages.remove(0);
mMessages.remove(1);
}
}

Expand Down Expand Up @@ -174,4 +156,35 @@ public void unregisterDataSetObserver(DataSetObserver observer) {
}
super.unregisterDataSetObserver(observer);
}

/**
* Update the history from the conversation.
*
* @param conversation
*/
public void update(Conversation conversation)
{
messages.clear();

// Render channel name as first message in channel
if (conversation.getType() != Conversation.TYPE_SERVER) {
Message header = new Message(conversation.getName());
header.setColor(Message.COLOR_RED);
messages.add(header.renderTextView(context));
}

// Optimization - cache field lookups
LinkedList<Message> mHistory = conversation.getHistory();
int mSize = mHistory.size();

for (int i = 0; i < mSize; i++) {
messages.add(mHistory.get(i).renderTextView(context));
}

// XXX: We don't want to clear the buffer, we want to add only
// buffered messages that are not already added (history)
conversation.clearBuffer();

notifyDataSetChanged();
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/org/yaaic/command/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.yaaic.command.handler.AMsgHandler;
import org.yaaic.command.handler.AwayHandler;
import org.yaaic.command.handler.BackHandler;
import org.yaaic.command.handler.ClearHandler;
import org.yaaic.command.handler.CloseHandler;
import org.yaaic.command.handler.DCCHandler;
import org.yaaic.command.handler.DeopHandler;
Expand Down Expand Up @@ -100,6 +101,7 @@ private CommandParser()
commands.put("msg", new MsgHandler());
commands.put("quote", new RawHandler());
commands.put("amsg", new AMsgHandler());
commands.put("clear", new ClearHandler());

aliases = new HashMap<String, String>();

Expand Down
59 changes: 59 additions & 0 deletions app/src/main/java/org/yaaic/command/handler/ClearHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.yaaic.command.handler;

import org.yaaic.R;
import org.yaaic.command.BaseHandler;
import org.yaaic.exception.CommandException;
import org.yaaic.irc.IRCService;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Conversation;
import org.yaaic.model.Server;

import android.content.Context;
import android.content.Intent;

/**
* Command: /clear
*
* Clear the history of the current window
*
* @author Xenega <[email protected]>
*/
public class ClearHandler extends BaseHandler
{
/**
* Execute /clear
*/
@Override
public void execute(String[] params, Server server, Conversation conversation, IRCService service) throws CommandException
{
if (params.length == 1) {
conversation.clearHistory();
conversation.clearBuffer();

Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_CLEAR,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
}

/**
* Usage of /clear
*/
@Override
public String getUsage()
{
return "/clear";
}

/**
* Description of /clear
*/
@Override
public String getDescription(Context context)
{
return context.getString(R.string.command_desc_clear);
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/org/yaaic/fragment/ConversationFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public void onResume() {
getActivity().registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE));
getActivity().registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_NEW));
getActivity().registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_REMOVE));
getActivity().registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_CLEAR));
getActivity().registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_TOPIC));

serverReceiver = new ServerReceiver(this);
Expand Down Expand Up @@ -548,6 +549,15 @@ public void onRemoveConversation(String target) {
tabLayout.update();
}

@Override
public void onClearConversation(String target) {
int position = pagerAdapter.getPositionByName(target);

if (position != -1) {
pagerAdapter.updateConversation(position);
}
}

/**
* On topic change
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ public interface ConversationListener
*/
public void onRemoveConversation(String target);

/**
* On conversation cleared (for given target)
*
* @param target
*/
public void onClearConversation(String target);

/**
* On topic changed (for given target)
*
* @param target
*/
public void onTopicChanged(String target);


}
1 change: 1 addition & 0 deletions app/src/main/java/org/yaaic/model/Broadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class Broadcast
public static final String CONVERSATION_MESSAGE = "org.yaaic.conversation.message";
public static final String CONVERSATION_NEW = "org.yaaic.conversation.new";
public static final String CONVERSATION_REMOVE = "org.yaaic.conversation.remove";
public static final String CONVERSATION_CLEAR = "org.yaaic.conversation.clear";
public static final String CONVERSATION_TOPIC = "org.yaaic.conversation.topic";

/**
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/yaaic/model/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,12 @@ public void setHistorySize(int size)
history.subList(size, history.size()).clear();
}
}

/**
* Clear the message history
*/
public void clearHistory()
{
history.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void onReceive(Context context, Intent intent)
listener.onNewConversation(intent.getExtras().getString(Extra.CONVERSATION));
} else if (action.equals(Broadcast.CONVERSATION_REMOVE)) {
listener.onRemoveConversation(intent.getExtras().getString(Extra.CONVERSATION));
} else if (action.equals(Broadcast.CONVERSATION_CLEAR)) {
listener.onClearConversation(intent.getExtras().getString(Extra.CONVERSATION));
} else if (action.equals(Broadcast.CONVERSATION_TOPIC)) {
listener.onTopicChanged(intent.getExtras().getString(Extra.CONVERSATION));
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<string name="command_desc_topic">Show or change the current topic</string>
<string name="command_desc_voice">Give a user voice status</string>
<string name="command_desc_whois">Get information about a user</string>
<string name="command_desc_clear">Clear the history of the current window</string>

<string name="notification_running">Yaaic is running</string>
<string name="notification_not_connected">Not connected</string>
Expand Down