Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

First impl #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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 'de.hdodenhof:circleimageview:3.0.0'
}
120 changes: 119 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,132 @@
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 fname, Boolean isMale, int pic) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fname это сокращение от чего?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

family name

EditText nameEdit = findViewById(R.id.names_layout__edit_name);
EditText fnameEdit = 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);
fnameEdit.setText(fname);
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(MainActivity.this, R.string.empty_student, Toast.LENGTH_SHORT).show();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно просто this

} 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 fname = 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 (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(fname.getText().toString())) {
Toast.makeText(MainActivity.this, R.string.wrong_enter, Toast.LENGTH_SHORT).show();
} else {
if (currentStudent == null || currentStudent.getFirstName().equals("")) {
students.add(currentStudent = new Student(name.getText().toString(), fname.getText().toString(), male.isChecked(), picId = getRandomPick(male.isChecked())));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name.getText() иногда может вернуть null

pic.setImageResource(picId);
studentAdapter.notifyDataSetChanged();
} else {
currentStudent.setFirstName(name.getText().toString());
currentStudent.setSecondName(fname.getText().toString());
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 app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java
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 pImageCircleImageView;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pImageCircleImageView что означет буква p вначале?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

profileImage, исправлю

private final TextView nameTextView;
private final TextView fNameTextView;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);
pImageCircleImageView = itemView.findViewById(R.id.student_item__img);
nameTextView = itemView.findViewById(R.id.student_item__name);
fNameTextView = itemView.findViewById(R.id.student_item__fname);
}

private void bind(@NonNull Student student) {
nameTextView.setText(student.getFirstName());
fNameTextView.setText(student.getSecondName());
pImageCircleImageView.setImageResource(student.getPhoto());
}

}
}
Loading