diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 30aa626..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml deleted file mode 100644 index 7ac24c7..0000000 --- a/.idea/gradle.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index af0bbdd..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 7f68460..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 3710062..a8afda8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,7 +21,9 @@ android { 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' + api 'com.android.support.constraint:constraint-layout:1.1.3' + api 'com.android.support:recyclerview-v7:28.0.0' + api 'com.android.support:design: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..f48d719 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,204 @@ package ru.ok.technopolis.students; -import android.support.v7.app.AppCompatActivity; +import android.app.Activity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Toast; -public class MainActivity extends AppCompatActivity { +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class MainActivity extends Activity { + + private List students; + private StudentAdapter studentAdapter; + private Student currentStudent; + private Student prevStudent; + private final Student DEFAULTPREV = new Student(" ", " ", false, android.R.color.transparent); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + RecyclerView recyclerView = findViewById(R.id.list); + students = new ArrayList<>(); + prevStudent = DEFAULTPREV; + studentAdapter = new StudentAdapter(students, new StudentAdapter.Listener() { + @Override + public void onStudentClick(Student student) { + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(student.getFirstName()); + if (student.getFirstName().equals("")) { + firstName.setHint(R.string.default_firstname); + } + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(student.getSecondName()); + if (student.getSecondName().equals("")) { + secondName.setHint(R.string.default_secondname); + } + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(student.getPhoto()); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(student.isMaleGender()); + student.setInFocus(true); + if (currentStudent != null) { + prevStudent = currentStudent; + } + currentStudent = student; + if (!prevStudent.equals(currentStudent)) { + prevStudent.setInFocus(false); + } + studentAdapter.notifyDataSetChanged(); + } + }); + recyclerView.setAdapter(studentAdapter); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(linearLayoutManager); + setupAddButton(); + setupDeleteButton(); + setupSaveButton(); + } + + private void setupAddButton() { + Button addButton = findViewById(R.id.add_button); + addButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Random random = new Random(); + boolean maleGender = random.nextBoolean(); + int startPhoto = 0; + if (maleGender) { + switch (random.nextInt(3)) { + case 0: startPhoto = R.drawable.male_1; + break; + case 1: startPhoto = R.drawable.male_2; + break; + case 2: startPhoto = R.drawable.male_3; + break; + } + } else { + switch (random.nextInt(3)) { + case 0: startPhoto = R.drawable.female_1; + break; + case 1: startPhoto = R.drawable.female_2; + break; + case 2: startPhoto = R.drawable.female_3; + break; + } + } + students.add(new Student("", "", maleGender, startPhoto)); + studentAdapter.notifyDataSetChanged(); + } + }); } + private void setupDeleteButton() { + Button deleteButton = findViewById(R.id.delete_button); + deleteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int size = students.size(); + if (size != 0) { + if (size > 1) { + int index = students.indexOf(currentStudent); + if (index == 0) { + ++index; + } else { + if (index == size - 1) { + --index; + } else { + ++index; + } + } + Student tempStd = currentStudent; + currentStudent = students.get(index); + currentStudent.setInFocus(true); + students.remove(tempStd); + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(currentStudent.getFirstName()); + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(currentStudent.getSecondName()); + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(currentStudent.getPhoto()); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(currentStudent.isMaleGender()); + } else { + students.remove(currentStudent); + currentStudent = null; + prevStudent = DEFAULTPREV; + EditText firstName = findViewById(R.id.first_name_info); + firstName.setText(""); + firstName.setHint(R.string.default_firstname); + EditText secondName = findViewById(R.id.second_name_info); + secondName.setText(""); + secondName.setHint(R.string.default_secondname); + ImageView photo = findViewById(R.id.big_photo); + photo.setImageResource(android.R.color.transparent); + CheckBox checkBox = findViewById(R.id.checkbox); + checkBox.setChecked(false); + } + } + studentAdapter.notifyDataSetChanged(); + } + }); + } + + private void setupSaveButton() { + Button saveButton = findViewById(R.id.save_button); + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (currentStudent != null) { + EditText firstNameInfo = findViewById(R.id.first_name_info); + EditText secondNameInfo = findViewById(R.id.second_name_info); + firstNameInfo.setText(firstNameInfo.getText().toString().trim().replace("\n", "").replace(" ", "")); + secondNameInfo.setText(secondNameInfo.getText().toString().trim().replace("\n", "").replace(" ", "")); + if (!firstNameInfo.getText().toString().equals("") && + !secondNameInfo.getText().toString().equals("")) { + int index = students.indexOf(currentStudent); + currentStudent.setFirstName(firstNameInfo.getText().toString()); + currentStudent.setSecondName(secondNameInfo.getText().toString()); + ImageView image = findViewById(R.id.big_photo); + CheckBox checkBox = findViewById(R.id.checkbox); + if (checkBox.isChecked() != currentStudent.isMaleGender()) { + Random random = new Random(); + if (checkBox.isChecked()) { + switch (random.nextInt(3)) { + case 0: currentStudent.setPhoto(R.drawable.male_1); + break; + case 1: currentStudent.setPhoto(R.drawable.male_2); + break; + case 2: currentStudent.setPhoto(R.drawable.male_3); + break; + } + } else { + switch (random.nextInt(3)) { + case 0: currentStudent.setPhoto(R.drawable.female_1); + break; + case 1: currentStudent.setPhoto(R.drawable.female_2); + break; + case 2: currentStudent.setPhoto(R.drawable.female_3); + break; + } + } + } + image.setImageResource(currentStudent.getPhoto()); + currentStudent.setMaleGender(checkBox.isChecked()); + students.set(index, currentStudent); + studentAdapter.notifyDataSetChanged(); + } + else { + Toast.makeText(MainActivity.this, R.string.toast_invalid, Toast.LENGTH_SHORT).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..c89f020 100644 --- a/app/src/main/java/ru/ok/technopolis/students/Student.java +++ b/app/src/main/java/ru/ok/technopolis/students/Student.java @@ -6,12 +6,14 @@ public class Student { private String secondName; private boolean maleGender; private int photo; + private boolean inFocus; public Student(String firstName, String secondName, boolean maleGender, int photo) { this.firstName = firstName; this.secondName = secondName; this.maleGender = maleGender; this.photo = photo; + this.inFocus = false; } public String getFirstName() { @@ -45,4 +47,12 @@ public int getPhoto() { public void setPhoto(int photo) { this.photo = photo; } + + public void setInFocus(boolean focus) { + this.inFocus = focus; + } + + public boolean getInFocus() { + return this.inFocus; + } } 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..48c432d --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java @@ -0,0 +1,97 @@ +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 listener; + + public StudentAdapter(List list, Listener listener) { + this.students = list; + this.listener = listener; + } + + @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) { + listener.onStudentClick((Student)v.getTag()); + } + }); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) { + Student student = students.get(position); + holder.bind(student); + holder.itemView.setTag(student); + } + + @Override + public int getItemCount() { + return students.size(); + } + + static final class StudentViewHolder extends RecyclerView.ViewHolder{ + private final TextView firstNameTextView; + private final TextView secondNameTextView; + private final ImageView photoTextView; + private final View studentView; + + + public StudentViewHolder(@NonNull View itemView) { + super(itemView); + firstNameTextView = itemView.findViewById(R.id.firstname); + secondNameTextView = itemView.findViewById(R.id.secondname); + photoTextView = itemView.findViewById(R.id.small_photo); + studentView = itemView; + } + + private void bind(Student student) { + firstNameTextView.setText(reflectTextInTextView(student.getFirstName())); + if (firstNameTextView.getText().toString().equals("")) { + firstNameTextView.setHint(R.string.default_firstname); + } + secondNameTextView.setText(reflectTextInTextView(student.getSecondName())); + if (secondNameTextView.getText().toString().equals("")) { + secondNameTextView.setHint(R.string.default_secondname); + } + photoTextView.setImageResource(student.getPhoto()); + + if (student.getInFocus()) { + studentView.setBackgroundResource(R.color.current_student_color); + } + else { + studentView.setBackgroundResource(R.color.activity_color); + } + } + + private String reflectTextInTextView(String text) { + if (text.length() > 10) { + return text.substring(0, 9) + "."; + } + else { + return text; + } + } + + } + + interface Listener { + void onStudentClick(Student student); + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..f25994c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,132 @@ - + - \ No newline at end of file + + + + +