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

Shakhov #20

Open
wants to merge 6 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
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'

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'
Expand Down
170 changes: 169 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,182 @@
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.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

private final List<Student> students = new ArrayList<>();
private Student currentStudent;
private EditText firstNameView;
private String firstName;
private EditText secondNameView;
private String secondName;
private Spinner genderView;
private ArrayAdapter genderAdapter;
Copy link

Choose a reason for hiding this comment

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

Для дженериков стоит указывать тип

private String gender;
private ImageView photoView;
private int photoResId = 0;
private StudentAdapter studentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Test data
students.add(new Student("Lyla", "Foy", "Андрогин", R.drawable.female_1));
students.add(new Student("Evgeny", "Sobko", "Бигендер", R.drawable.male_2));
students.add(new Student("Habib", "Nurmagomedov", "Дрэг", R.drawable.male_1));

firstNameView = findViewById(R.id.activity_main_student_first_name);
secondNameView = findViewById(R.id.activity_main_student_second_name);
genderView = findViewById(R.id.activity_main_student_gender);
genderAdapter = (ArrayAdapter) genderView.getAdapter();
photoView = findViewById(R.id.activity_main_student_photo);

findViewById(R.id.activity_main_button_remove).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentStudent != null) {
students.remove(currentStudent);
studentAdapter.notifyDataSetChanged();
}
setCurrentStudent(null);
}
});

findViewById(R.id.activity_main_button_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentStudent == null) {
saveNewStudent();
} else {
saveStudent();
}
}
});

findViewById(R.id.activity_main_button_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addStudent();
}
});

RecyclerView recyclerView = findViewById(R.id.recycler_view);
studentAdapter = new StudentAdapter(students, new StudentAdapter.Listener() {
@Override
public void onStudentClick(Student student) {
setCurrentStudent(student);
}
});
recyclerView.setAdapter(studentAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

private void getInputData() {
firstName = firstNameView.getText().toString().trim();
secondName = secondNameView.getText().toString().trim();
gender = (String) genderView.getSelectedItem();
}

private void setInputData() {
firstNameView.setText(firstName);
secondNameView.setText(secondName);
genderView.setSelection(genderAdapter.getPosition(gender));
photoView.setImageResource(photoResId);
}

private boolean isStudentProfileValid() {
if (firstName.isEmpty() || secondName.isEmpty()) return false;
Copy link

Choose a reason for hiding this comment

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

Лучше всегда ставить фигурные скобки

return true;
}

private void setCurrentStudent(Student student) {
if (student == null) {
firstNameView.setText("");
secondNameView.setText("");
genderView.setSelection(0);
Random random = new Random();
int randomDrawable = 0;
switch (random.nextInt(6)) {
case 0: {
randomDrawable = R.drawable.female_1;
break;
}
case 1: {
randomDrawable = R.drawable.female_2;
break;
}
case 2: {
randomDrawable = R.drawable.female_3;
break;
}
case 3: {
randomDrawable = R.drawable.male_1;
break;
}
case 4: {
randomDrawable = R.drawable.male_2;
break;
}
case 5: {
randomDrawable = R.drawable.male_3;
break;
}
}
photoResId = randomDrawable;
photoView.setImageResource(randomDrawable);
currentStudent = null;
} else {
firstName = student.getFirstName();
secondName = student.getSecondName();
gender = student.getGender();
photoResId = student.getPhoto();
setInputData();
currentStudent = student;
}
}

private void addStudent() {
setCurrentStudent(null);
studentAdapter.setChosenStudent(null);
}

private void saveStudent() {
getInputData();
if (isStudentProfileValid()) {
currentStudent.setFirstName(firstName);
currentStudent.setSecondName(secondName);
currentStudent.setGender(gender);
currentStudent.setPhoto(photoResId);
studentAdapter.notifyDataSetChanged();
} else {
Toast.makeText(this, R.string.invalid_profile_toast, Toast.LENGTH_LONG).show();
}
}

private void saveNewStudent() {
getInputData();
if (isStudentProfileValid()) {
currentStudent = new Student(firstName, secondName, gender, photoResId);
students.add(currentStudent);
studentAdapter.setChosenStudent(currentStudent);
studentAdapter.notifyDataSetChanged();
} else {
Toast.makeText(this, R.string.invalid_profile_toast, Toast.LENGTH_LONG).show();
}
}
}
14 changes: 7 additions & 7 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public class Student {

private String firstName;
private String secondName;
private boolean maleGender;
private String gender;
private int photo;

public Student(String firstName, String secondName, boolean maleGender, int photo) {
public Student(String firstName, String secondName, String gender, int photo) {
this.firstName = firstName;
this.secondName = secondName;
this.maleGender = maleGender;
this.gender = gender;
this.photo = photo;
}

Expand All @@ -30,12 +30,12 @@ public void setSecondName(String secondName) {
this.secondName = secondName;
}

public boolean isMaleGender() {
return maleGender;
public String getGender() {
return gender;
}

public void setMaleGender(boolean maleGender) {
this.maleGender = maleGender;
public void setGender(String gender) {
this.gender = gender;
}

public int getPhoto() {
Expand Down
88 changes: 88 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,88 @@
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 StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

private final List<Student> students;
private final Listener onStudentClickListener;
private Student chosenStudent;
private View chosenStudentView;

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

@NonNull
@Override
public StudentViewHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (chosenStudentView != null) chosenStudentView.setSelected(false);
chosenStudent = (Student) v.getTag();
onStudentClickListener.onStudentClick(chosenStudent);
chosenStudentView = v;
chosenStudentView.setSelected(true);
}
});
return new StudentViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i) {
Student student = students.get(i);
studentViewHolder.bind(student);
View studentView = studentViewHolder.itemView;
studentView.setTag(student);
if (student == chosenStudent) {
studentView.setSelected(true);
chosenStudentView = studentView;
} else {
studentView.setSelected(false);
}
}

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

public void setChosenStudent(Student student) {
chosenStudent = student;
if (chosenStudentView != null) {
chosenStudentView.setSelected(false);
chosenStudentView = null;
}
}

static final class StudentViewHolder extends RecyclerView.ViewHolder {
private final ImageView photoImageView;
private final TextView nameTextView;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);
photoImageView = itemView.findViewById(R.id.list_item_photo);
nameTextView = itemView.findViewById(R.id.list_item_name);
}

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

interface Listener {
void onStudentClick(Student student);
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<size android:height="1dp" />
<solid android:color="#979797" />

</shape>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/list_item_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#979797" />
</shape>
</item>

</selector>
Binary file added app/src/main/res/drawable/template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading