From 579274e109cc38e1d569d7bd49bb48e2030fc1b8 Mon Sep 17 00:00:00 2001 From: Lapter57 Date: Mon, 25 Feb 2019 01:12:47 +0300 Subject: [PATCH 1/5] Homework was resolved --- .idea/misc.xml | 30 +++- app/build.gradle | 6 + app/src/main/AndroidManifest.xml | 9 +- .../ok/technopolis/students/MainActivity.java | 168 ++++++++++++++++++ .../ru/ok/technopolis/students/Student.java | 15 +- .../technopolis/students/StudentsAdapter.java | 64 +++++++ app/src/main/res/anim/shake.xml | 11 ++ app/src/main/res/layout/activity_main.xml | 45 +++++ app/src/main/res/layout/student_item.xml | 35 ++++ app/src/main/res/layout/student_list.xml | 46 +++++ app/src/main/res/layout/student_profile.xml | 134 ++++++++++++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 10 ++ app/src/main/res/values/styles.xml | 2 +- 14 files changed, 567 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java create mode 100644 app/src/main/res/anim/shake.xml create mode 100644 app/src/main/res/layout/student_item.xml create mode 100644 app/src/main/res/layout/student_list.xml create mode 100644 app/src/main/res/layout/student_profile.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index af0bbdd..c0f68ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,11 +1,31 @@ - - - - + + + - + diff --git a/app/build.gradle b/app/build.gradle index 3710062..a16ef66 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,12 +16,18 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + compileOptions { + targetCompatibility 1.8 + sourceCompatibility 1.8 + } } 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 'de.hdodenhof:circleimageview:3.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/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 625f38c..ade2193 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,14 +8,19 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme"> - + android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"> + + + + \ No newline at end of file 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..1f75e8a 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.content.Context; +import android.os.Vibrator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.Toast; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import de.hdodenhof.circleimageview.CircleImageView; public class MainActivity extends AppCompatActivity { + private List students = new ArrayList<>(); + private StudentsAdapter studentsAdapter; + + private CircleImageView studentAvatar; + private EditText studentSecondName; + private EditText studentFirstName; + + private CheckBox maleGender; + private Student activeStudent; + + private int[] femalesPhoto = new int[3]; + private int[] malesPhoto = new int[3]; + + Random random = new Random(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + setupPhotos(); + generateStudentsList(); + setupRecyclerView(); + + studentAvatar = findViewById(R.id.student_profile__avatar); + studentSecondName = findViewById(R.id.student_profile__second_name); + studentFirstName = findViewById(R.id.student_profile__first_name); + + maleGender = findViewById(R.id.student_profile__male_gender); + maleGender.setOnCheckedChangeListener((compoundButton, b) -> { + if (activeStudent != null) { + activeStudent.setMaleGender(b); + } + }); + + + Button addStudentButton = findViewById(R.id.student_list__button_add); + addStudentButton.setOnClickListener(view -> clearFields()); + + Button saveStudentButton = findViewById(R.id.student_profile__buttons__save); + saveStudentButton.setOnClickListener(view -> addNewStudent()); + + Button removeStudentButton = findViewById(R.id.student_profile__buttons__remove); + removeStudentButton.setOnClickListener(view -> removeStudent()); + } + + private void clearFields() { + activeStudent = null; + studentAvatar.setImageBitmap(null); + maleGender.setChecked(false); + studentSecondName.setText(""); + studentFirstName.setText(""); + } + + private void addNewStudent() { + Animation shakeAnimations = AnimationUtils.loadAnimation(this, R.anim.shake); + Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); + + String secondName = studentSecondName.getText().toString(); + String firstName = studentFirstName.getText().toString(); + Boolean isMale = maleGender.isChecked(); + + if (secondName.equals("") || firstName.equals("")) { + if (secondName.equals("")) { + studentSecondName.startAnimation(shakeAnimations); + } + if (firstName.equals("")) { + studentFirstName.startAnimation(shakeAnimations); + } + if (vibe != null) { + vibe.vibrate(100); + } + } else { + if (activeStudent == null) { + int photo = (isMale) ? malesPhoto[random.nextInt(3)] : femalesPhoto[random.nextInt(3)]; + students.add(new Student(firstName, secondName, isMale, photo)); + Collections.sort(students); + } else { + activeStudent.setFirstName(firstName); + activeStudent.setSecondName(secondName); + activeStudent.setMaleGender(maleGender.isChecked()); + } + studentsAdapter.notifyDataSetChanged(); + clearFields(); + } + } + + private void removeStudent() { + if (activeStudent != null) { + students.remove(activeStudent); + studentsAdapter.notifyDataSetChanged(); + } + clearFields(); + } + + private void setupRecyclerView() { + RecyclerView recyclerView = findViewById(R.id.student_list__body); + studentsAdapter = new StudentsAdapter(students, this::onStudentClick); + recyclerView.setAdapter(studentsAdapter); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(linearLayoutManager); + } + + private void onStudentClick(Student student) { + activeStudent = student; + + studentAvatar.setImageResource(activeStudent.getPhoto()); + studentSecondName.setText(activeStudent.getSecondName()); + studentFirstName.setText(activeStudent.getFirstName()); + maleGender.setChecked(activeStudent.isMaleGender()); + } + + private void setupPhotos() { + femalesPhoto[0] = R.drawable.female_1; + femalesPhoto[1] = R.drawable.female_2; + femalesPhoto[2] = R.drawable.female_3; + arrayShuffle(femalesPhoto); + + malesPhoto[0] = R.drawable.male_1; + malesPhoto[1] = R.drawable.male_2; + malesPhoto[2] = R.drawable.male_3; + arrayShuffle(malesPhoto); + } + + + private void generateStudentsList() { + for (int i = 0; i < 3; i++) { + switch (i) { + case 0: + students.add(new Student("Иван", "Иванов", true, malesPhoto[random.nextInt(3)])); + students.add(new Student("Анастасия", "Медведева", false, femalesPhoto[random.nextInt(3)])); + break; + + case 1: + students.add(new Student("Петр", "Петров", true, malesPhoto[random.nextInt(3)])); + students.add(new Student("Валерия", "Прокофьева", false, femalesPhoto[random.nextInt(3)])); + break; + + case 2: + students.add(new Student("Денис", "Денисов", true, malesPhoto[random.nextInt(3)])); + students.add(new Student("Александра", "Волкова", false, femalesPhoto[random.nextInt(3)])); + break; + } + } + Collections.sort(students); + } + + private static void arrayShuffle (int[] arr) { + Random random = new Random(); + for (int i=0; i < arr.length; i++) { + int randomPosition = random.nextInt(arr.length); + int temp = arr[i]; + arr[i] = arr[randomPosition]; + arr[randomPosition] = temp; + } } } 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..431aa64 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,10 @@ package ru.ok.technopolis.students; -public class Student { +import android.support.annotation.NonNull; + +import java.util.Comparator; + +public class Student implements Comparable { private String firstName; private String secondName; @@ -45,4 +49,13 @@ public int getPhoto() { public void setPhoto(int photo) { this.photo = photo; } + + @Override + public int compareTo(@NonNull Student student) { + int res = secondName.toLowerCase().compareTo(student.getSecondName().toLowerCase()); + if (res != 0) { + return res; + } + return firstName.toLowerCase().compareTo(student.getFirstName().toLowerCase()); + } } 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..f55d7fe --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java @@ -0,0 +1,64 @@ +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{ + + private final List students; + private final Listener onStudentClickListener; + + public StudentsAdapter(List 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 -> onStudentClickListener.onStudentClick((Student) v.getTag())); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder viewHolder, int i) { + Student movie = students.get(i); + viewHolder.bind(movie); + viewHolder.itemView.setTag(movie); + } + + @Override + public int getItemCount() { + return students.size(); + } + + static final class StudentViewHolder extends RecyclerView.ViewHolder { + + private final TextView nameTextView; + private final ImageView avatarImageView; + + private StudentViewHolder(@NonNull View itemView) { + super(itemView); + nameTextView = itemView.findViewById(R.id.student_item__full_name); + avatarImageView = itemView.findViewById(R.id.student_item__avatar); + } + + private void bind(@NonNull Student student) { + String fullname = student.getSecondName() + " " + student.getFirstName(); + nameTextView.setText(fullname); + avatarImageView.setImageResource(student.getPhoto()); + } + } + + interface Listener { + void onStudentClick(Student student); + } +} diff --git a/app/src/main/res/anim/shake.xml b/app/src/main/res/anim/shake.xml new file mode 100644 index 0000000..628c6db --- /dev/null +++ b/app/src/main/res/anim/shake.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..767bb49 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,7 +3,52 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity"> + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/student_item.xml b/app/src/main/res/layout/student_item.xml new file mode 100644 index 0000000..e69a3d8 --- /dev/null +++ b/app/src/main/res/layout/student_item.xml @@ -0,0 +1,35 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/student_list.xml b/app/src/main/res/layout/student_list.xml new file mode 100644 index 0000000..5beead4 --- /dev/null +++ b/app/src/main/res/layout/student_list.xml @@ -0,0 +1,46 @@ + + + + + + + +