<commit_before>package com.razortooth.smile.ui.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.razortooth.smile.R;
import com.razortooth.smile.bu.BoardManager;
import com.razortooth.smile.bu.Constants;
import com.razortooth.smile.bu.QuestionsManager;
import com.razortooth.smile.bu.exception.DataAccessException;
import com.razortooth.smile.domain.Board;
import com.razortooth.smile.domain.Question;
import com.razortooth.smile.domain.Results;
import com.razortooth.smile.ui.GeneralActivity;
import com.razortooth.smile.ui.adapter.QuestionListAdapter;
import com.razortooth.smile.util.ActivityUtil;

public class QuestionsFragment extends AbstractFragment {

    private final List<Question> questions = new ArrayList<Question>();
    private List<Question> listQuestionsSelected = new ArrayList<Question>();

    private ArrayAdapter<Question> adapter;

    private Button btSave;

    private ListView lvListQuestions;
    private TextView tvServer;

    private String ip;
    private Results results;

    private boolean run;
    private boolean loadItems;

    @Override
    protected int getLayout() {
        return R.layout.questions;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        btSave = (Button) getActivity().findViewById(R.id.bt_save);
        lvListQuestions = (ListView) getActivity().findViewById(R.id.lv_questions);
        tvServer = (TextView) getActivity().findViewById(R.id.tv_server);
    }

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

        btSave.setEnabled(false);
        btSave.setOnClickListener(new SaveButtonListener());

        ip = getActivity().getIntent().getStringExtra(GeneralActivity.PARAM_IP);
        results = (Results) getActivity().getIntent().getSerializableExtra(
            GeneralActivity.PARAM_RESULTS);

        tvServer.setText("CONNECTED TO\n" + ip);

        adapter = new QuestionListAdapter(getActivity(), questions, results, ip);

        lvListQuestions.setAdapter(adapter);
        lvListQuestions.setOnItemClickListener(new CheckedItemListener());
        lvListQuestions.setItemsCanFocus(false);
        lvListQuestions.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        run = true;
        loadItems = true;
    }

    @Override
    public void onStop() {
        super.onStop();

        run = false;
    }

    private void loadSelections() {
        for (int i = 0; i < questions.size(); i++) {
            if (!lvListQuestions.isItemChecked(i)) {
                lvListQuestions.setItemChecked(i, true);
                listQuestionsSelected.add(questions.get(i));
            }
        }

        if (!questions.isEmpty() && !listQuestionsSelected.isEmpty()) {
            btSave.setEnabled(true);
        } else {
            btSave.setEnabled(false);
        }
    }

    private class CheckedItemListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

            if (lvListQuestions.isItemChecked(position)) {
                Question question = adapter.getItem(position);
                listQuestionsSelected.add(question);
            } else {
                Question question = adapter.getItem(position);
                for (Iterator<Question> iterator = listQuestionsSelected.iterator(); iterator
                    .hasNext();) {
                    Question item = iterator.next();
                    if (item.getNumber() == question.getNumber()) {
                        iterator.remove();
                    }
                }
            }

            if (!listQuestionsSelected.isEmpty()) {
                btSave.setEnabled(true);
            } else {
                btSave.setEnabled(false);
            }
        }
    }

    @Override
    public void updateFragment(final Board board) {

        questions.clear();

        if (run) {
            Collection<Question> newQuestions = null;
            newQuestions = board.getQuestions();

            if (newQuestions != null) {
                questions.addAll(newQuestions);
            }

            new UpdateResultsTask(getActivity()).execute();

            if (loadItems) {
                listQuestionsSelected.clear();

                loadSelections();
                loadItems = false;
            }
        }

        adapter.notifyDataSetChanged();

    }

    private class SaveButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            FragmentActivity activity = QuestionsFragment.this.getActivity();

            Dialog saveDialog = new Dialog(activity, R.style.Dialog);
            saveDialog.setContentView(R.layout.save);
            Display displaySize = ActivityUtil.getDisplaySize(activity);
            saveDialog.getWindow().setLayout(displaySize.getWidth(), displaySize.getHeight());
            saveDialog.show();

            Button save = (Button) saveDialog.findViewById(R.id.bt_save_file);
            save.setOnClickListener(new SaveFileDialogListener(saveDialog));
        }<commit_msg>Fixed problem with %correct<commit_after>package com.razortooth.smile.ui.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.razortooth.smile.R;
import com.razortooth.smile.bu.BoardManager;
import com.razortooth.smile.bu.Constants;
import com.razortooth.smile.bu.QuestionsManager;
import com.razortooth.smile.bu.exception.DataAccessException;
import com.razortooth.smile.domain.Board;
import com.razortooth.smile.domain.Question;
import com.razortooth.smile.domain.Results;
import com.razortooth.smile.ui.GeneralActivity;
import com.razortooth.smile.ui.adapter.QuestionListAdapter;
import com.razortooth.smile.util.ActivityUtil;

public class QuestionsFragment extends AbstractFragment {

    private final List<Question> questions = new ArrayList<Question>();
    private List<Question> listQuestionsSelected = new ArrayList<Question>();

    private ArrayAdapter<Question> adapter;

    private Button btSave;

    private ListView lvListQuestions;
    private TextView tvServer;

    private String ip;
    private Results results;

    private boolean run;
    private boolean loadItems;

    @Override
    protected int getLayout() {
        return R.layout.questions;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        btSave = (Button) getActivity().findViewById(R.id.bt_save);
        lvListQuestions = (ListView) getActivity().findViewById(R.id.lv_questions);
        tvServer = (TextView) getActivity().findViewById(R.id.tv_server);
    }

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

        btSave.setEnabled(false);
        btSave.setOnClickListener(new SaveButtonListener());

        ip = getActivity().getIntent().getStringExtra(GeneralActivity.PARAM_IP);
        results = (Results) getActivity().getIntent().getSerializableExtra(
            GeneralActivity.PARAM_RESULTS);

        tvServer.setText("CONNECTED TO\n" + ip);

        listQuestions();

        run = true;
        loadItems = true;
    }

    private void listQuestions() {
        adapter = new QuestionListAdapter(getActivity(), questions, results, ip);

        lvListQuestions.setAdapter(adapter);
        lvListQuestions.setOnItemClickListener(new CheckedItemListener());
        lvListQuestions.setItemsCanFocus(false);
        lvListQuestions.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    @Override
    public void onStop() {
        super.onStop();

        run = false;
    }

    private void loadSelections() {
        for (int i = 0; i < questions.size(); i++) {
            if (!lvListQuestions.isItemChecked(i)) {
                lvListQuestions.setItemChecked(i, true);
                listQuestionsSelected.add(questions.get(i));
            }
        }

        if (!questions.isEmpty() && !listQuestionsSelected.isEmpty()) {
            btSave.setEnabled(true);
        } else {
            btSave.setEnabled(false);
        }
    }

    private class CheckedItemListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

            if (lvListQuestions.isItemChecked(position)) {
                Question question = adapter.getItem(position);
                listQuestionsSelected.add(question);
            } else {
                Question question = adapter.getItem(position);
                for (Iterator<Question> iterator = listQuestionsSelected.iterator(); iterator
                    .hasNext();) {
                    Question item = iterator.next();
                    if (item.getNumber() == question.getNumber()) {
                        iterator.remove();
                    }
                }
            }

            if (!listQuestionsSelected.isEmpty()) {
                btSave.setEnabled(true);
            } else {
                btSave.setEnabled(false);
            }
        }
    }

    @Override
    public void updateFragment(final Board board) {

        questions.clear();

        if (run) {
            Collection<Question> newQuestions = null;
            newQuestions = board.getQuestions();

            if (newQuestions != null) {
                questions.addAll(newQuestions);
            }

            new UpdateResultsTask(getActivity()).execute();

            if (loadItems) {
                listQuestionsSelected.clear();

                loadSelections();
                loadItems = false;
            }

            listQuestions();
        }

        adapter.notifyDataSetChanged();

    }

    private class SaveButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            FragmentActivity activity = QuestionsFragment.this.getActivity();

            Dialog saveDialog = new Dialog(activity, R.style.Dialog);
            saveDialog.setContentView(R.layout.save);
            Display displaySize = ActivityUtil.getDisplaySize(activity);
            saveDialog.getWindow().setLayout(displaySize.getWidth(), displaySize.getHeight());
            saveDialog.show();

            Button save = (Button) saveDialog.findViewById(R.id.bt_save_file);
            save.setOnClickListener(new SaveFileDialogListener(saveDialog));
        }
{'fraction_non_alphanumeric': 0.09857714918633381, 'fraction_numerical': 0.0006816051801993695, 'mean_word_length': 2.6284389489953632, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, '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': '6724743', 'n_tokens_mistral': 3155, 'n_tokens_neox': 2944, 'n_words': 586}