anilchahal7

Untitled

Jun 15th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.73 KB | None | 0 0
  1. package in.hopscotch.android.fragment;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8. import android.text.TextUtils;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.AbsListView;
  13. import android.widget.ExpandableListView;
  14. import android.widget.RelativeLayout;
  15. import android.widget.TextView;
  16.  
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19.  
  20. import butterknife.ButterKnife;
  21. import in.hopscotch.android.BuildConfig;
  22. import in.hopscotch.android.R;
  23. import in.hopscotch.android.activity.FavouriteBrandsActivity;
  24. import in.hopscotch.android.activity.HSWebviewActivity;
  25. import in.hopscotch.android.activity.InviteFriendsActivity;
  26. import in.hopscotch.android.activity.JoinActivity;
  27. import in.hopscotch.android.activity.MyKidsActivity;
  28. import in.hopscotch.android.activity.OrdersActivity;
  29. import in.hopscotch.android.activity.StoredCardsActivity;
  30. import in.hopscotch.android.activity.parent.BottombarNavigationActivity;
  31. import in.hopscotch.android.adapter.AboutMeExpandAdapter;
  32. import in.hopscotch.android.api.RequestFactory;
  33. import in.hopscotch.android.api.listener.ApiErrorListener;
  34. import in.hopscotch.android.api.listener.AutoLoginRequiredListener;
  35. import in.hopscotch.android.api.response.ActionResponse;
  36. import in.hopscotch.android.api.response.MyCreditsResponse;
  37. import in.hopscotch.android.application.IntentHelper;
  38. import in.hopscotch.android.listener.AbsListViewOnScrollListener;
  39. import in.hopscotch.android.model.UserStatus;
  40. import in.hopscotch.android.util.Constants;
  41. import in.hopscotch.android.util.DialogUtil;
  42. import in.hopscotch.android.util.Util;
  43. import in.hopscotch.android.volley.widget.VolleyCircleImageView;
  44. import in.hopscotch.android.widget.NavigationView;
  45. import io.branch.referral.Branch;
  46.  
  47. /**
  48. * Created by Anil Chahal on 4/5/16.
  49. */
  50.  
  51. public class AboutMeFragment extends Fragment {
  52.  
  53. private NavigationView aboutMeNavigationList;
  54.  
  55. private LayoutInflater mInflater;
  56.  
  57. private TextView userNameView, signInBenefits;
  58.  
  59. private AboutMeExpandAdapter adapter;
  60.  
  61. private ProgressDialog progressDialog;
  62.  
  63. private RelativeLayout aboutMeHeaderLayout;
  64.  
  65. @Override
  66. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  67. Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. View view = inflater.inflate(R.layout.fragment_aboutme, container, false);
  70. aboutMeNavigationList = ButterKnife.findById(view, R.id.aboutMeNavigationList);
  71.  
  72. adapter = new AboutMeExpandAdapter(getActivity());
  73.  
  74. mInflater = LayoutInflater.from(view.getContext());
  75. setAboutMeHeaderView();
  76. setAboutMeFooterView();
  77. aboutMeNavigationList.setAdapter(adapter);
  78.  
  79. aboutMeNavigationList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  80. @Override
  81. public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
  82. int childPosition, long id) {
  83. switch (groupPosition) {
  84. case 0:
  85. if (!UserStatus.getInstance().getLoginStatus()) {
  86. Intent i = IntentHelper.createIntent(getActivity(), JoinActivity.class);
  87. int fromActivity = Constants.NORMAL_SIGN_FLAG + childPosition + 1;
  88. i.putExtra(IntentHelper.INTENT_FLAG_SIGN_ACTION, fromActivity);
  89. startActivity(i);
  90. return true;
  91. }
  92. switch (childPosition) {
  93. case 0: // Open Order Activity ...
  94. if(!(getActivity() instanceof OrdersActivity)) {
  95. startActivity(new Intent(getActivity(), OrdersActivity.class));
  96. }
  97. break;
  98. case 1: // Open Stored Cards ...
  99. if(!(getActivity() instanceof StoredCardsActivity)) {
  100. startActivity(new Intent(getActivity(), StoredCardsActivity.class));
  101. }
  102. break;
  103. case 2: // My kids ...
  104. if(!(getActivity() instanceof MyKidsActivity)) {
  105. startActivity(new Intent(getActivity(), MyKidsActivity.class));
  106. }
  107. break;
  108. case 3: // Favourite Brands ...
  109. if(!(getActivity() instanceof FavouriteBrandsActivity)) {
  110. startActivity(new Intent(getActivity(), FavouriteBrandsActivity.class));
  111. }
  112. break;
  113. case 4: // Invite Friends ...
  114. if(!(getActivity() instanceof InviteFriendsActivity)) {
  115. startActivity(new Intent(getActivity(), InviteFriendsActivity.class));
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. break;
  122. case 1:
  123. switch (childPosition) {
  124. case 0: // Help ...
  125. Util.sendEmailTo(getActivity());
  126. break;
  127. case 1: // Share App ...
  128. Util.invokeShareDialog(getActivity(), getString(R.string.share_app_message), "");
  129. break;
  130. case 2: // Rate App ...
  131. Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName());
  132. startActivity(new Intent(Intent.ACTION_VIEW, uri));
  133. break;
  134. default:
  135. break;
  136. }
  137. break;
  138. case 2: // About Hopscotch & Terms and Conditions ...
  139. Intent webViewIntent = new Intent(getActivity(),
  140. HSWebviewActivity.class);
  141.  
  142. webViewIntent.putExtra(IntentHelper.WEB_URL,getString(
  143. childPosition == 0 ? R.string.drawer_about_us_link
  144. : R.string.drawer_terms_and_conditions_link));
  145.  
  146. webViewIntent.putExtra(IntentHelper.INTENT_EXTRA_TITLE,
  147. childPosition == 0 ? getString(R.string.about_hopscotch) :
  148. getString(R.string.terms_and_conditions));
  149.  
  150. startActivity(webViewIntent);
  151. break;
  152. case 3: // This is just for providing Spacing between last two rows ...
  153. break;
  154. case 4:
  155. if (UserStatus.getInstance().getLoginStatus()) {
  156. showProgress();
  157. RequestFactory.createGetApiRequest(ActionResponse.class,
  158. R.string.api_logout,
  159. null,
  160. new Response.Listener<ActionResponse>() {
  161. @Override
  162. public void onResponse(ActionResponse response) {
  163. dismissProgress();
  164. if (response != null && response.action
  165. .equals(ActionResponse.SUCCESS)) {
  166. UserStatus.getInstance().customerLogout(
  167. getActivity());
  168. Util.startGcmRegistrationService(
  169. getActivity());
  170. Branch.getInstance().logout();
  171. ((BottombarNavigationActivity) getActivity()).
  172. bottomNavigation.setCurrentItem(0);
  173. updateSignInSignOutStatus(getResources().
  174. getString(R.string.hello_there));
  175. }
  176. else {
  177. Util.showErrorDialog(getActivity(), response);
  178. }
  179. }
  180. },
  181. new ApiErrorListener(getActivity()) {
  182. @Override
  183. public void onOtherErrorResponse(VolleyError error) {
  184. DialogUtil.showOneButtonAlert(getActivity(),
  185. getActivity().getString(
  186. R.string.sign_out_failed));
  187. }
  188. });
  189. } else {
  190. Intent intent = IntentHelper.createIntent(getActivity(), JoinActivity.class);
  191. startActivity(intent);
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. return true;
  198. }
  199. });
  200.  
  201. aboutMeNavigationList.setOnScrollListener(new AbsListViewOnScrollListener() {
  202. @Override
  203. public void onScrollUp() {
  204. ((BottombarNavigationActivity)getActivity()).restoreBottomNavigationBar(true);
  205. }
  206. @Override
  207. public void onScrollDown() {
  208. ((BottombarNavigationActivity)getActivity()).hideBottomNavigationBar();
  209. }
  210. int last = 0;
  211. boolean control = true;
  212. @Override
  213. public void onScroll(AbsListView view, int firstVisibleItem,
  214. int visibleItemCount, int totalItemCount) {
  215. if (firstVisibleItem < last && !control) {
  216. onScrollUp();
  217. control = true;
  218. } else if (firstVisibleItem > last && control) {
  219. onScrollDown();
  220. control = false;
  221. }
  222. last = firstVisibleItem;
  223. }
  224. });
  225. return view;
  226. }
  227.  
  228. private void setAboutMeHeaderView() {
  229. aboutMeNavigationList.addHeaderView(
  230. mInflater.inflate(R.layout.aboutme_header_layout, aboutMeNavigationList, false),
  231. null, false);
  232. aboutMeHeaderLayout = ButterKnife.findById(aboutMeNavigationList, R.id.aboutMeHeaderLayout);
  233. aboutMeHeaderLayout.setOnClickListener(new View.OnClickListener() {
  234. @Override
  235. public void onClick(View view) {
  236. if (!UserStatus.getInstance().getLoginStatus()) {
  237. Intent i = IntentHelper.createIntent(getActivity(), JoinActivity.class);
  238. startActivity(i);
  239. }
  240. }
  241. });
  242. userNameView = (TextView) aboutMeNavigationList.findViewById(R.id.userNameView);
  243. signInBenefits = (TextView) aboutMeNavigationList.findViewById(R.id.signInBenefits);
  244. updateHeaderLoginStatus();
  245.  
  246. VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
  247. findViewById(R.id.userPortrait);
  248. imageView.setDefaultImage(R.drawable.menuuser);
  249. if(UserStatus.getInstance().getLoginStatus()) {
  250. if(UserStatus.getInstance().getUserProfilePhoto() != null &&
  251. UserStatus.getInstance().getUserProfilePhoto().trim().length() > 0) {
  252. imageView.setImageUrl(UserStatus.getInstance().getUserProfilePhoto());
  253. } else {
  254. imageView.setImageResource(R.drawable.menuuser);
  255. }
  256. } else {
  257. imageView.setImageResource(R.drawable.menuuser);
  258. }
  259. }
  260.  
  261. private void updateHeaderLoginStatus() {
  262. if (UserStatus.getInstance().getLoginStatus()) {
  263. VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
  264. findViewById(R.id.userPortrait);
  265. if(UserStatus.getInstance().getUserProfilePhoto() != null &&
  266. UserStatus.getInstance().getUserProfilePhoto().trim().length() > 0) {
  267. imageView.setImageUrl(UserStatus.getInstance().getUserProfilePhoto());
  268. } else {
  269. imageView.setImageResource(R.drawable.menuuser);
  270. }
  271. if (TextUtils.isEmpty(UserStatus.getInstance().getFirstName()) &&
  272. TextUtils.isEmpty(UserStatus.getInstance().getLastName())) {
  273. userNameView.setText(R.string.hello_there);
  274. } else {
  275. userNameView.setText("Hello " + UserStatus.getInstance().getFirstName() + " " +
  276. UserStatus.getInstance().getLastName() + "!");
  277. userNameView.setTextColor(getResources().getColor(R.color.black));
  278. }
  279. signInBenefits.setVisibility(View.INVISIBLE);
  280. } else {
  281. userNameView.setText(getString(R.string.drawer_sign_in_now));
  282. userNameView.setTextColor(getResources().getColor(R.color.colorPrimary));
  283. signInBenefits.setVisibility(View.VISIBLE);
  284. signInBenefits.setText(getResources().getText(R.string.signInBenefits));
  285. VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
  286. findViewById(R.id.userPortrait);
  287. imageView.setImageResource(R.drawable.menuuser);
  288. }
  289. }
  290.  
  291. private void updateSignInSignOutStatus(String actionBarTitleName) {
  292. updateHeaderLoginStatus();
  293. adapter.notifyDataSetChanged();
  294. }
  295.  
  296. private void setAboutMeFooterView() {
  297. aboutMeNavigationList.addFooterView(
  298. mInflater.inflate(R.layout.aboutme_footer_layout, aboutMeNavigationList, false), null,
  299. false);
  300. TextView versionView = (TextView) aboutMeNavigationList.findViewById(R.id.versionTextView);
  301. String versionText = getResources().getText(R.string.version_label) + " " +
  302. BuildConfig.VERSION_NAME;
  303. if (BuildConfig.DEBUG) {
  304. versionView.setText(versionText + "\n\n" + BuildConfig.CURRENT_DATE);
  305. } else {
  306. versionView.setText(versionText);
  307. }
  308. }
  309.  
  310. public void showProgress() {
  311. if (null == progressDialog) {
  312. progressDialog = new ProgressDialog(getActivity(), R.style.AppDialogThemeTransparent);
  313. progressDialog.setCancelable(true);
  314. progressDialog.setCanceledOnTouchOutside(false);
  315.  
  316. }
  317. try {
  318. progressDialog.show();
  319. progressDialog.setContentView(R.layout.my_progress);
  320. } catch (Exception e) {
  321. e.printStackTrace();
  322. }
  323. }
  324.  
  325. public void dismissProgress() {
  326. try {
  327. if (null != progressDialog && progressDialog.isShowing()) {
  328. progressDialog.dismiss();
  329. }
  330. } catch (IllegalStateException e) {
  331. e.printStackTrace();
  332. } catch (IllegalArgumentException e) {
  333. e.printStackTrace();
  334. }
  335. }
  336.  
  337. @Override
  338. public void onResume() {
  339. super.onResume();
  340. updateHeaderLoginStatus();
  341. String actionBarName;
  342. if (UserStatus.getInstance().getLoginStatus()) {
  343. actionBarName = UserStatus.getInstance().getFirstName() + " " +
  344. UserStatus.getInstance().getLastName();
  345. updateSignInSignOutStatus(actionBarName);
  346. } else {
  347. actionBarName = getResources().getString(R.string.hello_there);
  348. updateSignInSignOutStatus(actionBarName);
  349. }
  350. actionBarName = Util.getActionBarTitleName(getActivity(), ((BottombarNavigationActivity)
  351. getActivity()).bottomNavigation.getCurrentItem());
  352. ((BottombarNavigationActivity) getActivity()).setActionBarTitle(actionBarName);
  353. }
  354.  
  355. public void goOnTop() {
  356. if (aboutMeNavigationList != null) {
  357. aboutMeNavigationList.smoothScrollToPosition(0);
  358. }
  359. }
  360.  
  361. private void getMyCreditsRequest() {
  362. showProgress();
  363. RequestFactory.createGetApiRequest(MyCreditsResponse.class, R.string.api_my_credits, null,
  364. new Response.Listener<MyCreditsResponse>() {
  365. @Override
  366. public void onResponse(MyCreditsResponse response) {
  367. dismissProgress();
  368. if (Util.isResponseSuccessful(response.action)) {
  369. MyCreditsResponse.CreditItem creditItem = response.getReferralCredit();
  370. if (creditItem != null && signInBenefits != null) {
  371. if (creditItem.creditAmount != 0) {
  372. signInBenefits.setText("You have " + Util.getNormalPrice(getActivity(),
  373. creditItem.creditAmount) + " credits");
  374. } else {
  375. signInBenefits.setText(getResources().
  376. getString(R.string.NoCreditsAvailable));
  377. }
  378. }
  379. } else {
  380. Util.showErrorDialog(getActivity(), response);
  381. }
  382. }
  383. }, new AutoLoginRequiredListener(getActivity()) {
  384. @Override
  385. public void onAutoLoginProcess() {
  386. getMyCreditsRequest();
  387. }
  388.  
  389. @Override
  390. public void onLoginRequired() {
  391.  
  392. }
  393.  
  394. @Override
  395. public void onOtherErrorResponse(VolleyError error) {
  396.  
  397. }
  398. });
  399. }
  400. }
Add Comment
Please, Sign In to add comment