Permalink
Browse files

subreddit list upgrade

Subreddit list now displays additional info from the API:
- description
- age
- subscribers

Additionally the layout isn't just the default android simple list entry so it
looks a lot nicer
  • Loading branch information...
1 parent d8e1278 commit 32b63465f7218ea0c54c3f30c5b7a258e424c2a4 @zagaberoo committed Mar 20, 2013
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright 2013 Michael Shick
+ *
+ * This file is part of "diode".
+ *
+ * "diode" is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * "diode" is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with "diode". If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:padding="6dip"
+ >
+
+ <TextView
+ android:id="@+id/name"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_alignParentTop="true"
+ android:singleLine="true"
+ android:textAppearance="?android:attr/textAppearanceLarge"
+ android:textSize="24sp"
+ />
+ <TextView
+ android:id="@+id/age"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_alignParentTop="true"
+ android:singleLine="true"
+ android:textAppearance="?android:attr/textAppearanceSmall"
+ />
+ <TextView
+ android:id="@+id/subscribers"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_below="@id/age"
+ android:singleLine="true"
+ android:textAppearance="?android:attr/textAppearanceSmall"
+ />
+
+ <!--<View
+ android:id="@+id/separator"
+ android:layout_width="fill_parent"
+ android:layout_height="0px"
+ android:layout_alignParentLeft="true"
+ android:layout_below="@id/subscribers"
+ android:layout_marginLeft="8dp"
+ android:layout_marginRight="8dp"
+ android:background="#808080"
+ />-->
+
+ <TextView
+ android:id="@+id/description"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_below="@id/name"
+ android:singleLine="true"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ />
+
+</RelativeLayout>
View
@@ -56,6 +56,13 @@
<string name="loading_splash_text">Loading…</string>
+ <string name="subscriber_count_format" formatted="false">%s subs.</string>
+ <string name="second_age_format" formatted="false">%d second</string>
+ <string name="minute_age_format" formatted="false">%d minute</string>
+ <string name="hour_age_format" formatted="false">%d hour</string>
+ <string name="day_age_format" formatted="false">%d day</string>
+ <string name="month_age_format" formatted="false">%d month</string>
+ <string name="year_age_format" formatted="false">%d year</string>
<string name="pick_subreddit_label">Subreddit name:</string>
<string name="pick_subreddit_button">Go</string>
<string name="frontpage">reddit front page</string>
@@ -31,6 +31,8 @@
import android.content.Context;
import android.util.Log;
+import in.shick.diode.reddits.SubredditInfo;
+
public class CacheInfo implements Serializable {
static final long serialVersionUID = 39;
static final String TAG = "CacheInfo";
@@ -45,7 +47,7 @@
// the ids for the cached JSON objects
public String subredditUrl = null;
public String threadUrl = null;
- public ArrayList<String> subredditList = null;
+ public ArrayList<SubredditInfo> subredditList = null;
@@ -143,7 +145,7 @@ static long getCachedThreadTime(Context context) {
}
}
- public static ArrayList<String> getCachedSubredditList(Context context) {
+ public static ArrayList<SubredditInfo> getCachedSubredditList(Context context) {
try {
return getCacheInfo(context).subredditList;
} catch (Exception e) {
@@ -293,7 +295,7 @@ static void setCachedThreadUrl(Context context, String threadUrl) throws IOExcep
}
}
- public static void setCachedSubredditList(Context context, ArrayList<String> subredditList) throws IOException {
+ public static void setCachedSubredditList(Context context, ArrayList<SubredditInfo> subredditList) throws IOException {
if (!Constants.USE_SUBREDDITS_CACHE)
return;
@@ -23,8 +23,11 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
+import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -85,7 +88,7 @@
private HttpClient mClient = RedditIsFunHttpClientFactory.getGzipHttpClient();
private PickSubredditAdapter mSubredditsAdapter;
- private ArrayList<String> mSubredditsList;
+ private ArrayList<SubredditInfo> mSubredditsList;
private static final Object ADAPTER_LOCK = new Object();
private EditText mEt;
@@ -185,7 +188,7 @@ public Object onRetainNonConfigurationInstance() {
@SuppressWarnings("unchecked")
private void restoreLastNonConfigurationInstance() {
- mSubredditsList = (ArrayList<String>) getLastNonConfigurationInstance();
+ mSubredditsList = (ArrayList<SubredditInfo>) getLastNonConfigurationInstance();
}
void resetUI(PickSubredditAdapter adapter) {
@@ -195,7 +198,7 @@ void resetUI(PickSubredditAdapter adapter) {
synchronized (ADAPTER_LOCK) {
if (adapter == null) {
// Reset the list to be empty.
- mSubredditsList = new ArrayList<String>();
+ mSubredditsList = new ArrayList<SubredditInfo>();
mSubredditsAdapter = new PickSubredditAdapter(this, mSubredditsList);
} else {
mSubredditsAdapter = adapter;
@@ -235,13 +238,13 @@ public void onClick(View v) {
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
- String item = mSubredditsAdapter.getItem(position);
- returnSubreddit(item);
+ SubredditInfo item = mSubredditsAdapter.getItem(position);
+ returnSubreddit(item.name);
}
private void returnSubreddit(String subreddit) {
Intent intent = new Intent();
- intent.setData(Util.createSubredditUri(subreddit));
+ intent.setData(Util.createSubredditUri(subreddit.toLowerCase()));
setResult(RESULT_OK, intent);
finish();
}
@@ -266,17 +269,17 @@ private void disableLoadingScreen() {
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_END);
}
- class DownloadRedditsTask extends AsyncTask<Void, Void, ArrayList<String>> {
+ class DownloadRedditsTask extends AsyncTask<Void, Void, ArrayList<SubredditInfo>> {
@Override
- public ArrayList<String> doInBackground(Void... voidz) {
- ArrayList<String> reddits = null;
+ public ArrayList<SubredditInfo> doInBackground(Void... voidz) {
+ ArrayList<SubredditInfo> reddits = null;
HttpEntity entity = null;
try {
reddits = cacheSubredditsList(reddits);
if (reddits == null) {
- reddits = new ArrayList<String>();
+ reddits = new ArrayList<SubredditInfo>();
HttpGet request = new HttpGet(Constants.REDDIT_BASE_URL + "/subreddits/mine/subscriber.json?limit=100");
// Set timeout to 15 seconds
@@ -292,10 +295,17 @@ private void disableLoadingScreen() {
for(JsonNode ee : rootNode.get("data").get("children")) {
ee = ee.get("data");
- reddits.add("" + ee.get("display_name").getTextValue() + " :: \"" +
- ee.get("title").getTextValue() + "\"");
+ SubredditInfo sr = new SubredditInfo();
+ sr.name = ee.get("display_name").getTextValue();
+ sr.description = ee.get("title").getTextValue();
+ sr.nsfw = ee.get("over18").getBooleanValue();
+ sr.subscribers = ee.get("subscribers").getIntValue();
+ sr.url = new URL(Constants.REDDIT_BASE_URL + ee.get("url").getTextValue());
+ sr.created = new Date((long) ee.get("created").getIntValue() * 1000);
+ reddits.add(sr);
}
}
+ Collections.sort(reddits);
return reddits;
}
catch(Throwable e) {
@@ -317,20 +327,24 @@ public void onPreExecute() {
}
@Override
- public void onPostExecute(ArrayList<String> reddits) {
+ public void onPostExecute(ArrayList<SubredditInfo> reddits) {
synchronized (mCurrentTaskLock) {
mCurrentTask = null;
}
disableLoadingScreen();
if (reddits == null || reddits.size() == 0) {
// Need to make a copy because Arrays.asList returns List backed by original array
- mSubredditsList = new ArrayList<String>();
- mSubredditsList.addAll(Arrays.asList(DEFAULT_SUBREDDITS));
+ mSubredditsList = new ArrayList<SubredditInfo>();
+ for(String ee : DEFAULT_SUBREDDITS) {
+ SubredditInfo info = new SubredditInfo();
+ info.name = ee;
+ mSubredditsList.add(info);
+ }
} else {
mSubredditsList = reddits;
}
- addFakeSubredditsUnlessSuppressed();
+ //addFakeSubredditsUnlessSuppressed();
resetUI(new PickSubredditAdapter(PickSubredditActivity.this, mSubredditsList));
}
}
@@ -350,20 +364,26 @@ private void addFakeSubredditsUnlessSuppressed() {
}
if (addFakeSubreddits)
{
- mSubredditsList.addAll(0, Arrays.asList(FAKE_SUBREDDITS));
+ for(String ee : DEFAULT_SUBREDDITS) {
+ SubredditInfo info = new SubredditInfo();
+ info.name = ee;
+ mSubredditsList.add(info);
+ }
}
}
- private final class PickSubredditAdapter extends ArrayAdapter<String> {
+ private final class PickSubredditAdapter extends ArrayAdapter<SubredditInfo> {
private LayoutInflater mInflater;
private boolean mLoading = true;
private int mFrequentSeparatorPos = ListView.INVALID_POSITION;
+ private NumberFormat mSubscriberFormat;
- public PickSubredditAdapter(Context context, List<String> objects) {
+ public PickSubredditAdapter(Context context, List<SubredditInfo> objects) {
super(context, 0, objects);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ mSubscriberFormat = NumberFormat.getInstance();
}
@Override
@@ -392,13 +412,31 @@ public View getView(int position, View convertView, ViewGroup parent) {
// Here view may be passed in for re-use, or we make a new one.
if (convertView == null) {
- view = mInflater.inflate(android.R.layout.simple_list_item_1, null);
+ view = mInflater.inflate(R.layout.subreddit_list_entry, null);
} else {
view = convertView;
}
+
+ SubredditInfo subject = mSubredditsAdapter.getItem(position);
- TextView text = (TextView) view.findViewById(android.R.id.text1);
- text.setText(mSubredditsAdapter.getItem(position));
+ TextView text = (TextView) view.findViewById(R.id.name);
+ text.setText(subject.name);
+
+ if(subject.created != null)
+ {
+ text = (TextView) view.findViewById(R.id.age);
+ text.setText(subject.getAgeString(PickSubredditActivity.this));
+ }
+
+ if(subject.subscribers > 0)
+ {
+ text = (TextView) view.findViewById(R.id.subscribers);
+ text.setText(String.format(getString(R.string.subscriber_count_format),
+ mSubscriberFormat.format(subject.subscribers)));
+ }
+
+ text = (TextView) view.findViewById(R.id.description);
+ text.setText(subject.description);
return view;
}
@@ -453,7 +491,7 @@ protected void onRestoreInstanceState(Bundle state) {
}
}
- protected ArrayList<String> cacheSubredditsList(ArrayList<String> reddits){
+ protected ArrayList<SubredditInfo> cacheSubredditsList(ArrayList<SubredditInfo> reddits){
if (Constants.USE_SUBREDDITS_CACHE) {
if (CacheInfo.checkFreshSubredditListCache(getApplicationContext())) {
reddits = CacheInfo.getCachedSubredditList(getApplicationContext());
Oops, something went wrong.

0 comments on commit 32b6346

Please sign in to comment.