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

Students homework android #9

Open
wants to merge 6 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
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="ru.ok.technopolis.students">

<application
Expand All @@ -8,8 +9,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
86 changes: 49 additions & 37 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

private List<Student> students;
private EditText surName, name;
private EditText studentSurName;
private EditText studentName;
private CheckBox gender;
private ImageView avatar;
private int index = -1, imageResource;
private StudentAdapter studentAdapter;
private Random random = new Random();
private int[] men, women;
private View currentView;
private View studentLeftBorder;
private View studentRightBorder;
private View studentTopBorder;
private View studentBottomBorder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).hide();
setupFields();
setupRecyclerView();
setupButtons();
Expand All @@ -53,8 +56,8 @@ private void setupRecyclerView() {
}

private void setupFields() {
surName = findViewById(R.id.activity_main__studentSurName);
name = findViewById(R.id.activity_main__studentName);
studentSurName = findViewById(R.id.activity_main__studentSurName);
studentName = findViewById(R.id.activity_main__studentName);
gender = findViewById(R.id.activity_main__checkGender);
avatar = findViewById(R.id.activity_main__studentPicture);
men = new int[]{R.drawable.male_1, R.drawable.male_2, R.drawable.male_3};
Expand All @@ -64,7 +67,7 @@ private void setupFields() {

private void onAddClick() {
if (index != -1 && isDataSaved())
Toast.makeText(MainActivity.this, "Для начала сохраните уже созданного студента", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, getResources().getString(R.string.userShouldSaveCreated_warning), Toast.LENGTH_SHORT).show();
else {
int randomGender = random.nextInt(2);
Student student = new Student("", "", randomGender == 1, 0);
Expand All @@ -77,8 +80,9 @@ private void onAddClick() {
}

private int getRandomPhoto(boolean maleGender) {
if (maleGender)
if (maleGender) {
return men[random.nextInt(men.length)];
}
return women[random.nextInt(women.length)];
}

Expand All @@ -89,67 +93,79 @@ private void onChecked() {

private void clearData() {
index = -1;
name.setText("");
surName.setText("");
studentName.setText("");
studentSurName.setText("");
gender.setChecked(false);
avatar.setImageResource(0);
name.setError(null);
surName.setError(null);
studentName.setError(null);
studentSurName.setError(null);
setBorderVisibility(currentView, View.INVISIBLE);
}

private void onStudentClick(View view, int i) {
if (index != -1) {
if (checkFields())
if (isDataSaved())
Toast.makeText(MainActivity.this, "Вам следует сохранить изменения", Toast.LENGTH_SHORT).show();
else
if (checkFields()) {
if (isDataSaved()) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.userShouldSave_warning), 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.

1)Можно писать просто this
2) Toast.makeText может принимать вторым аргументом прямо id строки: R.string.userShouldSaveCreated_warning

} else {
setStudent(view, i);
} else
}
}
} else {
setStudent(view, i);
}
}

private boolean isDataSaved() {
return students.get(index).getFirstName().trim().length() == 0 || students.get(index).getSecondName().trim().length() == 0;
}

private boolean checkFields() {
if (name.getText().toString().trim().length() > 0 && surName.getText().toString().trim().length() > 0)
Copy link

Choose a reason for hiding this comment

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

Лучше всегда расставлять фигурные скобки

if (studentName.getText().toString().trim().length() > 0 && studentSurName.getText().toString().trim().length() > 0)
return true;
if (name.getText().toString().trim().length() == 0)
name.setError("Имя не может быть пустым");
if (surName.getText().toString().trim().length() == 0)
surName.setError("Фамилия не может быть пустой");
if (studentName.getText().toString().trim().length() == 0)
studentName.setError(getResources().getString(R.string.studentName_error));
if (studentSurName.getText().toString().trim().length() == 0)
studentSurName.setError(getResources().getString(R.string.studentSurName_error));
return false;
}

private void setStudent(View view, int i) {
surName.setText(students.get(i).getSecondName());
name.setText(students.get(i).getFirstName());
studentSurName.setText(students.get(i).getSecondName());
studentName.setText(students.get(i).getFirstName());
gender.setChecked(students.get(i).isMaleGender());
avatar.setImageResource(students.get(i).getPhoto());
index = i;
if (currentView != null)
setBorderVisibility(currentView, View.INVISIBLE);
if (view != null)
setBorders(view);
currentView = view;
setBorderVisibility(view, View.VISIBLE);
updateButtons(true);
}

private void setBorders(View view) {
studentLeftBorder = view.findViewById(R.id.student_item__left);
studentRightBorder = view.findViewById(R.id.student_item__right);
studentTopBorder = view.findViewById(R.id.student_item__top);
studentBottomBorder = view.findViewById(R.id.student_item__bottom);
}

private void updateButtons(boolean value) {
name.setEnabled(value);
surName.setEnabled(value);
studentName.setEnabled(value);
studentSurName.setEnabled(value);
gender.setEnabled(value);
findViewById(R.id.activity_main__save_student_button).setEnabled(value);
findViewById(R.id.activity_main__delete_student_button).setEnabled(value);
}

private void setBorderVisibility(View view, int visible) {
if (view != null) {
view.findViewById(R.id.student_item__top).setVisibility(visible);
view.findViewById(R.id.student_item__bottom).setVisibility(visible);
view.findViewById(R.id.student_item__left).setVisibility(visible);
view.findViewById(R.id.student_item__right).setVisibility(visible);
studentTopBorder.setVisibility(visible);
studentBottomBorder.setVisibility(visible);
studentLeftBorder.setVisibility(visible);
studentRightBorder.setVisibility(visible);
}
}

Expand All @@ -159,28 +175,24 @@ private void onSaveClick() {
students.get(index).setSecondName(getLastName());
students.get(index).setMaleGender(gender.isChecked());
students.get(index).setPhoto(imageResource);
Toast.makeText(MainActivity.this, "Студент сохранён", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, getResources().getString(R.string.student_saved), Toast.LENGTH_SHORT).show();
studentAdapter.notifyDataSetChanged();
}

}

private void onDeleteClick() {
updateButtons(false);
students.remove(index);
studentAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Студент удалён", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, getResources().getString(R.string.student_removed), Toast.LENGTH_SHORT).show();
clearData();

}

private String getFirstName() {
return String.valueOf(name.getText());
return String.valueOf(studentName.getText());
}

private String getLastName() {
return String.valueOf(surName.getText());
return String.valueOf(studentSurName.getText());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i
Student student = students.get(i);
studentViewHolder.bind(student);
studentViewHolder.itemView.setTag(i);

}

@Override
Expand All @@ -56,13 +55,11 @@ private void bind(@NonNull Student student) {
nameTextView.setText(String.valueOf(student.getSecondName() + " " + student.getFirstName()));
avatarImageView.setImageResource(student.getPhoto());
}


}

interface Listener {

void onStudentClick(View view, int i);

}
}
}
Loading