Skip to content

Commit

Permalink
fix custom message bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KenChoi authored and KenChoi committed Dec 22, 2017
1 parent 13073d4 commit 87222ed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,15 @@ enum MessageType {
SEND_CUSTOM,
RECEIVE_CUSTOM;

private int type;

MessageType() {
}

public void setCustomType(int type) {
if (type > 12 || type < 0) {
this.type = type;
} else {
throw new IllegalArgumentException("Custom message type should not set 0-12");
}
}

public int getCustomType() {
return this.type;
}
}

/**
* Type of message, enum.
* @return Message Type
* Type of message
* @return integer
*/
MessageType getType();
int getType();

/**
* Status of message, enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -178,31 +179,30 @@ public int getItemViewType(int position) {
Wrapper wrapper = mItems.get(position);
if (wrapper.item instanceof IMessage) {
IMessage message = (IMessage) wrapper.item;
switch (message.getType()) {
case SEND_TEXT:
return TYPE_SEND_TXT;
case RECEIVE_TEXT:
return TYPE_RECEIVE_TXT;
case SEND_LOCATION:
return TYPE_SEND_LOCATION;
case RECEIVE_LOCATION:
return TYPE_RECEIVER_LOCATION;
case SEND_VOICE:
return TYPE_SEND_VOICE;
case RECEIVE_VOICE:
return TYPE_RECEIVER_VOICE;
case SEND_IMAGE:
return TYPE_SEND_IMAGE;
case RECEIVE_IMAGE:
return TYPE_RECEIVER_IMAGE;
case SEND_VIDEO:
return TYPE_SEND_VIDEO;
case RECEIVE_VIDEO:
return TYPE_RECEIVE_VIDEO;
case EVENT:
return TYPE_EVENT;
default:
return getCustomType(message);
if (message.getType() == IMessage.MessageType.EVENT.ordinal()) {
return TYPE_EVENT;
} else if (message.getType() == IMessage.MessageType.SEND_TEXT.ordinal()) {
return TYPE_SEND_TXT;
} else if (message.getType() == IMessage.MessageType.RECEIVE_TEXT.ordinal()) {
return TYPE_RECEIVE_TXT;
} else if (message.getType() == IMessage.MessageType.SEND_IMAGE.ordinal()) {
return TYPE_SEND_IMAGE;
} else if (message.getType() == IMessage.MessageType.RECEIVE_IMAGE.ordinal()) {
return TYPE_RECEIVER_IMAGE;
} else if (message.getType() == IMessage.MessageType.SEND_VOICE.ordinal()) {
return TYPE_SEND_VOICE;
} else if (message.getType() == IMessage.MessageType.RECEIVE_VOICE.ordinal()) {
return TYPE_RECEIVER_VOICE;
} else if (message.getType() == IMessage.MessageType.SEND_VIDEO.ordinal()) {
return TYPE_SEND_VIDEO;
} else if (message.getType() == IMessage.MessageType.RECEIVE_VIDEO.ordinal()) {
return TYPE_RECEIVE_VIDEO;
} else if (message.getType() == IMessage.MessageType.SEND_LOCATION.ordinal()) {
return TYPE_SEND_LOCATION;
} else if (message.getType() == IMessage.MessageType.RECEIVE_LOCATION.ordinal()) {
return TYPE_RECEIVER_LOCATION;
} else {
return getCustomType(message);
}
}
return TYPE_SEND_TXT;
Expand All @@ -211,10 +211,12 @@ public int getItemViewType(int position) {
private int getCustomType(IMessage message) {
for (int i=0; i < mCustomMsgList.size(); i++) {
CustomMsgConfig config = mCustomMsgList.valueAt(i);
if (message.getType().getCustomType() == config.getViewType()) {
if (message.getType() == config.getViewType()) {
return config.getViewType();
}
}
Log.d("MsgListAdapter", "Can not find custom type, maybe you are forget to call " +
"setType() in your <? extends IMessage> class");
return TYPE_SEND_TXT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public boolean onSendTextMessage(CharSequence input) {
if (input.length() == 0) {
return false;
}
MyMessage message = new MyMessage(input.toString(), IMessage.MessageType.SEND_TEXT);
MyMessage message = new MyMessage(input.toString(), IMessage.MessageType.SEND_TEXT.ordinal());
message.setUserInfo(new DefaultUser("1", "Ironman", "R.drawable.ironman"));
message.setTimeString(new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date()));
mAdapter.addToStart(message, true);
Expand All @@ -120,10 +120,10 @@ public void onSendFiles(List<FileItem> list) {
MyMessage message;
for (FileItem item : list) {
if (item.getType() == FileItem.Type.Image) {
message = new MyMessage(null, IMessage.MessageType.SEND_IMAGE);
message = new MyMessage(null, IMessage.MessageType.SEND_IMAGE.ordinal());

} else if (item.getType() == FileItem.Type.Video) {
message = new MyMessage(null, IMessage.MessageType.SEND_VIDEO);
message = new MyMessage(null, IMessage.MessageType.SEND_VIDEO.ordinal());
message.setDuration(((VideoItem) item).getDuration());

} else {
Expand Down Expand Up @@ -219,7 +219,7 @@ public void onStartRecord() {

@Override
public void onFinishRecord(File voiceFile, int duration) {
MyMessage message = new MyMessage(null, IMessage.MessageType.SEND_VOICE);
MyMessage message = new MyMessage(null, IMessage.MessageType.SEND_VOICE.ordinal());
message.setUserInfo(new DefaultUser("1", "Ironman", "R.drawable.ironman"));
message.setMediaFilePath(voiceFile.getPath());
message.setDuration(duration);
Expand All @@ -236,7 +236,7 @@ public void onCancelRecord() {
mChatView.setOnCameraCallbackListener(new OnCameraCallbackListener() {
@Override
public void onTakePictureCompleted(String photoPath) {
final MyMessage message = new MyMessage(null, IMessage.MessageType.SEND_IMAGE);
final MyMessage message = new MyMessage(null, IMessage.MessageType.SEND_IMAGE.ordinal());
message.setTimeString(new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date()));
message.setMediaFilePath(photoPath);
message.setUserInfo(new DefaultUser("1", "Ironman", "R.drawable.ironman"));
Expand Down Expand Up @@ -379,10 +379,10 @@ private List<MyMessage> getMessages() {
for (int i = 0; i < messages.length; i++) {
MyMessage message;
if (i % 2 == 0) {
message = new MyMessage(messages[i], IMessage.MessageType.RECEIVE_TEXT);
message = new MyMessage(messages[i], IMessage.MessageType.RECEIVE_TEXT.ordinal());
message.setUserInfo(new DefaultUser("0", "DeadPool", "R.drawable.deadpool"));
} else {
message = new MyMessage(messages[i], IMessage.MessageType.SEND_TEXT);
message = new MyMessage(messages[i], IMessage.MessageType.SEND_TEXT.ordinal());
message.setUserInfo(new DefaultUser("1", "IronMan", "R.drawable.ironman"));
}
message.setTimeString(new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date()));
Expand Down Expand Up @@ -435,8 +435,8 @@ public void loadImage(ImageView imageView, String string) {
@Override
public void onMessageClick(MyMessage message) {
// do something
if (message.getType() == IMessage.MessageType.RECEIVE_VIDEO
|| message.getType() == IMessage.MessageType.SEND_VIDEO) {
if (message.getType() == IMessage.MessageType.RECEIVE_VIDEO.ordinal()
|| message.getType() == IMessage.MessageType.SEND_VIDEO.ordinal()) {
if (!TextUtils.isEmpty(message.getMediaFilePath())) {
Intent intent = new Intent(MessageListActivity.this, VideoActivity.class);
intent.putExtra(VideoActivity.VIDEO_PATH, message.getMediaFilePath());
Expand Down Expand Up @@ -478,10 +478,10 @@ public void onStatusViewClick(MyMessage message) {
}
});

MyMessage message = new MyMessage("Hello World", IMessage.MessageType.RECEIVE_TEXT);
MyMessage message = new MyMessage("Hello World", IMessage.MessageType.RECEIVE_TEXT.ordinal());
message.setUserInfo(new DefaultUser("0", "Deadpool", "R.drawable.deadpool"));
mAdapter.addToStart(message, true);
MyMessage eventMsg = new MyMessage("haha", IMessage.MessageType.EVENT);
MyMessage eventMsg = new MyMessage("haha", IMessage.MessageType.EVENT.ordinal());
mAdapter.addToStart(eventMsg, true);
mAdapter.addToEnd(mData);
PullToRefreshLayout layout = mChatView.getPtrLayout();
Expand Down Expand Up @@ -515,10 +515,10 @@ public void run() {
for (int i = 0; i < messages.length; i++) {
MyMessage message;
if (i % 2 == 0) {
message = new MyMessage(messages[i], IMessage.MessageType.RECEIVE_TEXT);
message = new MyMessage(messages[i], IMessage.MessageType.RECEIVE_TEXT.ordinal());
message.setUserInfo(new DefaultUser("0", "DeadPool", "R.drawable.deadpool"));
} else {
message = new MyMessage(messages[i], IMessage.MessageType.SEND_TEXT);
message = new MyMessage(messages[i], IMessage.MessageType.SEND_TEXT.ordinal());
message.setUserInfo(new DefaultUser("1", "IronMan", "R.drawable.ironman"));
}
message.setTimeString(new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public class MyMessage implements IMessage {
private long id;
private String text;
private String timeString;
private MessageType type;
private int type;
private IUser user;
private String mediaFilePath;
private long duration;
private String progress;

public MyMessage(String text, MessageType type) {
public MyMessage(String text, int type) {
this.text = text;
this.type = type;
this.id = UUID.randomUUID().getLeastSignificantBits();
Expand Down Expand Up @@ -78,11 +78,14 @@ public String getTimeString() {
}

public void setType(int type) {
this.type.setCustomType(type);
if (type >= 0 && type <= 12) {
throw new IllegalArgumentException("Message type should not take the value between 0 and 12");
}
this.type = type;
}

@Override
public MessageType getType() {
public int getType() {
return type;
}

Expand Down

0 comments on commit 87222ed

Please sign in to comment.