Skip to content

Commit

Permalink
Configure bottom pagination navigation for questionnaire long scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Sep 15, 2023
1 parent d639b63 commit 71e1856
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
internal sealed interface QuestionnaireAdapterItem {
/** A row for a question in a Questionnaire RecyclerView. */
data class Question(val item: QuestionnaireViewItem) : QuestionnaireAdapterItem

data class Navigation(val questionnairePageNavigationState: QuestionnairePageNavigationState) :
QuestionnaireAdapterItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.google.android.fhir.datacapture

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.google.android.fhir.datacapture.contrib.views.PhoneNumberViewHolderFactory
import com.google.android.fhir.datacapture.extensions.itemControl
import com.google.android.fhir.datacapture.views.NavigationViewHolder
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.fhir.datacapture.views.factories.AttachmentViewHolderFactory
import com.google.android.fhir.datacapture.views.factories.AutoCompleteViewHolderFactory
Expand All @@ -46,16 +49,21 @@ internal class QuestionnaireEditAdapter(
private val questionnaireItemViewHolderMatchers:
List<QuestionnaireFragment.QuestionnaireItemViewHolderFactoryMatcher> =
emptyList(),
) : ListAdapter<QuestionnaireAdapterItem, QuestionnaireItemViewHolder>(DiffCallbacks.ITEMS) {
) : ListAdapter<QuestionnaireAdapterItem, RecyclerView.ViewHolder>(DiffCallbacks.ITEMS) {
/**
* @param viewType the integer value of the [QuestionnaireViewHolderType] used to render the
* [QuestionnaireViewItem].
* [QuestionnaireViewItem].
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionnaireItemViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val typedViewType = ViewType.parse(viewType)
val subtype = typedViewType.subtype
return when (typedViewType.type) {
ViewType.Type.QUESTION -> onCreateViewHolderQuestion(parent = parent, subtype = subtype)
ViewType.Type.NAVIGATION ->
NavigationViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.pagination_navigation_view, parent, false),
)
}
}

Expand All @@ -69,9 +77,11 @@ internal class QuestionnaireEditAdapter(
}

// Map custom widget viewTypes to their corresponding widget factories
if (subtype >= numOfCanonicalWidgets)
if (subtype >= numOfCanonicalWidgets) {
return questionnaireItemViewHolderMatchers[subtype - numOfCanonicalWidgets]
.factory.create(parent)
.factory
.create(parent)
}

val viewHolderFactory =
when (QuestionnaireViewHolderType.fromInt(subtype)) {
Expand All @@ -97,11 +107,16 @@ internal class QuestionnaireEditAdapter(
return viewHolderFactory.create(parent)
}

override fun onBindViewHolder(holder: QuestionnaireItemViewHolder, position: Int) {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (val item = getItem(position)) {
is QuestionnaireAdapterItem.Question -> {
holder as QuestionnaireItemViewHolder
holder.bind(item.item)
}
is QuestionnaireAdapterItem.Navigation -> {
holder as NavigationViewHolder
holder.bind(item.questionnairePageNavigationState)
}
}
}

Expand All @@ -118,6 +133,10 @@ internal class QuestionnaireEditAdapter(
type = ViewType.Type.QUESTION
subtype = getItemViewTypeForQuestion(item.item)
}
is QuestionnaireAdapterItem.Navigation -> {
type = ViewType.Type.NAVIGATION
subtype = 0xFFFFFF
}
}
return ViewType.from(type = type, subtype = subtype).viewType
}
Expand All @@ -136,6 +155,7 @@ internal class QuestionnaireEditAdapter(
internal value class ViewType(val viewType: Int) {
val subtype: Int
get() = viewType and 0xFFFFFF

val type: Type
get() = Type.values()[viewType shr 24]

Expand All @@ -147,6 +167,7 @@ internal class QuestionnaireEditAdapter(

enum class Type {
QUESTION,
NAVIGATION,
}
}

Expand Down Expand Up @@ -258,6 +279,7 @@ internal object DiffCallbacks {
newItem is QuestionnaireAdapterItem.Question &&
QUESTIONS.areItemsTheSame(oldItem, newItem)
}
is QuestionnaireAdapterItem.Navigation -> newItem is QuestionnaireAdapterItem.Navigation
}

override fun areContentsTheSame(
Expand All @@ -269,6 +291,10 @@ internal object DiffCallbacks {
newItem is QuestionnaireAdapterItem.Question &&
QUESTIONS.areContentsTheSame(oldItem, newItem)
}
is QuestionnaireAdapterItem.Navigation -> {
newItem is QuestionnaireAdapterItem.Navigation &&
oldItem.questionnairePageNavigationState == newItem.questionnairePageNavigationState
}
}
}

Expand Down
Loading

0 comments on commit 71e1856

Please sign in to comment.