use of org.wordpress.android.editor.EditorFragment.IllegalEditorStateException in project WordPress-Android by wordpress-mobile.
the class EditPostActivity method publishPost.
private boolean publishPost() {
if (!NetworkUtils.isNetworkAvailable(this)) {
ToastUtils.showToast(this, R.string.error_publish_no_network, Duration.SHORT);
return false;
}
// Show an Alert Dialog asking the user if he wants to remove all failed media before upload
if (mEditorFragment.hasFailedMediaUploads()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.editor_toast_failed_uploads).setPositiveButton(R.string.editor_remove_failed_uploads, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Clear failed uploads
mEditorFragment.removeAllFailedMediaUploads();
}
}).setNegativeButton(android.R.string.cancel, null);
builder.create().show();
return true;
}
// Update post, save to db and publish in its own Thread, because 1. update can be pretty slow with a lot of
// text 2. better not to call `updatePostObject()` from the UI thread due to weird thread blocking behavior
// on API 16 with the visual editor.
new Thread(new Runnable() {
@Override
public void run() {
boolean isFirstTimePublish = false;
if (PostStatus.fromPost(mPost) == PostStatus.PUBLISHED && (mPost.isLocalDraft() || PostStatus.fromPost(mOriginalPost) == PostStatus.DRAFT)) {
isFirstTimePublish = true;
}
try {
updatePostObject(false);
} catch (IllegalEditorStateException e) {
AppLog.e(T.EDITOR, "Impossible to save and publish the post, we weren't able to update it.");
return;
}
savePostToDb();
// If the post is empty, don't publish
if (!PostUtils.isPublishable(mPost)) {
mHandler.post(new Runnable() {
@Override
public void run() {
ToastUtils.showToast(EditPostActivity.this, R.string.error_publish_empty_post, Duration.SHORT);
}
});
return;
}
PostUtils.trackSavePostAnalytics(mPost, mSiteStore.getSiteByLocalId(mPost.getLocalSiteId()));
if (isFirstTimePublish) {
PostUploadService.addPostToUploadAndTrackAnalytics(mPost);
} else {
PostUploadService.addPostToUpload(mPost);
}
PostUploadService.setLegacyMode(!mShowNewEditor && !mShowAztecEditor);
startService(new Intent(EditPostActivity.this, PostUploadService.class));
PendingDraftsNotificationsUtils.cancelPendingDraftAlarms(EditPostActivity.this, mPost.getId());
setResult(RESULT_OK);
finish();
}
}).start();
return true;
}
use of org.wordpress.android.editor.EditorFragment.IllegalEditorStateException in project WordPress-Android by wordpress-mobile.
the class EditorFragmentTest method testHtmlModeToggleTextTransfer.
public void testHtmlModeToggleTextTransfer() throws InterruptedException, IllegalEditorStateException {
waitForOnDomLoaded();
final View view = mFragment.getView();
if (view == null) {
throw (new IllegalStateException("Fragment view is empty"));
}
final ToggleButton htmlButton = (ToggleButton) view.findViewById(R.id.format_bar_button_html);
String content = mFragment.getContent().toString();
final SourceViewEditText titleText = (SourceViewEditText) view.findViewById(R.id.sourceview_title);
final SourceViewEditText contentText = (SourceViewEditText) view.findViewById(R.id.sourceview_content);
// -- Check that title and content text is properly loaded into the EditTexts when switching to HTML mode
final CountDownLatch uiThreadLatch1 = new CountDownLatch(1);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Turn on HTML mode
htmlButton.performClick();
uiThreadLatch1.countDown();
}
});
uiThreadLatch1.await();
waitFor(500);
// The HTML mode fields should be populated with the raw HTML loaded into the WebView on load
// (see MockEditorActivity)
assertEquals("A title", titleText.getText().toString());
assertEquals(content, contentText.getText().toString());
// -- Check that the title and content text is updated in the WebView when switching back from HTML mode
final CountDownLatch uiThreadLatch2 = new CountDownLatch(1);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
titleText.setText("new title");
contentText.setText("new <b>content</b>");
// Check that getTitle() and getContent() return latest version even in HTML mode
try {
assertEquals("new title", mFragment.getTitle());
assertEquals("new <b>content</b>", mFragment.getContent());
} catch (IllegalEditorStateException e) {
throw new RuntimeException();
}
// Turn off HTML mode
htmlButton.performClick();
uiThreadLatch2.countDown();
}
});
uiThreadLatch2.await();
// Wait for JS to update the title/content
waitFor(300);
assertEquals("new title", mFragment.getTitle());
assertEquals("new <b>content</b>", mFragment.getContent());
}
Aggregations