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

implementation #11

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.getbase:floatingactionbutton:1.9.1'
implementation 'com.google.dagger:dagger-android:2.20'
Copy link

Choose a reason for hiding this comment

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

Зачем добавил эти зависимости?

implementation 'com.google.dagger:dagger-android-support:2.20' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
annotationProcessor 'com.google.dagger:dagger-compiler:2.20'
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'
Expand Down
15 changes: 7 additions & 8 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import com.getbase.floatingactionbutton.FloatingActionButton;

import ru.ok.technopolis.students.Repository.StudentDataRepository;
import ru.ok.technopolis.students.repository.StudentDataRepository;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Expand All @@ -21,6 +21,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private final String dataResponse = "Student";
private final int ACTION_ON_STUDENT_CLICK = 2;
private final int ACTION_CREATE_STUDENT_CLICK = 1;
private final String dataRequestNewStudent = "NewStudent";
Copy link

Choose a reason for hiding this comment

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

  1. Константы лучше делать private static final
  2. Имя константы пишется капсом через подчеркивание, как у ACTION_CREATE_STUDENT_CLICK

private final String dataRequestModifyStudent = "ModifyStudent";
private final String dataRequestDeleteStudent = "StudentForDelete";


@Override
Expand Down Expand Up @@ -60,21 +63,17 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
return;
}
Student student;
String dataRequest;
switch (resultCode) {
case 1:
Copy link

Choose a reason for hiding this comment

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

  1. Константы создал, а чего не используешь? ACTION_CREATE_STUDENT_CLICK?
  2. "NewStudent" это тоже константа

dataRequest = "NewStudent";
student = (Student) data.getSerializableExtra(dataRequest);
student = (Student) data.getSerializableExtra(dataRequestNewStudent);
studentDataRepository.add(student);
break;
case 2:
dataRequest = "ModifyStudent";
student = (Student) data.getSerializableExtra(dataRequest);
student = (Student) data.getSerializableExtra(dataRequestModifyStudent);
studentDataRepository.edit(student);
break;
case 3:
dataRequest = "StudentForDelete";
student = (Student) data.getSerializableExtra(dataRequest);
student = (Student) data.getSerializableExtra(dataRequestDeleteStudent);
studentDataRepository.delete(student);
break;
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public String getFirstName() {
return firstName;
}

public boolean photoAvailable() {
return photo != 0;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}
Expand Down
121 changes: 49 additions & 72 deletions app/src/main/java/ru/ok/technopolis/students/StudentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.NoSuchElementException;

import ru.ok.technopolis.students.Repository.FemalePhotoRepository;
import ru.ok.technopolis.students.Repository.MalePhotoRepository;
import ru.ok.technopolis.students.Repository.PhotoRepository;

import ru.ok.technopolis.students.controller.StudentActivityController;

public class StudentActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView studentImageViewPhoto;
private EditText studentFirstName;
private EditText studentSecondName;
private CheckBox genderCheckbox;
private Student currentStudent;
private PhotoRepository photoRepository;
private String firstName;
private String secondName;
private boolean isMale;
private StudentActivityController studentActivityController = StudentActivityController.Instance;
private final int CREATE_STUDENT_RESULT_CODE = 1;
private final int MODIFY_STUDENT_RESULT_CODE = 2;
private final int DELETE_STUDENT_RESULT_CODE = 3;
private final String RESPONSE_CREATE_STUDENT = "NewStudent";
private final String RESPONSE_DELETE_STUDENT = "StudentForDelete";
private final String RESPONSE_MODIFY_STUDENT = "ModifyStudent";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -36,92 +41,64 @@ protected void onCreate(Bundle savedInstanceState) {
genderCheckbox = findViewById(R.id.checkBox__gender);
studentImageViewPhoto = findViewById(R.id.student_activity__photo);
if (getIntent().getExtras() != null) {
currentStudent = (Student) getIntent().getExtras().getSerializable("Student");
if (currentStudent.isMaleGender()) {
photoRepository = MalePhotoRepository.Instance;
} else {
photoRepository = FemalePhotoRepository.Instance;
}
genderCheckbox.setVisibility(View.GONE);
setStudent();
studentActivityController.setCurrentStudent((Student) getIntent().getExtras().getSerializable("Student"));
studentFirstName.setText(studentActivityController.getCurrentStudent().getFirstName());
studentSecondName.setText(studentActivityController.getCurrentStudent().getSecondName());
if (studentActivityController.getCurrentStudent().isMaleGender())
genderCheckbox.setChecked(true);
studentImageViewPhoto.setImageResource(studentActivityController.getCurrentStudent().getPhoto());
genderCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
studentActivityController.putPhotoInPhotoRepository(studentActivityController.getCurrentStudent());
if(isChecked) {
studentActivityController.createStudent(isMale);
}
else {
studentActivityController.createStudent(isMale);
}
}
});
} else {
currentStudent = new Student();
studentActivityController.setCurrentStudent(new Student());
deleteStudent.setVisibility(View.GONE);
}
saveStudent.setOnClickListener(this);
deleteStudent.setOnClickListener(this);
}


private void setStudent() {
studentFirstName.setText(currentStudent.getFirstName());
studentSecondName.setText(currentStudent.getSecondName());
if (currentStudent.isMaleGender())
genderCheckbox.isChecked();
studentImageViewPhoto.setImageResource(currentStudent.getPhoto());
private void setFields() {
firstName = studentFirstName.getText().toString();
secondName = studentSecondName.getText().toString();
isMale = genderCheckbox.isChecked();
}


private boolean createStudent() {
try {
if (genderCheckbox.isChecked()) {
photoRepository = MalePhotoRepository.Instance;
} else {
photoRepository = FemalePhotoRepository.Instance;
}
currentStudent.setPhoto(photoRepository.getPhotoInRepository());
} catch (NoSuchElementException e) {
Toast.makeText(StudentActivity.this, "No Photo in DataBase", Toast.LENGTH_LONG).show();
return false;
}
setInitials();
return true;
}

private boolean modifyStudent() {
if (currentStudent.getPhoto() == 0) {
return false;
}
setInitials();
return true;
}

private void setInitials() {
currentStudent.setMaleGender(genderCheckbox.isChecked());
currentStudent.setFirstName(studentFirstName.getText().toString());
currentStudent.setSecondName(studentSecondName.getText().toString());
}


@Override
public void onClick(View v) {
int resultCode;
String dataResponse;
setFields();
studentActivityController.setStudentInitials(firstName, secondName, isMale);
Student student = studentActivityController.getCurrentStudent();
switch (v.getId()) {
case R.id.activity_student__save_button: {
if (studentFirstName.getText().toString().equals("") || studentSecondName.getText().toString().equals("")) {
Toast.makeText(StudentActivity.this, "Please, fill all the fields", Toast.LENGTH_LONG).show();
return;
}
if (modifyStudent()) {
resultCode = 2;
dataResponse = "ModifyStudent";
setResult(resultCode, new Intent().putExtra(dataResponse, currentStudent));
studentImageViewPhoto.setImageResource(currentStudent.getPhoto());
} else if (createStudent()) {
resultCode = 1;
dataResponse = "NewStudent";
setResult(resultCode, new Intent().putExtra(dataResponse, currentStudent));
studentImageViewPhoto.setImageResource(currentStudent.getPhoto());
if (studentActivityController.modifyStudent()) {
setResult(MODIFY_STUDENT_RESULT_CODE, new Intent().putExtra(RESPONSE_MODIFY_STUDENT, student));
studentImageViewPhoto.setImageResource(student.getPhoto());
} else if (studentActivityController.createStudent(isMale)) {
setResult(CREATE_STUDENT_RESULT_CODE, new Intent().putExtra(RESPONSE_CREATE_STUDENT, student));
studentImageViewPhoto.setImageResource(student.getPhoto());
} else {
Toast.makeText(StudentActivity.this, "No Photo in DataBase", Toast.LENGTH_LONG).show();
Copy link

Choose a reason for hiding this comment

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

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

}
finish();
break;
}
case R.id.activity_student__delete_button: {
resultCode = 3;
dataResponse = "StudentForDelete";
setResult(resultCode, new Intent().putExtra(dataResponse, currentStudent));
photoRepository.putPhotoInRepository(currentStudent.getPhoto());
setResult(DELETE_STUDENT_RESULT_CODE, new Intent().putExtra(RESPONSE_DELETE_STUDENT, student));
Copy link

Choose a reason for hiding this comment

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

Смотри то такое result code в описании метода setResult. Это должны быть коды из набора:
* @see #RESULT_CANCELED
* @see #RESULT_OK
* @see #RESULT_FIRST_USER

studentActivityController.putPhotoInPhotoRepository(student);
finish();
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.ok.technopolis.students.controller;

import java.util.NoSuchElementException;

import ru.ok.technopolis.students.Student;
import ru.ok.technopolis.students.repository.FemalePhotoRepository;
import ru.ok.technopolis.students.repository.MalePhotoRepository;
import ru.ok.technopolis.students.repository.PhotoRepository;

public class StudentActivityController {

public static final StudentActivityController Instance = new StudentActivityController();

private PhotoRepository photoRepository;
private Student currentStudent;

public void setCurrentStudent(Student student) {
currentStudent = student;
}

public Student getCurrentStudent() {
return currentStudent;
}

private StudentActivityController() {

}

Copy link

Choose a reason for hiding this comment

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

Следи за лишними строками


public boolean modifyStudent() {
return currentStudent.photoAvailable();
}


private void setPhotoRepository(boolean genderFlag) {
if (genderFlag) {
photoRepository = MalePhotoRepository.Instance;
} else {
photoRepository = FemalePhotoRepository.Instance;
}
}

public void putPhotoInPhotoRepository(Student student) {
setPhotoRepository(student.isMaleGender());
photoRepository.putPhotoInRepository(student.getPhoto());
}


public boolean createStudent(boolean genderFlag) {
return setPhotoForStudent(genderFlag);
}

private boolean setPhotoForStudent(boolean isChecked) {
try {
setPhotoRepository(isChecked);
currentStudent.setPhoto(photoRepository.getPhotoInRepository());
} catch (NoSuchElementException e) {
return false;
}
return true;
}

public void setStudentInitials(String firstName, String secondName, boolean isMale) {
currentStudent.setMaleGender(isMale);
currentStudent.setFirstName(firstName);
currentStudent.setSecondName(secondName);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.ok.technopolis.students.Repository;
package ru.ok.technopolis.students.repository;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,7 +21,6 @@ private FemalePhotoRepository() {
}};
}


@Override
public int getPhotoInRepository() throws NoSuchElementException {
if (femalePhotos.isEmpty()) {
Expand All @@ -37,4 +36,4 @@ public int getPhotoInRepository() throws NoSuchElementException {
public void putPhotoInRepository(int photo) {
femalePhotos.add(photo);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.ok.technopolis.students.Repository;
package ru.ok.technopolis.students.repository;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.ok.technopolis.students.Repository;
package ru.ok.technopolis.students.repository;

public interface PhotoRepository {
int getPhotoInRepository();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ru.ok.technopolis.students.Repository;
package ru.ok.technopolis.students.repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import ru.ok.technopolis.students.Student;

Expand Down Expand Up @@ -45,6 +44,7 @@ public void edit(Student student) {
if (students.get(i).getId().equals(student.getId())) {
students.get(i).setFirstName(student.getFirstName());
students.get(i).setSecondName(student.getSecondName());
students.get(i).setPhoto(student.getPhoto());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.ok.technopolis.students.Repository;
package ru.ok.technopolis.students.repository;

import java.util.List;

Expand Down
Loading