Search in sources :

Example 1 with MainViewPagerScrolled

use of org.wordpress.android.util.CoreEvents.MainViewPagerScrolled in project WordPress-Android by wordpress-mobile.

the class WPMainActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    ProfilingUtils.split("WPMainActivity.onCreate");
    ((WordPress) getApplication()).component().inject(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    mViewPager = (WPViewPager) findViewById(R.id.viewpager_main);
    mViewPager.setOffscreenPageLimit(WPMainTabAdapter.NUM_TABS - 1);
    mTabAdapter = new WPMainTabAdapter(getFragmentManager());
    mViewPager.setAdapter(mTabAdapter);
    mConnectionBar = (TextView) findViewById(R.id.connection_bar);
    mConnectionBar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // slide out the bar on click, then re-check connection after a brief delay
            AniUtils.animateBottomBar(mConnectionBar, false);
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    if (!isFinishing()) {
                        checkConnection();
                    }
                }
            }, 2000);
        }
    });
    mTabLayout = (WPMainTabLayout) findViewById(R.id.tab_layout);
    mTabLayout.createTabs();
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        //  nop
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            //scroll the active fragment's contents to the top when user taps the current tab
            Fragment fragment = mTabAdapter.getFragment(tab.getPosition());
            if (fragment instanceof OnScrollToTopListener) {
                ((OnScrollToTopListener) fragment).onScrollToTop();
            }
        }
    });
    mAppBarElevation = getResources().getDimensionPixelSize(R.dimen.appbar_elevation);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            AppPrefs.setMainTabIndex(position);
            switch(position) {
                case WPMainTabAdapter.TAB_MY_SITE:
                    setTabLayoutElevation(mAppBarElevation);
                    break;
                case WPMainTabAdapter.TAB_READER:
                    setTabLayoutElevation(0);
                    break;
                case WPMainTabAdapter.TAB_ME:
                    setTabLayoutElevation(mAppBarElevation);
                    break;
                case WPMainTabAdapter.TAB_NOTIFS:
                    setTabLayoutElevation(mAppBarElevation);
                    Fragment fragment = mTabAdapter.getFragment(position);
                    if (fragment instanceof OnScrollToTopListener) {
                        ((OnScrollToTopListener) fragment).onScrollToTop();
                    }
                    break;
            }
            trackLastVisibleTab(position, true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        // noop
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // animate its fab to match
            if (position == WPMainTabAdapter.TAB_MY_SITE) {
                EventBus.getDefault().post(new MainViewPagerScrolled(positionOffset));
            }
        }
    });
    if (savedInstanceState == null) {
        if (FluxCUtils.isSignedInWPComOrHasWPOrgSite(mAccountStore, mSiteStore)) {
            // open note detail if activity called from a push, otherwise return to the tab
            // that was showing last time
            boolean openedFromPush = (getIntent() != null && getIntent().getBooleanExtra(ARG_OPENED_FROM_PUSH, false));
            if (openedFromPush) {
                getIntent().putExtra(ARG_OPENED_FROM_PUSH, false);
                if (getIntent().hasExtra(NotificationsPendingDraftsReceiver.POST_ID_EXTRA)) {
                    launchWithPostId(getIntent().getIntExtra(NotificationsPendingDraftsReceiver.POST_ID_EXTRA, 0), getIntent().getBooleanExtra(NotificationsPendingDraftsReceiver.IS_PAGE_EXTRA, false));
                } else {
                    launchWithNoteId();
                }
            } else {
                int position = AppPrefs.getMainTabIndex();
                if (mTabAdapter.isValidPosition(position) && position != mViewPager.getCurrentItem()) {
                    mViewPager.setCurrentItem(position);
                }
                checkMagicLinkSignIn();
            }
        } else {
            ActivityLauncher.showSignInForResult(this);
        }
    }
    startService(new Intent(this, NotificationsScreenLockWatchService.class));
    // ensure the deep linking activity is enabled. It may have been disabled elsewhere and failed to get re-enabled
    WPActivityUtils.enableComponent(this, ReaderPostPagerActivity.class);
    // monitor whether we're not the default app
    trackDefaultApp();
}
Also used : Handler(android.os.Handler) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) SiteSettingsFragment(org.wordpress.android.ui.prefs.SiteSettingsFragment) NotificationsListFragment(org.wordpress.android.ui.notifications.NotificationsListFragment) DialogFragment(android.app.DialogFragment) ReaderPostListFragment(org.wordpress.android.ui.reader.ReaderPostListFragment) Fragment(android.app.Fragment) AppSettingsFragment(org.wordpress.android.ui.prefs.AppSettingsFragment) ViewPager(android.support.v4.view.ViewPager) WPViewPager(org.wordpress.android.widgets.WPViewPager) NotificationsScreenLockWatchService(org.wordpress.android.push.NotificationsScreenLockWatchService) TabLayout(android.support.design.widget.TabLayout) MainViewPagerScrolled(org.wordpress.android.util.CoreEvents.MainViewPagerScrolled)

Aggregations

DialogFragment (android.app.DialogFragment)1 Fragment (android.app.Fragment)1 Intent (android.content.Intent)1 Handler (android.os.Handler)1 TabLayout (android.support.design.widget.TabLayout)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 TextView (android.widget.TextView)1 NotificationsScreenLockWatchService (org.wordpress.android.push.NotificationsScreenLockWatchService)1 NotificationsListFragment (org.wordpress.android.ui.notifications.NotificationsListFragment)1 AppSettingsFragment (org.wordpress.android.ui.prefs.AppSettingsFragment)1 SiteSettingsFragment (org.wordpress.android.ui.prefs.SiteSettingsFragment)1 ReaderPostListFragment (org.wordpress.android.ui.reader.ReaderPostListFragment)1 MainViewPagerScrolled (org.wordpress.android.util.CoreEvents.MainViewPagerScrolled)1 WPViewPager (org.wordpress.android.widgets.WPViewPager)1