Search in sources :

Example 1 with IllegalEditorStateException

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;
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) PostUploadService(org.wordpress.android.ui.posts.services.PostUploadService) PostActionBuilder(org.wordpress.android.fluxc.generated.PostActionBuilder) MediaActionBuilder(org.wordpress.android.fluxc.generated.MediaActionBuilder) SpannableStringBuilder(android.text.SpannableStringBuilder) IllegalEditorStateException(org.wordpress.android.editor.EditorFragment.IllegalEditorStateException) Intent(android.content.Intent)

Example 2 with IllegalEditorStateException

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());
}
Also used : ToggleButton(android.widget.ToggleButton) IllegalEditorStateException(org.wordpress.android.editor.EditorFragment.IllegalEditorStateException) CountDownLatch(java.util.concurrent.CountDownLatch) View(android.view.View)

Aggregations

IllegalEditorStateException (org.wordpress.android.editor.EditorFragment.IllegalEditorStateException)2 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 AlertDialog (android.support.v7.app.AlertDialog)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 View (android.view.View)1 ToggleButton (android.widget.ToggleButton)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 MediaActionBuilder (org.wordpress.android.fluxc.generated.MediaActionBuilder)1 PostActionBuilder (org.wordpress.android.fluxc.generated.PostActionBuilder)1 PostUploadService (org.wordpress.android.ui.posts.services.PostUploadService)1