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..ed03cdc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,4 +25,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 'com.android.support:design:28.0.0' } 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..bebc659 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,152 @@ package ru.ok.technopolis.students; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; + +import java.util.ArrayList; +import java.util.Random; public class MainActivity extends AppCompatActivity { + private static final String DIALOG_TAG = "DialogAdder"; + private String[] mansFirstNames; + private String[] womenFirstNames; + private String[] lastNames; + private int[] manPhotos; + private int[] womanPhotos; + private Random random; + private int manPhotoIndex, womanPhotoIndex; + private ArrayList students; + private StudentsAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + random = new Random(); + manPhotoIndex = womanPhotoIndex = 0; + mansFirstNames = getResources().getStringArray(R.array.mans_first_names); + womenFirstNames = getResources().getStringArray(R.array.women_first_names); + lastNames = getResources().getStringArray(R.array.last_names); + manPhotos = new int[]{R.drawable.male_01, R.drawable.male_02, R.drawable.male_03}; + womanPhotos = new int[]{R.drawable.female_01, R.drawable.female_02, R.drawable.female_03}; + + + RecyclerView recyclerView = findViewById(R.id.activity_main__recycler_view); + FloatingActionButton fabAdd = findViewById(R.id.activity_main__fab_add); + + fabAdd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + StudentAdderDialog dialog = new StudentAdderDialog(); + dialog.setOnAdderStudentListener(new StudentAdderDialog.OnAdderStudentListener() { + @Override + public void add(String firstName, String lastName, boolean maleGender) { + Student student = new Student(firstName, lastName, maleGender, maleGender ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]); + students.add(student); + adapter.notifyDataSetChanged(); + } + }); + dialog.show(getSupportFragmentManager(), DIALOG_TAG); + } + }); + + students = createStudents(); + adapter = new StudentsAdapter(students, new StudentsAdapter.OnStudentClickListener() { + @Override + public void onStudentClick(Student student) { + showDialog(student); + } + }); + recyclerView.setAdapter(adapter); + LinearLayoutManager mLayoutManager = new GridLayoutManager(this, 2); + recyclerView.setLayoutManager(mLayoutManager); + } + + private void showDialog(final Student student) { + ImageView view = new ImageView(this); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300); + view.setScaleType(ImageView.ScaleType.CENTER_CROP); + view.setLayoutParams(layoutParams); + view.setImageResource(student.getPhoto()); + + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + + builder.setView(view); + builder.setTitle(student.getFirstName() + " " + student.getSecondName()); + builder.setMessage(student.isMaleGender() ? R.string.guy : R.string.girl); + builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.cancel(); + } + }); + + builder.setNegativeButton(R.string.edit, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + StudentAdderDialog dialog = new StudentAdderDialog(); + Bundle bundle = new Bundle(); + bundle.putSerializable(StudentAdderDialog.STUDENT_TAG, student); + dialog.setArguments(bundle); + + dialog.setOnUpdateStudentListener(new StudentAdderDialog.OnUpdateStudentListener() { + @Override + public void update(Student oldStudent, Student newStudent) { + int index = students.indexOf(oldStudent); + students.remove(oldStudent); + if (oldStudent.isMaleGender() != newStudent.isMaleGender()) { + newStudent.setPhoto(newStudent.isMaleGender() ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]); + } + students.add(index, newStudent); + adapter.notifyDataSetChanged(); + } + }); + + dialog.show(getSupportFragmentManager(), DIALOG_TAG); + dialogInterface.cancel(); + } + }); + + builder.setNeutralButton(R.string.delete, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + students.remove(student); + adapter.notifyDataSetChanged(); + dialogInterface.cancel(); + } + }); + builder.create().show(); + } + + + ArrayList createStudents() { + ArrayList students = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + students.add(nextStudent(i % 2 == 0)); + } + return students; } + + Student nextStudent(boolean maleGender) { + if (maleGender) { + int nameIndex = random.nextInt(mansFirstNames.length); + int lastNameIndex = random.nextInt(mansFirstNames.length); + return new Student(mansFirstNames[nameIndex], lastNames[lastNameIndex], true, manPhotos[manPhotoIndex++ % 3]); + } + int nameIndex = random.nextInt(womenFirstNames.length); + int lastNameIndex = random.nextInt(womenFirstNames.length); + return new Student(womenFirstNames[nameIndex], lastNames[lastNameIndex] + "a", false, womanPhotos[womanPhotoIndex++ % 3]); + } + + } 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..35c6c6c 100644 --- a/app/src/main/java/ru/ok/technopolis/students/Student.java +++ b/app/src/main/java/ru/ok/technopolis/students/Student.java @@ -1,6 +1,9 @@ package ru.ok.technopolis.students; -public class Student { + +import java.io.Serializable; + +public class Student implements Serializable { private String firstName; private String secondName; @@ -14,6 +17,7 @@ public Student(String firstName, String secondName, boolean maleGender, int phot this.photo = photo; } + public String getFirstName() { return firstName; } @@ -45,4 +49,10 @@ public int getPhoto() { public void setPhoto(int photo) { this.photo = photo; } + + @Override + public String toString() { + return firstName + " " + secondName + (isMaleGender() ? " М" : " Ж"); + } + } diff --git a/app/src/main/java/ru/ok/technopolis/students/StudentAdderDialog.java b/app/src/main/java/ru/ok/technopolis/students/StudentAdderDialog.java new file mode 100644 index 0000000..ff16122 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentAdderDialog.java @@ -0,0 +1,121 @@ +package ru.ok.technopolis.students; + +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.text.Editable; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.RadioGroup; +import android.widget.Toast; + + +public class StudentAdderDialog extends DialogFragment implements DialogInterface.OnShowListener { + private AlertDialog dialog; + private OnAdderStudentListener onAdderStudentListener; + private OnUpdateStudentListener onUpdateStudentListener; + public static final String STUDENT_TAG = "STUDENT_TAG"; + private EditText editTextFirstName; + private EditText editTextLastName; + private RadioGroup radioGroup; + private Context context; + private Student oldStudent; + + @NonNull + public Dialog onCreateDialog(Bundle savedInstanceState) { + context = getContext(); + Bundle args = getArguments(); + if (args != null) { + oldStudent = (Student) args.getSerializable(STUDENT_TAG); + } + View view = LayoutInflater.from(context).inflate(R.layout.dialog_student_adder, null); + initView(view); + + AlertDialog.Builder builder = new AlertDialog.Builder(context) + .setView(view) + .setTitle(R.string.student) + .setMessage(R.string.trydroid) + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.back, null); + + dialog = builder.create(); + dialog.setOnShowListener(this); + return dialog; + } + + private void initView(View view) { + editTextFirstName = view.findViewById(R.id.dialog_student_adder__first_name); + editTextLastName = view.findViewById(R.id.dialog_student_adder__last_name); + ImageView imageView = view.findViewById(R.id.dialog_student_adder__avatar); + radioGroup = view.findViewById(R.id.dialog_student_adder__radio_group); + + if (oldStudent != null) { + editTextFirstName.setText(oldStudent.getFirstName()); + editTextLastName.setText(oldStudent.getSecondName()); + imageView.setImageResource(oldStudent.getPhoto()); + radioGroup.check(oldStudent.isMaleGender() ? R.id.dialog_student_adder__male : R.id.dialog_student_adder__female); + } + } + + public void setOnAdderStudentListener(OnAdderStudentListener onAdderStudentListener) { + this.onAdderStudentListener = onAdderStudentListener; + } + + public void setOnUpdateStudentListener(OnUpdateStudentListener onUpdateStudentListener) { + this.onUpdateStudentListener = onUpdateStudentListener; + } + + @Override + public void onShow(DialogInterface dialogInterface) { + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Editable textFirstName = editTextFirstName.getText(); + Editable textLastName = editTextLastName.getText(); + + if (TextUtils.isEmpty(textFirstName) || TextUtils.isEmpty(textLastName) || radioGroup.getCheckedRadioButtonId() == -1) { + Toast.makeText(context, R.string.incorrect, Toast.LENGTH_SHORT).show(); + return; + } + + String name = textFirstName.toString(); + String lastName = textLastName.toString(); + boolean maleGender = radioGroup.getCheckedRadioButtonId() == R.id.dialog_student_adder__male; + + if (oldStudent == null) { + if (onAdderStudentListener != null) { + onAdderStudentListener.add(name, lastName, maleGender); + } + } else { + if (onUpdateStudentListener != null) { + Student student = new Student(name, lastName, maleGender, oldStudent.getPhoto()); + onUpdateStudentListener.update(oldStudent, student); + } + } + dialog.cancel(); + } + }); + + dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + dialog.cancel(); + } + }); + } + + interface OnAdderStudentListener { + void add(String firstName, String lastName, boolean maleGender); + } + + interface OnUpdateStudentListener { + void update(Student oldStudent, Student newStudent); + } +} diff --git a/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java b/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java new file mode 100644 index 0000000..e4b4ad8 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java @@ -0,0 +1,74 @@ +package ru.ok.technopolis.students; + + +import android.graphics.Bitmap; +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 { + + private final List students; + private OnStudentClickListener listener; + + StudentsAdapter(List students, OnStudentClickListener listener) { + this.listener = listener; + this.students = students; + } + + @NonNull + @Override + public StudentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_student, viewGroup, false); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + listener.onStudentClick((Student) view.getTag()); + } + }); + return new StudentHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentHolder studentHolder, int i) { + Student student = students.get(i); + studentHolder.bind(student); + studentHolder.itemView.setTag(student); + } + + @Override + public int getItemCount() { + return students.size(); + } + + interface OnStudentClickListener { + void onStudentClick(Student student); + } + + class StudentHolder extends RecyclerView.ViewHolder { + + private ImageView imageView; + private TextView textFirstName, textLastName; + + StudentHolder(@NonNull View itemView) { + super(itemView); + + imageView = itemView.findViewById(R.id.item_student__avatar); + + textFirstName = itemView.findViewById(R.id.item_student__first_name); + textLastName = itemView.findViewById(R.id.item_student__last_name); + } + + void bind(Student student) { + imageView.setImageResource(student.getPhoto()); + textFirstName.setText(student.getFirstName()); + textLastName.setText(student.getSecondName()); + } + } +} diff --git a/app/src/main/res/drawable/female_01.png b/app/src/main/res/drawable/female_01.png new file mode 100644 index 0000000..c4f737c Binary files /dev/null and b/app/src/main/res/drawable/female_01.png differ diff --git a/app/src/main/res/drawable/female_02.png b/app/src/main/res/drawable/female_02.png new file mode 100644 index 0000000..22bd9be Binary files /dev/null and b/app/src/main/res/drawable/female_02.png differ diff --git a/app/src/main/res/drawable/female_03.png b/app/src/main/res/drawable/female_03.png new file mode 100644 index 0000000..e6f675c Binary files /dev/null and b/app/src/main/res/drawable/female_03.png differ diff --git a/app/src/main/res/drawable/female_1.jpg b/app/src/main/res/drawable/female_1.jpg deleted file mode 100644 index 470da25..0000000 Binary files a/app/src/main/res/drawable/female_1.jpg and /dev/null differ diff --git a/app/src/main/res/drawable/female_2.jpg b/app/src/main/res/drawable/female_2.jpg deleted file mode 100644 index c345c3a..0000000 Binary files a/app/src/main/res/drawable/female_2.jpg and /dev/null differ diff --git a/app/src/main/res/drawable/female_3.jpg b/app/src/main/res/drawable/female_3.jpg deleted file mode 100644 index 4c354df..0000000 Binary files a/app/src/main/res/drawable/female_3.jpg and /dev/null differ diff --git a/app/src/main/res/drawable/male_01.png b/app/src/main/res/drawable/male_01.png new file mode 100644 index 0000000..c720be1 Binary files /dev/null and b/app/src/main/res/drawable/male_01.png differ diff --git a/app/src/main/res/drawable/male_02.png b/app/src/main/res/drawable/male_02.png new file mode 100644 index 0000000..bbaf2ab Binary files /dev/null and b/app/src/main/res/drawable/male_02.png differ diff --git a/app/src/main/res/drawable/male_03.png b/app/src/main/res/drawable/male_03.png new file mode 100644 index 0000000..c6fe5a9 Binary files /dev/null and b/app/src/main/res/drawable/male_03.png differ diff --git a/app/src/main/res/drawable/male_1.jpg b/app/src/main/res/drawable/male_1.jpg deleted file mode 100644 index 4af532d..0000000 Binary files a/app/src/main/res/drawable/male_1.jpg and /dev/null differ diff --git a/app/src/main/res/drawable/male_2.jpg b/app/src/main/res/drawable/male_2.jpg deleted file mode 100644 index c22b55f..0000000 Binary files a/app/src/main/res/drawable/male_2.jpg and /dev/null differ diff --git a/app/src/main/res/drawable/male_3.jpg b/app/src/main/res/drawable/male_3.jpg deleted file mode 100644 index 4496d83..0000000 Binary files a/app/src/main/res/drawable/male_3.jpg and /dev/null differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..b1bd92b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,22 @@ - + - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_student_adder.xml b/app/src/main/res/layout/dialog_student_adder.xml new file mode 100644 index 0000000..4b038f2 --- /dev/null +++ b/app/src/main/res/layout/dialog_student_adder.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_student.xml b/app/src/main/res/layout/item_student.xml new file mode 100644 index 0000000..114347e --- /dev/null +++ b/app/src/main/res/layout/item_student.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2bd9c09..2a3868a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,57 @@ Students + Да + Нет + Имя + Фамилия + ОК + Назад + Удалить + Парень + Девушка + Изменить + Данные введены некорректно + Студент + Попробуйте нажать на Android + Введите имя + Введите фамилию + + + Кирилл + Пьер + Дмитрий + Павел + Аркадий + Василий + Евгений + Лев + Илья + Родион + + + + Анна + Катерина + Марфа + Евгения + Мария + Софья + Инна + Арина + Елена + Татьяна + + + + Каренин + Безухов + Кабанов + Ковалев + Логинов + Тёркин + Онегин + Мышкин + Обломов + Расскольников +