diff --git a/app/src/main/java/org/yaaic/adapter/ConversationPagerAdapter.java b/app/src/main/java/org/yaaic/adapter/ConversationPagerAdapter.java index c79acca8..8e0f9536 100644 --- a/app/src/main/java/org/yaaic/adapter/ConversationPagerAdapter.java +++ b/app/src/main/java/org/yaaic/adapter/ConversationPagerAdapter.java @@ -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. */ diff --git a/app/src/main/java/org/yaaic/adapter/MessageListAdapter.java b/app/src/main/java/org/yaaic/adapter/MessageListAdapter.java index caad4c38..7071ace1 100644 --- a/app/src/main/java/org/yaaic/adapter/MessageListAdapter.java +++ b/app/src/main/java/org/yaaic/adapter/MessageListAdapter.java @@ -51,29 +51,11 @@ public class MessageListAdapter extends BaseAdapter */ public MessageListAdapter(Conversation conversation, Context context) { - LinkedList messages = new LinkedList(); - - // 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 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(); - // 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(); } @@ -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(); @@ -108,7 +90,7 @@ public void addBulkMessages(LinkedList messages) mMessages.add(messages.get(i).renderTextView(mContext)); if (mMessages.size() > historySize) { - mMessages.remove(0); + mMessages.remove(1); } } @@ -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 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(); + } } diff --git a/app/src/main/java/org/yaaic/command/CommandParser.java b/app/src/main/java/org/yaaic/command/CommandParser.java index cfdd7877..082b0083 100644 --- a/app/src/main/java/org/yaaic/command/CommandParser.java +++ b/app/src/main/java/org/yaaic/command/CommandParser.java @@ -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; @@ -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(); diff --git a/app/src/main/java/org/yaaic/command/handler/ClearHandler.java b/app/src/main/java/org/yaaic/command/handler/ClearHandler.java new file mode 100644 index 00000000..51d147ed --- /dev/null +++ b/app/src/main/java/org/yaaic/command/handler/ClearHandler.java @@ -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 + */ +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); + } +} diff --git a/app/src/main/java/org/yaaic/fragment/ConversationFragment.java b/app/src/main/java/org/yaaic/fragment/ConversationFragment.java index f22334d9..833a76b8 100644 --- a/app/src/main/java/org/yaaic/fragment/ConversationFragment.java +++ b/app/src/main/java/org/yaaic/fragment/ConversationFragment.java @@ -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); @@ -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 */ diff --git a/app/src/main/java/org/yaaic/listener/ConversationListener.java b/app/src/main/java/org/yaaic/listener/ConversationListener.java index f6dc1d8a..27b3d0e9 100644 --- a/app/src/main/java/org/yaaic/listener/ConversationListener.java +++ b/app/src/main/java/org/yaaic/listener/ConversationListener.java @@ -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); + + } diff --git a/app/src/main/java/org/yaaic/model/Broadcast.java b/app/src/main/java/org/yaaic/model/Broadcast.java index 0e3dcef6..178bb486 100644 --- a/app/src/main/java/org/yaaic/model/Broadcast.java +++ b/app/src/main/java/org/yaaic/model/Broadcast.java @@ -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"; /** diff --git a/app/src/main/java/org/yaaic/model/Conversation.java b/app/src/main/java/org/yaaic/model/Conversation.java index 17132c54..f57dad99 100644 --- a/app/src/main/java/org/yaaic/model/Conversation.java +++ b/app/src/main/java/org/yaaic/model/Conversation.java @@ -232,4 +232,12 @@ public void setHistorySize(int size) history.subList(size, history.size()).clear(); } } + + /** + * Clear the message history + */ + public void clearHistory() + { + history.clear(); + } } diff --git a/app/src/main/java/org/yaaic/receiver/ConversationReceiver.java b/app/src/main/java/org/yaaic/receiver/ConversationReceiver.java index 65851e23..537c97f6 100644 --- a/app/src/main/java/org/yaaic/receiver/ConversationReceiver.java +++ b/app/src/main/java/org/yaaic/receiver/ConversationReceiver.java @@ -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)); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 314233b8..e2dc8a5c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,6 +123,7 @@ Show or change the current topic Give a user voice status Get information about a user + Clear the history of the current window Yaaic is running Not connected