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 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
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'
}
163 changes: 163 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,176 @@

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.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.activity_main_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.activity_main_student_first_name);
lastNameView = findViewById(R.id.activity_main_student_last_name);
genderView = findViewById(R.id.activity_main_student_gender);
photoView = findViewById(R.id.activity_main_student_photo);
buttonAdd = findViewById(R.id.activity_main_button_add);
buttonSave = findViewById(R.id.activity_main_button_save);
buttonDelete = findViewById(R.id.activity_main_button_delete);
setupAddButton();
setupDeleteButton();
setupSaveButton();
}

private void setupAddButton() {
buttonAdd.setOnClickListener(v -> {
currentStudent = generateNewStudent();
if (currentStudent != null) {
students.add(currentStudent);
studentsAdapter.notifyDataSetChanged();
cleanCard();
return;
}
Toast toast = Toast.makeText(this,
"Fill first name and last name", Toast.LENGTH_SHORT);
toast.show();
});
}

private void setupSaveButton() {
buttonSave.setOnClickListener(v -> {
if (currentStudent != null) {
if (editStudent()) {
studentsAdapter.notifyDataSetChanged();
cleanCard();
return;
}
Toast toast = Toast.makeText(this,
"Fill first name and last name", Toast.LENGTH_SHORT);
toast.show();
return;
}
Toast toast = Toast.makeText(this,
"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(this,
"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) {
String photoName;
if (isMale) {
photoName = "male_";
} else {
photoName = "female_";
}
return getResources().getIdentifier(photoName + (random.nextInt(3) + 1), "drawable", getPackageName());

}

private Student generateNewStudent() {
String firstName;
String lastName;
try {
firstName = firstNameView.getText().toString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше не заворачивать всю конструкцию в try-catch. Возьми отдельно значение getText и проверяй его

lastName = lastNameView.getText().toString();
} catch (NullPointerException e) {
return null;
}
if (firstName.length() != 0 && lastName.length() != 0) {
boolean isMale = genderView.isChecked();
return new Student(firstName, lastName, isMale, choosePhoto(isMale));
}
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;
}
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;
private int focusedItem = -1;

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 -> {
focusedItem = ((RecyclerView.LayoutParams) v.getLayoutParams()).getViewLayoutPosition();
notifyDataSetChanged();
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);
viewHolder.itemView.setSelected(focusedItem == i);
}

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

static final class StudentViewHolder extends RecyclerView.ViewHolder {
private final TextView nameView;
private final ImageView photoView;

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

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

interface Listener {
void onStudentClick(Student student);
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/background_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorGray" android:state_selected="true" />
</selector>
92 changes: 92 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.constraint.Guideline
android:id="@+id/activity_main_line_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />

<android.support.constraint.Guideline
android:id="@+id/activity_main_line_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />

<android.support.constraint.Guideline
android:id="@+id/activity_main_line_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />

<android.support.v7.widget.RecyclerView
android:id="@+id/activity_main_students_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/activity_main_button_add"
app:layout_constraintEnd_toStartOf="@+id/activity_main_line_right"
app:layout_constraintStart_toEndOf="@+id/activity_main_line_left"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/activity_main_button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_add"
app:layout_constraintBottom_toTopOf="@id/activity_main_student_photo"
app:layout_constraintEnd_toStartOf="@id/activity_main_line_right"
app:layout_constraintStart_toEndOf="@id/activity_main_line_left" />

<Button
android:id="@+id/activity_main_button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_delete"
app:layout_constraintBottom_toTopOf="@id/activity_main_line_bottom"
app:layout_constraintStart_toEndOf="@id/activity_main_line_left" />

<Button
android:id="@+id/activity_main_button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save"
app:layout_constraintBottom_toTopOf="@id/activity_main_line_bottom"
app:layout_constraintEnd_toStartOf="@id/activity_main_line_right" />

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/activity_main_student_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@color/colorGray"
app:layout_constraintBottom_toBottomOf="@id/activity_main_student_first_name"
app:layout_constraintStart_toEndOf="@id/activity_main_line_left" />

<EditText
android:id="@+id/activity_main_student_last_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/last_name"
android:maxLines="1"
app:layout_constraintBottom_toTopOf="@id/activity_main_student_first_name"
app:layout_constraintEnd_toStartOf="@id/activity_main_line_right"
app:layout_constraintStart_toEndOf="@id/activity_main_student_photo" />

<EditText
android:id="@+id/activity_main_student_first_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/first_name"
android:maxLines="1"
app:layout_constraintBottom_toTopOf="@id/activity_main_student_gender"
app:layout_constraintEnd_toStartOf="@id/activity_main_line_right"
app:layout_constraintStart_toEndOf="@id/activity_main_student_photo" />

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя строка


<CheckBox
android:id="@+id/activity_main_student_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
app:layout_constraintBottom_toTopOf="@id/activity_main_button_delete"
app:layout_constraintStart_toEndOf="@id/activity_main_student_photo" />

</android.support.constraint.ConstraintLayout>
Loading