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
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ dependencies {
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 'com.android.support:design:28.0.0'
}
143 changes: 142 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,155 @@
package ru.ok.technopolis.students;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
import java.util.ArrayList;
import java.util.Random;

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

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;
private int[] manPhotos;
private int[] womanPhotos;
private Random random;
private int manPhotoIndex, womanPhotoIndex;
private ArrayList<Student> students;
private StudentsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
manPhotoIndex = womanPhotoIndex = 0;
mansFirstNames = getResources().getStringArray(R.array.mans_first_names);
womenFirstNames = getResources().getStringArray(R.array.women_first_names);
lastNames = getResources().getStringArray(R.array.last_names);
manPhotos = new int[]{R.drawable.male_01, R.drawable.male_02, R.drawable.male_03};
womanPhotos = new int[]{R.drawable.female_01, R.drawable.female_02, R.drawable.female_03};


RecyclerView recyclerView = findViewById(R.id.activity_main__recycler_view);
FloatingActionButton fabAdd = findViewById(R.id.activity_main__fab_add);

fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment dialog = new StudentAdderDialog();
dialog.show(getSupportFragmentManager(), "DialogAdder");
l0gark marked this conversation as resolved.
Show resolved Hide resolved
}
});

students = createStudents();
adapter = new StudentsAdapter(students, new StudentsAdapter.OnStudentClickListener() {
@Override
public void onStudentClick(Student student) {
showDialog(student);
}
});
recyclerView.setAdapter(adapter);
LinearLayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
}

private void showDialog(final Student student) {
ImageView view = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(layoutParams);

l0gark marked this conversation as resolved.
Show resolved Hide resolved
if (student.getBitmap() == null) {
view.setImageResource(student.getPhoto());
} else {
view.setImageBitmap(student.getBitmap());
}

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setView(view);
builder.setTitle(student.getFirstName() + " " + student.getSecondName());
builder.setMessage(student.isMaleGender() ? R.string.guy : R.string.girl);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});

builder.setNegativeButton(R.string.edit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
DialogFragment dialog = new StudentAdderDialog();
Bundle bundle = new Bundle();
bundle.putSerializable(StudentAdderDialog.STUDENT_TAG, student);
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogAdder");
dialogInterface.cancel();
}
});

builder.setNeutralButton(R.string.delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
students.remove(student);
adapter.notifyDataSetChanged();
dialogInterface.cancel();
}
});
builder.create().show();
}


ArrayList<Student> createStudents() {
ArrayList<Student> students = new ArrayList<>();
for (int i = 0; i < 20; i++) {
students.add(nextStudent(i % 2 == 0));
}
return students;
}


Student nextStudent(boolean maleGender) {
if (maleGender) {
int nameIndex = random.nextInt(mansFirstNames.length);
int lastNameIndex = random.nextInt(mansFirstNames.length);
return new Student(mansFirstNames[nameIndex], lastNames[lastNameIndex], true, manPhotos[manPhotoIndex++ % 3]);
}
int nameIndex = random.nextInt(womenFirstNames.length);
int lastNameIndex = random.nextInt(womenFirstNames.length);
return new Student(womenFirstNames[nameIndex], lastNames[lastNameIndex] + "a", false, womanPhotos[womanPhotoIndex++ % 3]);
}


@Override
public void add(String firstName, String lastName, boolean maleGender, Bitmap bitmap) {
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();
}

@Override
public void update(Student oldStudent, Student newStudent) {
int index = students.indexOf(oldStudent);
students.remove(oldStudent);
students.add(index, newStudent);
adapter.notifyDataSetChanged();
}
}
23 changes: 22 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package ru.ok.technopolis.students;

public class Student {
import android.graphics.Bitmap;

import java.io.Serializable;

public class Student implements Serializable {

private String firstName;
private String secondName;
private boolean maleGender;
private int photo;
private Bitmap bitmap;
Copy link

Choose a reason for hiding this comment

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

Тут битмапу не нужно хранить. Это занимает слишком много места. Лучше хранить путь до изображения


public Student(String firstName, String secondName, boolean maleGender, int photo) {
this.firstName = firstName;
Expand All @@ -14,6 +19,7 @@ public Student(String firstName, String secondName, boolean maleGender, int phot
this.photo = photo;
}


public String getFirstName() {
return firstName;
}
Expand Down Expand Up @@ -42,7 +48,22 @@ 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;
}

@Override
public String toString() {
return firstName + " " + secondName + (isMaleGender() ? " М" : " Ж");
}


}
Loading