implement unshare function

This commit is contained in:
dosiecki 2017-07-26 18:38:15 -07:00
parent 09b40b80be
commit 7e8c1a1910
9 changed files with 112 additions and 26 deletions

View file

@ -54,7 +54,8 @@
<string name="share_this">SHARE</string>
<string name="already_shared">SHARED</string>
<string name="share_this_story">SHARE</string>
<string name="update_shared">Update comment</string>
<string name="unshare">DELETE SHARE</string>
<string name="update_shared">UPDATE COMMENT</string>
<string name="save_this">SAVE</string>
<string name="unsave_this">REMOVE FROM SAVED</string>

View file

@ -4,7 +4,6 @@ import android.database.Cursor;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.util.SparseArray;
import android.view.ViewGroup;

View file

@ -809,7 +809,7 @@ public class BlurDatabaseHelper {
}
}
public void setStoryShared(String hash) {
public void setStoryShared(String hash, boolean shared) {
// get a fresh copy of the story from the DB so we can append to the shared ID set
Cursor c = dbRO.query(DatabaseConstants.STORY_TABLE,
new String[]{DatabaseConstants.STORY_SHARED_USER_IDS},
@ -825,12 +825,16 @@ public class BlurDatabaseHelper {
String[] sharedUserIds = TextUtils.split(c.getString(c.getColumnIndex(DatabaseConstants.STORY_SHARED_USER_IDS)), ",");
closeQuietly(c);
// the new id to append to the shared list (the current user)
// the id to append to or remove from the shared list (the current user)
String currentUser = PrefsUtils.getUserDetails(context).id;
// append to set and update DB
Set<String> newIds = new HashSet<String>(Arrays.asList(sharedUserIds));
newIds.add(currentUser);
if (shared) {
newIds.add(currentUser);
} else {
newIds.remove(currentUser);
}
ContentValues values = new ContentValues();
values.put(DatabaseConstants.STORY_SHARED_USER_IDS, TextUtils.join(",", newIds));
synchronized (RW_MUTEX) {dbRW.update(DatabaseConstants.STORY_TABLE, values, DatabaseConstants.STORY_HASH + " = ?", new String[]{hash});}
@ -1178,6 +1182,13 @@ public class BlurDatabaseHelper {
}
}
public void clearSelfComments(String storyId) {
String userId = PrefsUtils.getUserDetails(context).id;
synchronized (RW_MUTEX) {dbRW.delete(DatabaseConstants.COMMENT_TABLE,
DatabaseConstants.COMMENT_STORYID + " = ? AND " + DatabaseConstants.COMMENT_USERID + " = ?",
new String[]{storyId, userId});}
}
/* TODO: we cannot locally like comments without their proper ID
public void setCommentLiked(String storyId, String userId, String feedId, boolean liked) {
String commentKey = Comment.constructId(storyId, feedId, userId);

View file

@ -484,17 +484,20 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
if (story == null) return;
if (! TextUtils.equals(story.storyHash, this.story.storyHash)) return;
this.story = story;
com.newsblur.util.Log.d(this.getClass().getName(), "got fresh story");
}
public void handleUpdate(int updateType) {
if ((updateType & NbActivity.UPDATE_STORY) != 0) {
updateSaveButton();
updateShareButton();
setupItemCommentsAndShares();
}
if ((updateType & NbActivity.UPDATE_TEXT) != 0) {
reloadStoryContent();
}
if ((updateType & NbActivity.UPDATE_SOCIAL) != 0) {
updateShareButton();
setupItemCommentsAndShares();
}
}

View file

@ -67,11 +67,13 @@ public class ShareDialogFragment extends DialogFragment {
commentEditText = (EditText) replyView.findViewById(R.id.comment_field);
int positiveButtonText = R.string.share_this_story;
int negativeButtonText = R.string.alert_dialog_cancel;
if (hasBeenShared) {
positiveButtonText = R.string.update_shared;
if (previousComment != null ) {
commentEditText.setText(previousComment.commentText);
}
negativeButtonText = R.string.unshare;
}
builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
@ -82,12 +84,24 @@ public class ShareDialogFragment extends DialogFragment {
ShareDialogFragment.this.dismiss();
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ShareDialogFragment.this.dismiss();
}
});
if (hasBeenShared) {
// unshare
builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
FeedUtils.unshareStory(story, activity);
ShareDialogFragment.this.dismiss();
}
});
} else {
// cancel
builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ShareDialogFragment.this.dismiss();
}
});
}
return builder.create();
}

View file

@ -46,6 +46,7 @@ public class APIConstants {
public static final String PATH_MARK_ALL_AS_READ = "/reader/mark_all_as_read/";
public static final String PATH_MARK_STORIES_READ = "/reader/mark_story_hashes_as_read/";
public static final String PATH_SHARE_STORY = "/social/share_story";
public static final String PATH_UNSHARE_STORY = "/social/unshare_story";
public static final String PATH_MARK_STORY_AS_STARRED = "/reader/mark_story_hash_as_starred/";
public static final String PATH_MARK_STORY_AS_UNSTARRED = "/reader/mark_story_hash_as_unstarred/";
public static final String PATH_MARK_STORY_AS_UNREAD = "/reader/mark_story_as_unread/";

View file

@ -369,21 +369,31 @@ public class APIManager {
}
}
public StoriesResponse shareStory(final String storyId, final String feedId, final String comment, final String sourceUserId) {
final ContentValues values = new ContentValues();
if (!TextUtils.isEmpty(comment)) {
values.put(APIConstants.PARAMETER_SHARE_COMMENT, comment);
}
if (!TextUtils.isEmpty(sourceUserId)) {
values.put(APIConstants.PARAMETER_SHARE_SOURCEID, sourceUserId);
}
values.put(APIConstants.PARAMETER_FEEDID, feedId);
values.put(APIConstants.PARAMETER_STORYID, storyId);
public StoriesResponse shareStory(String storyId, String feedId, String comment, String sourceUserId) {
ContentValues values = new ContentValues();
if (!TextUtils.isEmpty(comment)) {
values.put(APIConstants.PARAMETER_SHARE_COMMENT, comment);
}
if (!TextUtils.isEmpty(sourceUserId)) {
values.put(APIConstants.PARAMETER_SHARE_SOURCEID, sourceUserId);
}
values.put(APIConstants.PARAMETER_FEEDID, feedId);
values.put(APIConstants.PARAMETER_STORYID, storyId);
APIResponse response = post(buildUrl(APIConstants.PATH_SHARE_STORY), values);
APIResponse response = post(buildUrl(APIConstants.PATH_SHARE_STORY), values);
// this call returns a new copy of the story with all fields updated and some metadata
return (StoriesResponse) response.getResponse(gson, StoriesResponse.class);
}
}
public StoriesResponse unshareStory(String storyId, String feedId) {
ContentValues values = new ContentValues();
values.put(APIConstants.PARAMETER_FEEDID, feedId);
values.put(APIConstants.PARAMETER_STORYID, storyId);
APIResponse response = post(buildUrl(APIConstants.PATH_UNSHARE_STORY), values);
// this call returns a new copy of the story with all fields updated and some metadata
return (StoriesResponse) response.getResponse(gson, StoriesResponse.class);
}
/**
* Fetch the list of feeds/folders/socials from the backend.

View file

@ -334,14 +334,22 @@ public class FeedUtils {
context.startActivity(Intent.createChooser(intent, "Send using"));
}
public static void shareStory(Story story, String comment, String sourceUserId, Context context) {
public static void shareStory(Story story, String comment, String sourceUserId, Context context) {
if (story.sourceUserId != null) {
sourceUserId = story.sourceUserId;
}
ReadingAction ra = ReadingAction.shareStory(story.storyHash, story.id, story.feedId, sourceUserId, comment);
dbHelper.enqueueAction(ra);
ra.doLocal(dbHelper);
NbActivity.updateAllActivities(NbActivity.UPDATE_SOCIAL);
NbActivity.updateAllActivities(NbActivity.UPDATE_SOCIAL | NbActivity.UPDATE_STORY);
triggerSync(context);
}
public static void unshareStory(Story story, Context context) {
ReadingAction ra = ReadingAction.unshareStory(story.storyHash, story.id, story.feedId);
dbHelper.enqueueAction(ra);
ra.doLocal(dbHelper);
NbActivity.updateAllActivities(NbActivity.UPDATE_SOCIAL | NbActivity.UPDATE_STORY);
triggerSync(context);
}

View file

@ -27,6 +27,7 @@ public class ReadingAction implements Serializable {
SAVE,
UNSAVE,
SHARE,
UNSHARE,
REPLY,
EDIT_REPLY,
DELETE_REPLY,
@ -121,6 +122,15 @@ public class ReadingAction implements Serializable {
return ra;
}
public static ReadingAction unshareStory(String hash, String storyId, String feedId) {
ReadingAction ra = new ReadingAction();
ra.type = ActionType.UNSHARE;
ra.storyHash = hash;
ra.storyId = storyId;
ra.feedId = feedId;
return ra;
}
public static ReadingAction likeComment(String storyId, String commentUserId, String feedId) {
ReadingAction ra = new ReadingAction();
ra.type = ActionType.LIKE_COMMENT;
@ -238,6 +248,12 @@ public class ReadingAction implements Serializable {
values.put(DatabaseConstants.ACTION_COMMENT_TEXT, commentReplyText);
break;
case UNSHARE:
values.put(DatabaseConstants.ACTION_STORY_HASH, storyHash);
values.put(DatabaseConstants.ACTION_STORY_ID, storyId);
values.put(DatabaseConstants.ACTION_FEED_ID, feedId);
break;
case LIKE_COMMENT:
values.put(DatabaseConstants.ACTION_STORY_ID, storyId);
values.put(DatabaseConstants.ACTION_FEED_ID, feedId);
@ -327,6 +343,10 @@ public class ReadingAction implements Serializable {
ra.feedId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_FEED_ID));
ra.sourceUserId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_SOURCE_USER_ID));
ra.commentReplyText = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_COMMENT_TEXT));
} else if (ra.type == ActionType.UNSHARE) {
ra.storyHash = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_STORY_HASH));
ra.storyId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_STORY_ID));
ra.feedId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_FEED_ID));
} else if (ra.type == ActionType.LIKE_COMMENT) {
ra.storyId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_STORY_ID));
ra.feedId = c.getString(c.getColumnIndexOrThrow(DatabaseConstants.ACTION_FEED_ID));
@ -406,6 +426,17 @@ public class ReadingAction implements Serializable {
result = response;
break;
case UNSHARE:
StoriesResponse unshareResponse = apiManager.unshareStory(storyId, feedId);
if ((unshareResponse != null) && (unshareResponse.story != null)) {
dbHelper.insertStories(unshareResponse, true);
impact |= NbActivity.UPDATE_SOCIAL;
} else {
com.newsblur.util.Log.i(this.getClass().getName(), "unshare failed to refresh story");
}
result = unshareResponse;
break;
case LIKE_COMMENT:
result = apiManager.favouriteComment(storyId, commentUserId, feedId);
break;
@ -487,9 +518,17 @@ public class ReadingAction implements Serializable {
case SHARE:
if (isFollowup) break; // shares are only placeholders
dbHelper.setStoryShared(storyHash);
dbHelper.setStoryShared(storyHash, true);
dbHelper.insertCommentPlaceholder(storyId, feedId, commentReplyText);
impact |= NbActivity.UPDATE_SOCIAL;
impact |= NbActivity.UPDATE_STORY;
break;
case UNSHARE:
dbHelper.setStoryShared(storyHash, false);
dbHelper.clearSelfComments(storyId);
impact |= NbActivity.UPDATE_SOCIAL;
impact |= NbActivity.UPDATE_STORY;
break;
case LIKE_COMMENT: