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

Students | Vadim Dyachkov #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/build
/captures
.externalNativeBuild
.idea
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 @@ -22,6 +22,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:2.2.0'
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
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".StudentsApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java

This file was deleted.

147 changes: 147 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package ru.ok.technopolis.students;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class StudentView extends ConstraintLayout {

private static final Random rand = new Random();
private static final int[] malePhotos = {R.drawable.male_1, R.drawable.male_2, R.drawable.male_3};
private static final int[] femalePhotos = {R.drawable.female_1, R.drawable.female_2, R.drawable.female_3};

private Student student;

private TextView firstNameTv;
private TextView secondNameTv;
private ImageView photoIv;
private CheckBox isMaleCb;

private OnClickListener onSave;
private OnClickListener onCreate;
private OnClickListener onRemove;
private OnClickListener onHide;

public StudentView(Context context) {
super(context);
init(context);
}

public StudentView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public StudentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.student_view, this, false);
Copy link

Choose a reason for hiding this comment

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

Что бы не писать addView(view); можно третим аргументов поставить true

Copy link
Author

Choose a reason for hiding this comment

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

fixed

addView(view);

photoIv = view.findViewById(R.id.student_view__photo);
firstNameTv = view.findViewById(R.id.student_view__first_name);
secondNameTv = view.findViewById(R.id.student_view__second_name);
isMaleCb = view.findViewById(R.id.student_view__is_male);

Button saveBtn = view.findViewById(R.id.student_view__save);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (student != null) {
student.setFirstName(firstNameTv.getText().toString());
Copy link

Choose a reason for hiding this comment

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

firstNameTv.getText() иногда возвращает null

Copy link
Author

Choose a reason for hiding this comment

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

fixed

student.setSecondName(secondNameTv.getText().toString());
student.setMaleGender(isMaleCb.isChecked());
StudentView.this.onSave.onClick(v);
} else {
String firstName = firstNameTv.getText().toString();
String secondName = secondNameTv.getText().toString();
if (firstName.isEmpty() || secondName.isEmpty()) {
String text = getContext().getString(R.string.error_insert_first_name_and_second_name);
Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
return;
}
boolean isMale = isMaleCb.isChecked();
int photo = randomPhoto(isMale);
photoIv.setImageResource(photo);
student = new Student(firstName, secondName, isMale, photo);
v.setTag(student);
onCreate.onClick(v);
}
}
});
Button hideBtn = view.findViewById(R.id.student_view__hide);
hideBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onHide.onClick(v);
Copy link

Choose a reason for hiding this comment

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

Во всех лисенерах нет проверки на нул. Если не был установлен лисенер

Copy link
Author

Choose a reason for hiding this comment

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

fixed

StudentView.this.setVisibility(GONE);
}
});
Button removeBtn = view.findViewById(R.id.student_view__remove);
removeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (student != null) {
onRemove.onClick(v);
StudentView.this.setVisibility(GONE);
clearFields();
}
}
});
}

public void setOnRemove(OnClickListener onRemove) {
this.onRemove = onRemove;
}

public void setOnSave(OnClickListener onSave) {
this.onSave = onSave;
}

public void setOnCreate(OnClickListener onCreate) {
this.onCreate = onCreate;
}

public void setOnHide(OnClickListener onHide) {
this.onHide = onHide;
}

public void setStudent(Student student) {
this.student = student;
if (student != null) {
photoIv.setImageResource(student.getPhoto());
firstNameTv.setText(student.getFirstName());
secondNameTv.setText(student.getSecondName());
isMaleCb.setChecked(student.isMaleGender());
} else {
clearFields();
}
setVisibility(VISIBLE);
}

private void clearFields() {
photoIv.setImageResource(R.color.colorWhite);
firstNameTv.setText("");
secondNameTv.setText("");
isMaleCb.setChecked(false);
}

private static int randomPhoto(boolean isMale) {
return isMale
? malePhotos[rand.nextInt(malePhotos.length)]
: femalePhotos[rand.nextInt(femalePhotos.length)];
}
}
75 changes: 75 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ru.ok.technopolis.students;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.StudentViewHolder> {

private final StudentsRepository repository;
private final View.OnClickListener onClick;

public StudentsAdapter(StudentsRepository repository, View.OnClickListener onClick) {
this.repository = repository;
this.onClick = onClick;
}

@NonNull
@Override
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_list_item, viewGroup, false);
return new StudentViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull StudentViewHolder viewHolder, int i) {
viewHolder.bind(repository.get(i));
viewHolder.itemView.setTag(i);

Integer active = repository.getActive();
if (active != null && active.equals(i)) {
viewHolder.itemView.setBackgroundResource(R.color.colorLightGray);
} else {
viewHolder.itemView.setBackgroundResource(R.color.colorWhite);
}

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
repository.setActive(position);
onClick.onClick(v);
notifyDataSetChanged();
}
});
}

@Override
public int getItemCount() {
return repository.size();
}

static final class StudentViewHolder extends RecyclerView.ViewHolder {

private final ImageView photo;
private final TextView name;

StudentViewHolder(@NonNull View itemView) {
super(itemView);
photo = itemView.findViewById(R.id.students_list_item_photo);
name = itemView.findViewById(R.id.students_list_item_name);
}

@SuppressLint("SetTextI18n")
private void bind(@NonNull Student student) {
photo.setImageResource(student.getPhoto());
name.setText(student.getFirstName() + " " + student.getSecondName());
}
}

}
Loading