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
First impl #21
Open
kaifeur
wants to merge
4
commits into
polis-vk:master
Choose a base branch
from
kaifeur:impl
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
First impl #21
Changes from 2 commits
Commits
Show all changes
4 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
127 changes: 126 additions & 1 deletion
127
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,139 @@ | ||
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.text.TextUtils; | ||
import android.widget.Button; | ||
import android.widget.CheckBox; | ||
import android.widget.EditText; | ||
import android.widget.Toast; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import de.hdodenhof.circleimageview.CircleImageView; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
private List<Student> students; | ||
private StudentAdapter studentAdapter; | ||
private int pics[]; | ||
private Student currentStudent; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
students = new ArrayList<>(); | ||
pics = new int[6]; | ||
currentStudent = null; | ||
setContentView(R.layout.activity_main); | ||
setupRecyclerView(); | ||
setupAddButton(); | ||
setupSaveButton(); | ||
setupDeleteButton(); | ||
setupPics(); | ||
} | ||
|
||
private void setupRecyclerView() { | ||
RecyclerView recyclerView = findViewById(R.id.activity_main__rv_students); | ||
studentAdapter = new StudentAdapter(students, this::onStudentClick); | ||
recyclerView.setAdapter(studentAdapter); | ||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | ||
recyclerView.setLayoutManager(linearLayoutManager); | ||
} | ||
|
||
private void setupPics() { | ||
pics[0] = R.drawable.male_1; | ||
pics[1] = R.drawable.male_2; | ||
pics[2] = R.drawable.male_3; | ||
pics[3] = R.drawable.female_1; | ||
pics[4] = R.drawable.female_2; | ||
pics[5] = R.drawable.female_3; | ||
} | ||
|
||
private void setupAddButton() { | ||
Button button = findViewById(R.id.activity_main__btn_add); | ||
button.setOnClickListener(v -> onAddClick()); | ||
} | ||
|
||
private void onAddClick() { | ||
setupEdit("", "", false, 0); | ||
currentStudent = new Student("", "", false, 0); | ||
} | ||
|
||
private void setupEdit(String name, String familyName, Boolean isMale, int pic) { | ||
EditText nameEdit = findViewById(R.id.names_layout__edit_name); | ||
EditText familyNameEdit = findViewById(R.id.names_layout__edit_fname); | ||
CheckBox male = findViewById(R.id.sex_layout__checkbox); | ||
CircleImageView pics = findViewById(R.id.data_layout__edit_img); | ||
nameEdit.setText(name); | ||
familyNameEdit.setText(familyName); | ||
male.setChecked(isMale); | ||
pics.setImageResource(pic); | ||
} | ||
|
||
private void setupSaveButton() { | ||
Button button = findViewById(R.id.cl_edit__save); | ||
button.setOnClickListener(v -> onSaveClick()); | ||
} | ||
|
||
private void setupDeleteButton() { | ||
Button button = findViewById(R.id.cl_edit__delete); | ||
button.setOnClickListener(v -> onDeleteClick()); | ||
} | ||
|
||
private void onDeleteClick() { | ||
if (currentStudent == null) { | ||
Toast.makeText(this, R.string.empty_student, Toast.LENGTH_SHORT).show(); | ||
} else { | ||
students.remove(currentStudent); | ||
currentStudent = null; | ||
studentAdapter.notifyDataSetChanged(); | ||
setupEdit("", "", Boolean.FALSE, 0); | ||
} | ||
} | ||
|
||
private void onSaveClick() { | ||
EditText name = findViewById(R.id.names_layout__edit_name); | ||
EditText familyName = findViewById(R.id.names_layout__edit_fname); | ||
CheckBox male = findViewById(R.id.sex_layout__checkbox); | ||
CircleImageView pic = findViewById(R.id.data_layout__edit_img); | ||
int picId; | ||
if ((name.getText() != null) && (familyName.getText() != null)) { | ||
if (TextUtils.isEmpty(name.getText().toString().trim()) | ||
|| TextUtils.isEmpty(familyName.getText().toString().trim())) { | ||
Toast.makeText(this, R.string.wrong_enter, Toast.LENGTH_SHORT).show(); | ||
} else { | ||
if (currentStudent == null || currentStudent.getFirstName().equals("")) { | ||
students.add(currentStudent = new Student(name.getText().toString().trim(), | ||
familyName.getText().toString().trim(), | ||
male.isChecked(), | ||
picId = getRandomPick(male.isChecked()))); | ||
pic.setImageResource(picId); | ||
studentAdapter.notifyDataSetChanged(); | ||
} else { | ||
currentStudent.setFirstName(name.getText().toString().trim()); | ||
currentStudent.setSecondName(familyName.getText().toString().trim()); | ||
currentStudent.setMaleGender(male.isChecked()); | ||
studentAdapter.notifyDataSetChanged(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private int getRandomPick(Boolean isMale) { | ||
Random random = new Random(); | ||
int maleRandom = isMale ? 0 : 3; | ||
return pics[maleRandom + random.nextInt(3)]; | ||
} | ||
|
||
private void onStudentClick(Student student) { | ||
setupEdit(student.getFirstName(), student.getSecondName(), student.isMaleGender(), student.getPhoto()); | ||
currentStudent = student; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
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.TextView; | ||
|
||
import java.util.List; | ||
|
||
import de.hdodenhof.circleimageview.CircleImageView; | ||
|
||
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> { | ||
|
||
private final List<Student> students; | ||
private final Listener onStudentClickListener; | ||
|
||
public StudentAdapter(List<Student> 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(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
onStudentClickListener.onStudentClick((Student) v.getTag()); | ||
} | ||
}); | ||
return new StudentViewHolder(view); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull StudentViewHolder holder, final int position) { | ||
Student student = students.get(position); | ||
holder.bind(student); | ||
holder.itemView.setTag(student); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return students.size(); | ||
} | ||
|
||
interface Listener { | ||
|
||
void onStudentClick(Student student); | ||
|
||
} | ||
|
||
static final class StudentViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private final CircleImageView profileImage; | ||
private final TextView nameTextView; | ||
private final TextView familyNameTextView; | ||
|
||
public StudentViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
profileImage = itemView.findViewById(R.id.student_item__img); | ||
nameTextView = itemView.findViewById(R.id.student_item__name); | ||
familyNameTextView = itemView.findViewById(R.id.student_item__fname); | ||
} | ||
|
||
private void bind(@NonNull Student student) { | ||
nameTextView.setText(student.getFirstName()); | ||
familyNameTextView.setText(student.getSecondName()); | ||
profileImage.setImageResource(student.getPhoto()); | ||
} | ||
|
||
} | ||
} |
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
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.
Очень большая вложенность. Не стоит вкладывать констрейнт в констрейнт