<commit_before>/*
 * Copyright (c) 2014 - 2018. The Trustees of Indiana University, Moi University
 * and Vanderbilt University Medical Center.
 *
 * This version of the code is licensed under the MPL 2.0 Open Source license
 * with additional health care disclaimer.
 * If the user is an entity intending to commercialize any application that uses
 *  this code in a for-profit venture,please contact the copyright holder.
 */
package com.muzima.adapters.forms;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersAdapter;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersSectionIndexerAdapterWrapper;
import com.muzima.R;
import com.muzima.api.model.Patient;
import com.muzima.controller.FormController;
import com.muzima.model.FormWithData;
import com.muzima.utils.Fonts;
import com.muzima.utils.PatientComparator;
import com.muzima.utils.StringUtils;
import com.muzima.view.CheckedRelativeLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Responsible to list down the forms in the order of the Patient details. Here you can identify forms by the patient name.
 * @param <T> T is of the type FormsWithData.
 */
public abstract class SectionedFormsAdapter<T extends FormWithData> extends FormsAdapter<T> implements StickyListHeadersAdapter, SectionIndexer {
    private static final String TAG = "SectionedFormsAdapter";

    private List<Patient> patients;
    private ListView listView;
    private final PatientComparator patientComparator;
    private final List<String> selectedFormsUuid;
    private MuzimaClickListener muzimaClickListener;

    SectionedFormsAdapter(Context context, int textViewResourceId, FormController formController) {
        super(context, textViewResourceId, formController);
        patients = new ArrayList<>();
        patientComparator = new PatientComparator();
        selectedFormsUuid = new ArrayList<>();
    }

    @Override
    protected int getFormItemLayout() {
        return R.layout.item_forms_list_selectable;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if(!isEmpty()) {
            HeaderViewHolder holder;
            if (convertView == null) {
                holder = new HeaderViewHolder();
                LayoutInflater layoutInflater = LayoutInflater.from(getContext());
                convertView = layoutInflater.inflate(R.layout.layout_forms_list_section_header, parent, false);
                holder.patientName = convertView.findViewById(R.id.patientName);
                holder.patientIdentifier = convertView.findViewById(R.id.patientId);
                setClickListenersOnView(position, convertView);
                convertView.setTag(holder);
            } else {
                holder = (HeaderViewHolder) convertView.getTag();
            }

            if (!patients.isEmpty()) {
                Patient patient = patients.get(getSectionForPosition(position));
                if (patient != null) {
                    holder.patientName.setText(patient.getDisplayName());
                    holder.patientIdentifier.setText(patient.getIdentifier());
                } else {
                    holder.patientName.setText(getContext().getString(R.string.default_form_register));
                }
            }
        }
        return convertView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    @Override
    public long getHeaderId(int position) {
        int section = 0;
        if (!patients.isEmpty() && !isEmpty()) {
            section = patients.indexOf(getItem(position).getPatient());
        }
        return section;
    }

    @Override
    public Object[] getSections() {
        String[] familyNames = new String[patients.size()];
        for (int i = 0; i < patients.size(); i++) {
            familyNames[i] = String.valueOf(patients.get(i).getFamilyName().charAt(0));
        }
        return familyNames;
    }

    @Override
    public int getPositionForSection(int section) {
        if (section >= patients.size()) {
            section = patients.size() - 1;
        } else if (section < 0) {
            section = 0;
        }

        int position = 0;
        for (int i = 0; i < getCount(); i++) {
            Patient patient = getItem(i).getPatient();
            if (patient != null && patient.equals(patients.get(section))) {
                position = i;
                break;
            }
        }
        return position;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= getCount()) {
            position = getCount() - 1;
        } else if (position < 0) {
            position = 0;
        }

        int section = 0;
        if (!patients.isEmpty()){
            Patient patient = getItem(position).getPatient();
            if (patient != null) {
                section = patients.indexOf(patient);
            }
        }
        return section;
    }

    void sortFormsByPatientName(List<T> forms) {
        Collections.sort(patients, patientComparator);
        Collections.sort(forms, alphabaticalComparator);
        setNotifyOnChange(false);
        clear();
        addAll(forms);

        StickyListHeadersSectionIndexerAdapterWrapper adapter = (StickyListHeadersSectionIndexerAdapterWrapper) listView.getAdapter();
        adapter.notifyDataSetChanged();
    }

    List<Patient> getPatients() {
        return patients;
    }

    void setPatients(List<Patient> patients) {
        this.patients = patients;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        if(!isEmpty()) {
            setClickListenersOnView(position, convertView);
            FormWithData form = getItem(position);

            String formSaveTime = null;
            if (form.getLastModifiedDate() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("STANDARD_DATE_FORMAT HH:mm:ss");
                formSaveTime = dateFormat.format(form.getLastModifiedDate());
            }

            ViewHolder holder = (ViewHolder) convertView.getTag();

            if (!StringUtils.isEmpty(formSaveTime)) {
                holder.savedTime.setText(formSaveTime);
            }
            holder.savedTime.setTypeface(Fonts.roboto_italic(getContext()));
            holder.savedTime.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private void setClickListenersOnView(final int position, View convertView) {
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (view instanceof CheckedRelativeLayout) {
                    CheckedRelativeLayout checkedLinearLayout = (CheckedRelativeLayout) view;
                    checkedLinearLayout.toggle();
                    boolean selected = checkedLinearLayout.isChecked();

                    FormWithData formWithPatientData = getItem(position);
                    if (selected && !selectedFormsUuid.contains(formWithPatientData.getFormDataUuid())) {
                        selectedFormsUuid.add(formWithPatientData.getFormDataUuid());
                        checkedLinearLayout.setActivated(true);
                    } else if (!selected && selectedFormsUuid.contains(formWithPatientData.getFormDataUuid())) {<commit_msg>MUZIMA-588: Changed Simple date format error<commit_after>/*
 * Copyright (c) 2014 - 2018. The Trustees of Indiana University, Moi University
 * and Vanderbilt University Medical Center.
 *
 * This version of the code is licensed under the MPL 2.0 Open Source license
 * with additional health care disclaimer.
 * If the user is an entity intending to commercialize any application that uses
 *  this code in a for-profit venture,please contact the copyright holder.
 */
package com.muzima.adapters.forms;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersAdapter;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersSectionIndexerAdapterWrapper;
import com.muzima.R;
import com.muzima.api.model.Patient;
import com.muzima.controller.FormController;
import com.muzima.model.FormWithData;
import com.muzima.utils.Fonts;
import com.muzima.utils.PatientComparator;
import com.muzima.utils.StringUtils;
import com.muzima.view.CheckedRelativeLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.muzima.utils.Constants.STANDARD_DATE_FORMAT;

/**
 * Responsible to list down the forms in the order of the Patient details. Here you can identify forms by the patient name.
 * @param <T> T is of the type FormsWithData.
 */
public abstract class SectionedFormsAdapter<T extends FormWithData> extends FormsAdapter<T> implements StickyListHeadersAdapter, SectionIndexer {
    private static final String TAG = "SectionedFormsAdapter";

    private List<Patient> patients;
    private ListView listView;
    private final PatientComparator patientComparator;
    private final List<String> selectedFormsUuid;
    private MuzimaClickListener muzimaClickListener;

    SectionedFormsAdapter(Context context, int textViewResourceId, FormController formController) {
        super(context, textViewResourceId, formController);
        patients = new ArrayList<>();
        patientComparator = new PatientComparator();
        selectedFormsUuid = new ArrayList<>();
    }

    @Override
    protected int getFormItemLayout() {
        return R.layout.item_forms_list_selectable;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if(!isEmpty()) {
            HeaderViewHolder holder;
            if (convertView == null) {
                holder = new HeaderViewHolder();
                LayoutInflater layoutInflater = LayoutInflater.from(getContext());
                convertView = layoutInflater.inflate(R.layout.layout_forms_list_section_header, parent, false);
                holder.patientName = convertView.findViewById(R.id.patientName);
                holder.patientIdentifier = convertView.findViewById(R.id.patientId);
                setClickListenersOnView(position, convertView);
                convertView.setTag(holder);
            } else {
                holder = (HeaderViewHolder) convertView.getTag();
            }

            if (!patients.isEmpty()) {
                Patient patient = patients.get(getSectionForPosition(position));
                if (patient != null) {
                    holder.patientName.setText(patient.getDisplayName());
                    holder.patientIdentifier.setText(patient.getIdentifier());
                } else {
                    holder.patientName.setText(getContext().getString(R.string.default_form_register));
                }
            }
        }
        return convertView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    @Override
    public long getHeaderId(int position) {
        int section = 0;
        if (!patients.isEmpty() && !isEmpty()) {
            section = patients.indexOf(getItem(position).getPatient());
        }
        return section;
    }

    @Override
    public Object[] getSections() {
        String[] familyNames = new String[patients.size()];
        for (int i = 0; i < patients.size(); i++) {
            familyNames[i] = String.valueOf(patients.get(i).getFamilyName().charAt(0));
        }
        return familyNames;
    }

    @Override
    public int getPositionForSection(int section) {
        if (section >= patients.size()) {
            section = patients.size() - 1;
        } else if (section < 0) {
            section = 0;
        }

        int position = 0;
        for (int i = 0; i < getCount(); i++) {
            Patient patient = getItem(i).getPatient();
            if (patient != null && patient.equals(patients.get(section))) {
                position = i;
                break;
            }
        }
        return position;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= getCount()) {
            position = getCount() - 1;
        } else if (position < 0) {
            position = 0;
        }

        int section = 0;
        if (!patients.isEmpty()){
            Patient patient = getItem(position).getPatient();
            if (patient != null) {
                section = patients.indexOf(patient);
            }
        }
        return section;
    }

    void sortFormsByPatientName(List<T> forms) {
        Collections.sort(patients, patientComparator);
        Collections.sort(forms, alphabaticalComparator);
        setNotifyOnChange(false);
        clear();
        addAll(forms);

        StickyListHeadersSectionIndexerAdapterWrapper adapter = (StickyListHeadersSectionIndexerAdapterWrapper) listView.getAdapter();
        adapter.notifyDataSetChanged();
    }

    List<Patient> getPatients() {
        return patients;
    }

    void setPatients(List<Patient> patients) {
        this.patients = patients;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        if(!isEmpty()) {
            setClickListenersOnView(position, convertView);
            FormWithData form = getItem(position);

            String formSaveTime = null;
            if (form.getLastModifiedDate() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat(STANDARD_DATE_FORMAT.concat(" HH:mm:ss"));
                formSaveTime = dateFormat.format(form.getLastModifiedDate());
            }

            ViewHolder holder = (ViewHolder) convertView.getTag();

            if (!StringUtils.isEmpty(formSaveTime)) {
                holder.savedTime.setText(formSaveTime);
            }
            holder.savedTime.setTypeface(Fonts.roboto_italic(getContext()));
            holder.savedTime.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private void setClickListenersOnView(final int position, View convertView) {
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (view instanceof CheckedRelativeLayout) {
                    CheckedRelativeLayout checkedLinearLayout = (CheckedRelativeLayout) view;
                    checkedLinearLayout.toggle();
                    boolean selected = checkedLinearLayout.isChecked();

                    FormWithData formWithPatientData = getItem(position);
                    if (selected && !selectedFormsUuid.contains(formWithPatientData.getFormDataUuid())) {
                        selectedFormsUuid.add(formWithPatientData.getFormDataUuid());
                        checkedLinearLayout.setActivated(true);
                    } else if (!selected && selectedFormsUuid.contains(formWithPatientData.getFormDataUuid())) {
{'fraction_non_alphanumeric': 0.09200603318250378, 'fraction_numerical': 0.0029537456008044242, 'mean_word_length': 2.5969710669077757, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6441170', 'n_tokens_mistral': 4193, 'n_tokens_neox': 3952, 'n_words': 936}