com.annanovas.bestprice.Fragment.DashBoardFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.annanovas.bestprice.Fragment.DashBoardFragment.java

Source

package com.annanovas.bestprice.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.annanovas.bestprice.AddQuotationActivity;
import com.annanovas.bestprice.CarListActivity;
import com.annanovas.bestprice.PendingTaskActivity;
import com.annanovas.bestprice.QuotationActivity;
import com.annanovas.bestprice.R;
import com.annanovas.bestprice.SelectUserTypeActivity;
import com.annanovas.bestprice.Utils.AppGlobal;
import com.annanovas.bestprice.Utils.MyDatabaseManager;
import com.bumptech.glide.Glide;

import static android.content.Context.MODE_PRIVATE;

public class DashBoardFragment extends Fragment {

    private LinearLayout carLayout, quotationLayout, pendingTaskLayout, searchProductLayout;
    private TextView tvAddress, tvDealership, tvShopName, tvDrivingLicense, tvNid, tvExperience, tvUserType, tvName,
            tvPhoneNumber, tvEmail;
    private Button buttonUserType;
    private ImageView userImage;

    private SharedPreferences sharedPreferences;
    private MyDatabaseManager myDB;

    private int userType;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);

        carLayout = (LinearLayout) view.findViewById(R.id.layout_car);
        quotationLayout = (LinearLayout) view.findViewById(R.id.layout_quotation);
        pendingTaskLayout = (LinearLayout) view.findViewById(R.id.layout_pending_task);
        tvAddress = (TextView) view.findViewById(R.id.tv_address);
        tvDealership = (TextView) view.findViewById(R.id.tv_dealership);
        tvShopName = (TextView) view.findViewById(R.id.tv_shop_name);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvDrivingLicense = (TextView) view.findViewById(R.id.tv_draiving_license);
        tvNid = (TextView) view.findViewById(R.id.tv_nid);
        tvExperience = (TextView) view.findViewById(R.id.tv_experience);
        tvUserType = (TextView) view.findViewById(R.id.tv_user_type);
        tvPhoneNumber = (TextView) view.findViewById(R.id.tv_phone_number);
        tvEmail = (TextView) view.findViewById(R.id.tv_email);
        buttonUserType = (Button) view.findViewById(R.id.button_user_type);
        searchProductLayout = (LinearLayout) view.findViewById(R.id.layout_search_product);
        userImage = (ImageView) view.findViewById(R.id.iv_user_image);

        myDB = MyDatabaseManager.getInstance(getActivity());
        sharedPreferences = getActivity().getSharedPreferences(AppGlobal.MY_PREFS_NAME, MODE_PRIVATE);

        userType = sharedPreferences.getInt("user_type", 1);

        buttonUserType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getActivity(), SelectUserTypeActivity.class);
                startActivity(myIntent);
                getActivity().finish();
            }
        });

        carLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getActivity(), CarListActivity.class);
                startActivity(myIntent);
            }
        });

        searchProductLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getActivity(), AddQuotationActivity.class);
                startActivity(myIntent);
            }
        });

        quotationLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getActivity(), QuotationActivity.class);
                startActivity(myIntent);
            }
        });

        pendingTaskLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(getActivity(), PendingTaskActivity.class);
                startActivity(myIntent);
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateDashboard();
    }

    private void updateDashboard() {
        int imageDimen = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
                getResources().getDimension(R.dimen.size_150dp), getResources().getDisplayMetrics());

        myDB.open();
        Cursor cursor = myDB.getUserInfo(userType, sharedPreferences.getString("user_phone_number", ""));

        if (userType == AppGlobal.CAR_OWNER) {
            carLayout.setVisibility(View.VISIBLE);
            quotationLayout.setVisibility(View.VISIBLE);
            searchProductLayout.setVisibility(View.VISIBLE);
            if (myDB.getUserCount() > 1) {
                buttonUserType.setVisibility(View.VISIBLE);
            }

            if (!cursor.isLast()) {
                while (cursor.moveToNext()) {
                    String name = "";
                    if (cursor.getString(cursor.getColumnIndex("firstname")) != null
                            && !cursor.getString(cursor.getColumnIndex("firstname")).equals("null")) {
                        name = cursor.getString(cursor.getColumnIndex("firstname")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("middlename")) != null
                            && !cursor.getString(cursor.getColumnIndex("middlename")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("middlename")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("lastname")) != null
                            && !cursor.getString(cursor.getColumnIndex("lastname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("lastname"));
                    }
                    tvName.setText(name.trim());
                    tvPhoneNumber.setText(cursor.getString(cursor.getColumnIndex("phone")));
                    tvEmail.setText(cursor.getString(cursor.getColumnIndex("email")));

                    if (cursor.getString(cursor.getColumnIndex("image")) != null
                            && !cursor.getString(cursor.getColumnIndex("image")).equals("null")) {
                        //showLog("Image Path: " + AppGlobal.userImagePath + cursor.getString(cursor.getColumnIndex("image")));
                        Glide.with(getActivity())
                                .load(AppGlobal.userImagePath + cursor.getString(cursor.getColumnIndex("image")))
                                .error(R.drawable.ic_car_owner).dontAnimate().override(imageDimen, imageDimen)
                                .into(userImage);
                    } else {
                        userImage.setImageResource(R.drawable.ic_car_owner);
                    }

                    sharedPreferences.edit()
                            .putString("api_token", cursor.getString(cursor.getColumnIndex("api_token"))).apply();
                    sharedPreferences.edit().putInt("user_id", cursor.getInt(cursor.getColumnIndex("id"))).apply();
                }
            }
            cursor.close();
            tvUserType.setText(getResources().getString(R.string.user_type1));

        } else if (userType == AppGlobal.SUPPLIER) {
            tvAddress.setVisibility(View.VISIBLE);
            tvDealership.setVisibility(View.VISIBLE);
            quotationLayout.setVisibility(View.VISIBLE);
            if (myDB.getUserCount() > 1) {
                buttonUserType.setVisibility(View.VISIBLE);
            }

            if (!cursor.isLast()) {
                while (cursor.moveToNext()) {
                    String name = "";
                    if (cursor.getString(cursor.getColumnIndex("firstname")) != null
                            && !cursor.getString(cursor.getColumnIndex("firstname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("firstname")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("middlename")) != null
                            && !cursor.getString(cursor.getColumnIndex("middlename")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("middlename")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("lastname")) != null
                            && !cursor.getString(cursor.getColumnIndex("lastname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("lastname"));
                    }
                    tvName.setText(name.trim());
                    tvAddress.setText(cursor.getString(cursor.getColumnIndex("address")));
                    tvPhoneNumber.setText(cursor.getString(cursor.getColumnIndex("phone")));
                    tvEmail.setText(cursor.getString(cursor.getColumnIndex("email")));
                    tvDealership.setText("Dealership With: "
                            + myDB.getBrandsNameFromUserId(cursor.getInt(cursor.getColumnIndex("id"))));

                    if (cursor.getString(cursor.getColumnIndex("image")) != null
                            && !cursor.getString(cursor.getColumnIndex("image")).equals("null")) {
                        Glide.with(getActivity())
                                .load(AppGlobal.userImagePath + cursor.getString(cursor.getColumnIndex("image")))
                                .error(R.drawable.ic_supplier).dontAnimate().override(imageDimen, imageDimen)
                                .into(userImage);
                    } else {
                        userImage.setImageResource(R.drawable.ic_supplier);
                    }

                    sharedPreferences.edit()
                            .putString("api_token", cursor.getString(cursor.getColumnIndex("api_token"))).apply();
                    sharedPreferences.edit().putInt("user_id", cursor.getInt(cursor.getColumnIndex("id"))).apply();
                }
            }
            cursor.close();
            tvUserType.setText(getResources().getString(R.string.user_type2));

        } else if (userType == AppGlobal.DRIVER) {
            tvDrivingLicense.setVisibility(View.VISIBLE);
            tvNid.setVisibility(View.VISIBLE);
            tvExperience.setVisibility(View.VISIBLE);
            pendingTaskLayout.setVisibility(View.VISIBLE);

            if (!cursor.isLast()) {
                while (cursor.moveToNext()) {
                    String name = "";
                    if (cursor.getString(cursor.getColumnIndex("firstname")) != null
                            && !cursor.getString(cursor.getColumnIndex("firstname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("firstname")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("middlename")) != null
                            && !cursor.getString(cursor.getColumnIndex("middlename")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("middlename")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("lastname")) != null
                            && !cursor.getString(cursor.getColumnIndex("lastname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("lastname"));
                    }
                    tvName.setText(name.trim());
                    tvPhoneNumber.setText(cursor.getString(cursor.getColumnIndex("phone")));
                    tvEmail.setText(cursor.getString(cursor.getColumnIndex("email")));
                    tvDrivingLicense.setText(
                            "Driving License no.: " + cursor.getString(cursor.getColumnIndex("drivinglicence")));
                    tvNid.setText("NID no.: " + cursor.getString(cursor.getColumnIndex("nid")));
                    tvExperience.setText(
                            "Years of Experience: " + cursor.getString(cursor.getColumnIndex("yearofexperience")));

                    if (cursor.getString(cursor.getColumnIndex("image")) != null
                            && !cursor.getString(cursor.getColumnIndex("image")).equals("null")) {
                        Glide.with(getActivity())
                                .load(AppGlobal.userImagePath + cursor.getString(cursor.getColumnIndex("image")))
                                .error(R.drawable.ic_driver).dontAnimate().override(imageDimen, imageDimen)
                                .into(userImage);
                    } else {
                        userImage.setImageResource(R.drawable.ic_driver);
                    }

                    sharedPreferences.edit()
                            .putString("api_token", cursor.getString(cursor.getColumnIndex("api_token"))).apply();
                    sharedPreferences.edit().putInt("user_id", cursor.getInt(cursor.getColumnIndex("id"))).apply();
                }
            }
            cursor.close();
            tvUserType.setText(getResources().getString(R.string.user_type3));

        } else if (userType == AppGlobal.SHOP_WORKER) {
            tvShopName.setVisibility(View.VISIBLE);
            tvNid.setVisibility(View.VISIBLE);
            pendingTaskLayout.setVisibility(View.VISIBLE);

            if (!cursor.isLast()) {
                while (cursor.moveToNext()) {
                    String name = "";
                    if (cursor.getString(cursor.getColumnIndex("firstname")) != null
                            && !cursor.getString(cursor.getColumnIndex("firstname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("firstname")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("middlename")) != null
                            && !cursor.getString(cursor.getColumnIndex("middlename")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("middlename")) + " ";
                    }
                    if (cursor.getString(cursor.getColumnIndex("lastname")) != null
                            && !cursor.getString(cursor.getColumnIndex("lastname")).equals("null")) {
                        name = name + cursor.getString(cursor.getColumnIndex("lastname"));
                    }
                    tvName.setText(name.trim());
                    tvPhoneNumber.setText(cursor.getString(cursor.getColumnIndex("phone")));
                    tvEmail.setText(cursor.getString(cursor.getColumnIndex("email")));
                    tvShopName.setText("Shop Name: " + cursor.getString(cursor.getColumnIndex("shopname")));
                    tvNid.setText("NID no.: " + cursor.getString(cursor.getColumnIndex("nid")));

                    if (cursor.getString(cursor.getColumnIndex("image")) != null
                            && !cursor.getString(cursor.getColumnIndex("image")).equals("null")) {
                        Glide.with(getActivity())
                                .load(AppGlobal.userImagePath + cursor.getString(cursor.getColumnIndex("image")))
                                .error(R.drawable.ic_shop_worker).dontAnimate().override(imageDimen, imageDimen)
                                .into(userImage);
                    } else {
                        userImage.setImageResource(R.drawable.ic_shop_worker);
                    }

                    sharedPreferences.edit()
                            .putString("api_token", cursor.getString(cursor.getColumnIndex("api_token"))).apply();
                    sharedPreferences.edit().putInt("user_id", cursor.getInt(cursor.getColumnIndex("id"))).apply();
                }
            }
            cursor.close();
            tvUserType.setText(getResources().getString(R.string.user_type4));
        }
        myDB.close();
    }

}