anilchahal7

HomePageActivity

Jun 1st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.70 KB | None | 0 0
  1. package in.hopscotch.android.activity;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.os.Parcel;
  9. import android.os.Parcelable;
  10. import android.support.annotation.VisibleForTesting;
  11. import android.support.v4.app.Fragment;
  12. import android.support.v4.app.FragmentTransaction;
  13. import android.support.v4.content.LocalBroadcastManager;
  14. import android.support.v4.view.ViewPager;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.Toast;
  18.  
  19. import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
  20. import com.crashlytics.android.Crashlytics;
  21.  
  22. import java.util.ArrayList;
  23. import java.util.Calendar;
  24. import java.util.List;
  25.  
  26. import butterknife.ButterKnife;
  27. import in.hopscotch.android.R;
  28. import in.hopscotch.android.activity.parent.BottombarNavigationActivity;
  29. import in.hopscotch.android.adapter.HomePagerAdapter;
  30. import in.hopscotch.android.application.ForceUpdateManager;
  31. import in.hopscotch.android.application.IntentHelper;
  32. import in.hopscotch.android.application.SourceTracker;
  33. import in.hopscotch.android.db.AppRecordData;
  34. import in.hopscotch.android.fragment.BoutiquesFragment;
  35. import in.hopscotch.android.fragment.RecentlyFragment;
  36. import in.hopscotch.android.fragment.base.SlidingTabsColorsFragment;
  37. import in.hopscotch.android.model.UserStatus;
  38. import in.hopscotch.android.util.Constants;
  39. import in.hopscotch.android.util.Util;
  40. import in.hopscotch.android.widget.SlidingTabLayout;
  41. import in.hopscotch.android.widget.tansformer.DepthPageTransformer;
  42.  
  43.  
  44. /**
  45. * Created by Jay.Jiang on 03/02/2015.
  46. *
  47. * @author Jay.Jiang
  48. */
  49. public class HomePageActivity extends BottombarNavigationActivity {
  50.  
  51. private ViewPager mViewPager;
  52. private BroadcastReceiver mForceUpdateBroadcastReceiver;
  53. private ForceUpdateManager mForceUpdateManager;
  54. private Fragment tabFragment;
  55. //private MenuItem recentItem;
  56. //private MenuItem searchItem;
  57. //private final int mCartPosition = 4;
  58.  
  59. public ViewPager getViewPager() {
  60. return mViewPager;
  61. }
  62.  
  63.  
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. Intent intent = getIntent();
  67. if (intent != null) {
  68. super.getIntent().putExtra(BOTTOM_NAVIGATION_ITEM,
  69. intent.getIntExtra(IntentHelper.HOME_TAB, -1));
  70. }
  71. super.onCreate(savedInstanceState);
  72. AppRecordData.setHomeActivityStartTime(Calendar.getInstance().getTimeInMillis());
  73. mBlankLayout.setVisibility(View.GONE);
  74. mForceUpdateManager = new ForceUpdateManager(this);
  75. mForceUpdateBroadcastReceiver = mForceUpdateManager.getForceUpdateBroadcastReceiver();
  76. Crashlytics.getInstance().core.setString(Constants.activityName,
  77. this.getClass().getName());
  78. removeContentMargin(); // make MainLayout's margin top as 0.
  79. View view = getLayoutInflater().inflate(R.layout.activity_home, mMainLayout);
  80. ButterKnife.bind(this, view);
  81. mViewPager = (ViewPager) findViewById(R.id.viewPager);
  82. //Custom transformer does not work for versions earlier than JELLY_BEAN_MR2
  83. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  84. mViewPager.setPageTransformer(true, new DepthPageTransformer());
  85. }
  86. mFloatingButton.setVisibility(View.INVISIBLE);
  87. bottomNavigation.setVisibility(View.VISIBLE);
  88. }
  89.  
  90. /*
  91. @Override
  92. public boolean onCreateOptionsMenu(Menu menu) {
  93. getMenuInflater().inflate(R.menu.include_recently_viewed, menu);
  94. recentItem = menu.findItem(R.id.actionbar_recently_viewed);
  95. recentItem.getActionView().setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View v) {
  98. MixPanelUtil.getInstance(getApplicationContext()).trackEvent(
  99. getString(
  100. R.string.event_navigation), getString(R.string.page_name_discover),
  101. getString(R.string.prop_type), getString(R.string.type_recently_clicked));
  102. onOptionsItemSelected(recentItem);
  103. }
  104. });
  105.  
  106. getMenuInflater().inflate(R.menu.include_search, menu);
  107. searchItem = menu.findItem(R.id.actionbar_search);
  108. searchItem.getActionView().setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View v) {
  111. MixPanelUtil.getInstance(getApplicationContext()).trackEvent(
  112. getString(
  113. R.string.event_navigation), getString(R.string.page_name_discover),
  114. getString(R.string.prop_type), getString(R.string.type_search_clicked));
  115. onOptionsItemSelected(searchItem);
  116. }
  117. });
  118. return super.onCreateOptionsMenu(menu);
  119. }
  120. */
  121. @Override
  122. public boolean onOptionsItemSelected(MenuItem item) {
  123. boolean ret = true;
  124. switch (item.getItemId()) {
  125. case R.id.actionbar_recently_viewed:
  126. if(!UserStatus.getInstance().getLoginStatus()) {
  127. Intent intent = IntentHelper.createIntent(this, JoinActivity.class);
  128. intent.putExtra(IntentHelper.INTENT_EXTRA_FLAG, JoinActivity.LOGIN_FLAG_GO);
  129. intent.putExtra(IntentHelper.INTENT_FLAG_SIGN_ACTION,
  130. Constants.FROM_RECENTLY_VIEWED);
  131. startActivity(intent);
  132. }
  133. else {
  134. if (tabFragment != null && tabFragment.isAdded()) {
  135. return true;
  136. }
  137.  
  138. showRecentlyView();
  139. }
  140. ret = true;
  141. break;
  142. case R.id.actionbar_search_field:
  143. bottomNavigation.hideBottomNavigation();
  144. break;
  145. default:
  146. ret = super.onOptionsItemSelected(item);
  147. }
  148. return ret;
  149. }
  150.  
  151. @Override
  152. public void onNewIntent(Intent intent) {
  153. super.onNewIntent(intent);
  154. int selectedTab = intent.getIntExtra(IntentHelper.HOME_TAB, -1);
  155. if (selectedTab != -1) {
  156. setCurrentItem(selectedTab);
  157. Fragment fragment = mTabsFragments.get(selectedTab);
  158. if (fragment instanceof BoutiquesFragment) {
  159. //mPullToRefreshContainer.setEnabled(true);
  160. } else {
  161. //mPullToRefreshContainer.setEnabled(false);
  162. }
  163. }
  164. if(intent.getBooleanExtra(IntentHelper.GO_TO_RECENTLY_VIEWED, false)) {
  165. showRecentlyView();
  166. }
  167. if(intent.getBooleanExtra(IntentHelper.EXTERNAL_ACTION, false) ||
  168. intent.getBooleanExtra(IntentHelper.INTERNAL_ACTION, false)) {
  169. setIntent(intent);
  170. this.recreate();
  171. }
  172. }
  173.  
  174. @Override
  175. public void onBackPressed() {
  176. if (tabFragment != null && tabFragment.isVisible()) {
  177. dismissRecentlyView();
  178. } else {
  179. super.onBackPressed();
  180. }
  181. }
  182.  
  183. @Override
  184. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  185. super.onActivityResult(requestCode, resultCode, data);
  186. if(resultCode == ProductListPageActivity.ADD_TO_CART_RESULT_CODE) {
  187. Toast.makeText(this, getString(
  188. R.string.add_to_cart_transition_added_to_cart_text), Toast.LENGTH_SHORT).show();
  189. }
  190. if(resultCode == ProductListPageActivity.ADD_TO_WISHLIST_RESULT_CODE) {
  191. Toast.makeText(this, getString(R.string.added_to_wishlist_text), Toast.LENGTH_SHORT).show();
  192. }
  193. if(requestCode == Constants.FROM_RECENT_BOUTIQUES_TO_PLP &&
  194. resultCode == Constants.FROM_PLP_TO_RECENT_BOUTIQUES) {
  195. List<Fragment> fragments = getSupportFragmentManager().getFragments();
  196. if(fragments != null) {
  197. for(Fragment fragment : fragments) {
  198. if(fragment instanceof SlidingTabsColorsFragment) {
  199. ((RecentlyFragment)tabFragment.getChildFragmentManager().getFragments().get(1)).refresh();
  200. }
  201. }
  202. }
  203. }
  204. }
  205.  
  206. @Override
  207. protected void onStart() {
  208. super.onStart();
  209. LocalBroadcastManager.getInstance(this).
  210. registerReceiver(mForceUpdateBroadcastReceiver,
  211. new IntentFilter(Constants.FORCE_UPDATE_ACTION));
  212. }
  213.  
  214. @Override
  215. protected void onStop() {
  216. LocalBroadcastManager.getInstance(this).unregisterReceiver(mForceUpdateBroadcastReceiver);
  217. super.onStop();
  218. }
  219.  
  220. @Override
  221. protected void onResume() {
  222. super.onResume();
  223. mForceUpdateManager.resumeShowingForceUpdateDialog();
  224. long elapsedTime = Calendar.getInstance().getTimeInMillis() -
  225. AppRecordData.getHomeActivityStartTime();
  226. if(elapsedTime/(1000*60*24) > 1) {
  227. if (getSupportFragmentManager().getFragments() != null) {
  228. for (int i = 0; i < getSupportFragmentManager().getFragments().size(); i++) {
  229. if (getSupportFragmentManager().getFragments()
  230. .get(i) instanceof BoutiquesFragment) {
  231. BoutiquesFragment frag = (BoutiquesFragment)
  232. getSupportFragmentManager().getFragments().get(i);
  233. frag.refresh();
  234. }
  235. }
  236. }
  237. AppRecordData.setHomeActivityStartTime(Calendar.getInstance().getTimeInMillis());
  238. }
  239. SourceTracker sourceTracker = SourceTracker.getInstance();
  240. boolean isExternalAction = false;
  241. if (getIntent() != null) {
  242. isExternalAction = getIntent().getBooleanExtra(IntentHelper.EXTERNAL_ACTION, false);
  243. }
  244. if (sourceTracker.getNavigationHook() != null && !isExternalAction) {
  245. sourceTracker.setNavigationHook(null);
  246. }
  247. if (this instanceof BottombarNavigationActivity && bottomNavigation != null) {
  248. ((BottombarNavigationActivity) this).bottomNavigation.setCurrentItem(Util.getSelectedBar());
  249. }
  250.  
  251. if (this instanceof BottombarNavigationActivity) {
  252. if (!(bottomNavigation.getCurrentItem() == Constants.SEARCH_TAB)) {
  253. bottomNavigation.restoreBottomNavigation(true);
  254. setActionBarTitle(Util.getActionBarTitleName(this, bottomNavigation.getCurrentItem()));
  255. }
  256. }
  257.  
  258. }
  259.  
  260. @Override
  261. protected void onDestroy() {
  262. //LocalBroadcastManager.getInstance(
  263. // getApplicationContext()).unregisterReceiver(mCartUpdateBroadcastReceiver);
  264. HomePagerAdapter adapter = (HomePagerAdapter)mViewPager.getAdapter();
  265. mViewPager.setAdapter(null);
  266. adapter = null;
  267.  
  268. // Clean up the Bottom Bar Navigation elements ...
  269. FragmentTransaction fragmenTransaction = fragmentManager.beginTransaction();
  270. for (int i = 0; i < mTabsFragments.size(); i++) {
  271. fragmenTransaction.remove(mTabsFragments.get(i));
  272. }
  273. fragmenTransaction.commitAllowingStateLoss();
  274. mTabsFragments.clear();
  275. mTabsFragments = null;
  276. bottomNavigationItems.clear();
  277. bottomNavigationItems = null;
  278.  
  279. super.onDestroy();
  280. }
  281.  
  282. private void showRecentlyView() {
  283. try {
  284. mRecentlyViewLayout.setVisibility(View.VISIBLE);
  285. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  286. ft.setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom,
  287. R.anim.abc_slide_in_bottom,
  288. R.anim.abc_slide_out_bottom);
  289. if (tabFragment == null) {
  290. prepareRecentlyView();
  291. ft.add(R.id.recently_viewed_container, tabFragment, SlidingTabsColorsFragment.TAG);
  292. } else {
  293. ft.attach(tabFragment);
  294. }
  295. ft.commit();
  296. getSupportFragmentManager().executePendingTransactions();
  297. toolbarContainer.setVisibility(View.GONE);
  298. Util.setStaueBarColor(this, getResources().getColor(R.color.navy_dark));
  299. } catch (IllegalStateException e) {
  300. e.printStackTrace();
  301. Crashlytics.logException(e);
  302. }
  303. }
  304.  
  305. public void dismissRecentlyView() {
  306. mRecentlyViewLayout.setVisibility(View.GONE);
  307. if (tabFragment != null) {
  308. getSupportFragmentManager().beginTransaction().detach(tabFragment).commit();
  309. }
  310. toolbarContainer.setVisibility(View.VISIBLE);
  311. Util.setStaueBarColor(this, getResources().getColor(R.color.colorPrimaryDark));
  312. }
  313.  
  314. private void prepareRecentlyView() {
  315. tabFragment = getSupportFragmentManager()
  316. .findFragmentByTag(SlidingTabsColorsFragment.TAG);
  317. if (tabFragment == null) {
  318. ArrayList<RecentlyFragment.RecentlyViewTab> tabs = new ArrayList<>();
  319. tabs.add(new RecentlyFragment.RecentlyViewTab(getString(R.string.boutiques),
  320. getResources().getColor(R.color.white), false));
  321. tabs.add(new RecentlyFragment.RecentlyViewTab(getString(R.string.products),
  322. getResources().getColor(R.color.white), true));
  323. tabFragment = SlidingTabsColorsFragment.newInstance(tabs, R.layout.weight_tab_textview,
  324. R.id.textView3);
  325. }
  326. }
  327.  
  328. public AHBottomNavigation getBottomNavigationBar() {
  329. return bottomNavigation;
  330. }
  331.  
  332. /**
  333. * This class represents a tab to be displayed by {@link android.support.v4.view.ViewPager}
  334. * and it's associated {@link SlidingTabLayout}.
  335. */
  336. public abstract static class PagerItem implements Parcelable {
  337.  
  338. private final CharSequence mTitle;
  339. private final int mIndicatorColor;
  340.  
  341. protected PagerItem(CharSequence title, int indicatorColor) {
  342. mTitle = title;
  343. mIndicatorColor = indicatorColor;
  344. }
  345.  
  346. @SuppressWarnings("unused")
  347. protected PagerItem(final Parcel source) {
  348. this(source.readString(), source.readInt());
  349. }
  350.  
  351. /**
  352. * @return A new {@link android.support.v4.app.Fragment} to be displayed by a
  353. * {@link android.support.v4.view.ViewPager}
  354. */
  355. public abstract Fragment createFragment();
  356.  
  357. public abstract Fragment getFragment();
  358.  
  359. /**
  360. * @return the title which represents this tab. In this sample this is used directly by
  361. * {@link android.support.v4.view.PagerAdapter#getPageTitle(int)}
  362. */
  363. public CharSequence getTitle() {
  364. return mTitle;
  365. }
  366.  
  367. /**
  368. * @return the color to be used for indicator on the {@link SlidingTabLayout}
  369. */
  370. public int getIndicatorColor() {
  371. return mIndicatorColor;
  372. }
  373.  
  374. @Override
  375. public int describeContents() { return 0; }
  376.  
  377. @Override
  378. public void writeToParcel(final Parcel dest, int flags) {
  379. dest.writeString(mTitle.toString());
  380. dest.writeInt(mIndicatorColor);
  381. }
  382. }
  383.  
  384. @VisibleForTesting
  385. public void setTabFragment(Fragment tabFragment) {
  386. this.tabFragment = tabFragment;
  387. }
  388.  
  389. /*
  390. @VisibleForTesting
  391. public MenuItem getRecentItem() {
  392. return recentItem;
  393. }*/
  394.  
  395. @VisibleForTesting
  396. public UserStatus getUserStatus() {
  397. return UserStatus.getInstance();
  398. }
  399. }
Add Comment
Please, Sign In to add comment