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
Open
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'
}
138 changes: 138 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,152 @@
package ru.ok.technopolis.students;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.design.widget.FloatingActionButton;
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.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
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;
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) {
StudentAdderDialog dialog = new StudentAdderDialog();
dialog.setOnAdderStudentListener(new StudentAdderDialog.OnAdderStudentListener() {
@Override
public void add(String firstName, String lastName, boolean maleGender) {
Student student = new Student(firstName, lastName, maleGender, maleGender ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]);
students.add(student);
adapter.notifyDataSetChanged();
}
});
dialog.show(getSupportFragmentManager(), DIALOG_TAG);
}
});

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);
view.setImageResource(student.getPhoto());

l0gark marked this conversation as resolved.
Show resolved Hide resolved
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) {
StudentAdderDialog dialog = new StudentAdderDialog();
Bundle bundle = new Bundle();
bundle.putSerializable(StudentAdderDialog.STUDENT_TAG, student);
dialog.setArguments(bundle);

dialog.setOnUpdateStudentListener(new StudentAdderDialog.OnUpdateStudentListener() {
@Override
public void update(Student oldStudent, Student newStudent) {
int index = students.indexOf(oldStudent);
students.remove(oldStudent);
if (oldStudent.isMaleGender() != newStudent.isMaleGender()) {
newStudent.setPhoto(newStudent.isMaleGender() ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]);
}
students.add(index, newStudent);
adapter.notifyDataSetChanged();
}
});

dialog.show(getSupportFragmentManager(), DIALOG_TAG);
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]);
}


}
12 changes: 11 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.ok.technopolis.students;

public class Student {

import java.io.Serializable;

public class Student implements Serializable {

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


public String getFirstName() {
return firstName;
}
Expand Down Expand Up @@ -45,4 +49,10 @@ public int getPhoto() {
public void setPhoto(int photo) {
this.photo = photo;
}

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

}
Loading