Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package in.hopscotch.android.activity;
- import android.content.BroadcastReceiver;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Parcel;
- import android.os.Parcelable;
- import android.support.annotation.VisibleForTesting;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v4.content.LocalBroadcastManager;
- import android.support.v4.view.ViewPager;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Toast;
- import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
- import com.crashlytics.android.Crashlytics;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import butterknife.ButterKnife;
- import in.hopscotch.android.R;
- import in.hopscotch.android.activity.parent.BottombarNavigationActivity;
- import in.hopscotch.android.adapter.HomePagerAdapter;
- import in.hopscotch.android.application.ForceUpdateManager;
- import in.hopscotch.android.application.IntentHelper;
- import in.hopscotch.android.application.SourceTracker;
- import in.hopscotch.android.db.AppRecordData;
- import in.hopscotch.android.fragment.BoutiquesFragment;
- import in.hopscotch.android.fragment.RecentlyFragment;
- import in.hopscotch.android.fragment.base.SlidingTabsColorsFragment;
- import in.hopscotch.android.model.UserStatus;
- import in.hopscotch.android.util.Constants;
- import in.hopscotch.android.util.Util;
- import in.hopscotch.android.widget.SlidingTabLayout;
- import in.hopscotch.android.widget.tansformer.DepthPageTransformer;
- /**
- * Created by Jay.Jiang on 03/02/2015.
- *
- * @author Jay.Jiang
- */
- public class HomePageActivity extends BottombarNavigationActivity {
- private ViewPager mViewPager;
- private BroadcastReceiver mForceUpdateBroadcastReceiver;
- private ForceUpdateManager mForceUpdateManager;
- private Fragment tabFragment;
- //private MenuItem recentItem;
- //private MenuItem searchItem;
- //private final int mCartPosition = 4;
- public ViewPager getViewPager() {
- return mViewPager;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Intent intent = getIntent();
- if (intent != null) {
- super.getIntent().putExtra(BOTTOM_NAVIGATION_ITEM,
- intent.getIntExtra(IntentHelper.HOME_TAB, -1));
- }
- super.onCreate(savedInstanceState);
- AppRecordData.setHomeActivityStartTime(Calendar.getInstance().getTimeInMillis());
- mBlankLayout.setVisibility(View.GONE);
- mForceUpdateManager = new ForceUpdateManager(this);
- mForceUpdateBroadcastReceiver = mForceUpdateManager.getForceUpdateBroadcastReceiver();
- Crashlytics.getInstance().core.setString(Constants.activityName,
- this.getClass().getName());
- removeContentMargin(); // make MainLayout's margin top as 0.
- View view = getLayoutInflater().inflate(R.layout.activity_home, mMainLayout);
- ButterKnife.bind(this, view);
- mViewPager = (ViewPager) findViewById(R.id.viewPager);
- //Custom transformer does not work for versions earlier than JELLY_BEAN_MR2
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
- mViewPager.setPageTransformer(true, new DepthPageTransformer());
- }
- mFloatingButton.setVisibility(View.INVISIBLE);
- bottomNavigation.setVisibility(View.VISIBLE);
- }
- /*
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.include_recently_viewed, menu);
- recentItem = menu.findItem(R.id.actionbar_recently_viewed);
- recentItem.getActionView().setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- MixPanelUtil.getInstance(getApplicationContext()).trackEvent(
- getString(
- R.string.event_navigation), getString(R.string.page_name_discover),
- getString(R.string.prop_type), getString(R.string.type_recently_clicked));
- onOptionsItemSelected(recentItem);
- }
- });
- getMenuInflater().inflate(R.menu.include_search, menu);
- searchItem = menu.findItem(R.id.actionbar_search);
- searchItem.getActionView().setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- MixPanelUtil.getInstance(getApplicationContext()).trackEvent(
- getString(
- R.string.event_navigation), getString(R.string.page_name_discover),
- getString(R.string.prop_type), getString(R.string.type_search_clicked));
- onOptionsItemSelected(searchItem);
- }
- });
- return super.onCreateOptionsMenu(menu);
- }
- */
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- boolean ret = true;
- switch (item.getItemId()) {
- case R.id.actionbar_recently_viewed:
- if(!UserStatus.getInstance().getLoginStatus()) {
- Intent intent = IntentHelper.createIntent(this, JoinActivity.class);
- intent.putExtra(IntentHelper.INTENT_EXTRA_FLAG, JoinActivity.LOGIN_FLAG_GO);
- intent.putExtra(IntentHelper.INTENT_FLAG_SIGN_ACTION,
- Constants.FROM_RECENTLY_VIEWED);
- startActivity(intent);
- }
- else {
- if (tabFragment != null && tabFragment.isAdded()) {
- return true;
- }
- showRecentlyView();
- }
- ret = true;
- break;
- case R.id.actionbar_search_field:
- bottomNavigation.hideBottomNavigation();
- break;
- default:
- ret = super.onOptionsItemSelected(item);
- }
- return ret;
- }
- @Override
- public void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- int selectedTab = intent.getIntExtra(IntentHelper.HOME_TAB, -1);
- if (selectedTab != -1) {
- setCurrentItem(selectedTab);
- Fragment fragment = mTabsFragments.get(selectedTab);
- if (fragment instanceof BoutiquesFragment) {
- //mPullToRefreshContainer.setEnabled(true);
- } else {
- //mPullToRefreshContainer.setEnabled(false);
- }
- }
- if(intent.getBooleanExtra(IntentHelper.GO_TO_RECENTLY_VIEWED, false)) {
- showRecentlyView();
- }
- if(intent.getBooleanExtra(IntentHelper.EXTERNAL_ACTION, false) ||
- intent.getBooleanExtra(IntentHelper.INTERNAL_ACTION, false)) {
- setIntent(intent);
- this.recreate();
- }
- }
- @Override
- public void onBackPressed() {
- if (tabFragment != null && tabFragment.isVisible()) {
- dismissRecentlyView();
- } else {
- super.onBackPressed();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(resultCode == ProductListPageActivity.ADD_TO_CART_RESULT_CODE) {
- Toast.makeText(this, getString(
- R.string.add_to_cart_transition_added_to_cart_text), Toast.LENGTH_SHORT).show();
- }
- if(resultCode == ProductListPageActivity.ADD_TO_WISHLIST_RESULT_CODE) {
- Toast.makeText(this, getString(R.string.added_to_wishlist_text), Toast.LENGTH_SHORT).show();
- }
- if(requestCode == Constants.FROM_RECENT_BOUTIQUES_TO_PLP &&
- resultCode == Constants.FROM_PLP_TO_RECENT_BOUTIQUES) {
- List<Fragment> fragments = getSupportFragmentManager().getFragments();
- if(fragments != null) {
- for(Fragment fragment : fragments) {
- if(fragment instanceof SlidingTabsColorsFragment) {
- ((RecentlyFragment)tabFragment.getChildFragmentManager().getFragments().get(1)).refresh();
- }
- }
- }
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- LocalBroadcastManager.getInstance(this).
- registerReceiver(mForceUpdateBroadcastReceiver,
- new IntentFilter(Constants.FORCE_UPDATE_ACTION));
- }
- @Override
- protected void onStop() {
- LocalBroadcastManager.getInstance(this).unregisterReceiver(mForceUpdateBroadcastReceiver);
- super.onStop();
- }
- @Override
- protected void onResume() {
- super.onResume();
- mForceUpdateManager.resumeShowingForceUpdateDialog();
- long elapsedTime = Calendar.getInstance().getTimeInMillis() -
- AppRecordData.getHomeActivityStartTime();
- if(elapsedTime/(1000*60*24) > 1) {
- if (getSupportFragmentManager().getFragments() != null) {
- for (int i = 0; i < getSupportFragmentManager().getFragments().size(); i++) {
- if (getSupportFragmentManager().getFragments()
- .get(i) instanceof BoutiquesFragment) {
- BoutiquesFragment frag = (BoutiquesFragment)
- getSupportFragmentManager().getFragments().get(i);
- frag.refresh();
- }
- }
- }
- AppRecordData.setHomeActivityStartTime(Calendar.getInstance().getTimeInMillis());
- }
- SourceTracker sourceTracker = SourceTracker.getInstance();
- boolean isExternalAction = false;
- if (getIntent() != null) {
- isExternalAction = getIntent().getBooleanExtra(IntentHelper.EXTERNAL_ACTION, false);
- }
- if (sourceTracker.getNavigationHook() != null && !isExternalAction) {
- sourceTracker.setNavigationHook(null);
- }
- if (this instanceof BottombarNavigationActivity && bottomNavigation != null) {
- ((BottombarNavigationActivity) this).bottomNavigation.setCurrentItem(Util.getSelectedBar());
- }
- if (this instanceof BottombarNavigationActivity) {
- if (!(bottomNavigation.getCurrentItem() == Constants.SEARCH_TAB)) {
- bottomNavigation.restoreBottomNavigation(true);
- setActionBarTitle(Util.getActionBarTitleName(this, bottomNavigation.getCurrentItem()));
- }
- }
- }
- @Override
- protected void onDestroy() {
- //LocalBroadcastManager.getInstance(
- // getApplicationContext()).unregisterReceiver(mCartUpdateBroadcastReceiver);
- HomePagerAdapter adapter = (HomePagerAdapter)mViewPager.getAdapter();
- mViewPager.setAdapter(null);
- adapter = null;
- // Clean up the Bottom Bar Navigation elements ...
- FragmentTransaction fragmenTransaction = fragmentManager.beginTransaction();
- for (int i = 0; i < mTabsFragments.size(); i++) {
- fragmenTransaction.remove(mTabsFragments.get(i));
- }
- fragmenTransaction.commitAllowingStateLoss();
- mTabsFragments.clear();
- mTabsFragments = null;
- bottomNavigationItems.clear();
- bottomNavigationItems = null;
- super.onDestroy();
- }
- private void showRecentlyView() {
- try {
- mRecentlyViewLayout.setVisibility(View.VISIBLE);
- FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
- ft.setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom,
- R.anim.abc_slide_in_bottom,
- R.anim.abc_slide_out_bottom);
- if (tabFragment == null) {
- prepareRecentlyView();
- ft.add(R.id.recently_viewed_container, tabFragment, SlidingTabsColorsFragment.TAG);
- } else {
- ft.attach(tabFragment);
- }
- ft.commit();
- getSupportFragmentManager().executePendingTransactions();
- toolbarContainer.setVisibility(View.GONE);
- Util.setStaueBarColor(this, getResources().getColor(R.color.navy_dark));
- } catch (IllegalStateException e) {
- e.printStackTrace();
- Crashlytics.logException(e);
- }
- }
- public void dismissRecentlyView() {
- mRecentlyViewLayout.setVisibility(View.GONE);
- if (tabFragment != null) {
- getSupportFragmentManager().beginTransaction().detach(tabFragment).commit();
- }
- toolbarContainer.setVisibility(View.VISIBLE);
- Util.setStaueBarColor(this, getResources().getColor(R.color.colorPrimaryDark));
- }
- private void prepareRecentlyView() {
- tabFragment = getSupportFragmentManager()
- .findFragmentByTag(SlidingTabsColorsFragment.TAG);
- if (tabFragment == null) {
- ArrayList<RecentlyFragment.RecentlyViewTab> tabs = new ArrayList<>();
- tabs.add(new RecentlyFragment.RecentlyViewTab(getString(R.string.boutiques),
- getResources().getColor(R.color.white), false));
- tabs.add(new RecentlyFragment.RecentlyViewTab(getString(R.string.products),
- getResources().getColor(R.color.white), true));
- tabFragment = SlidingTabsColorsFragment.newInstance(tabs, R.layout.weight_tab_textview,
- R.id.textView3);
- }
- }
- public AHBottomNavigation getBottomNavigationBar() {
- return bottomNavigation;
- }
- /**
- * This class represents a tab to be displayed by {@link android.support.v4.view.ViewPager}
- * and it's associated {@link SlidingTabLayout}.
- */
- public abstract static class PagerItem implements Parcelable {
- private final CharSequence mTitle;
- private final int mIndicatorColor;
- protected PagerItem(CharSequence title, int indicatorColor) {
- mTitle = title;
- mIndicatorColor = indicatorColor;
- }
- @SuppressWarnings("unused")
- protected PagerItem(final Parcel source) {
- this(source.readString(), source.readInt());
- }
- /**
- * @return A new {@link android.support.v4.app.Fragment} to be displayed by a
- * {@link android.support.v4.view.ViewPager}
- */
- public abstract Fragment createFragment();
- public abstract Fragment getFragment();
- /**
- * @return the title which represents this tab. In this sample this is used directly by
- * {@link android.support.v4.view.PagerAdapter#getPageTitle(int)}
- */
- public CharSequence getTitle() {
- return mTitle;
- }
- /**
- * @return the color to be used for indicator on the {@link SlidingTabLayout}
- */
- public int getIndicatorColor() {
- return mIndicatorColor;
- }
- @Override
- public int describeContents() { return 0; }
- @Override
- public void writeToParcel(final Parcel dest, int flags) {
- dest.writeString(mTitle.toString());
- dest.writeInt(mIndicatorColor);
- }
- }
- @VisibleForTesting
- public void setTabFragment(Fragment tabFragment) {
- this.tabFragment = tabFragment;
- }
- /*
- @VisibleForTesting
- public MenuItem getRecentItem() {
- return recentItem;
- }*/
- @VisibleForTesting
- public UserStatus getUserStatus() {
- return UserStatus.getInstance();
- }
- }
Add Comment
Please, Sign In to add comment