[Grades] Implement Grades editor.

This commit is contained in:
Kuba Szczodrzyński 2020-03-06 21:24:01 +01:00
parent f17a02be54
commit 1bec6d281c
2 changed files with 23 additions and 3 deletions

View File

@ -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<RecyclerView.ViewHolder>() {
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)
}
}

View File

@ -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
))
}
}