<commit_before>package com.ashish.cop290assign0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.github.siyamed.shapeimageview.CircularImageView;

import org.json.JSONObject;

public final class PagerFragment extends Fragment {
    private String name,entryCode,image,teamName;
    private  int visibility;
    LinearLayout l;
    public static PagerFragment newInstance(int p,int v, String n,String e,String i,String t) {
        PagerFragment fragment = new PagerFragment();
//        try {
//            byte[] b = Base64.decode(img, Base64.DEFAULT);
//            fragment.image = BitmapFactory.decodeByteArray(b, 0, b.length);
//        }catch(Exception e){
//            fragment.image = null;
//        }
        fragment.position = p;
        fragment.visibility = v;
        fragment.name = n;
        fragment.entryCode = e;
        fragment.image = i;
        fragment.teamName = t;
        return fragment;
    }
    private int position = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            if(savedInstanceState.containsKey("teamName"))
                teamName = savedInstanceState.getString("teamName");
            name = savedInstanceState.getString("name");
            entryCode = savedInstanceState.getString("entryCode");
        }
    }

    private void init(LinearLayout layout){
        if(visibility == 1){
            layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
            layout.findViewById(R.id.input_layout).setVisibility(View.GONE);
        }
        if(position == 0) {
            ((TextView)layout.findViewById(R.id.display_team_name)).setText(teamName);
            ((TextView)layout.findViewById(R.id.display_team_name)).setMovementMethod(new ScrollingMovementMethod());
            ((EditText)layout.findViewById(R.id.team_name)).setText(teamName);
        }else{
            ((TextView)layout.findViewById(R.id.display_name)).setText(name);
            ((TextView)layout.findViewById(R.id.display_entry_code)).setText(entryCode);
            if(image!=null && !image.isEmpty())
                ((ImageView)layout.findViewById(R.id.img)).setImageBitmap(decodeBase64(image));
            ((EditText)layout.findViewById(R.id.name)).setText(name);
            ((EditText)layout.findViewById(R.id.entryCode)).setText(entryCode);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,final ViewGroup container, Bundle savedInstanceState) {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        View child;
        if(position == 0) {
            child = inflater.inflate(R.layout.team_input_layout, null);
            layout.addView(child);

        }
        else {
            child = inflater.inflate(R.layout.student_info_layout, null);
            layout.addView(child);
            addOnTextChangeListener(layout);
            ((TextView) layout.findViewById(R.id.member_no)).setText("#" + position);
        }
        l = layout;
        init(layout);
        setOnClickListeners(layout);
        return layout;
    }
    private boolean isInvalid = false;
    
    private void setOnClickListeners(final LinearLayout layout){

        layout.findViewById(R.id.save_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position != 0) {
                    Log.d("save","Btn Pressed!");
                    String entryCode = ((EditText) layout.findViewById(R.id.entryCode)).getText().toString().trim();
                    String name = ((EditText) layout.findViewById(R.id.name)).getText().toString().trim();
                    Log.d("save_data",String.format("entry number = %s, name = %s",entryCode,name));
                    boolean isEntryCodeValid = InputValidator.validateEntryCode(entryCode);
                    boolean isNameValid = InputValidator.validateName(name);
                    if (!entryCode.isEmpty() && !name.isEmpty()) {
                        //boolean isInvalid = false;
                        //isInvalid = false; //cannot do this
                        if (!isEntryCodeValid) {
                            ((EditText) layout.findViewById(R.id.entryCode)).setError("Invalid entry no!");
                            isInvalid = true;
                        }
                        if (!isNameValid) {
                            ((EditText) layout.findViewById(R.id.name)).setError("Invalid name!");
                            isInvalid = true;
                        }
                        if (isInvalid) return;
                        MainActivity.names[position] = name;
                        MainActivity.entryCodes[position] = entryCode;
                        ((TextView) layout.findViewById(R.id.display_entry_code)).setText(entryCode);
                        ((TextView) layout.findViewById(R.id.display_name)).setText(name);
                        MainActivity.visibility[position] = 1;
                        hideKeyboard(v); //hidden keyboard
                        layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
                        layout.findViewById(R.id.input_layout).setVisibility(View.GONE);

                        if(position == 2 || position == 3) {
                            layout.findViewById(R.id.submit_bttn).setVisibility(View.VISIBLE);
                        }

                    } else {
                        if(name.isEmpty()) {
                            ((EditText) layout.findViewById(R.id.name)).setError("Name cannot be empty!");
                        }
                        if(entryCode.isEmpty()) {
                            ((EditText) layout.findViewById(R.id.entryCode)).setError("Entry Number cannot be empty!");
                        } else {
                            if(!isEntryCodeValid)
                                ((EditText) layout.findViewById(R.id.entryCode)).setError("Invalid entry no!");
                        }
                    }
                }else{
                    String teamName = ((EditText) layout.findViewById(R.id.team_name)).getText().toString().trim();
                    if(!teamName.isEmpty()){
                        MainActivity.teamName = teamName;
                        ((TextView) layout.findViewById(R.id.display_team_name)).setText(teamName);
                        MainActivity.visibility[position] = 1;
                        hideKeyboard(v); //hidden keyboard
                        layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
                        layout.findViewById(R.id.input_layout).setVisibility(View.GONE);
                    } else{
                        ((EditText) layout.findViewById(R.id.team_name)).setError("Team name cannot be empty");
                    }
                }
            }
        });
        layout.findViewById(R.id.edit_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.visibility[position] = 0;

                if(position == 2 || position == 3) {
                    layout.findViewById(R.id.submit_bttn).setVisibility(View.GONE);
                }

                layout.findViewById(R.id.display_layout).setVisibility(View.GONE);
                layout.findViewById(R.id.input_layout).setVisibility(View.VISIBLE);
            }
        });
    }

    private void addOnTextChangeListener(final LinearLayout layout){
        final EditText editText = ((EditText)layout.findViewById(R.id.entryCode)); //also entry number text box
        //EditText entryNumBox = (EditText) layout.findViewById(R.id.entryCode);
        final EditText nameBox = (EditText) layout.findViewById(R.id.name);
        final View okBttn = layout.findViewById(R.id.save_data);<commit_msg>add onClickListner in submit button.<commit_after>package com.ashish.cop290assign0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.github.siyamed.shapeimageview.CircularImageView;

import org.json.JSONObject;

public final class PagerFragment extends Fragment {
    private String name,entryCode,image,teamName;
    private  int visibility;
    LinearLayout l;
    public static PagerFragment newInstance(int p,int v, String n,String e,String i,String t) {
        PagerFragment fragment = new PagerFragment();
//        try {
//            byte[] b = Base64.decode(img, Base64.DEFAULT);
//            fragment.image = BitmapFactory.decodeByteArray(b, 0, b.length);
//        }catch(Exception e){
//            fragment.image = null;
//        }
        fragment.position = p;
        fragment.visibility = v;
        fragment.name = n;
        fragment.entryCode = e;
        fragment.image = i;
        fragment.teamName = t;
        return fragment;
    }
    private int position = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            if(savedInstanceState.containsKey("teamName"))
                teamName = savedInstanceState.getString("teamName");
            name = savedInstanceState.getString("name");
            entryCode = savedInstanceState.getString("entryCode");
        }
    }

    private void init(LinearLayout layout){
        if(visibility == 1){
            layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
            layout.findViewById(R.id.input_layout).setVisibility(View.GONE);
        }
        if(position == 0) {
            ((TextView)layout.findViewById(R.id.display_team_name)).setText(teamName);
            ((TextView)layout.findViewById(R.id.display_team_name)).setMovementMethod(new ScrollingMovementMethod());
            ((EditText)layout.findViewById(R.id.team_name)).setText(teamName);
        }else{
            ((TextView)layout.findViewById(R.id.display_name)).setText(name);
            ((TextView)layout.findViewById(R.id.display_entry_code)).setText(entryCode);
            if(image!=null && !image.isEmpty())
                ((ImageView)layout.findViewById(R.id.img)).setImageBitmap(decodeBase64(image));
            ((EditText)layout.findViewById(R.id.name)).setText(name);
            ((EditText)layout.findViewById(R.id.entryCode)).setText(entryCode);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,final ViewGroup container, Bundle savedInstanceState) {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        View child;
        if(position == 0) {
            child = inflater.inflate(R.layout.team_input_layout, null);
            layout.addView(child);

        }
        else {
            child = inflater.inflate(R.layout.student_info_layout, null);
            layout.addView(child);
            addOnTextChangeListener(layout);
            ((TextView) layout.findViewById(R.id.member_no)).setText("#" + position);
        }
        l = layout;
        init(layout);
        setOnClickListeners(layout);
        return layout;
    }
    private boolean isInvalid = false;
    
    private void setOnClickListeners(final LinearLayout layout){

        layout.findViewById(R.id.save_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position != 0) {
                    Log.d("save","Btn Pressed!");
                    String entryCode = ((EditText) layout.findViewById(R.id.entryCode)).getText().toString().trim();
                    String name = ((EditText) layout.findViewById(R.id.name)).getText().toString().trim();
                    Log.d("save_data",String.format("entry number = %s, name = %s",entryCode,name));
                    boolean isEntryCodeValid = InputValidator.validateEntryCode(entryCode);
                    boolean isNameValid = InputValidator.validateName(name);
                    if (!entryCode.isEmpty() && !name.isEmpty()) {
                        //boolean isInvalid = false;
                        //isInvalid = false; //cannot do this
                        if (!isEntryCodeValid) {
                            ((EditText) layout.findViewById(R.id.entryCode)).setError("Invalid entry no!");
                            isInvalid = true;
                        }
                        if (!isNameValid) {
                            ((EditText) layout.findViewById(R.id.name)).setError("Invalid name!");
                            isInvalid = true;
                        }
                        if (isInvalid) return;
                        MainActivity.names[position] = name;
                        MainActivity.entryCodes[position] = entryCode;
                        ((TextView) layout.findViewById(R.id.display_entry_code)).setText(entryCode);
                        ((TextView) layout.findViewById(R.id.display_name)).setText(name);
                        MainActivity.visibility[position] = 1;
                        hideKeyboard(v); //hidden keyboard
                        layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
                        layout.findViewById(R.id.input_layout).setVisibility(View.GONE);

                        if(position == 2 || position == 3) {
                            layout.findViewById(R.id.submit_bttn).setVisibility(View.VISIBLE);
                        }

                    } else {
                        if(name.isEmpty()) {
                            ((EditText) layout.findViewById(R.id.name)).setError("Name cannot be empty!");
                        }
                        if(entryCode.isEmpty()) {
                            ((EditText) layout.findViewById(R.id.entryCode)).setError("Entry Number cannot be empty!");
                        } else {
                            if(!isEntryCodeValid)
                                ((EditText) layout.findViewById(R.id.entryCode)).setError("Invalid entry no!");
                        }
                    }
                }else{
                    String teamName = ((EditText) layout.findViewById(R.id.team_name)).getText().toString().trim();
                    if(!teamName.isEmpty()){
                        MainActivity.teamName = teamName;
                        ((TextView) layout.findViewById(R.id.display_team_name)).setText(teamName);
                        MainActivity.visibility[position] = 1;
                        hideKeyboard(v); //hidden keyboard
                        layout.findViewById(R.id.display_layout).setVisibility(View.VISIBLE);
                        layout.findViewById(R.id.input_layout).setVisibility(View.GONE);
                    } else{
                        ((EditText) layout.findViewById(R.id.team_name)).setError("Team name cannot be empty");
                    }
                }
            }
        });
        layout.findViewById(R.id.edit_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.visibility[position] = 0;

                if(position == 2 || position == 3) {
                    layout.findViewById(R.id.submit_bttn).setVisibility(View.GONE);
                }

                layout.findViewById(R.id.display_layout).setVisibility(View.GONE);
                layout.findViewById(R.id.input_layout).setVisibility(View.VISIBLE);
            }
        });
        layout.findViewById(R.id.submit_bttn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //call method to send via volley
            }
        });
    }

    private void addOnTextChangeListener(final LinearLayout layout){
        final EditText editText = ((EditText)layout.findViewById(R.id.entryCode)); //also entry number text box
        //EditText entryNumBox = (EditText) layout.findViewById(R.id.entryCode);
        final EditText nameBox = (EditText) layout.findViewById(R.id.name);
        final View okBttn = layout.findViewById(R.id.save_data);
{'fraction_non_alphanumeric': 0.11197771587743732, 'fraction_numerical': 0.0028969359331476323, 'mean_word_length': 2.052891156462585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1370786', 'n_tokens_mistral': 4718, 'n_tokens_neox': 4392, 'n_words': 758}