From 1bec6d281c1746208fe6cb7a83ff8724584d499b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 6 Mar 2020 21:24:01 +0100 Subject: [PATCH] [Grades] Implement Grades editor. --- .../ui/modules/grades/GradesAdapter.kt | 5 +++-- .../ui/modules/grades/GradesFragment.kt | 21 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesAdapter.kt index 0aa92184..39796686 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesAdapter.kt @@ -21,7 +21,7 @@ import pl.szczodrzynski.edziennik.ui.modules.grades.viewholder.* class GradesAdapter( val activity: AppCompatActivity, var onGradeClick: ((item: GradeFull) -> Unit)? = null, - var onGradesEditorClick: ((subjectId: Long, semesterNumber: Int) -> Unit)? = null + var onGradesEditorClick: ((subject: GradesSubject, semester: GradesSemester) -> Unit)? = null ) : RecyclerView.Adapter() { companion object { private const val TAG = "GradesAdapter" @@ -176,7 +176,8 @@ class GradesAdapter( if (holder is SemesterViewHolder && item is GradesSemester) { holder.b.editButton.onClick { - onGradesEditorClick?.invoke(item.subjectId, item.number) + val subject = items.firstOrNull { it is GradesSubject && it.subjectId == item.subjectId } as? GradesSubject ?: return@onClick + onGradesEditorClick?.invoke(subject, item) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesFragment.kt index d58af876..0e80f806 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/grades/GradesFragment.kt @@ -13,7 +13,9 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.Bundle import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.MainActivity.Companion.TARGET_GRADES_EDITOR import pl.szczodrzynski.edziennik.averageOrNull import pl.szczodrzynski.edziennik.data.db.entity.Grade import pl.szczodrzynski.edziennik.data.db.full.GradeFull @@ -89,8 +91,25 @@ class GradesFragment : Fragment(), CoroutineScope { GradeDetailsDialog(activity, it) } - adapter.onGradesEditorClick = { subjectId, semesterNumber -> + adapter.onGradesEditorClick = { subject, semester -> + val otherSemester = subject.semesters.firstOrNull { it != semester } + var gradeSumOtherSemester = otherSemester?.averages?.normalWeightedSum + var gradeCountOtherSemester = otherSemester?.averages?.normalWeightedCount + if (gradeSumOtherSemester ?: 0f == 0f || gradeCountOtherSemester ?: 0f == 0f) { + gradeSumOtherSemester = otherSemester?.averages?.normalSum + gradeCountOtherSemester = otherSemester?.averages?.normalCount?.toFloat() + } + activity.loadTarget(TARGET_GRADES_EDITOR, Bundle( + "subjectId" to subject.subjectId, + "semester" to semester.number, + "averageMode" to manager.yearAverageMode, + "yearAverageBefore" to subject.averages.normalAvg, + "gradeSumOtherSemester" to gradeSumOtherSemester, + "gradeCountOtherSemester" to gradeCountOtherSemester, + "averageOtherSemester" to otherSemester?.averages?.normalAvg, + "finalOtherSemester" to otherSemester?.finalGrade?.value + )) } }