Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.saholic.profittill.main;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import com.saholic.profittill.Constants.ProfitTillConstants;
import com.saholic.profittill.R;
import com.squareup.picasso.Transformation;
import com.testin.agent.TestinAgent;

import org.json.JSONException;
import org.json.JSONObject;

public class MyProfile extends Fragment {
    SharedPreferences userData;
    SharedPreferences apiData;
    SharedPreferences.Editor userDataEditor;
    SharedPreferences.Editor apiSettingsEditor;
    public MyProfile() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my_profile, container, false);
        TestinAgent.init(getActivity());
        MixpanelAPI mixpanel = MixpanelAPI.getInstance(getActivity(), ProfitTillConstants.MIX_PANEL_TOKEN);

        Tracker t = ((GoogleAnalyticsTracker) getActivity().getApplicationContext()).getTracker(
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
        t.setScreenName("Profile Screen");
        t.send(new HitBuilders.ScreenViewBuilder().build());

        userData = getActivity().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
        apiData = getActivity().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
        userDataEditor = userData.edit();
        apiSettingsEditor = apiData.edit();
        TestinAgent.setUserInfo(userData.getString("id", ""));
        JSONObject props = new JSONObject();
        try {
            mixpanel.identify(userData.getString("id",null));
            props.put("Screen", "My Profile");
            mixpanel.track("My Profile", props);
        } catch (JSONException e) {
            e.printStackTrace();
        }
      /*  if(isInternetOn()){
            new Analytics(ProfitTillConstants.ANALYTICS_URL,
                    AnalyticsUtility.getAnalyticsRequest(getActivity(), userData.getString("id", ""), "Screen", "My Profile", "My Profile Screen"),
                    AnalyticsJsonResponse.getAnalyticsRequestInstance(),
                    AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getActivity());
            pd = new ProgressDialog(getActivity());
            pd.setTitle("Please wait");
            pd.setMessage("Fetching Profile Information..");
            pd.show();

        }
        else{
            SearchFragment nextFrag= new SearchFragment();
            Bundle args = new Bundle();
            args.putString("finishingurl","");
            args.putString("fragmentNumber","10");
            nextFrag.setArguments(args);
            getActivity().getFragmentManager().beginTransaction()
                    .replace(R.id.frame_container, nextFrag)
                    .addToBackStack(null)
                    .commit();
       }*/
        rootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.d("In my profile" , "In my profile " );
                if(event.getAction() == KeyEvent.ACTION_DOWN){
                    switch(keyCode) {
                        case KeyEvent.KEYCODE_BACK:
                            Intent i = new Intent(getActivity(), MainActivity.class);
                            i.putExtra("displayView", "0");
                            startActivity(i);
                            return true;
                    }
                }
                return false;
            }
        });
        return rootView;

    }

      @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public class CircleTransform implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());

            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                source.recycle();
            }

            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap,
                    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);

            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);

            squaredBitmap.recycle();
            return bitmap;
        }

        @Override
        public String key() {
            return "circle";
        }
    }

    ProgressDialog pd = null;

    public final boolean isInternetOn() {

        ConnectivityManager connec =
                (ConnectivityManager)getActivity().getSystemService(getActivity().getBaseContext().CONNECTIVITY_SERVICE);

        if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

            return true;

        } else if (
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
            return false;
        }
        return false;
    }
    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                    Intent i = new Intent(getActivity(), MainActivity.class);
                    i.putExtra("displayView", "0");
                    startActivity(i);
                    return true;
                }
                return false;
            }
        });
    }

    public void dealsPageOnBack(){
        Intent i = new Intent(getActivity(), MainActivity.class);
        i.putExtra("displayView", "0");
        startActivity(i);
    }
}