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
Shakhov #20
Open
Denispok
wants to merge
6
commits into
polis-vk:master
Choose a base branch
from
Denispok:master
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
Shakhov #20
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
170 changes: 169 additions & 1 deletion
170
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,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<Student> 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше всегда ставить фигурные скобки |
||
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(); | ||
} | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
app/src/main/java/ru/ok/technopolis/students/StudentAdapter.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,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<StudentAdapter.StudentViewHolder> { | ||
|
||
private final List<Student> students; | ||
private final Listener onStudentClickListener; | ||
private Student chosenStudent; | ||
private View chosenStudentView; | ||
|
||
public StudentAdapter(List<Student> 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); | ||
} | ||
} |
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" > | ||
|
||
<size android:height="1dp" /> | ||
<solid android:color="#979797" /> | ||
|
||
</shape> |
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,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item android:state_selected="true"> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:shape="rectangle" > | ||
<stroke | ||
android:width="2dp" | ||
android:color="#979797" /> | ||
</shape> | ||
</item> | ||
|
||
</selector> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Для дженериков стоит указывать тип