<commit_before>package bg.mentormate.academy.radarapp.views;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import bg.mentormate.academy.radarapp.Constants;
import bg.mentormate.academy.radarapp.R;
import bg.mentormate.academy.radarapp.activities.ProfileActivity;
import bg.mentormate.academy.radarapp.activities.RoomActivity;
import bg.mentormate.academy.radarapp.models.Room;
import bg.mentormate.academy.radarapp.models.User;
import bg.mentormate.academy.radarapp.tools.AlertHelper;

/**
 * Created by tl on 18.02.15.
 */
public class RoomItem extends LinearLayout implements View.OnClickListener {

    private static final String USER_ID = "USER_ID";

    private TextView mTvRoomName;
    private TextView mTvUsername;
    private ParseImageView mPivAvatar;
    private RegisterButton mRbRegister;
    private Button mBtnJoin;

    private User mCurrentUser;
    private Room mRoom;

    public RoomItem(Context context) {
        super(context);
        init();
    }

    public RoomItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.item_room, this);

        mTvRoomName = (TextView) findViewById(R.id.tvRoomName);
        mTvUsername = (TextView) findViewById(R.id.tvUsername);
        mPivAvatar = (ParseImageView) findViewById(R.id.pivAvatar);
        mRbRegister = (RegisterButton) findViewById(R.id.rbRegister);
        mBtnJoin = (Button) findViewById(R.id.btnJoin);

        mTvUsername.setOnClickListener(this);
        mPivAvatar.setOnClickListener(this);
        mBtnJoin.setOnClickListener(this);
        mRbRegister.setOnClickListener(this);
    }

    public void setData(User currentUser,Room room) {
        mRoom = room;
        mCurrentUser = currentUser;

        mRoom.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject parseObject, ParseException e) {
                mTvRoomName.setText(mRoom.getName());

                final User user = mRoom.getCreatedBy();

                user.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject parseObject, ParseException e) {
                        mTvUsername.setText(user.getUsername());
                        mPivAvatar.setParseFile(user.getAvatar());
                        mPivAvatar.loadInBackground();
                    }
                });

                mCurrentUser.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject parseObject, ParseException e) {
                        mRbRegister.setData(mCurrentUser, mRoom);

                        if (mRbRegister.isChecked()) {
                            setRegisteredVisibility();
                        } else {
                            setUnregisteredVisibility();
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.tvUsername:
            case R.id.pivAvatar:
                goToProfile();
                break;
            case R.id.rbRegister:
                onRegisterClicked();
                break;
            case R.id.btnJoin:
                onJoinClicked();
                break;
        }
    }

    private void onRegisterClicked() {
        if (!mRbRegister.isChecked()) {
            removeUserFromRoom();
        } else {
            checkForPassKey();<commit_msg>bug fix - check for the existing of the user before remove or add to the list, set visibilities after every click of the Register/Unregister button<commit_after>package bg.mentormate.academy.radarapp.views;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import bg.mentormate.academy.radarapp.Constants;
import bg.mentormate.academy.radarapp.R;
import bg.mentormate.academy.radarapp.activities.ProfileActivity;
import bg.mentormate.academy.radarapp.activities.RoomActivity;
import bg.mentormate.academy.radarapp.models.Room;
import bg.mentormate.academy.radarapp.models.User;
import bg.mentormate.academy.radarapp.tools.AlertHelper;

/**
 * Created by tl on 18.02.15.
 */
public class RoomItem extends LinearLayout implements View.OnClickListener {

    private static final String USER_ID = "USER_ID";

    private TextView mTvRoomName;
    private TextView mTvUsername;
    private ParseImageView mPivAvatar;
    private RegisterButton mRbRegister;
    private Button mBtnJoin;

    private User mCurrentUser;
    private Room mRoom;

    public RoomItem(Context context) {
        super(context);
        init();
    }

    public RoomItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.item_room, this);

        mTvRoomName = (TextView) findViewById(R.id.tvRoomName);
        mTvUsername = (TextView) findViewById(R.id.tvUsername);
        mPivAvatar = (ParseImageView) findViewById(R.id.pivAvatar);
        mRbRegister = (RegisterButton) findViewById(R.id.rbRegister);
        mBtnJoin = (Button) findViewById(R.id.btnJoin);

        mTvUsername.setOnClickListener(this);
        mPivAvatar.setOnClickListener(this);
        mBtnJoin.setOnClickListener(this);
        mRbRegister.setOnClickListener(this);
    }

    public void setData(User currentUser,Room room) {
        mRoom = room;
        mCurrentUser = currentUser;

        mRoom.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject parseObject, ParseException e) {
                mTvRoomName.setText(mRoom.getName());

                final User user = mRoom.getCreatedBy();

                user.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject parseObject, ParseException e) {
                        mTvUsername.setText(user.getUsername());
                        mPivAvatar.setParseFile(user.getAvatar());
                        mPivAvatar.loadInBackground();
                    }
                });

                mCurrentUser.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject parseObject, ParseException e) {
                        mRbRegister.setData(mCurrentUser, mRoom);

                        if (mRbRegister.isChecked()) {
                            setRegisteredVisibility();
                        } else {
                            setUnregisteredVisibility();
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.tvUsername:
            case R.id.pivAvatar:
                goToProfile();
                break;
            case R.id.rbRegister:
                onRegisterClicked();
                break;
            case R.id.btnJoin:
                onJoinClicked();
                break;
        }
    }

    private void onRegisterClicked() {
        if (!mRbRegister.isChecked()) {
            if (mRoom.getUsers().contains(mCurrentUser)) {
                removeUserFromRoom();
            } else {
                setUnregisteredVisibility();
            }
        } else {
            if (!mRoom.getUsers().contains(mCurrentUser)) {
                checkForPassKey();
            } else {
                setRegisteredVisibility();
            }
{'fraction_non_alphanumeric': 0.08945396753320467, 'fraction_numerical': 0.0013622431604041322, 'mean_word_length': 2.166786484543494, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1921347', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2314, 'n_words': 451}