添加 tag 投票数显示

This commit is contained in:
seven332 committed 2014-05-07 21:41:54 +08:00
1 parent acec30ae5a
commit 3da180beb0
4 files changed
+54 -40

No files matched your search

@@ -1,6 +1,10 @@
package com.hippo.ehviewer.activity;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.BeautifyScreen;
@@ -10,6 +14,7 @@ import com.hippo.ehviewer.data.Data;
import com.hippo.ehviewer.data.GalleryDetail;
import com.hippo.ehviewer.data.GalleryInfo;
import com.hippo.ehviewer.data.PreviewList;
import com.hippo.ehviewer.ehclient.DetailParser;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
@@ -694,9 +699,7 @@ public class MangaDetailActivity extends Activity {
pagesSize.setText(String.format(getString(R.string.detail_pages_size), mangaDetail.pages, mangaDetail.size));
// TODO
LinearLayout tagsLayout = (LinearLayout) findViewById(R.id.tags_layout);
addTags(tagsLayout, mangaDetail.tags);
addTags();
mangaDetailNormal.setVisibility(View.VISIBLE);
@@ -727,7 +730,10 @@ public class MangaDetailActivity extends Activity {
}
}
private void addTags(LinearLayout tagsLayout, String[][] tagGroups) {
private void addTags() {
LinearLayout tagsLayout = (LinearLayout) findViewById(R.id.tags_layout);
LinkedHashMap<String, LinkedList<SimpleEntry<String, Integer>>> tagGroups =
mangaDetail.tags;
tagsLayout.removeAllViews();
int x = Ui.dp2pix(8);
int y = Ui.dp2pix(6);
@@ -737,15 +743,13 @@ public class MangaDetailActivity extends Activity {
ColorStateList tagTextColor = resources.getColorStateList(R.color.blue_bn_text);
int tagPaddingX = resources.getDimensionPixelOffset(R.dimen.button_tag_padding_x);
int tagPaddingY = resources.getDimensionPixelOffset(R.dimen.button_tag_padding_y);
for (String[] tagGroup : tagGroups) {
if (tagGroup == null || tagGroup.length == 0)
continue;
for (Entry<String, LinkedList<SimpleEntry<String, Integer>>> tagGroup : tagGroups.entrySet()) {
LinearLayout tagGroupLayout = new LinearLayout(this);
tagGroupLayout.setOrientation(LinearLayout.HORIZONTAL);
AutoWrapLayout tagLayout = new AutoWrapLayout(this);
// Group name
final String groupName = tagGroup[0];
final String groupName = tagGroup.getKey();
TextView groupNameView = new TextView(new ContextThemeWrapper(this, R.style.TextTag));
groupNameView.setText(groupName);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
@@ -753,11 +757,11 @@ public class MangaDetailActivity extends Activity {
tagGroupLayout.addView(groupNameView, lp);
// tags
for (int i = 1; i < tagGroup.length; i ++) {
final String tagText = tagGroup[i];
for (SimpleEntry<String, Integer> tag : tagGroup.getValue()) {
final String tagText = tag.getKey();
Button tagView = new Button(this);
tagView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tagTextSize);
tagView.setText(tagText);
tagView.setText(String.format("%s (%d)", tagText, tag.getValue()));
tagView.setTextColor(tagTextColor);
tagView.setBackgroundResource(R.drawable.blue_bn_bg);
tagView.setPadding(tagPaddingX, tagPaddingY, tagPaddingX, tagPaddingY);
@@ -864,6 +868,18 @@ public class MangaDetailActivity extends Activity {
public void onSuccess(String tagPane) {
Toast.makeText(MangaDetailActivity.this,
"投票成功", Toast.LENGTH_SHORT).show(); // TODO
DetailParser parser = new DetailParser();
parser.setMode(DetailParser.TAG);
if (parser.parser(tagPane) == DetailParser.TAG) {
mangaDetail.tags = parser.tags;
// TODO Only change target text view
addTags();
} else {
Toast.makeText(MangaDetailActivity.this,
getString(R.string.em_parser_error),
Toast.LENGTH_SHORT).show();
}
}
@Override
@@ -8,6 +8,6 @@ public class ApiGetGalleryInfo extends GalleryInfo{
public long filesize;
public boolean expunged;
public String torrentcount;
public String[][] tags;
public String[] tags;
}
@@ -1,5 +1,11 @@
package com.hippo.ehviewer.data;
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import android.os.Parcel;
import android.os.Parcelable;
@@ -13,7 +19,8 @@ public class GalleryDetail extends GalleryInfo{
public String language;
public int people;
public String firstPage;
public String[][] tags;
// I think LinkedHashMap may provides better performance
public LinkedHashMap<String, LinkedList<SimpleEntry<String, Integer>>> tags;
// For Preview
public int previewSum;
@@ -1,6 +1,9 @@
package com.hippo.ehviewer.ehclient;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -37,7 +40,7 @@ public class DetailParser {
public String firstPage;
public int previewPerPage;
public int previewSum;
public String[][] tags;
public LinkedHashMap<String, LinkedList<SimpleEntry<String, Integer>>> tags;
public PreviewList previewList;
public void setMode(int mode) {
@@ -87,25 +90,18 @@ public class DetailParser {
}
// Get tag
if ((mMode & TAG) != 0) {
ArrayList<String[]> list = new ArrayList<String[]>();
tags = new LinkedHashMap<String, LinkedList<SimpleEntry<String, Integer>>>();
p = Pattern
.compile("<tr><td[^<>]*>([^<>]+):</td><td>(?:<div[^<>]*><a[^<>]*>[^<>]*</a>[^<>]*<span[^<>]*>\\d+</span>[^<>]*</div>)+</td></tr>");
m = p.matcher(pageContent);
while (m.find()) {
re |= TAG;
String groupName = m.group(1);
String[] group = getTagGroup(m.group(0));
if (groupName != null && group != null) {
group[0] = groupName;
list.add(group);
LinkedList<SimpleEntry<String, Integer>> group = getTagGroup(m.group(0));
if (group != null) {
tags.put(groupName, group);
}
}
tags = new String[list.size()][];
int i = 0;
for (String[] item : list) {
tags[i] = item;
i++;
}
}
// Get preview info
@@ -140,24 +136,19 @@ public class DetailParser {
return re;
}
private String[] getTagGroup(String pageContent) {
ArrayList<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("<a[^<>]*>([^<>]+)</a>");
private LinkedList<SimpleEntry<String, Integer>> getTagGroup(String pageContent) {
LinkedList<SimpleEntry<String, Integer>> list =
new LinkedList<SimpleEntry<String, Integer>>();
Pattern p = Pattern.compile("<a[^<>]*>([^<>]+)</a> \\(<span[^<>]*>([\\d|,]+)</span>\\)");
Matcher m = p.matcher(pageContent);
while (m.find()) {
String str = m.group(1);
if (str != null)
list.add(str);
}
while (m.find())
list.add(new SimpleEntry<String, Integer>(m.group(1),
Integer.parseInt(m.group(2).replace(",", ""))));
if (list.size() == 0)
return null;
String[] strs = new String[list.size() + 1];
int i = 1;
for (String str : list) {
strs[i] = str;
i++;
}
return strs;
else
return list;
}
private int getType(String rawType) {