Ville Saalo avatar Ville Saalo committed bf38b6b

#2: Better formatting of the user newsfeed items.

Comments (0)

Files changed (1)

src/com/saibotd/bitbeaker/adapters/NewsfeedAdapter.java

 import zeroone.rss.Item;
 import android.content.Context;
 import android.text.Html;
+import android.text.Spanned;
 import android.text.method.LinkMovementMethod;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 		TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
 		final Item item = getItem(position);
 		try {
-			// Cut the long changeset ids in item titles:
-			String titleText = item.getTitle();
-			titleText = titleText.replaceAll("([a-fA-F0-9]{7})([a-fA-F0-9]{25})", "$1");
-			title.setText(titleText);
+			title.setText(getTitle(item));
+			title.setMovementMethod(LinkMovementMethod.getInstance());
 			
-			subtitle.setText(Html.fromHtml(item.getDescription()));
+			final Spanned subtitleText = getSubtitle(item);
+			if( subtitleText != null ) {
+				subtitle.setText(subtitleText);
+				subtitle.setVisibility(View.VISIBLE);
+			} else {
+				subtitle.setVisibility(View.GONE);
+			}
 			subtitle.setMovementMethod(LinkMovementMethod.getInstance());
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
-		//view.setOnClickListener(clickListener);
 		
 		return view;
 	}
+	
+	private Spanned getTitle(Item item) {
+		String[] itemDescription = splitItemDescription(item.getDescription());
+		return Html.fromHtml(trimChangesetIds(itemDescription[0]));
+	}
 
+	private Spanned getSubtitle(Item item) {
+		String[] itemDescription = splitItemDescription(item.getDescription());
+		if(itemDescription.length == 1) {
+			return null;
+		}
+		return Html.fromHtml(itemDescription[1]);
+	}
+	
+	/**
+	 * The Bitbucket RSS feed duplicates the contents of the title field in the
+	 * description field, but in such a fashion that the description field also
+	 * contains links and is thus more useful. In the description field the
+	 * title is separated from other content (such as user's commit message) so
+	 * that the other content is wrapped with paragraph tags.
+	 * 
+	 * @param description The description field of an item element
+	 * @return The description split to a title and the other part,
+	 *         or just the title if there wasn't more.
+	 */
+	private String[] splitItemDescription(final String description) {
+		String[] result;
+		if (description.contains("<p>")) {
+			Log.d("News", "contains p");
+			result = description.split("<p>", 2);
+			result[0] = result[0].trim();
+			result[1] = result[1].replace("</p>", "").trim();
+		} else {
+			Log.d("News", "not p: "+description);
+			result = new String[1];
+			result[0] = description;
+		}
+		return result;
+	}
+
+	/**
+	 * Cuts the long changeset ids.
+	 * @param text A string with some changeset ids
+	 * @return The input string but with shorter changeset ids.
+	 */
+	private String trimChangesetIds(String text) {
+		return text.replaceAll("([a-fA-F0-9]{7})([a-fA-F0-9]{25})", "$1");
+	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.