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

Add onChipAddListener #76

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public void onChipRemove(Chip chip) {
mNachoTextView.setSelection(mNachoTextView.getText().length());
}
});
nachoTextView.setOnChipAddListener(new NachoTextView.OnChipAddListener() {
@Override
public void onChipAdded(Chip chip) {
Log.d(TAG, "onChipAdd " + chip.getText() );
}
});
}

@SuppressWarnings("unused")
Expand Down
16 changes: 16 additions & 0 deletions nachos/src/main/java/com/hootsuite/nachos/NachoTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public class NachoTextView extends MultiAutoCompleteTextView implements TextWatc

// Text entry
@Nullable
private OnChipAddListener onChipAddListener;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to stay consistent, please use mOnChipAddListener

@Nullable
private ChipTokenizer mChipTokenizer;
@Nullable
private ChipTerminatorHandler mChipTerminatorHandler;
Expand Down Expand Up @@ -402,6 +404,13 @@ public void setChipTokenizer(@Nullable ChipTokenizer chipTokenizer) {
invalidateChips();
}

public void setOnChipAddListener(@Nullable OnChipAddListener onChipAddListener) {
this.onChipAddListener = onChipAddListener;
if (mChipTerminatorHandler != null) {
mChipTerminatorHandler.setChipAddListener(this.onChipAddListener);
}
}

public void setOnChipClickListener(@Nullable OnChipClickListener onChipClickListener) {
mOnChipClickListener = onChipClickListener;
}
Expand Down Expand Up @@ -796,6 +805,9 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon

editable.replace(start, end, mChipTokenizer.terminateToken(text, data));

if (onChipAddListener != null){
onChipAddListener.onChipAdded(getAllChips().get(getAllChips().size()-1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to format your code with Android Studio's code formatting. The -1 would be spread out to make it look nicer

}
endUnwatchedTextChange();
}

Expand Down Expand Up @@ -1127,4 +1139,8 @@ public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}

public interface OnChipAddListener {
void onChipAdded(Chip chip);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide the documentation and contract of when and what the onChipAdded is supposed to adhere to? It can be similar to the onChipRemove

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;
import android.text.Editable;

import com.hootsuite.nachos.NachoTextView;
import com.hootsuite.nachos.tokenizer.ChipTokenizer;

import java.util.Map;
Expand Down Expand Up @@ -91,4 +92,6 @@ public interface ChipTerminatorHandler {
* or a negative integer indicating that the cursor should not be moved.
*/
int findAndHandleChipTerminators(@NonNull ChipTokenizer tokenizer, @NonNull Editable text, int start, int end, boolean isPasteEvent);

void setChipAddListener (NachoTextView.OnChipAddListener onChipAddListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import android.support.annotation.Nullable;
import android.text.Editable;

import com.hootsuite.nachos.NachoTextView;
import com.hootsuite.nachos.chip.Chip;
import com.hootsuite.nachos.tokenizer.ChipTokenizer;

import java.util.HashMap;
import java.util.Map;

public class DefaultChipTerminatorHandler implements ChipTerminatorHandler {

private NachoTextView.OnChipAddListener mOnChipAddListener;

@Nullable
private Map<Character, Integer> mChipTerminators;
private int mPasteBehavior = BEHAVIOR_CHIPIFY_TO_TERMINATOR;
Expand Down Expand Up @@ -53,12 +57,28 @@ public int findAndHandleChipTerminators(@NonNull ChipTokenizer tokenizer, @NonNu
switch (behavior) {
case BEHAVIOR_CHIPIFY_ALL:
selectionIndex = handleChipifyAll(textIterator, tokenizer);
if (mOnChipAddListener != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harhsoni1110 it seems that these 3 blocks do the same thing. Why not put it into a private method?

Chip[] chips = tokenizer.findAllChips(0, text.length(), text);
if (chips.length > 0)
mOnChipAddListener.onChipAdded(chips[chips.length - 1]);
}
break characterLoop;
case BEHAVIOR_CHIPIFY_CURRENT_TOKEN:
newSelection = handleChipifyCurrentToken(textIterator, tokenizer);
if (mOnChipAddListener != null) {
Chip[] chips = tokenizer.findAllChips(0, text.length(), text);
if (chips.length > 0)
mOnChipAddListener.onChipAdded(chips[chips.length - 1]);
}

break;
case BEHAVIOR_CHIPIFY_TO_TERMINATOR:
newSelection = handleChipifyToTerminator(textIterator, tokenizer);
if (mOnChipAddListener != null) {
Chip[] chips = tokenizer.findAllChips(0, text.length(), text);
if (chips.length > 0)
mOnChipAddListener.onChipAdded(chips[chips.length - 1]);
}
break;
}

Expand Down Expand Up @@ -111,4 +131,9 @@ private int handleChipifyToTerminator(TextIterator textIterator, ChipTokenizer t
private boolean isChipTerminator(char character) {
return mChipTerminators != null && mChipTerminators.keySet().contains(character);
}

@Override
public void setChipAddListener(NachoTextView.OnChipAddListener onChipAddListener) {
this.mOnChipAddListener = onChipAddListener;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need a this since the names of the fields are different.

}
}