Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

First impl #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
}
135 changes: 134 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,147 @@
package ru.ok.technopolis.students;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

private List<Student> students;
private StudentAdapter studentAdapter;
private int pics[];
private Student currentStudent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
students = new ArrayList<>();
pics = new int[6];
currentStudent = null;
setContentView(R.layout.activity_main);
setupRecyclerView();
setupAddButton();
setupSaveButton();
setupDeleteButton();
setupPics();
}

private void setupRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.activity_main__rv_students);
studentAdapter = new StudentAdapter(students, this::onStudentClick);
recyclerView.setAdapter(studentAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
}

private void setupPics() {
pics[0] = R.drawable.male_1;
pics[1] = R.drawable.male_2;
pics[2] = R.drawable.male_3;
pics[3] = R.drawable.female_1;
pics[4] = R.drawable.female_2;
pics[5] = R.drawable.female_3;
}

private void setupAddButton() {
Button button = findViewById(R.id.activity_main__btn_add);
button.setOnClickListener(v -> onAddClick());
}

private void onAddClick() {
setupEdit("", "", false, 0);
currentStudent = new Student("", "", false, 0);
studentAdapter.resetSelectionPos();
studentAdapter.notifyDataSetChanged();
}

private void setupEdit(String name, String familyName, Boolean isMale, int pic) {
EditText nameEdit = findViewById(R.id.names_layout__edit_name);
EditText familyNameEdit = findViewById(R.id.names_layout__edit_fname);
CheckBox male = findViewById(R.id.sex_layout__checkbox);
CircleImageView pics = findViewById(R.id.data_layout__edit_img);
nameEdit.setText(name);
familyNameEdit.setText(familyName);
male.setChecked(isMale);
pics.setImageResource(pic);
}

private void setupSaveButton() {
Button button = findViewById(R.id.cl_edit__save);
button.setOnClickListener(v -> onSaveClick());
}

private void setupDeleteButton() {
Button button = findViewById(R.id.cl_edit__delete);
button.setOnClickListener(v -> onDeleteClick());
}

private void onDeleteClick() {
if (currentStudent == null) {
Toast.makeText(this, R.string.empty_student, Toast.LENGTH_SHORT).show();
} else {
students.remove(currentStudent);
currentStudent = null;
studentAdapter.resetSelectionPos();
studentAdapter.notifyDataSetChanged();
setupEdit("", "", Boolean.FALSE, 0);
}
}

private void onSaveClick() {
EditText name = findViewById(R.id.names_layout__edit_name);
EditText familyName = findViewById(R.id.names_layout__edit_fname);
CheckBox male = findViewById(R.id.sex_layout__checkbox);
CircleImageView pic = findViewById(R.id.data_layout__edit_img);
int picId;
if ((name.getText() != null) && (familyName.getText() != null)) {
if (TextUtils.isEmpty(name.getText().toString().trim())
|| TextUtils.isEmpty(familyName.getText().toString().trim())) {
Toast.makeText(this, R.string.wrong_enter, Toast.LENGTH_SHORT).show();
} else {
if (currentStudent == null || currentStudent.getFirstName().equals("")) {
students.add(currentStudent = new Student(name.getText().toString().trim(),
familyName.getText().toString().trim(),
male.isChecked(),
picId = getRandomPick(male.isChecked())));
pic.setImageResource(picId);
studentAdapter.resetSelectionPos();
studentAdapter.notifyDataSetChanged();
} else {
currentStudent.setFirstName(name.getText().toString().trim());
currentStudent.setSecondName(familyName.getText().toString().trim());
if (currentStudent.isMaleGender() != male.isChecked()) {
currentStudent.setPhoto(picId = getRandomPick(male.isChecked()));
currentStudent.setMaleGender(male.isChecked());
pic.setImageResource(picId);
}
studentAdapter.notifyDataSetChanged();
}
}
}

}

private int getRandomPick(Boolean isMale) {
Random random = new Random();
int maleRandom = isMale ? 0 : 3;
return pics[maleRandom + random.nextInt(3)];
}

private void onStudentClick(Student student) {
setupEdit(student.getFirstName(), student.getSecondName(), student.isMaleGender(), student.getPhoto());
currentStudent = student;
}

}
93 changes: 93 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ru.ok.technopolis.students;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

private final List<Student> students;
private final Listener onStudentClickListener;
private static final int SELECTED_COLOR = Color.parseColor("#F0F0F0");
private int selectedPosition;

public StudentAdapter(List<Student> students, Listener onStudentClickListener) {
this.students = students;
this.onStudentClickListener = onStudentClickListener;
}

@NonNull
@Override
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_item, viewGroup, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onStudentClickListener.onStudentClick((Student) v.getTag());
selectedPosition = ((RecyclerView.LayoutParams) v.getLayoutParams()).getViewLayoutPosition();
notifyDataSetChanged();
}
});
return new StudentViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull StudentViewHolder holder, final int position) {
Student student = students.get(position);
holder.bind(student);
holder.itemView.setTag(student);
selectionSetup(holder);
}

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

private void selectionSetup(StudentViewHolder holder) {
if (selectedPosition == holder.getLayoutPosition()) {
holder.itemView.setBackgroundColor(SELECTED_COLOR);
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
}

public void resetSelectionPos() {
selectedPosition = RecyclerView.NO_POSITION;
}

interface Listener {

void onStudentClick(Student student);

}

static final class StudentViewHolder extends RecyclerView.ViewHolder {

private final CircleImageView profileImage;
private final TextView nameTextView;
private final TextView familyNameTextView;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);
profileImage = itemView.findViewById(R.id.student_item__img);
nameTextView = itemView.findViewById(R.id.student_item__name);
familyNameTextView = itemView.findViewById(R.id.student_item__fname);
}

private void bind(@NonNull Student student) {
nameTextView.setText(student.getFirstName());
familyNameTextView.setText(student.getSecondName());
profileImage.setImageResource(student.getPhoto());
}

}
}
Loading