Tuesday, 22 May 2018

Adapter

public class AllSalonNew extends RecyclerView.Adapter<AllSalonNew.VHHolderHome> {

    Activity activity;

    ArrayList<SalonModel> arrayList = new ArrayList<>();

    String from = "Salon";


    public AllSalonNew(Activity act, ArrayList<SalonModel> list, String frm) {

        activity = act;

        arrayList = list;

        from = frm;


    }

    @Override    public AllSalonNew.VHHolderHome onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(activity).inflate(R.layout.row_all_salon, parent, false);

        if (rootView.getLayoutParams().width == RecyclerView.LayoutParams.MATCH_PARENT)
            rootView.getLayoutParams().width = parent.getWidth();

        return new AllSalonNew.VHHolderHome(rootView);
    }

    View.OnTouchListener listener = new View.OnTouchListener() {
        @Override        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                v.getParent().requestDisallowInterceptTouchEvent(true);

            } else {
                v.getParent().requestDisallowInterceptTouchEvent(false);

            }
            return false;
        }
    };

    @Override    public void onBindViewHolder(final AllSalonNew.VHHolderHome holder, final int position) {

        final SalonModel model = arrayList.get(position);
        String BannerPath;

        if (AppController.getCurrentLang().equals("ar")) {

            holder.txtSalonName.setText(model.getNameNative());


        } else {

            holder.txtSalonName.setText(model.getNameIntl());

        }

        if (model.getBannerName() != null) {

            BannerPath = AppConstant.IMAGE_PATH_SALON_BANNER + model.getBannerName();
            Glide.with(activity).load(BannerPath).into(holder.imgSalonBanner);

        }

        String imagePath = AppConstant.IMAGE_PATH_SALON_LOGO + model.getLogoName();


        Glide.with(activity).load(imagePath).into(holder.imgSalon);


        final ArrayList<SalonBranch> branchArrayList = (ArrayList<SalonBranch>) arrayList.get(position).getSalonBranches();

        if (!branchArrayList.isEmpty()) {

            if (branchArrayList.size() == 1) {

                holder.ratingBar.setVisibility(View.VISIBLE);

                SalonBranch salonBranchModel = branchArrayList.get(0);


                if (AppController.getCurrentLang().equals("ar")) {

                    holder.txtSalonName.setText(salonBranchModel.getNameNative());

                } else {

                    holder.txtSalonName.setText(salonBranchModel.getNameIntl());

                }

                if (salonBranchModel.getAverageRate() != 0f) {
                    holder.ratingBar.setVisibility(View.VISIBLE);

                    holder.ratingBar.setStar((int) (salonBranchModel.getAverageRate()));
                } else {

                    holder.ratingBar.setVisibility(View.GONE);
                }


                if (AppController.getCurrentLang().equals("ar")) {
                    holder.txtSalonArea.setText(salonBranchModel.getCity() != null ? salonBranchModel.getCity().getNameNative() : "");

                } else {
                    holder.txtSalonArea.setText(salonBranchModel.getCity() != null ? salonBranchModel.getCity().getNameIntl() : "");

                }


                if (!branchArrayList.get(0).getServices().isEmpty()) {

                    int homeServiceCount = 0;

                    for (ServiceModel serviceModel : branchArrayList.get(0).getServices()) {

                        if (serviceModel.getIsHomeService()) {

                            homeServiceCount++;
                        }

                    }

                    if (homeServiceCount > 0) {
                        holder.Redirect = "Home";
                        holder.imgHomeService.setVisibility(View.VISIBLE);
                    } else {
                        holder.Redirect = "Salon";
                        holder.imgHomeService.setVisibility(View.GONE);
                    }

                }


            } else {
                holder.txtSalonArea.setVisibility(View.VISIBLE);
                holder.txtSalonArea.setText(R.string.branches);

                setMoreSalonList(branchArrayList, holder);

            }

        } else {

            holder.ratingBar.setVisibility(View.GONE);

            holder.txtSalonArea.setVisibility(View.GONE);

        }

        if (model.isOpen()) {

            holder.recyclerView.setVisibility(View.VISIBLE);

        } else {

            holder.recyclerView.setVisibility(View.GONE);

            holder.imgHomeService.setVisibility(View.VISIBLE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {

                if (!branchArrayList.isEmpty()) {


                    if (branchArrayList.size() == 1) {


                        if (MainActivity.isSalonProfileOpen) {

                            ((MainActivity) activity).removeFragment(AppConstant.TAG_SALON_PROFILE_FRAGMENT);

                            openSalonProfile(model, from);

                        } else {

                            openSalonProfile(model, from);
                        }

                    } else {


                        if (model.isOpen()) {

                            model.setOpen(false);

                            holder.recyclerView.setVisibility(View.GONE);

                            if (holder.Redirect.equals("Home")) {
                                holder.imgHomeService.setVisibility(View.VISIBLE);
                            }


                        } else {

                            model.setOpen(true);


                            holder.recyclerView.setVisibility(View.VISIBLE);

                            holder.imgHomeService.setVisibility(View.GONE);
                        }

                        arrayList.set(position, model);

                    }
                }
            }
        });

        holder.imgHomeService.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {

                if (model.getSalonBranches() != null) {

                    if (model.getSalonBranches().size() == 1) {

                        if (MainActivity.isSalonProfileOpen) {

                            ((MainActivity) activity).removeFragment(AppConstant.TAG_SALON_PROFILE_FRAGMENT);

                            openSalonProfile(model, "Home");

                        } else {

                            openSalonProfile(model, "Home");
                        }
                    }

                }


            }
        });

        holder.ratingBar.setClickable(false);


        if (from.equals("Home")) {

            holder.imgHomeService.setVisibility(View.VISIBLE);

            holder.Redirect = "Home";

        } else {

            if (model.getSalonBranches() != null) {

                boolean isHomeService = false;

                if (!model.getSalonBranches().isEmpty()) {

                    for (SalonBranch salonBranch : model.getSalonBranches()) {

                        if (!salonBranch.getServices().isEmpty()) {

                            for (ServiceModel serviceModel : salonBranch.getServices()) {

                                if (serviceModel.getIsHomeService()) {

                                    isHomeService = true;

                                    break;
                                }

                            }

                        }

                    }


                    if (isHomeService) {

                        holder.imgHomeService.setVisibility(View.VISIBLE);
                        holder.Redirect = "Home";

                    } else {

                        holder.imgHomeService.setVisibility(View.GONE);
                        holder.Redirect = "Salon";
                    }


                } else {
                    holder.imgHomeService.setVisibility(View.GONE);
                    holder.Redirect = "Salon";
                }
            }

        }

    }

    private void openSalonProfile(SalonModel model, String redirect) {

        SalonProfileFragment salonProfileFragment = new SalonProfileFragment();

        Bundle args = new Bundle();

        args.putString("branchSalonId", String.valueOf(model.getSalonBranches().get(0).getId()));

        args.putString("Redirect", redirect /*holder.Redirect*/);

        salonProfileFragment.setArguments(args);


        ((MainActivity) activity).addFragment(salonProfileFragment, AppConstant.TAG_SALON_PROFILE_FRAGMENT, true);


    }

    @Override    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override    public int getItemCount() {
        return arrayList.size();
    }

    private void setMoreSalonList(ArrayList<SalonBranch> arrayList, AllSalonNew.VHHolderHome holder) {

        MoreSalonAdapter adapter = new MoreSalonAdapter(activity, arrayList, from);


        LinearLayoutManager llm = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        holder.recyclerView.setLayoutManager(llm);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));

        //  MoreSalonBaseAdapter adapter = new MoreSalonBaseAdapter(activity, arrayList, from);        holder.recyclerView.setAdapter(adapter);

        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            // Setting on Touch Listener for handling the touch inside ScrollView            @Override            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

    }

    class VHHolderHome extends RecyclerView.ViewHolder {

        private CustomTextView txtSalonName, txtSalonArea;
        private ImageView imgSalon, imgHomeService, imgSalonBanner;
        private RecyclerView recyclerView;

        private LinearLayout llMainSalonView;
        private RatingBar ratingBar;
        private String Redirect = "Salon";


        public VHHolderHome(View itemView) {
            super(itemView);

            txtSalonName = itemView.findViewById(R.id.txtSalonNameAllSalon);

            txtSalonArea = itemView.findViewById(R.id.txtSalonAreaAllSalon);

            imgSalon = itemView.findViewById(R.id.imgSalonLogoAllSalon);

            imgSalonBanner = itemView.findViewById(R.id.imgSalonBanner);

            imgHomeService = itemView.findViewById(R.id.imgHomeServiceAllSalon);

            recyclerView = itemView.findViewById(R.id.recyclerMoreSalonAllSalon);

            llMainSalonView = itemView.findViewById(R.id.llMainSalonView);

            ratingBar = itemView.findViewById(R.id.rattingSalon);

            ratingBar.setVisibility(View.GONE);

            recyclerView.setVisibility(View.GONE);

            imgHomeService.setVisibility(View.GONE);

            ratingBar.setStarEmptyDrawable(activity.getResources().getDrawable(R.drawable.ic_rating_unselect));

            ratingBar.setStarFillDrawable(activity.getResources().getDrawable(R.drawable.ic_rating_selected));

            ratingBar.setStarCount(5);

            ratingBar.setClickable(false);

            ratingBar.halfStar(false);

        }
    }
}

No comments:

Post a Comment