Skip to content

Commit

Permalink
Added context menu for post header at the top of each comment thread
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Apr 5, 2013
1 parent d00dc4c commit d7a001f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 20 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.quantumbadger.redreader" android:versionCode="4" android:versionName="1.2.1">
package="org.quantumbadger.redreader" android:versionCode="5" android:versionName="1.2.2">

<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="16" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.quantumbadger.redreader.reddit.things.RedditThing;
import org.quantumbadger.redreader.views.RedditCommentView;
import org.quantumbadger.redreader.views.RedditPostHeaderView;
import org.quantumbadger.redreader.views.RedditPostView;
import org.quantumbadger.redreader.views.liststatus.ErrorView;
import org.quantumbadger.redreader.views.liststatus.LoadingView;

Expand Down Expand Up @@ -463,10 +464,95 @@ public void onCreateContextMenu(ContextMenu menu, View v, android.view.ContextMe
menu.add(Menu.NONE, Action.SHARE.ordinal(), 0, R.string.action_share);
menu.add(Menu.NONE, Action.USER_PROFILE.ordinal(), 0, R.string.action_user_profile);
menu.add(Menu.NONE, Action.PROPERTIES.ordinal(), 0, R.string.action_properties);

} else {

if(post == null) {
General.quickToast(getSupportActivity(), "Parent post not downloaded yet."); // TODO string
return;
}

if(!RedditAccountManager.getInstance(context).getDefaultAccount().isAnonymous()) {

if(!post.isUpvoted()) {
menu.add(R.string.action_upvote).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.UPVOTE));
} else {
menu.add(R.string.action_upvote_remove).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.UNVOTE));
}

if(!post.isDownvoted()) {
menu.add(R.string.action_downvote).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.DOWNVOTE));
} else {
menu.add(R.string.action_downvote_remove).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.UNVOTE));
}

if(!post.isSaved()) {
menu.add(R.string.action_save).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.SAVE));
} else {
menu.add(R.string.action_unsave).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.UNSAVE));
}

if(!post.isHidden()) {
menu.add(R.string.action_hide).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.HIDE));
} else {
menu.add(R.string.action_unhide).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.UNHIDE));
}

menu.add(R.string.action_report).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.REPORT));
}

menu.add(R.string.action_external).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.EXTERNAL));
menu.add(R.string.action_share).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.SHARE));

if(post.src.selftext != null && post.src.selftext.length() > 1) menu.add("Links in Self Text").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {

final HashSet<String> linksInComment = LinkHandler.computeAllLinks(StringEscapeUtils.unescapeHtml4(post.src.selftext));

if(linksInComment.isEmpty()) {
General.quickToast(getSupportActivity(), "No URLs found in the self text."); // TODO string

} else {

final String[] linksArr = linksInComment.toArray(new String[linksInComment.size()]);

final AlertDialog.Builder builder = new AlertDialog.Builder(getSupportActivity());
builder.setItems(linksArr, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LinkHandler.onLinkClicked(getSupportActivity(), linksArr[which], false);
dialog.dismiss();
}
});

final AlertDialog alert = builder.create();
alert.setTitle("Links in Self Text");
alert.setCanceledOnTouchOutside(true);
alert.show();
}
return true;
}
});

menu.add(R.string.action_user_profile).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.USER_PROFILE));
menu.add(R.string.action_properties).setOnMenuItemClickListener(new MenuHandler(RedditPostView.Action.PROPERTIES));
}
}
}

private class MenuHandler implements MenuItem.OnMenuItemClickListener {

private final RedditPostView.Action action;

public MenuHandler(final RedditPostView.Action action) {
this.action = action;
}

public boolean onMenuItemClick(MenuItem item) {
RedditPostView.onActionSelected(post, getSupportActivity(), CommentListingFragment.this, action);
return true;
}
}

public void onClick(String url) {
if(url != null) LinkHandler.onLinkClicked(getSupportActivity(), url, false);
}
Expand All @@ -477,8 +563,11 @@ private static enum Action {

@Override
public boolean onContextItemSelected(MenuItem item) {

final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

if(info.position <= 0) return false;

final Action action = Action.values()[item.getItemId()];
final RedditPreparedComment comment = (RedditPreparedComment)lv.getAdapter().getItem(info.position);

Expand Down
40 changes: 21 additions & 19 deletions src/main/java/org/quantumbadger/redreader/views/RedditPostView.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.view.ViewGroup;
import android.widget.ImageView;
import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.preference.PreferenceManager;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.FrameLayout;
Expand Down Expand Up @@ -79,7 +80,7 @@ public final class RedditPostView extends SwipableListItemView implements Reddit

private final int offsetBeginAllowed, offsetActionPerformed;

private static enum Action {
public static enum Action {
UPVOTE, UNVOTE, DOWNVOTE, SAVE, HIDE, UNSAVE, UNHIDE, REPORT, SHARE, REPLY, USER_PROFILE, EXTERNAL, PROPERTIES, COMMENTS, LINK
}

Expand Down Expand Up @@ -163,10 +164,10 @@ protected void onSwipePositionChange(int xOffsetPixels) {
if(swipeReady && absOffset > offsetActionPerformed) {

if(xOffsetPixels > 0) {
onActionSelected(rightFlingAction.action);
onActionSelected(post, getContext(), fragmentParent, rightFlingAction.action);
leftOverlayText.setCompoundDrawablesWithIntrinsicBounds(null, rrIconTick, null, null);
} else {
onActionSelected(leftFlingAction.action);
onActionSelected(post, getContext(), fragmentParent, leftFlingAction.action);
rightOverlayText.setCompoundDrawablesWithIntrinsicBounds(null, rrIconTick, null, null);
}

Expand Down Expand Up @@ -404,7 +405,7 @@ public void rrOnLongClick() {

builder.setItems(menuText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onActionSelected(menu.get(which).action);
onActionSelected(post, getContext(), fragmentParent, menu.get(which).action);
}
});

Expand All @@ -416,47 +417,48 @@ public void onClick(DialogInterface dialog, int which) {
alert.show();
}

public void onActionSelected(final Action action) {
public static void onActionSelected(final RedditPreparedPost post, final Context context,
final Fragment fragmentParent, final Action action) {

switch(action) {

case UPVOTE:
post.action(getContext(), RedditAPI.RedditAction.UPVOTE);
post.action(context, RedditAPI.RedditAction.UPVOTE);
break;

case DOWNVOTE:
post.action(getContext(), RedditAPI.RedditAction.DOWNVOTE);
post.action(context, RedditAPI.RedditAction.DOWNVOTE);
break;

case UNVOTE:
post.action(getContext(), RedditAPI.RedditAction.UNVOTE);
post.action(context, RedditAPI.RedditAction.UNVOTE);
break;

case SAVE:
post.action(getContext(), RedditAPI.RedditAction.SAVE);
post.action(context, RedditAPI.RedditAction.SAVE);
break;

case UNSAVE:
post.action(getContext(), RedditAPI.RedditAction.UNSAVE);
post.action(context, RedditAPI.RedditAction.UNSAVE);
break;

case HIDE:
post.action(getContext(), RedditAPI.RedditAction.HIDE);
post.action(context, RedditAPI.RedditAction.HIDE);
break;

case UNHIDE:
post.action(getContext(), RedditAPI.RedditAction.UNHIDE);
post.action(context, RedditAPI.RedditAction.UNHIDE);
break;

case REPORT:

new AlertDialog.Builder(getContext())
new AlertDialog.Builder(context)
.setTitle(R.string.action_report)
.setMessage(R.string.action_report_sure)
.setPositiveButton(R.string.action_report,
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
post.action(getContext(), RedditAPI.RedditAction.REPORT);
post.action(context, RedditAPI.RedditAction.REPORT);
// TODO update the view to show the result
// TODO don't forget, this also hides
}
Expand All @@ -469,7 +471,7 @@ public void onClick(final DialogInterface dialog, final int which) {
case EXTERNAL:
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(post.url));
getContext().startActivity(intent);
context.startActivity(intent);
break;

case SHARE:
Expand All @@ -478,23 +480,23 @@ public void onClick(final DialogInterface dialog, final int which) {
mailer.setType("text/plain");
mailer.putExtra(Intent.EXTRA_SUBJECT, post.title);
mailer.putExtra(Intent.EXTRA_TEXT, post.url + "\r\n\r\nSent using RedReader on Android");
getContext().startActivity(Intent.createChooser(mailer, "Share Post")); // TODO string
context.startActivity(Intent.createChooser(mailer, "Share Post")); // TODO string
break;

case USER_PROFILE:
UserProfileDialog.newInstance(post.src.author).show(fragmentParent.getSupportActivity());
break;

case PROPERTIES:
PostPropertiesDialog.newInstance(post.src).show(fragmentParent.getSupportFragmentManager());
PostPropertiesDialog.newInstance(post.src).show(fragmentParent.getSupportActivity());
break;

case COMMENTS:
fragmentParent.onPostCommentsSelected(post);
((PostListingFragment)fragmentParent).onPostCommentsSelected(post);
break;

case LINK:
fragmentParent.onPostSelected(post);
((PostListingFragment)fragmentParent).onPostSelected(post);
break;
}
}
Expand Down

0 comments on commit d7a001f

Please sign in to comment.