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

HW2 #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

HW2 #16

Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 3 additions & 6 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
Expand All @@ -25,4 +29,6 @@ dependencies {
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'
}
156 changes: 156 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,169 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
irvasek marked this conversation as resolved.
Show resolved Hide resolved
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 StudentsAdapter studentsAdapter;
private Student currentStudent;
private EditText firstNameView;
private EditText lastNameView;
private CheckBox genderView;
private CircleImageView photoView;
private Button buttonAdd;
private Button buttonSave;
private Button buttonDelete;
final private Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.students_list);
students = generateStudentList();
studentsAdapter = new StudentsAdapter(students, this::onStudentClick);
recyclerView.setAdapter(studentsAdapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
firstNameView = findViewById(R.id.student_first_name);
lastNameView = findViewById(R.id.student_last_name);
genderView = findViewById(R.id.student_gender);
photoView = findViewById(R.id.student_photo);
buttonAdd = findViewById(R.id.button_add);
buttonSave = findViewById(R.id.button_save);
buttonDelete = findViewById(R.id.button_delete);
setupAddButton();
setupDeleteButton();
setupSaveButton();
}

private void setupAddButton() {
buttonAdd.setOnClickListener(v -> {
currentStudent = generateNewStudent();
if (currentStudent != null) {
students.add(currentStudent);
studentsAdapter.notifyDataSetChanged();
cleanCard();
}
});
}

private void setupSaveButton() {
buttonSave.setOnClickListener(v -> {
if (currentStudent != null) {
if(editStudent()) {
studentsAdapter.notifyDataSetChanged();
cleanCard();
}
return;
}
Toast toast = Toast.makeText(getApplicationContext(),
"Choose a student", Toast.LENGTH_SHORT);
toast.show();
});
}

private void setupDeleteButton() {
buttonDelete.setOnClickListener(v -> {
int index = students.indexOf(currentStudent);
if (index != -1) {
students.remove(currentStudent);
studentsAdapter.notifyDataSetChanged();
cleanCard();
return;
}
Toast toast = Toast.makeText(getApplicationContext(),
"Choose a student", Toast.LENGTH_SHORT);
toast.show();
});
}

private void cleanCard() {
firstNameView.setText("");
lastNameView.setText("");
genderView.setChecked(false);
photoView.setImageResource(R.color.colorGray);
currentStudent = null;
}

public void onStudentClick(Student student) {
currentStudent = student;
setStudentCard();
}

private void setStudentCard() {
firstNameView.setText(currentStudent.getFirstName());
lastNameView.setText(currentStudent.getSecondName());
genderView.setChecked(currentStudent.isMaleGender());
photoView.setImageResource(currentStudent.getPhoto());
}

private int choosePhoto(boolean isMale){
irvasek marked this conversation as resolved.
Show resolved Hide resolved
String photoName;
if (isMale) {
photoName = "male_";
} else {
photoName = "female_";
}
return getResources().getIdentifier(photoName + (random.nextInt(3)+1), "drawable", getPackageName());
irvasek marked this conversation as resolved.
Show resolved Hide resolved

}

private Student generateNewStudent() {
String firstName = firstNameView.getText().toString();
irvasek marked this conversation as resolved.
Show resolved Hide resolved
String lastName = lastNameView.getText().toString();
if (firstName.length() != 0 && lastName.length() != 0) {
boolean isMale = genderView.isChecked();
return new Student(firstName, lastName, isMale, choosePhoto(isMale));
}
Toast toast = Toast.makeText(getApplicationContext(),
irvasek marked this conversation as resolved.
Show resolved Hide resolved
irvasek marked this conversation as resolved.
Show resolved Hide resolved
"Fill first name and last name", Toast.LENGTH_SHORT);
toast.show();
return null;
}

private boolean editStudent() {
String firstName = firstNameView.getText().toString();
String lastName = lastNameView.getText().toString();
if (firstName.length() != 0 && lastName.length() != 0) {
currentStudent.setFirstName(firstName);
currentStudent.setSecondName(lastName);
boolean oldIsMale = currentStudent.isMaleGender();
currentStudent.setMaleGender(genderView.isChecked());
if (oldIsMale != currentStudent.isMaleGender()) {
currentStudent.setPhoto(choosePhoto(currentStudent.isMaleGender()));
cleanCard();
}
return true;
}
Toast toast = Toast.makeText(getApplicationContext(),
"Fill first name and last name", Toast.LENGTH_SHORT);
toast.show();
return false;
}

private List<Student> generateStudentList() {
List<Student> students = new ArrayList<>();
students.add(new Student("Ivan", "Ivanov", true, R.drawable.male_1));
students.add(new Student("Rita", "Ritina", false, R.drawable.female_1));
students.add(new Student("Petr", "Petrov", true, R.drawable.male_2));
students.add(new Student("Masha", "Mashina", false, R.drawable.female_2));
students.add(new Student("Kyzya", "Kyzin", true, R.drawable.male_3));
students.add(new Student("Tanya", "Tanina", false, R.drawable.female_3));
return students;
}
}
68 changes: 68 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.ok.technopolis.students;

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.ImageView;
import android.widget.TextView;

import java.util.List;

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

irvasek marked this conversation as resolved.
Show resolved Hide resolved
private final List<Student> students;
private final Listener onStudentClickListener;

public StudentsAdapter(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(v -> onStudentClickListener.onStudentClick((Student) v.getTag()));
return new StudentViewHolder(view);
}

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

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

static final class StudentViewHolder extends RecyclerView.ViewHolder {

private final TextView nameEditView;
irvasek marked this conversation as resolved.
Show resolved Hide resolved
private final ImageView photoImageView;

private StudentViewHolder(@NonNull View itemView) {
super(itemView);
nameEditView = itemView.findViewById(R.id.student_item_name);
photoImageView = itemView.findViewById(R.id.student_item_photo);
}

private void bind(@NonNull Student student) {
nameEditView.setText(student.getFirstName() + " " + student.getSecondName());
photoImageView.setImageResource(student.getPhoto());
}

}

interface Listener {

void onStudentClick(Student student);

}


}
Loading