diff --git a/app/build.gradle b/app/build.gradle index 3710062..dda8fc0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java index adde500..de5c25e 100644 --- a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java +++ b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java @@ -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 students = new ArrayList<>(); + private Student currentStudent; + private EditText firstNameView; + private String firstName; + private EditText secondNameView; + private String secondName; + private Spinner genderView; + private ArrayAdapter genderAdapter; + 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; + 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(); + } + } } diff --git a/app/src/main/java/ru/ok/technopolis/students/Student.java b/app/src/main/java/ru/ok/technopolis/students/Student.java index 7be4d10..b6e470c 100644 --- a/app/src/main/java/ru/ok/technopolis/students/Student.java +++ b/app/src/main/java/ru/ok/technopolis/students/Student.java @@ -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; } @@ -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() { diff --git a/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java new file mode 100644 index 0000000..2a60eca --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java @@ -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 { + + private final List students; + private final Listener onStudentClickListener; + private Student chosenStudent; + private View chosenStudentView; + + public StudentAdapter(List 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); + } +} diff --git a/app/src/main/res/drawable/divider.xml b/app/src/main/res/drawable/divider.xml new file mode 100644 index 0000000..b95b1c5 --- /dev/null +++ b/app/src/main/res/drawable/divider.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/list_item_border.xml b/app/src/main/res/drawable/list_item_border.xml new file mode 100644 index 0000000..0b455c3 --- /dev/null +++ b/app/src/main/res/drawable/list_item_border.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/template.png b/app/src/main/res/drawable/template.png new file mode 100644 index 0000000..7ddab3a Binary files /dev/null and b/app/src/main/res/drawable/template.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..934117e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,147 @@ - + - \ No newline at end of file + + + + +