From 3656d3161f1d5ae3c160f37526ad4e33cfd423fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 24 Mar 2019 17:31:39 +0100 Subject: [PATCH] Fix crash on duplicate items (#303) --- .../ui/modules/account/AccountItem.kt | 17 ++++++------- .../ui/modules/attendance/AttendanceItem.kt | 16 ++++++------ .../summary/AttendanceSummaryItem.kt | 12 ++++----- .../wulkanowy/ui/modules/exam/ExamItem.kt | 11 ++++---- .../modules/grade/details/GradeDetailsItem.kt | 9 ++----- .../modules/grade/summary/GradeSummaryItem.kt | 15 ++++------- .../ui/modules/homework/HomeworkItem.kt | 18 ++++++------- .../studentselect/LoginStudentSelectItem.kt | 25 +++++++------------ .../ui/modules/message/MessageItem.kt | 11 +++----- .../wulkanowy/ui/modules/more/MoreItem.kt | 11 ++++---- .../wulkanowy/ui/modules/note/NoteItem.kt | 10 +++----- .../ui/modules/timetable/TimetableItem.kt | 21 ++++++++-------- 12 files changed, 70 insertions(+), 106 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt index a564306d..cb7aabda 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt @@ -14,12 +14,12 @@ class AccountItem(val student: Student) : AbstractFlexibleItem>?): ViewHolder { + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ViewHolder?, position: Int, payloads: MutableList?) { - holder?.apply { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { + holder.apply { accountItemName.text = student.studentName accountItemSchool.text = student.schoolName accountItemImage.setBackgroundResource(if (student.isCurrent) R.drawable.ic_account_circular_border else 0) @@ -38,14 +38,13 @@ class AccountItem(val student: Student) : AbstractFlexibleItem>?) : FlexibleViewHolder(view, adapter), - LayoutContainer { - - override val containerView: View? + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { + override val containerView: View get() = contentView } } - diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceItem.kt index c2698ebb..16a140cb 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceItem.kt @@ -14,14 +14,13 @@ import kotlinx.android.synthetic.main.item_attendance.* class AttendanceItem(val attendance: Attendance) : AbstractFlexibleItem() { + override fun getLayoutRes() = R.layout.item_attendance + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - override fun getLayoutRes(): Int = R.layout.item_attendance - - override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, - position: Int, payloads: MutableList?) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.apply { attendanceItemNumber.text = attendance.number.toString() attendanceItemSubject.text = attendance.subject @@ -37,16 +36,17 @@ class AttendanceItem(val attendance: Attendance) : AbstractFlexibleItem) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryItem.kt index 3102ce11..265d6ce4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryItem.kt @@ -23,12 +23,12 @@ class AttendanceSummaryItem( override fun getLayoutRes() = R.layout.item_attendance_summary - override fun createViewHolder(view: View?, adapter: FlexibleAdapter>?): ViewHolder { + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ViewHolder?, position: Int, payloads: MutableList?) { - holder?.apply { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { + holder.apply { attendanceSummaryMonth.text = month attendanceSummaryPercentage.text = percentage attendanceSummaryPresent.text = present @@ -73,10 +73,8 @@ class AttendanceSummaryItem( return result } - class ViewHolder(view: View?, adapter: FlexibleAdapter>?) : FlexibleViewHolder(view, adapter), - LayoutContainer { - - override val containerView: View? + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { + override val containerView: View get() = contentView } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamItem.kt index 318685e9..8971b4df 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamItem.kt @@ -18,8 +18,7 @@ class ExamItem(header: ExamHeader, val exam: Exam) : AbstractSectionableItem>, holder: ViewHolder, - position: Int, payloads: MutableList?) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.run { examItemSubject.text = exam.subject examItemTeacher.text = exam.teacher @@ -39,12 +38,12 @@ class ExamItem(header: ExamHeader, val exam: Exam) : AbstractSectionableItem) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsItem.kt index de1d88f7..1e47eca5 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsItem.kt @@ -28,10 +28,7 @@ class GradeDetailsItem( } @SuppressLint("SetTextI18n") - override fun bindViewHolder( - adapter: FlexibleAdapter>, holder: ViewHolder, - position: Int, payloads: MutableList? - ) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.run { gradeItemValue.run { text = grade.entry @@ -70,9 +67,7 @@ class GradeDetailsItem( return result } - class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryItem.kt index 0daf0665..5737a832 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryItem.kt @@ -18,15 +18,12 @@ class GradeSummaryItem( override fun getLayoutRes() = R.layout.item_grade_summary - override fun createViewHolder(view: View?, adapter: FlexibleAdapter>?): ViewHolder { + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - override fun bindViewHolder( - adapter: FlexibleAdapter>?, holder: ViewHolder?, - position: Int, payloads: MutableList? - ) { - holder?.run { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { + holder.run { gradeSummaryItemTitle.text = title gradeSummaryItemAverage.text = average gradeSummaryItemPredicted.text = predictedGrade @@ -56,10 +53,8 @@ class GradeSummaryItem( return result } - class ViewHolder(view: View?, adapter: FlexibleAdapter>?) : FlexibleViewHolder(view, adapter), - LayoutContainer { - - override val containerView: View? + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { + override val containerView: View get() = contentView } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt index a2b8cc0a..2de9233f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt @@ -10,9 +10,8 @@ import io.github.wulkanowy.data.db.entities.Homework import kotlinx.android.extensions.LayoutContainer import kotlinx.android.synthetic.main.item_homework.* -class HomeworkItem( - header: HomeworkHeader, val homework: Homework -) : AbstractSectionableItem(header) { +class HomeworkItem(header: HomeworkHeader, val homework: Homework) : + AbstractSectionableItem(header) { override fun getLayoutRes() = R.layout.item_homework @@ -20,10 +19,7 @@ class HomeworkItem( return ViewHolder(view, adapter) } - override fun bindViewHolder( - adapter: FlexibleAdapter>, holder: ViewHolder, - position: Int, payloads: MutableList? - ) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.apply { homeworkItemSubject.text = homework.subject homeworkItemTeacher.text = homework.teacher @@ -42,12 +38,12 @@ class HomeworkItem( } override fun hashCode(): Int { - return homework.hashCode() + var result = homework.hashCode() + result = 31 * result + homework.id.toInt() + return result } - class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectItem.kt index c206a499..71a7d681 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectItem.kt @@ -8,19 +8,21 @@ import eu.davidea.viewholders.FlexibleViewHolder import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import kotlinx.android.extensions.LayoutContainer -import kotlinx.android.synthetic.main.item_login_student_select.view.* +import kotlinx.android.synthetic.main.item_login_student_select.* class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem() { override fun getLayoutRes(): Int = R.layout.item_login_student_select - override fun createViewHolder(view: View?, adapter: FlexibleAdapter>?): ItemViewHolder { + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ItemViewHolder { return ItemViewHolder(view, adapter) } - override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ItemViewHolder?, - position: Int, payloads: MutableList?) { - holder?.bind(student) + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ItemViewHolder, position: Int, payloads: MutableList?) { + holder.run { + loginItemName.text = student.studentName + loginItemSchool.text = student.schoolName + } } override fun equals(other: Any?): Boolean { @@ -38,17 +40,8 @@ class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem?) - : FlexibleViewHolder(view, adapter), LayoutContainer { - - override val containerView: View? + class ItemViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { + override val containerView: View get() = itemView - - fun bind(item: Student) { - itemView.run { - loginItemName.text = item.studentName - loginItemSchool.text = item.schoolName - } - } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageItem.kt index ec725a38..26568e22 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageItem.kt @@ -20,12 +20,9 @@ class MessageItem(val message: Message, private val noSubjectString: String) : return ViewHolder(view, adapter) } - override fun getLayoutRes(): Int = R.layout.item_message + override fun getLayoutRes() = R.layout.item_message - override fun bindViewHolder( - adapter: FlexibleAdapter>, holder: ViewHolder, - position: Int, payloads: MutableList? - ) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.apply { val style = if (message.unread) BOLD else NORMAL @@ -58,9 +55,7 @@ class MessageItem(val message: Message, private val noSubjectString: String) : return message.hashCode() } - class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreItem.kt index e8571a8c..85b604e7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreItem.kt @@ -14,12 +14,12 @@ class MoreItem(val title: String, private val drawable: Drawable?) : AbstractFle override fun getLayoutRes() = R.layout.item_more - override fun createViewHolder(view: View?, adapter: FlexibleAdapter>?): ViewHolder { + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ViewHolder?, position: Int, payloads: MutableList?) { - holder?.apply { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { + holder.apply { moreItemTitle.text = title moreItemImage.setImageDrawable(drawable) } @@ -40,9 +40,8 @@ class MoreItem(val title: String, private val drawable: Drawable?) : AbstractFle return title.hashCode() } - class ViewHolder(view: View?, adapter: FlexibleAdapter<*>?) : FlexibleViewHolder(view, adapter), LayoutContainer { - - override val containerView: View? + class ViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { + override val containerView: View get() = contentView } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteItem.kt index 71562fc4..dabeef74 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteItem.kt @@ -15,16 +15,13 @@ import kotlinx.android.synthetic.main.item_note.* class NoteItem(val note: Note) : AbstractFlexibleItem() { + override fun getLayoutRes() = R.layout.item_note + override fun createViewHolder(view: View, adapter: FlexibleAdapter>): NoteItem.ViewHolder { return NoteItem.ViewHolder(view, adapter) } - override fun getLayoutRes(): Int = R.layout.item_note - - override fun bindViewHolder( - adapter: FlexibleAdapter>, - holder: NoteItem.ViewHolder, position: Int, payloads: MutableList? - ) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: NoteItem.ViewHolder, position: Int, payloads: MutableList?) { holder.apply { noteItemDate.apply { text = note.date.toFormattedString() @@ -57,7 +54,6 @@ class NoteItem(val note: Note) : AbstractFlexibleItem() { } class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { - override val containerView: View get() = contentView } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableItem.kt index f37c0f51..c721401f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableItem.kt @@ -15,18 +15,17 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.android.extensions.LayoutContainer import kotlinx.android.synthetic.main.item_timetable.* -class TimetableItem(val lesson: Timetable, private val roomText: String) - : AbstractFlexibleItem() { +class TimetableItem(val lesson: Timetable, private val roomText: String) : + AbstractFlexibleItem() { - override fun getLayoutRes(): Int = R.layout.item_timetable + override fun getLayoutRes() = R.layout.item_timetable override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } @SuppressLint("SetTextI18n") - override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, - position: Int, payloads: MutableList?) { + override fun bindViewHolder(adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.apply { timetableItemNumber.text = lesson.number.toString() timetableItemSubject.text = lesson.subject @@ -34,8 +33,8 @@ class TimetableItem(val lesson: Timetable, private val roomText: String) timetableItemTime.text = "${lesson.start.toFormattedString("HH:mm")} - ${lesson.end.toFormattedString("HH:mm")}" timetableItemAlert.visibility = if (lesson.changes || lesson.canceled) VISIBLE else GONE timetableItemSubject.paintFlags = - if (lesson.canceled) timetableItemSubject.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG - else timetableItemSubject.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv() + if (lesson.canceled) timetableItemSubject.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG + else timetableItemSubject.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv() } } @@ -50,12 +49,12 @@ class TimetableItem(val lesson: Timetable, private val roomText: String) } override fun hashCode(): Int { - return lesson.hashCode() + var result = lesson.hashCode() + result = 31 * result + lesson.id.toInt() + return result } - class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), - LayoutContainer { - + class ViewHolder(val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { override val containerView: View get() = contentView }