Search in sources :

Example 6 with WPViewPager

use of org.wordpress.android.widgets.WPViewPager in project WordPress-Android by wordpress-mobile.

the class EditPostActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((WordPress) getApplication()).component().inject(this);
    mDispatcher.register(this);
    setContentView(R.layout.new_edit_post_activity);
    if (savedInstanceState == null) {
        mSite = (SiteModel) getIntent().getSerializableExtra(WordPress.SITE);
    } else {
        mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE);
    }
    // Check whether to show the visual editor
    PreferenceManager.setDefaultValues(this, R.xml.account_settings, false);
    //AppPrefs.setAztecEditorAvailable(true);
    //AppPrefs.setAztecEditorEnabled(true);
    mShowAztecEditor = AppPrefs.isAztecEditorEnabled();
    mShowNewEditor = AppPrefs.isVisualEditorEnabled();
    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    FragmentManager fragmentManager = getFragmentManager();
    Bundle extras = getIntent().getExtras();
    String action = getIntent().getAction();
    if (savedInstanceState == null) {
        if (!getIntent().hasExtra(EXTRA_POST) || Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action) || NEW_MEDIA_POST.equals(action) || getIntent().hasExtra(EXTRA_IS_QUICKPRESS)) {
            if (getIntent().hasExtra(EXTRA_QUICKPRESS_BLOG_ID)) {
                // QuickPress might want to use a different blog than the current blog
                int localSiteId = getIntent().getIntExtra(EXTRA_QUICKPRESS_BLOG_ID, -1);
                mSite = mSiteStore.getSiteByLocalId(localSiteId);
            }
            mIsPage = extras.getBoolean(EXTRA_IS_PAGE);
            mIsNewPost = true;
            if (mSite == null) {
                showErrorAndFinish(R.string.blog_not_found);
                return;
            }
            if (!mSite.isVisible()) {
                showErrorAndFinish(R.string.error_blog_hidden);
                return;
            }
            // Create a new post
            List<Long> categories = new ArrayList<>();
            categories.add((long) SiteSettingsInterface.getDefaultCategory(WordPress.getContext()));
            String postFormat = SiteSettingsInterface.getDefaultFormat(WordPress.getContext());
            mPost = mPostStore.instantiatePostModel(mSite, mIsPage, categories, postFormat);
        } else if (extras != null) {
            // Load post passed in extras
            mPost = (PostModel) extras.getSerializable(EXTRA_POST);
            if (mPost != null) {
                mOriginalPost = mPost.clone();
                mIsPage = mPost.isPage();
            }
        } else {
            // A postId extra must be passed to this activity
            showErrorAndFinish(R.string.post_not_found);
            return;
        }
    } else {
        mDroppedMediaUris = savedInstanceState.getParcelable(STATE_KEY_DROPPED_MEDIA_URIS);
        if (savedInstanceState.containsKey(STATE_KEY_ORIGINAL_POST)) {
            try {
                mPost = (PostModel) savedInstanceState.getSerializable(STATE_KEY_CURRENT_POST);
                mOriginalPost = (PostModel) savedInstanceState.getSerializable(STATE_KEY_ORIGINAL_POST);
            } catch (ClassCastException e) {
                mPost = null;
            }
        }
        mEditorFragment = (EditorFragmentAbstract) fragmentManager.getFragment(savedInstanceState, STATE_KEY_EDITOR_FRAGMENT);
        if (mEditorFragment instanceof EditorMediaUploadListener) {
            mEditorMediaUploadListener = (EditorMediaUploadListener) mEditorFragment;
        }
    }
    if (mSite == null) {
        ToastUtils.showToast(this, R.string.blog_not_found, ToastUtils.Duration.SHORT);
        finish();
        return;
    }
    if (mHasSetPostContent = mEditorFragment != null) {
        mEditorFragment.setImageLoader(mImageLoader);
    }
    // Ensure we have a valid post
    if (mPost == null) {
        showErrorAndFinish(R.string.post_not_found);
        return;
    }
    if (mIsNewPost) {
        trackEditorCreatedPost(action, getIntent());
    }
    setTitle(StringUtils.unescapeHTML(SiteUtils.getSiteNameOrHomeURL(mSite)));
    mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
    // Set up the ViewPager with the sections adapter.
    mViewPager = (WPViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setPagingEnabled(false);
    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
            if (position == PAGE_CONTENT) {
                setTitle(StringUtils.unescapeHTML(SiteUtils.getSiteNameOrHomeURL(mSite)));
            } else if (position == PAGE_SETTINGS) {
                setTitle(mPost.isPage() ? R.string.page_settings : R.string.post_settings);
                hidePhotoChooser();
            } else if (position == PAGE_PREVIEW) {
                setTitle(mPost.isPage() ? R.string.preview_page : R.string.preview_post);
                hidePhotoChooser();
                savePostAsync(new AfterSavePostListener() {

                    @Override
                    public void onPostSave() {
                        if (mEditPostPreviewFragment != null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    if (mEditPostPreviewFragment != null) {
                                        mEditPostPreviewFragment.loadPost();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    });
    ActivityId.trackLastActivity(ActivityId.POST_EDITOR);
}
Also used : EditorMediaUploadListener(org.wordpress.android.editor.EditorMediaUploadListener) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) WPViewPager(org.wordpress.android.widgets.WPViewPager) ViewPager(android.support.v4.view.ViewPager) FragmentManager(android.app.FragmentManager) PostModel(org.wordpress.android.fluxc.model.PostModel) ActionBar(android.support.v7.app.ActionBar)

Aggregations

ViewPager (android.support.v4.view.ViewPager)6 WPViewPager (org.wordpress.android.widgets.WPViewPager)6 ActionBar (android.support.v7.app.ActionBar)4 View (android.view.View)3 WPViewPagerTransformer (org.wordpress.android.widgets.WPViewPagerTransformer)3 TabLayout (android.support.design.widget.TabLayout)2 TextView (android.widget.TextView)2 DialogFragment (android.app.DialogFragment)1 Fragment (android.app.Fragment)1 FragmentManager (android.app.FragmentManager)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Toolbar (android.support.v7.widget.Toolbar)1 KeyEvent (android.view.KeyEvent)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EditorMediaUploadListener (org.wordpress.android.editor.EditorMediaUploadListener)1