Skip to content

Commit

Permalink
fixed issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
fennifith committed Apr 18, 2017
1 parent 79d8482 commit 26470c1
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package doubledotlabs.butteryslack.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class AlphabetItemAdapter<T extends RecyclerView.ViewHolder> extends BaseItemAdapter<T> {

private Map<Integer, Character> firstLetters;

public AlphabetItemAdapter(Context context, List<BaseItem<T>> baseItems) {
super(context, baseItems);
}

@Override
public void setItems(List<BaseItem<T>> items) {
Collections.sort(items, new Comparator<BaseItem<T>>() {
@Override
public int compare(BaseItem<T> o1, BaseItem<T> o2) {
return ((AlphabetItem) o1).getLetter().compareTo(((AlphabetItem) o2).getLetter());
}
});

firstLetters = new ArrayMap<>();
Character previousLetter = null;
for (int i = 0; i < items.size(); i++) {
AlphabetItem item = (AlphabetItem) items.get(i);
Character letter = item.getLetter();

if (previousLetter == null || !previousLetter.equals(letter)) {
firstLetters.put(i, letter);
previousLetter = letter;
}
}

super.setItems(items);
}

public boolean isFirst(AlphabetItem item) {
return firstLetters.containsKey(getItems().indexOf(item));
}

public abstract static class AlphabetItem<T extends RecyclerView.ViewHolder> extends BaseItemAdapter.BaseItem<T> {

@Nullable
public final boolean isFirst() {
return getAdapter() != null && ((AlphabetItemAdapter) getAdapter()).isFirst(this);
}

@NonNull
public abstract Character getLetter();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

public class AttachmentData extends BaseItemAdapter.BaseItem<AttachmentData.ViewHolder> implements View.OnClickListener {

public static final String TYPE_ATTACHMENT = "attachment";
public static final String TYPE_FILE = "file";

@Nullable
private String title, titleLink;
@Nullable
Expand All @@ -51,46 +54,49 @@ public AttachmentData(SlackAttachment attachment) {
footerIcon = attachment.getFooterIcon();
}

public AttachmentData(JSONObject object) {
if (object.get("initial_comment") != null) {
JSONObject comment = (JSONObject) object.get("initial_comment");

String title = (String) object.get("title");
String pretext = comment != null ? (String) comment.get("comment") : null;

this.title = title;
titleLink = (String) object.get("permalink");
this.pretext = pretext;

String type = (String) object.get("filetype");
if (type != null) {
switch (type) {
case "png":
pretext = null;
imageUrl = (String) object.get("url_private");
footer = pretext;
break;
case "text":
text = (String) object.get("preview");
break;
default:
text = (String) object.get("preview");
textType = type;
break;
public AttachmentData(String type, JSONObject object) {
switch (type) {
case TYPE_ATTACHMENT:
title = (String) object.get("title");
titleLink = (String) object.get("title_link");
authorName = (String) object.get("author_name");
authorLink = (String) object.get("author_link");
authorIcon = (String) object.get("author_icon");
pretext = (String) object.get("pretext");
text = (String) object.get("text");
imageUrl = (String) object.get("image_url");
thumbUrl = (String) object.get("thumb_url");
footer = (String) object.get("footer");
footerIcon = (String) object.get("footer_icon");
break;
case TYPE_FILE:
JSONObject comment = (JSONObject) object.get("initial_comment");

String title = (String) object.get("title");
String pretext = comment != null ? (String) comment.get("comment") : null;

this.title = title;
titleLink = (String) object.get("permalink");
this.pretext = pretext;

String fileType = (String) object.get("filetype");
if (fileType != null) {
switch (fileType) {
case "png":
pretext = null;
imageUrl = (String) object.get("url_private");
footer = pretext;
break;
case "text":
text = (String) object.get("preview");
break;
default:
text = (String) object.get("preview");
textType = fileType;
break;
}
}
}
} else {
title = (String) object.get("title");
titleLink = (String) object.get("title_link");
authorName = (String) object.get("author_name");
authorLink = (String) object.get("author_link");
authorIcon = (String) object.get("author_icon");
pretext = (String) object.get("pretext");
text = (String) object.get("text");
imageUrl = (String) object.get("image_url");
thumbUrl = (String) object.get("thumb_url");
footer = (String) object.get("footer");
footerIcon = (String) object.get("footer_icon");
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static MessageItemData from(Context context, JSONObject object) {
JSONArray array = (JSONArray) object.get("attachments");
if (array != null) {
for (Object attachment : array) {
attachments.add(new AttachmentData((JSONObject) attachment));
attachments.add(new AttachmentData(AttachmentData.TYPE_ATTACHMENT, (JSONObject) attachment));
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ public static MessageItemData from(Context context, JSONObject object) {

JSONObject file = (JSONObject) object.get("file");
if (file != null)
attachments.add(new AttachmentData(file));
attachments.add(new AttachmentData(AttachmentData.TYPE_FILE, file));
break;
case "me_message":
case "file_comment":
Expand Down

0 comments on commit 26470c1

Please sign in to comment.