From 0a18fefb1f149604f5e1ebada57d4854c44be07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 8 Apr 2020 18:17:20 +0200 Subject: [PATCH] Use recycler view in homework details dialog (#761) --- .../details/HomeworkDetailsAdapter.kt | 84 +++++++++ .../homework/details/HomeworkDetailsDialog.kt | 27 ++- app/src/main/res/layout/dialog_homework.xml | 168 ++++-------------- .../item_homework_dialog_attachment.xml | 21 +++ ...tem_homework_dialog_attachments_header.xml | 14 ++ .../layout/item_homework_dialog_details.xml | 96 ++++++++++ 6 files changed, 260 insertions(+), 150 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsAdapter.kt create mode 100644 app/src/main/res/layout/item_homework_dialog_attachment.xml create mode 100644 app/src/main/res/layout/item_homework_dialog_attachments_header.xml create mode 100644 app/src/main/res/layout/item_homework_dialog_details.xml diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsAdapter.kt new file mode 100644 index 00000000..3706b56e --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsAdapter.kt @@ -0,0 +1,84 @@ +package io.github.wulkanowy.ui.modules.homework.details + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import io.github.wulkanowy.R +import io.github.wulkanowy.data.db.entities.Homework +import io.github.wulkanowy.utils.toFormattedString +import kotlinx.android.synthetic.main.item_homework_dialog_attachment.view.* +import kotlinx.android.synthetic.main.item_homework_dialog_details.view.* +import javax.inject.Inject + +class HomeworkDetailsAdapter @Inject constructor() : + RecyclerView.Adapter() { + + private enum class ViewType(val id: Int) { + DETAILS(1), + ATTACHMENTS_HEADER(2), + ATTACHMENT(3) + } + + private var attachments = emptyList>() + + var homework: Homework? = null + set(value) { + field = value + attachments = value?.attachments.orEmpty() + } + + var onAttachmentClickListener: (url: String) -> Unit = {} + + override fun getItemCount() = 1 + if (attachments.isNotEmpty()) attachments.size + 1 else 0 + + override fun getItemViewType(position: Int) = when (position) { + 0 -> ViewType.DETAILS.id + 1 -> ViewType.ATTACHMENTS_HEADER.id + else -> ViewType.ATTACHMENT.id + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + val inflater = LayoutInflater.from(parent.context) + + return when (viewType) { + ViewType.ATTACHMENTS_HEADER.id -> AttachmentsHeaderViewHolder(inflater.inflate(R.layout.item_homework_dialog_attachments_header, parent, false)) + ViewType.ATTACHMENT.id -> AttachmentViewHolder(inflater.inflate(R.layout.item_homework_dialog_attachment, parent, false)) + else -> DetailsViewHolder(inflater.inflate(R.layout.item_homework_dialog_details, parent, false)) + } + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + when (holder) { + is DetailsViewHolder -> bindDetailsViewHolder(holder) + is AttachmentViewHolder -> bindAttachmentViewHolder(holder, position - 2) + } + } + + private fun bindDetailsViewHolder(holder: DetailsViewHolder) { + with(holder.view) { + homeworkDialogDate.text = homework?.date?.toFormattedString() + homeworkDialogEntryDate.text = homework?.entryDate?.toFormattedString() + homeworkDialogSubject.text = homework?.subject + homeworkDialogTeacher.text = homework?.teacher + homeworkDialogContent.text = homework?.content + } + } + + private fun bindAttachmentViewHolder(holder: AttachmentViewHolder, position: Int) { + val item = attachments[position] + + with(holder.view) { + homeworkDialogAttachment.text = item.second + setOnClickListener { + onAttachmentClickListener(item.first) + } + } + } + + class DetailsViewHolder(val view: View) : RecyclerView.ViewHolder(view) + + class AttachmentsHeaderViewHolder(val view: View) : RecyclerView.ViewHolder(view) + + class AttachmentViewHolder(val view: View) : RecyclerView.ViewHolder(view) +} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt index 58622848..54cb5c68 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt @@ -2,16 +2,15 @@ package io.github.wulkanowy.ui.modules.homework.details import android.annotation.SuppressLint import android.os.Bundle -import android.text.method.LinkMovementMethod import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.text.HtmlCompat +import androidx.recyclerview.widget.LinearLayoutManager import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.ui.base.BaseDialogFragment import io.github.wulkanowy.ui.modules.homework.HomeworkFragment -import io.github.wulkanowy.utils.toFormattedString +import io.github.wulkanowy.utils.openInternetBrowser import kotlinx.android.synthetic.main.dialog_homework.* import javax.inject.Inject @@ -20,6 +19,9 @@ class HomeworkDetailsDialog : BaseDialogFragment(), HomeworkDetailsView { @Inject lateinit var presenter: HomeworkDetailsPresenter + @Inject + lateinit var detailsAdapter: HomeworkDetailsAdapter + private lateinit var homework: Homework companion object { @@ -51,20 +53,17 @@ class HomeworkDetailsDialog : BaseDialogFragment(), HomeworkDetailsView { @SuppressLint("SetTextI18n") override fun initView() { - homeworkDialogDate.text = homework.date.toFormattedString() - homeworkDialogEntryDate.text = homework.entryDate.toFormattedString() - homeworkDialogSubject.text = homework.subject - homeworkDialogTeacher.text = homework.teacher - homeworkDialogContent.movementMethod = LinkMovementMethod.getInstance() - homeworkDialogContent.text = homework.content - homeworkDialogAttachments.movementMethod = LinkMovementMethod.getInstance() - homeworkDialogAttachments.text = HtmlCompat.fromHtml(homework.attachments.joinToString("
") { - "${it.second}" - }, HtmlCompat.FROM_HTML_MODE_COMPACT).ifBlank { getString(R.string.all_no_data) } - homeworkDialogRead.text = view?.context?.getString(if (homework.isDone) R.string.homework_mark_as_undone else R.string.homework_mark_as_done) homeworkDialogRead.setOnClickListener { presenter.toggleDone(homework) } homeworkDialogClose.setOnClickListener { dismiss() } + + with(homeworkDialogRecycler) { + layoutManager = LinearLayoutManager(context) + adapter = detailsAdapter.apply { + onAttachmentClickListener = { context.openInternetBrowser(it, ::showMessage) } + homework = this@HomeworkDetailsDialog.homework + } + } } override fun updateMarkAsDoneLabel(isDone: Boolean) { diff --git a/app/src/main/res/layout/dialog_homework.xml b/app/src/main/res/layout/dialog_homework.xml index f6d00049..a2c2ec47 100644 --- a/app/src/main/res/layout/dialog_homework.xml +++ b/app/src/main/res/layout/dialog_homework.xml @@ -1,143 +1,39 @@ - - - + android:layout_above="@+id/homeworkDialogClose" + android:layout_alignParentTop="true" + tools:itemCount="1" + tools:listitem="@layout/item_homework_dialog_details" /> - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/app/src/main/res/layout/item_homework_dialog_attachment.xml b/app/src/main/res/layout/item_homework_dialog_attachment.xml new file mode 100644 index 00000000..ec0b37f2 --- /dev/null +++ b/app/src/main/res/layout/item_homework_dialog_attachment.xml @@ -0,0 +1,21 @@ + + + + diff --git a/app/src/main/res/layout/item_homework_dialog_attachments_header.xml b/app/src/main/res/layout/item_homework_dialog_attachments_header.xml new file mode 100644 index 00000000..383cbeaf --- /dev/null +++ b/app/src/main/res/layout/item_homework_dialog_attachments_header.xml @@ -0,0 +1,14 @@ + + + + diff --git a/app/src/main/res/layout/item_homework_dialog_details.xml b/app/src/main/res/layout/item_homework_dialog_details.xml new file mode 100644 index 00000000..3ef389d0 --- /dev/null +++ b/app/src/main/res/layout/item_homework_dialog_details.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + +