This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Homework was resolved #3
Open
lapter57
wants to merge
7
commits into
polis-vk:master
Choose a base branch
from
lapter57:shakhmin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
579274e
Homework was resolved
lapter57 6f2b6bc
improved input validation when creating a student
lapter57 be6a6c0
.idea has removed
lapter57 bb40c4b
fixed
lapter57 a106199
Merge branch 'master' into shakhmin
lapter57 5ad432a
added avatar change logic
lapter57 d352d2e
Merge branch 'shakhmin' of https://github.com/Lapter57/students_homew…
lapter57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
/build | ||
/captures | ||
.externalNativeBuild | ||
/.idea/ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
app/src/main/java/ru/ok/technopolis/students/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,227 @@ | ||
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.EditText; | ||
|
||
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<Student> students = new ArrayList<>(); | ||
private StudentsAdapter studentsAdapter; | ||
|
||
private CircleImageView studentAvatar; | ||
private EditText studentSecondName; | ||
private EditText studentFirstName; | ||
private CheckBox maleGender; | ||
|
||
private Button addStudentButton; | ||
private Button saveStudentButton; | ||
private Button removeStudentButton; | ||
|
||
private Student activeStudent; | ||
private int newPhoto = -1; | ||
private boolean prevGender; | ||
|
||
private int[] femalesPhoto = new int[3]; | ||
private int[] malesPhoto = new int[3]; | ||
|
||
private Random random = new Random(); | ||
private Vibrator vibe; | ||
private Animation shakeAnimations; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
setupPhotos(); | ||
generateStudentsList(); | ||
setupRecyclerView(); | ||
|
||
vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); | ||
shakeAnimations = AnimationUtils.loadAnimation(this, R.anim.shake); | ||
|
||
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, isMaleGender) -> { | ||
if(activeStudent != null) { | ||
if (prevGender != isMaleGender){ | ||
prevGender = isMaleGender; | ||
newPhoto = (isMaleGender) | ||
? malesPhoto[random.nextInt(3)] | ||
: femalesPhoto[random.nextInt(3)]; | ||
studentAvatar.setImageResource(newPhoto); | ||
} else { | ||
studentAvatar.setImageResource(activeStudent.getPhoto()); | ||
} | ||
|
||
} | ||
}); | ||
|
||
lapter57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addStudentButton = findViewById(R.id.student_list__button_add); | ||
addStudentButton.setOnClickListener(view -> { | ||
setEnabledField(true); | ||
clearFields(); | ||
addStudentButton.setEnabled(false); | ||
int photo = femalesPhoto[random.nextInt(3)]; | ||
activeStudent = new Student(photo); | ||
studentAvatar.setImageResource(photo); | ||
}); | ||
|
||
saveStudentButton = findViewById(R.id.student_profile__buttons__save); | ||
saveStudentButton.setOnClickListener(view -> addNewStudent()); | ||
|
||
removeStudentButton = findViewById(R.id.student_profile__buttons__remove); | ||
removeStudentButton.setOnClickListener(view -> removeStudent()); | ||
|
||
setEnabledField(false); | ||
} | ||
|
||
private void clearFields() { | ||
activeStudent = null; | ||
studentAvatar.setImageBitmap(null); | ||
maleGender.setChecked(false); | ||
studentSecondName.setText(""); | ||
studentFirstName.setText(""); | ||
} | ||
|
||
private void setDefaultValues(){ | ||
clearFields(); | ||
addStudentButton.setEnabled(true); | ||
setEnabledField(false); | ||
studentsAdapter.removeHighlight(); | ||
prevGender = false; | ||
newPhoto = -1; | ||
} | ||
|
||
private void setEnabledField(boolean enabled){ | ||
studentAvatar.setEnabled(enabled); | ||
studentSecondName.setEnabled(enabled); | ||
studentFirstName.setEnabled(enabled); | ||
maleGender.setEnabled(enabled); | ||
saveStudentButton.setEnabled(enabled); | ||
removeStudentButton.setEnabled(enabled); | ||
} | ||
|
||
private void addNewStudent() { | ||
String secondName = studentSecondName.getText() != null | ||
? studentSecondName.getText().toString().trim() | ||
: ""; | ||
String firstName = studentFirstName.getText() != null | ||
? studentFirstName.getText().toString().trim() | ||
: ""; | ||
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 { | ||
activeStudent.setFirstName(firstName); | ||
activeStudent.setSecondName(secondName); | ||
activeStudent.setMaleGender(isMale); | ||
if (newPhoto != -1){ | ||
activeStudent.setPhoto(newPhoto); | ||
} | ||
if (!students.contains(activeStudent)) { | ||
students.add(activeStudent); | ||
} | ||
Collections.sort(students); | ||
studentsAdapter.notifyDataSetChanged(); | ||
setDefaultValues(); | ||
} | ||
} | ||
|
||
private void removeStudent() { | ||
students.remove(activeStudent); | ||
studentsAdapter.notifyDataSetChanged(); | ||
setDefaultValues(); | ||
} | ||
|
||
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) { | ||
setEnabledField(true); | ||
addStudentButton.setEnabled(true); | ||
prevGender = false; | ||
newPhoto = -1; | ||
activeStudent = student; | ||
studentAvatar.setImageResource(activeStudent.getPhoto()); | ||
studentSecondName.setText(activeStudent.getSecondName()); | ||
studentFirstName.setText(activeStudent.getFirstName()); | ||
prevGender = activeStudent.isMaleGender(); | ||
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); | ||
} | ||
|
||
lapter57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
import android.graphics.Color; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
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<StudentsAdapter.StudentViewHolder>{ | ||
|
||
private final List<Student> students; | ||
private final Listener onStudentClickListener; | ||
|
||
private int selectedStudent = -1; | ||
|
||
public StudentsAdapter(List<Student> students, Listener onStudentClickListener) { | ||
this.students = students; | ||
this.onStudentClickListener = onStudentClickListener; | ||
} | ||
|
||
public void removeHighlight() { | ||
selectedStudent = -1; | ||
notifyItemChanged(selectedStudent); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { | ||
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_item, viewGroup, false); | ||
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); | ||
|
||
viewHolder.itemView.setSelected(selectedStudent == i); | ||
|
||
if(selectedStudent == i){ | ||
viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(viewHolder.itemView.getContext(), R.color.lightGray)); | ||
} else { | ||
viewHolder.itemView.setBackgroundColor(Color.WHITE); | ||
} | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return students.size(); | ||
} | ||
|
||
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); | ||
|
||
itemView.setOnClickListener(v -> { | ||
onStudentClickListener.onStudentClick((Student) v.getTag()); | ||
notifyItemChanged(selectedStudent); | ||
selectedStudent = getLayoutPosition(); | ||
notifyItemChanged(selectedStudent); | ||
}); | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vibe? =)