Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package in.hopscotch.android.fragment;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.text.TextUtils;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.ExpandableListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import butterknife.ButterKnife;
- import in.hopscotch.android.BuildConfig;
- import in.hopscotch.android.R;
- import in.hopscotch.android.activity.FavouriteBrandsActivity;
- import in.hopscotch.android.activity.HSWebviewActivity;
- import in.hopscotch.android.activity.InviteFriendsActivity;
- import in.hopscotch.android.activity.JoinActivity;
- import in.hopscotch.android.activity.MyKidsActivity;
- import in.hopscotch.android.activity.OrdersActivity;
- import in.hopscotch.android.activity.StoredCardsActivity;
- import in.hopscotch.android.activity.parent.BottombarNavigationActivity;
- import in.hopscotch.android.adapter.AboutMeExpandAdapter;
- import in.hopscotch.android.api.RequestFactory;
- import in.hopscotch.android.api.listener.ApiErrorListener;
- import in.hopscotch.android.api.listener.AutoLoginRequiredListener;
- import in.hopscotch.android.api.response.ActionResponse;
- import in.hopscotch.android.api.response.MyCreditsResponse;
- import in.hopscotch.android.application.IntentHelper;
- import in.hopscotch.android.listener.AbsListViewOnScrollListener;
- import in.hopscotch.android.model.UserStatus;
- import in.hopscotch.android.util.Constants;
- import in.hopscotch.android.util.DialogUtil;
- import in.hopscotch.android.util.Util;
- import in.hopscotch.android.volley.widget.VolleyCircleImageView;
- import in.hopscotch.android.widget.NavigationView;
- import io.branch.referral.Branch;
- /**
- * Created by Anil Chahal on 4/5/16.
- */
- public class AboutMeFragment extends Fragment {
- private NavigationView aboutMeNavigationList;
- private LayoutInflater mInflater;
- private TextView userNameView, signInBenefits;
- private AboutMeExpandAdapter adapter;
- private ProgressDialog progressDialog;
- private RelativeLayout aboutMeHeaderLayout;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- View view = inflater.inflate(R.layout.fragment_aboutme, container, false);
- aboutMeNavigationList = ButterKnife.findById(view, R.id.aboutMeNavigationList);
- adapter = new AboutMeExpandAdapter(getActivity());
- mInflater = LayoutInflater.from(view.getContext());
- setAboutMeHeaderView();
- setAboutMeFooterView();
- aboutMeNavigationList.setAdapter(adapter);
- aboutMeNavigationList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
- @Override
- public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
- int childPosition, long id) {
- switch (groupPosition) {
- case 0:
- if (!UserStatus.getInstance().getLoginStatus()) {
- Intent i = IntentHelper.createIntent(getActivity(), JoinActivity.class);
- int fromActivity = Constants.NORMAL_SIGN_FLAG + childPosition + 1;
- i.putExtra(IntentHelper.INTENT_FLAG_SIGN_ACTION, fromActivity);
- startActivity(i);
- return true;
- }
- switch (childPosition) {
- case 0: // Open Order Activity ...
- if(!(getActivity() instanceof OrdersActivity)) {
- startActivity(new Intent(getActivity(), OrdersActivity.class));
- }
- break;
- case 1: // Open Stored Cards ...
- if(!(getActivity() instanceof StoredCardsActivity)) {
- startActivity(new Intent(getActivity(), StoredCardsActivity.class));
- }
- break;
- case 2: // My kids ...
- if(!(getActivity() instanceof MyKidsActivity)) {
- startActivity(new Intent(getActivity(), MyKidsActivity.class));
- }
- break;
- case 3: // Favourite Brands ...
- if(!(getActivity() instanceof FavouriteBrandsActivity)) {
- startActivity(new Intent(getActivity(), FavouriteBrandsActivity.class));
- }
- break;
- case 4: // Invite Friends ...
- if(!(getActivity() instanceof InviteFriendsActivity)) {
- startActivity(new Intent(getActivity(), InviteFriendsActivity.class));
- }
- break;
- default:
- break;
- }
- break;
- case 1:
- switch (childPosition) {
- case 0: // Help ...
- Util.sendEmailTo(getActivity());
- break;
- case 1: // Share App ...
- Util.invokeShareDialog(getActivity(), getString(R.string.share_app_message), "");
- break;
- case 2: // Rate App ...
- Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName());
- startActivity(new Intent(Intent.ACTION_VIEW, uri));
- break;
- default:
- break;
- }
- break;
- case 2: // About Hopscotch & Terms and Conditions ...
- Intent webViewIntent = new Intent(getActivity(),
- HSWebviewActivity.class);
- webViewIntent.putExtra(IntentHelper.WEB_URL,getString(
- childPosition == 0 ? R.string.drawer_about_us_link
- : R.string.drawer_terms_and_conditions_link));
- webViewIntent.putExtra(IntentHelper.INTENT_EXTRA_TITLE,
- childPosition == 0 ? getString(R.string.about_hopscotch) :
- getString(R.string.terms_and_conditions));
- startActivity(webViewIntent);
- break;
- case 3: // This is just for providing Spacing between last two rows ...
- break;
- case 4:
- if (UserStatus.getInstance().getLoginStatus()) {
- showProgress();
- RequestFactory.createGetApiRequest(ActionResponse.class,
- R.string.api_logout,
- null,
- new Response.Listener<ActionResponse>() {
- @Override
- public void onResponse(ActionResponse response) {
- dismissProgress();
- if (response != null && response.action
- .equals(ActionResponse.SUCCESS)) {
- UserStatus.getInstance().customerLogout(
- getActivity());
- Util.startGcmRegistrationService(
- getActivity());
- Branch.getInstance().logout();
- ((BottombarNavigationActivity) getActivity()).
- bottomNavigation.setCurrentItem(0);
- updateSignInSignOutStatus(getResources().
- getString(R.string.hello_there));
- }
- else {
- Util.showErrorDialog(getActivity(), response);
- }
- }
- },
- new ApiErrorListener(getActivity()) {
- @Override
- public void onOtherErrorResponse(VolleyError error) {
- DialogUtil.showOneButtonAlert(getActivity(),
- getActivity().getString(
- R.string.sign_out_failed));
- }
- });
- } else {
- Intent intent = IntentHelper.createIntent(getActivity(), JoinActivity.class);
- startActivity(intent);
- }
- break;
- default:
- break;
- }
- return true;
- }
- });
- aboutMeNavigationList.setOnScrollListener(new AbsListViewOnScrollListener() {
- @Override
- public void onScrollUp() {
- ((BottombarNavigationActivity)getActivity()).restoreBottomNavigationBar(true);
- }
- @Override
- public void onScrollDown() {
- ((BottombarNavigationActivity)getActivity()).hideBottomNavigationBar();
- }
- int last = 0;
- boolean control = true;
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- if (firstVisibleItem < last && !control) {
- onScrollUp();
- control = true;
- } else if (firstVisibleItem > last && control) {
- onScrollDown();
- control = false;
- }
- last = firstVisibleItem;
- }
- });
- return view;
- }
- private void setAboutMeHeaderView() {
- aboutMeNavigationList.addHeaderView(
- mInflater.inflate(R.layout.aboutme_header_layout, aboutMeNavigationList, false),
- null, false);
- aboutMeHeaderLayout = ButterKnife.findById(aboutMeNavigationList, R.id.aboutMeHeaderLayout);
- aboutMeHeaderLayout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (!UserStatus.getInstance().getLoginStatus()) {
- Intent i = IntentHelper.createIntent(getActivity(), JoinActivity.class);
- startActivity(i);
- }
- }
- });
- userNameView = (TextView) aboutMeNavigationList.findViewById(R.id.userNameView);
- signInBenefits = (TextView) aboutMeNavigationList.findViewById(R.id.signInBenefits);
- updateHeaderLoginStatus();
- VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
- findViewById(R.id.userPortrait);
- imageView.setDefaultImage(R.drawable.menuuser);
- if(UserStatus.getInstance().getLoginStatus()) {
- if(UserStatus.getInstance().getUserProfilePhoto() != null &&
- UserStatus.getInstance().getUserProfilePhoto().trim().length() > 0) {
- imageView.setImageUrl(UserStatus.getInstance().getUserProfilePhoto());
- } else {
- imageView.setImageResource(R.drawable.menuuser);
- }
- } else {
- imageView.setImageResource(R.drawable.menuuser);
- }
- }
- private void updateHeaderLoginStatus() {
- if (UserStatus.getInstance().getLoginStatus()) {
- VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
- findViewById(R.id.userPortrait);
- if(UserStatus.getInstance().getUserProfilePhoto() != null &&
- UserStatus.getInstance().getUserProfilePhoto().trim().length() > 0) {
- imageView.setImageUrl(UserStatus.getInstance().getUserProfilePhoto());
- } else {
- imageView.setImageResource(R.drawable.menuuser);
- }
- if (TextUtils.isEmpty(UserStatus.getInstance().getFirstName()) &&
- TextUtils.isEmpty(UserStatus.getInstance().getLastName())) {
- userNameView.setText(R.string.hello_there);
- } else {
- userNameView.setText("Hello " + UserStatus.getInstance().getFirstName() + " " +
- UserStatus.getInstance().getLastName() + "!");
- userNameView.setTextColor(getResources().getColor(R.color.black));
- }
- signInBenefits.setVisibility(View.INVISIBLE);
- } else {
- userNameView.setText(getString(R.string.drawer_sign_in_now));
- userNameView.setTextColor(getResources().getColor(R.color.colorPrimary));
- signInBenefits.setVisibility(View.VISIBLE);
- signInBenefits.setText(getResources().getText(R.string.signInBenefits));
- VolleyCircleImageView imageView = (VolleyCircleImageView) aboutMeNavigationList.
- findViewById(R.id.userPortrait);
- imageView.setImageResource(R.drawable.menuuser);
- }
- }
- private void updateSignInSignOutStatus(String actionBarTitleName) {
- updateHeaderLoginStatus();
- adapter.notifyDataSetChanged();
- }
- private void setAboutMeFooterView() {
- aboutMeNavigationList.addFooterView(
- mInflater.inflate(R.layout.aboutme_footer_layout, aboutMeNavigationList, false), null,
- false);
- TextView versionView = (TextView) aboutMeNavigationList.findViewById(R.id.versionTextView);
- String versionText = getResources().getText(R.string.version_label) + " " +
- BuildConfig.VERSION_NAME;
- if (BuildConfig.DEBUG) {
- versionView.setText(versionText + "\n\n" + BuildConfig.CURRENT_DATE);
- } else {
- versionView.setText(versionText);
- }
- }
- public void showProgress() {
- if (null == progressDialog) {
- progressDialog = new ProgressDialog(getActivity(), R.style.AppDialogThemeTransparent);
- progressDialog.setCancelable(true);
- progressDialog.setCanceledOnTouchOutside(false);
- }
- try {
- progressDialog.show();
- progressDialog.setContentView(R.layout.my_progress);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void dismissProgress() {
- try {
- if (null != progressDialog && progressDialog.isShowing()) {
- progressDialog.dismiss();
- }
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- updateHeaderLoginStatus();
- String actionBarName;
- if (UserStatus.getInstance().getLoginStatus()) {
- actionBarName = UserStatus.getInstance().getFirstName() + " " +
- UserStatus.getInstance().getLastName();
- updateSignInSignOutStatus(actionBarName);
- } else {
- actionBarName = getResources().getString(R.string.hello_there);
- updateSignInSignOutStatus(actionBarName);
- }
- actionBarName = Util.getActionBarTitleName(getActivity(), ((BottombarNavigationActivity)
- getActivity()).bottomNavigation.getCurrentItem());
- ((BottombarNavigationActivity) getActivity()).setActionBarTitle(actionBarName);
- }
- public void goOnTop() {
- if (aboutMeNavigationList != null) {
- aboutMeNavigationList.smoothScrollToPosition(0);
- }
- }
- private void getMyCreditsRequest() {
- showProgress();
- RequestFactory.createGetApiRequest(MyCreditsResponse.class, R.string.api_my_credits, null,
- new Response.Listener<MyCreditsResponse>() {
- @Override
- public void onResponse(MyCreditsResponse response) {
- dismissProgress();
- if (Util.isResponseSuccessful(response.action)) {
- MyCreditsResponse.CreditItem creditItem = response.getReferralCredit();
- if (creditItem != null && signInBenefits != null) {
- if (creditItem.creditAmount != 0) {
- signInBenefits.setText("You have " + Util.getNormalPrice(getActivity(),
- creditItem.creditAmount) + " credits");
- } else {
- signInBenefits.setText(getResources().
- getString(R.string.NoCreditsAvailable));
- }
- }
- } else {
- Util.showErrorDialog(getActivity(), response);
- }
- }
- }, new AutoLoginRequiredListener(getActivity()) {
- @Override
- public void onAutoLoginProcess() {
- getMyCreditsRequest();
- }
- @Override
- public void onLoginRequired() {
- }
- @Override
- public void onOtherErrorResponse(VolleyError error) {
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment