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

Version 1.0 Loginov #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
15 changes: 4 additions & 11 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

public class MainActivity extends AppCompatActivity implements StudentAdderDialog.OnAdderStudentListener, StudentAdderDialog.OnUpdateStudentListener {

private static final String DIALOG_TAG = "DialogAdder";
private String[] mansFirstNames;
Copy link

Choose a reason for hiding this comment

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

А если у модели Student было бы 50 полей? Лучше хранить список студентов

Copy link

Choose a reason for hiding this comment

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

Это решил не исправлять?

private String[] womenFirstNames;
private String[] lastNames;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View view) {
DialogFragment dialog = new StudentAdderDialog();
dialog.show(getSupportFragmentManager(), "DialogAdder");
dialog.show(getSupportFragmentManager(), DIALOG_TAG);
}
});

Expand All @@ -71,12 +72,7 @@ private void showDialog(final Student student) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(layoutParams);

if (student.getBitmap() == null) {
view.setImageResource(student.getPhoto());
} else {
view.setImageBitmap(student.getBitmap());
}
view.setImageResource(student.getPhoto());

l0gark marked this conversation as resolved.
Show resolved Hide resolved
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

Expand Down Expand Up @@ -136,11 +132,8 @@ Student nextStudent(boolean maleGender) {


@Override
public void add(String firstName, String lastName, boolean maleGender, Bitmap bitmap) {
public void add(String firstName, String lastName, boolean maleGender) {
Student student = new Student(firstName, lastName, maleGender, maleGender ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]);
if (bitmap != null) {
student.setBitmap(bitmap);
}
students.add(student);
adapter.notifyDataSetChanged();
}
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class Student implements Serializable {
private String secondName;
private boolean maleGender;
private int photo;
private Bitmap bitmap;

public Student(String firstName, String secondName, boolean maleGender, int photo) {
this.firstName = firstName;
Expand Down Expand Up @@ -48,14 +47,6 @@ public int getPhoto() {
return photo;
}

public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}

public Bitmap getBitmap() {
return bitmap;
}

public void setPhoto(int photo) {
this.photo = photo;
}
Expand All @@ -65,5 +56,4 @@ public String toString() {
return firstName + " " + secondName + (isMaleGender() ? " М" : " Ж");
}


}
143 changes: 55 additions & 88 deletions app/src/main/java/ru/ok/technopolis/students/StudentAdderDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
Expand All @@ -26,11 +28,11 @@


public class StudentAdderDialog extends DialogFragment implements DialogInterface.OnShowListener {
private Bitmap bitmap;
private AlertDialog dialog;
private static final int GALLERY_REQUEST = 1;
l0gark marked this conversation as resolved.
Show resolved Hide resolved
public static final String STUDENT_TAG = "STUDENT_TAG";
private EditText fnEdit, lnEdit;
private EditText editTextFirstName;
private EditText editTextLastName;
private ImageView imageView;
l0gark marked this conversation as resolved.
Show resolved Hide resolved
private RadioGroup radioGroup;
private Context context;
Expand All @@ -42,116 +44,81 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
if (args != null) {
oldStudent = (Student) args.getSerializable(STUDENT_TAG);
if (oldStudent != null) {
bitmap = oldStudent.getBitmap();
}
}
View view = LayoutInflater.from(context).inflate(R.layout.dialog_student_adder, null);
initView(view);
AlertDialog.Builder adb = new AlertDialog.Builder(context)
.setView(view).setTitle(R.string.student)
.setMessage(R.string.trydroid)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.back, null);
dialog = adb.create();
AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view).setTitle(R.string.student).setMessage(R.string.trydroid).setPositiveButton(R.string.ok, null).setNegativeButton(R.string.back, null);
dialog = builder.create();
dialog.setOnShowListener(this);

return dialog;
}

l0gark marked this conversation as resolved.
Show resolved Hide resolved

private void initView(View view) {
fnEdit = view.findViewById(R.id.dialog_student_adder__first_name);
lnEdit = view.findViewById(R.id.dialog_student_adder__last_name);
editTextFirstName = view.findViewById(R.id.dialog_student_adder__first_name);
editTextLastName = view.findViewById(R.id.dialog_student_adder__last_name);
imageView = view.findViewById(R.id.dialog_student_adder__avatar);
radioGroup = view.findViewById(R.id.dialog_student_adder__radio_group);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}
});

if (oldStudent != null) {
fnEdit.setText(oldStudent.getFirstName());
lnEdit.setText(oldStudent.getSecondName());
if (bitmap == null) {
imageView.setImageResource(oldStudent.getPhoto());
} else {
imageView.setImageBitmap(bitmap);
}
radioGroup.clearCheck();
int index = oldStudent.isMaleGender() ? 0 : 1;
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
editTextFirstName.setText(oldStudent.getFirstName());
editTextLastName.setText(oldStudent.getSecondName());
imageView.setImageResource(oldStudent.getPhoto());
radioGroup.check(oldStudent.isMaleGender() ? R.id.dialog_student_adder__male : R.id.dialog_student_adder__female);
}
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GALLERY_REQUEST:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), selectedImage);
int width = 1000;
int height = (int) ((double) width / bitmap.getWidth() * bitmap.getHeight());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
assert selectedImage != null;
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
public void onShow(DialogInterface dialogInterface) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Editable textFirstName = editTextFirstName.getText();
Editable textLastName = editTextLastName.getText();
if (textFirstName == null || textLastName == null) {
l0gark marked this conversation as resolved.
Show resolved Hide resolved
Toast.makeText(context, R.string.incorrect, Toast.LENGTH_SHORT).show();
return;
}

String name = textFirstName.toString();
String lastName = textLastName.toString();
boolean maleGender = radioGroup.getCheckedRadioButtonId() == R.id.dialog_student_adder__male;

if (TextUtils.isEmpty(name) || TextUtils.isEmpty(lastName) || radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(context, R.string.incorrect, Toast.LENGTH_SHORT).show();
return;
}

if (oldStudent == null) {
OnAdderStudentListener listener = (OnAdderStudentListener) context;
if (listener != null) {
listener.add(name, lastName, maleGender);
}
} else {
OnUpdateStudentListener listener = (OnUpdateStudentListener) context;
if (listener != null) {
Student student = new Student(name, lastName, maleGender, oldStudent.getPhoto());
listener.update(oldStudent, student);
}
}
}
}

@Override
public void onShow(DialogInterface dialogInterface) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = fnEdit.getText().toString();
String lastName = lnEdit.getText().toString();
boolean maleGender = radioGroup.getCheckedRadioButtonId() == R.id.radioButton1;

if (name.length() == 0 || lastName.length() == 0 || radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(context, R.string.incorrect, Toast.LENGTH_SHORT).show();
return;
}

if (oldStudent == null) {
OnAdderStudentListener listener = (OnAdderStudentListener) context;
if (listener != null) {
listener.add(name, lastName, maleGender, bitmap);
}
} else {
OnUpdateStudentListener listener = (OnUpdateStudentListener) context;
if (listener != null) {
Student student = new Student(name, lastName, maleGender, oldStudent.getPhoto());
student.setBitmap(bitmap);
listener.update(oldStudent, student);
}
}

dialog.cancel();
}
});


dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.cancel();
}
});


dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}

interface OnAdderStudentListener {
void add(String firstName, String lastName, boolean maleGender, Bitmap bitmap);
void add(String firstName, String lastName, boolean maleGender);
}


Expand Down
17 changes: 6 additions & 11 deletions app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,21 @@ interface OnStudentClickListener {
class StudentHolder extends RecyclerView.ViewHolder {

private ImageView imageView;
private TextView fnTextView, lnTextView;
private TextView textFirstName, textLastName;

StudentHolder(@NonNull View itemView) {
super(itemView);

imageView = itemView.findViewById(R.id.item_student__avatar);

fnTextView = itemView.findViewById(R.id.item_student__first_name);
lnTextView = itemView.findViewById(R.id.item_student__last_name);
textFirstName = itemView.findViewById(R.id.item_student__first_name);
textLastName = itemView.findViewById(R.id.item_student__last_name);
}

void bind(Student student) {
Bitmap bitmap = student.getBitmap();
if (bitmap == null) {
imageView.setImageResource(student.getPhoto());
} else {
imageView.setImageBitmap(bitmap);
}
fnTextView.setText(student.getFirstName());
lnTextView.setText(student.getSecondName());
imageView.setImageResource(student.getPhoto());
textFirstName.setText(student.getFirstName());
textLastName.setText(student.getSecondName());
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/dialog_student_adder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@
app:layout_constraintTop_toBottomOf="@+id/dialog_student_adder__last_name">

<RadioButton
android:id="@+id/radioButton1"
android:id="@+id/dialog_student_adder__male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/guy" />

<RadioButton
android:id="@+id/radioButton2"
android:id="@+id/dialog_student_adder__female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
Expand Down