From b3e44b84d2574eec52a4efc8cf5d03b5f7fcb3fd Mon Sep 17 00:00:00 2001 From: jendib Date: Tue, 3 Nov 2015 00:04:09 +0100 Subject: [PATCH] Closes #35: Android: Tag depth shown in tags tree --- .../sismics/docs/activity/MainActivity.java | 6 +- .../sismics/docs/adapter/TagListAdapter.java | 78 ++++++++++++++----- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java index 3d0e49c7..70ec4975 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java @@ -114,9 +114,9 @@ public class MainActivity extends AppCompatActivity { public void onItemClick(AdapterView parent, View view, int position, long id) { TagListAdapter adapter = (TagListAdapter) tagListView.getAdapter(); if (adapter == null) return; - JSONObject tag = adapter.getItem(position); - if (tag == null) return; - searchQuery("tag:" + tag.optString("name")); + TagListAdapter.TagItem tagItem = adapter.getItem(position); + if (tagItem == null) return; + searchQuery("tag:" + tagItem.getName()); } }); diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java index 0c124735..b10be67e 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java @@ -1,9 +1,11 @@ package com.sismics.docs.adapter; import android.content.Context; +import android.content.res.Resources; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -17,8 +19,6 @@ import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; /** @@ -30,7 +30,7 @@ public class TagListAdapter extends BaseAdapter { /** * Tags. */ - private List tags; + private List tagItemList = new ArrayList<>(); /** * Tag list adapter. @@ -38,33 +38,53 @@ public class TagListAdapter extends BaseAdapter { * @param tagsArray Tags */ public TagListAdapter(JSONArray tagsArray) { - this.tags = new ArrayList<>(); + List tags = new ArrayList<>(); for (int i = 0; i < tagsArray.length(); i++) { tags.add(tagsArray.optJSONObject(i)); } - // Sort tags by count desc - Collections.sort(tags, new Comparator() { - @Override - public int compare(JSONObject lhs, JSONObject rhs) { - return lhs.optInt("count") < rhs.optInt("count") ? 1 : -1; + // Reorder tags by parent/child relation and compute depth + int depth = 0; + initTags(tags, JSONObject.NULL.toString(), depth); + } + + /** + * Init tags model recursively. + * + * @param tags All tags from server + * @param parentId Parent ID + * @param depth Depth + */ + private void initTags(List tags, String parentId, int depth) { + // Get all tags with this parent + for (JSONObject tag : tags) { + String tagParentId = tag.optString("parent"); + if (tagParentId.equals(parentId)) { + TagItem tagItem = new TagItem(); + tagItem.id = tag.optString("id"); + tagItem.name = tag.optString("name"); + tagItem.count = tag.optInt("count"); + tagItem.color = tag.optString("color"); + tagItem.depth = depth; + tagItemList.add(tagItem); + initTags(tags, tagItem.id, depth + 1); } - }); + } } @Override public int getCount() { - return tags.size(); + return tagItemList.size(); } @Override - public JSONObject getItem(int position) { - return tags.get(position); + public TagItem getItem(int position) { + return tagItemList.get(position); } @Override public long getItemId(int position) { - return getItem(position).optString("id").hashCode(); + return getItem(position).id.hashCode(); } @Override @@ -75,19 +95,41 @@ public class TagListAdapter extends BaseAdapter { } // Fill the view - JSONObject tag = getItem(position); + TagItem tagItem = getItem(position); TextView tagTextView = (TextView) view.findViewById(R.id.tagTextView); - tagTextView.setText(tag.optString("name")); + tagTextView.setText(tagItem.name); TextView tagCountTextView = (TextView) view.findViewById(R.id.tagCountTextView); - tagCountTextView.setText(tag.optString("count")); + tagCountTextView.setText(String.format("%d", tagItem.count)); // Label color filtering ImageView labelImageView = (ImageView) view.findViewById(R.id.labelImageView); Drawable labelDrawable = labelImageView.getDrawable().mutate(); - labelDrawable.setColorFilter(Color.parseColor(tag.optString("color")), PorterDuff.Mode.MULTIPLY); + labelDrawable.setColorFilter(Color.parseColor(tagItem.color), PorterDuff.Mode.MULTIPLY); labelImageView.setImageDrawable(labelDrawable); labelImageView.invalidate(); + // Offset according to depth + Resources resources = parent.getContext().getResources(); + ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) labelImageView.getLayoutParams(); + layoutParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tagItem.depth * 12, resources.getDisplayMetrics()); + labelImageView.setLayoutParams(layoutParams); + labelImageView.requestLayout(); + return view; } + + /** + * A tag item in the tags list. + */ + public static class TagItem { + private String id; + private String name; + private int count; + private String color; + private int depth; + + public String getName() { + return name; + } + } }