Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Fix #85, fix #78: confirm before saving published posts.
Browse files Browse the repository at this point in the history
1. Ask for confirmation before saving changes to published posts
2. Ask to save published posts on tapping "View post on blog"
  • Loading branch information
vickychijwani committed Jul 6, 2015
1 parent a10324c commit fcc877e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import me.vickychijwani.spectre.util.PostUtils;
import me.vickychijwani.spectre.view.fragments.PostEditFragment;
import me.vickychijwani.spectre.view.fragments.PostViewFragment;
import rx.Observable;

public class PostViewActivity extends BaseActivity implements
PostViewFragment.OnEditClickListener,
Expand All @@ -48,7 +49,7 @@ public class PostViewActivity extends BaseActivity implements
private boolean mIsPreviewVisible = false;
private View mFocussedView = null;

private boolean mbPreviewDraft = false;
private boolean mbPreviewPost = false;
private ProgressDialog mProgressDialog;
private Handler mHandler = new Handler(Looper.getMainLooper());
private Runnable mSaveTimeoutRunnable;
Expand All @@ -65,9 +66,9 @@ protected void onCreate(Bundle savedInstanceState) {

mUpClickListener = v -> NavUtils.navigateUpFromSameTask(PostViewActivity.this);
mSaveTimeoutRunnable = () -> {
if (mbPreviewDraft) {
if (mbPreviewPost) {
mProgressDialog.dismiss();
mbPreviewDraft = false;
mbPreviewPost = false;
Toast.makeText(this, R.string.save_post_failed, Toast.LENGTH_LONG).show();
}
};
Expand Down Expand Up @@ -118,31 +119,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void viewPostInBrowser() {
if (Post.DRAFT.equals(mPost.getStatus())) {
mbPreviewDraft = true;
boolean isNetworkCallPending = mPostEditFragment.saveToServerExplicitly();
mbPreviewPost = true;
Observable<Boolean> waitForNetworkObservable = mPostEditFragment.onSaveClicked();
waitForNetworkObservable.subscribe(isNetworkCallPending -> {
if (isNetworkCallPending) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading draft preview...");
mProgressDialog.setMessage(getString(R.string.save_post_progress));
mProgressDialog.setCancelable(false);
mProgressDialog.show();
mHandler.postDelayed(mSaveTimeoutRunnable, 10000);
} else {
mbPreviewDraft = false;
mbPreviewPost = false;
startBrowserActivity(PostUtils.getPostUrl(mPost));
}
} else {
startBrowserActivity(PostUtils.getPostUrl(mPost));
}
});
}

@Subscribe
public void onPostSyncedEvent(PostSyncedEvent event) {
if (event.uuid.equals(mPost.getUuid()) && mbPreviewDraft) {
if (event.uuid.equals(mPost.getUuid()) && mbPreviewPost) {
mHandler.removeCallbacks(mSaveTimeoutRunnable);
startBrowserActivity(PostUtils.getPostUrl(mPost));
mProgressDialog.dismiss();
mbPreviewDraft = false;
mbPreviewPost = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import me.vickychijwani.spectre.view.EditTextActionModeManager;
import me.vickychijwani.spectre.view.PostViewActivity;
import me.vickychijwani.spectre.view.TagsEditText;
import rx.Observable;
import rx.functions.Actions;
import rx.subscriptions.Subscriptions;

public class PostEditFragment extends BaseFragment implements ObservableScrollViewCallbacks,
EditTextActionModeManager.Callbacks {
Expand Down Expand Up @@ -244,7 +247,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
mEditTextActionModeManager.stopActionMode(false);
return true;
case R.id.action_save:
saveToServerExplicitly();
// TODO hate having to do an empty subscribe here
onSaveClicked().subscribe(Actions.empty());
return true;
case R.id.action_publish:
onPublishUnpublishClicked();
Expand All @@ -257,19 +261,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

public boolean saveToServerExplicitly() {
private boolean saveToServerExplicitly() {
return saveToServerExplicitly(null);
}

public boolean saveToServerExplicitly(@Nullable @Post.Status String newStatus) {
private boolean saveToServerExplicitly(@Nullable @Post.Status String newStatus) {
return savePost(true, false, newStatus);
}

public boolean saveToMemory() {
private boolean saveToMemory() {
return savePost(false, true, null);
}

public boolean saveAutomatically() {
private boolean saveAutomatically() {
return savePost(true, true, null);
}

Expand Down Expand Up @@ -307,6 +311,34 @@ private boolean savePost(boolean persistChanges, boolean isAutoSave,
return false;
}

public Observable<Boolean> onSaveClicked() {
// can't use cleverness like !PostUtils.isDirty(mLastSavedPost, mPost) here
// consider: edit published post => hit back to auto-save => open again and hit "Save"
// in this case we will end up not asking for confirmation! here again, we're conflating 2
// kinds of "dirtiness": (1) dirty relative to auto-saved post, and, (2) dirty relative to
// post on server TODO fix this behaviour!
if (Post.DRAFT.equals(mPost.getStatus())) {
return Observable.just(saveToServerExplicitly());
}
return Observable.create((subscriber) -> {
// confirm save for published posts
final AlertDialog alertDialog = new AlertDialog.Builder(mActivity)
.setMessage(getString(R.string.alert_save_msg))
.setPositiveButton(R.string.alert_save_yes, (dialog, which) -> {
subscriber.onNext(saveToServerExplicitly());
subscriber.onCompleted();
})
.setNegativeButton(R.string.alert_save_no, (dialog, which) -> {
dialog.dismiss();
subscriber.onNext(false);
subscriber.onCompleted();
})
.create();
subscriber.add(Subscriptions.create(alertDialog::dismiss));
alertDialog.show();
});
}

private void onPublishUnpublishClicked() {
int msg = R.string.alert_publish;
String targetStatus = Post.PUBLISHED;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
<string name="unpublish">Unpublish</string>
<string name="alert_publish">Publish draft?</string>
<string name="alert_unpublish">Unpublish post?</string>
<string name="alert_save_msg">Publish changes?</string>
<string name="alert_save_yes">Yes, publish</string>
<string name="alert_save_no">Not yet</string>
<string name="discard_changes">Discard changes</string>
<string name="alert_discard_changes_title">Discard ALL changes since you opened this post?</string>
<string name="alert_discard_changes_msg">There\'s no going back!</string>
<string name="discard">Discard</string>
<string name="view_homepage">View homepage</string>
<string name="view_post_on_blog">View post on blog</string>
<string name="save_post_progress">Saving post…</string>
<string name="save_post_failed">Couldn\'t save post</string>

<!-- about -->
Expand Down

0 comments on commit fcc877e

Please sign in to comment.