Skip to content

Commit

Permalink
Adding module list to the release view
Browse files Browse the repository at this point in the history
  • Loading branch information
zostay committed Dec 13, 2012
1 parent c478bd1 commit b8dae78
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 54 deletions.
27 changes: 27 additions & 0 deletions assets/template/module_for_release.json.tmpl
@@ -0,0 +1,27 @@
{
"query": {
"filtered": {
"query": { "match_all": {} },
"filter": {
"and": [
{ "term": { "release": "%release_version|s%" } },
{ "term": { "author": "%author_pauseid|s%" } },
{ "or": [
{ "and": [
{ "exists": { "field": "file.module.name" } },
{ "term": { "file.module.indexed": true } }
] },
{ "and": [
{ "exists": { "field": "file.pod.analyzed" } },
{ "term": { "file.indexed": true } }
] }
] }
]
}
}
},
"size": %size|j%,
"from": %from|j%,
"sort": [ "documentation" ],
"fields": [ "documentation", "_source.abstract", "_source.module", "path", "status" ]
}
101 changes: 101 additions & 0 deletions src/com/qubling/sidekick/fetch/cpan/ModuleForReleaseFetcher.java
@@ -0,0 +1,101 @@
package com.qubling.sidekick.fetch.cpan;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.qubling.sidekick.instance.Module;
import com.qubling.sidekick.instance.Release;
import com.qubling.sidekick.model.Model;
import com.qubling.sidekick.search.ResultSet;

public class ModuleForReleaseFetcher extends CPANQueryFetcher<Module> {
private Release release;

public ModuleForReleaseFetcher(Model<Module> model, Release release) {
super(model, SearchSection.FILE, "module_for_release");

this.release = release;
}

@Override
protected boolean shouldCompleteRequest() {
return true;
}

@Override
protected void prepareRequest(Map<String, Object> variables) {
variables.put("release_version", release.getName() + "-" + release.getVersion());
variables.put("author_pauseid", release.getAuthorPauseId());
}

@Override
protected void consumeResponse(JSONObject response) throws JSONException {
ResultSet<Module> results = getResultSet();

if (response == null) {
Log.e("ModuleForReleaseFetcher", "Unexpected response (response is null)");
return;
}

JSONObject topHits = response.getJSONObject("hits");
if (topHits == null) {
Log.e("ModuleKeywordSearch", "Unexpected response (top hits missing): " + response);
return;
}

JSONArray hits = topHits.getJSONArray("hits");
if (hits == null) {
Log.e("ModuleKeywordSearch", "Unexpected response (nested hits missing): " + response);
return;
}

// Slurp up the matches
for (int i = 0; i < hits.length(); i++) {
JSONObject hit = hits.getJSONObject(i).getJSONObject("fields");

String name = null;
try {
if (!hit.isNull("_source.module")) {
name = hit.getJSONArray("_source.module").getJSONObject(0).getString("name");
}
else if (!hit.isNull("documentation")) {
name = hit.getString("documentation");
}
else if (!hit.isNull("path")) {
name = hit.getString("path");
}
}
catch (JSONException e) {
name = "Unknown Module Name";
}

String moduleAbstract = null;

try { if (!hit.isNull("_source.abstract")) moduleAbstract = hit.getString("_source.abstract"); } catch (JSONException e) {}

// Log.d("ModuleForReleaseFetcher", "name: " + name);
// Log.d("ModuleForReleaseFetcher", "abstract: " + moduleAbstract);
// Log.d("ModuleForReleaseFetcher", "hit: " + hit.toString());

Module module = getModel().acquireInstance(name);
if (moduleAbstract != null) module.setModuleAbstract(moduleAbstract);
module.setRelease(release);

results.add(module);
}
}

@Override
public String toString() {
return getModel() + ":ModuleForReleaseFetcher("
+ release.getAuthorPauseId() + "/"
+ release.getName() + "-"
+ release.getVersion() + ")";
}

}
4 changes: 4 additions & 0 deletions src/com/qubling/sidekick/instance/Module.java
Expand Up @@ -62,6 +62,10 @@ public Release getRelease() {
return release;
}

public void setRelease(Release release) {
this.release = release;
}

public String getAuthorPauseId() {
return release == null ? null : release.getAuthorPauseId();
}
Expand Down
8 changes: 8 additions & 0 deletions src/com/qubling/sidekick/model/ModuleModel.java
Expand Up @@ -6,7 +6,9 @@
import com.qubling.sidekick.fetch.Fetcher;
import com.qubling.sidekick.fetch.SubqueryFetcher;
import com.qubling.sidekick.fetch.UpdateFetcher;
import com.qubling.sidekick.fetch.cpan.CPANQueryFetcher;
import com.qubling.sidekick.fetch.cpan.ModuleDetailsFetcher;
import com.qubling.sidekick.fetch.cpan.ModuleForReleaseFetcher;
import com.qubling.sidekick.fetch.cpan.ModuleKeywordSearch;
import com.qubling.sidekick.fetch.cpan.ModulePodFetcher;
import com.qubling.sidekick.instance.Author;
Expand All @@ -27,6 +29,12 @@ protected Module constructInstance(String name) {
public Fetcher<Module> searchByKeyword(String keywords) {
return new ModuleKeywordSearch(this, keywords);
}

public Fetcher<Module> fetchModulesForRelease(Release release) {
CPANQueryFetcher<Module> fetcher = new ModuleForReleaseFetcher(this, release);
fetcher.setSize(999);
return fetcher;
}

public UpdateFetcher<Module> fetchPod() {
return new ModulePodFetcher(this);
Expand Down
46 changes: 35 additions & 11 deletions src/com/qubling/sidekick/ui/release/ReleaseInfoFragment.java
Expand Up @@ -3,11 +3,14 @@
import com.qubling.sidekick.R;
import com.qubling.sidekick.fetch.Fetcher;
import com.qubling.sidekick.instance.Gravatar;
import com.qubling.sidekick.instance.Module;
import com.qubling.sidekick.instance.Release;
import com.qubling.sidekick.model.ModuleModel;
import com.qubling.sidekick.model.ReleaseModel;
import com.qubling.sidekick.search.ResultSet;
import com.qubling.sidekick.search.Schema;
import com.qubling.sidekick.search.Search;
import com.qubling.sidekick.widget.ModuleListAdapter;

import android.graphics.Color;
import android.os.Bundle;
Expand All @@ -16,15 +19,18 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.QuickContactBadge;
import android.widget.RatingBar;
import android.widget.TextView;

public class ReleaseInfoFragment extends Fragment implements Fetcher.OnFinished<Release> {
public class ReleaseInfoFragment extends Fragment {

private Schema searchSession;
private Release release;

private Search<Module> moduleSearch;

public void setRelease(Release release) {
this.release = release;

Expand Down Expand Up @@ -113,22 +119,40 @@ public void onActivityCreated(Bundle savedInstanceState) {
fetchRelease();
}

public void fetchRelease() {
private void fetchRelease() {
fetchReleaseMetadata();
fetchReleaseModules();
}

private void fetchReleaseMetadata() {
ReleaseModel releases = searchSession.getReleaseModel();
Fetcher<Release> releaseFetch = releases.fetch();
releaseFetch.getResultSet().add(release);

Search<Release> search = searchSession.doFetch(releaseFetch, this);

search.start();
Search<Release> releaseMetaSearch = searchSession.doFetch(releaseFetch, new Fetcher.OnFinished<Release>() {
@Override
public void onFinishedFetch(Fetcher<Release> fetcher, ResultSet<Release> results) {
// Don't do anything if we don't have an activity (i.e., don't NPE either)
if (getActivity() == null) return;
updateReleaseInfo();
}
});
releaseMetaSearch.start();
}

@Override
public void onFinishedFetch(Fetcher<Release> fetcher, ResultSet<Release> results) {

private void fetchReleaseModules() {
ModuleModel modules = searchSession.getModuleModel();
Fetcher<Module> modulesFetch = modules.fetchModulesForRelease(release);

// Don't do anything if we don't have an activity (i.e., don't NPE either)
if (getActivity() == null) return;
moduleSearch = searchSession.doFetch(modulesFetch, new Fetcher.OnFinished<Module>() {
@Override
public void onFinishedFetch(Fetcher<Module> fetcher, ResultSet<Module> results) {
ModuleListAdapter adapter = new ModuleListAdapter(getActivity(), moduleSearch, R.layout.module_list_item_simplified);
ListView moduleListView = (ListView) getActivity().findViewById(R.id.release_modules_list);
moduleListView.setAdapter(adapter);
}
});

updateReleaseInfo();
moduleSearch.start();
}
}
98 changes: 58 additions & 40 deletions src/com/qubling/sidekick/widget/ModuleHelper.java
Expand Up @@ -33,17 +33,25 @@ public static void updateItem(View row, Module item) {

// Set the module name and abstract
TextView moduleName = (TextView) row.findViewById(R.id.module_name);
TextView moduleAbstract = (TextView) row.findViewById(R.id.module_abstract);

SpannableStringBuilder formattedString = new SpannableStringBuilder(item.getName());
if (item.getModuleAbstract() != null) {
formattedString.append(" - " + item.getModuleAbstract());

ForegroundColorSpan color = new ForegroundColorSpan(Color.GRAY);
formattedString.setSpan(
color,
formattedString.length() - item.getModuleAbstract().length() - 3,
formattedString.length(),
0);
if (moduleAbstract != null) {
moduleAbstract.setText(item.getModuleAbstract());
}
else {
formattedString.append(" - " + item.getModuleAbstract());

ForegroundColorSpan color = new ForegroundColorSpan(Color.GRAY);
formattedString.setSpan(
color,
formattedString.length() - item.getModuleAbstract().length() - 3,
formattedString.length(),
0);
}
}

moduleName.setText(formattedString);

Release release = item.getRelease();
Expand All @@ -62,51 +70,61 @@ public static void updateItem(View row, Module item) {

// Set the distribution author, name, and version
TextView releaseName = (TextView) row.findViewById(R.id.module_author_distribution);
StringBuilder authorDist = new StringBuilder();
authorDist.append(author.getPauseId());
authorDist.append('/');
authorDist.append(release.getName());
authorDist.append('-');
authorDist.append(release.getVersion());
releaseName.setText(authorDist);
if (releaseName != null) {
StringBuilder authorDist = new StringBuilder();
authorDist.append(author.getPauseId());
authorDist.append('/');
authorDist.append(release.getName());
authorDist.append('-');
authorDist.append(release.getVersion());
releaseName.setText(authorDist);
}

// Set the rating bar
RatingBar distRating = (RatingBar) row.findViewById(R.id.module_release_rating);
distRating.setRating((float) release.getRatingMean());
if (distRating != null) {
distRating.setRating((float) release.getRatingMean());
}

// Set the rating count
TextView distRatingCount = (TextView) row.findViewById(R.id.module_release_rating_count);
distRatingCount.setText(String.valueOf(release.getRatingCount()));
if (distRatingCount != null) {
distRatingCount.setText(String.valueOf(release.getRatingCount()));
}

// Set the favorite count
Button favoriteCount = (Button) row.findViewById(R.id.module_release_favorite);
if (release.getFavoriteCount() > 0) {
favoriteCount.setText(release.getFavoriteCount() + "++ ");
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_others);
favoriteCount.setShadowLayer(1.5f, 1f, 1f, R.color.favorite_text_shadow_color);
}

// Not favorited yet, set it to a blank
else {
favoriteCount.setText("++ ");
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_default);
favoriteCount.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
}

// Mark this as our favorite
if (release.isMyFavorite()) {
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_mine);
if (favoriteCount != null) {
if (release.getFavoriteCount() > 0) {
favoriteCount.setText(release.getFavoriteCount() + "++ ");
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_others);
favoriteCount.setShadowLayer(1.5f, 1f, 1f, R.color.favorite_text_shadow_color);
}

// Not favorited yet, set it to a blank
else {
favoriteCount.setText("++ ");
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_default);
favoriteCount.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
}

// Mark this as our favorite
if (release.isMyFavorite()) {
favoriteCount.setBackgroundResource(R.drawable.btn_favorite_mine);
}
}

// Set the quick contact badge to the author's picture
QuickContactBadge badge = (QuickContactBadge) row.findViewById(R.id.module_author_avatar);
if (gravatar != null && gravatar.getBitmap() != null) {
badge.setImageBitmap(gravatar.getBitmap());
}

// No user picture, set to default
else {
badge.setImageResource(R.drawable.ic_contact_picture);
if (badge != null) {
if (gravatar != null && gravatar.getBitmap() != null) {
badge.setImageBitmap(gravatar.getBitmap());
}

// No user picture, set to default
else {
badge.setImageResource(R.drawable.ic_contact_picture);
}
}
}
}

0 comments on commit b8dae78

Please sign in to comment.