Skip to content

Commit

Permalink
Shared layout for bottom navigation items, for scroll and sticky
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Oct 26, 2023
1 parent eaec177 commit 5ed31e7
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ 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) :
data class Navigation(val questionnaireNavigationUIState: QuestionnaireNavigationUIState) :
QuestionnaireAdapterItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal class QuestionnaireEditAdapter(
}
is QuestionnaireAdapterItem.Navigation -> {
holder as NavigationViewHolder
holder.bind(item.questionnairePageNavigationState)
holder.bind(item.questionnaireNavigationUIState)
}
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ internal object DiffCallbacks {
}
is QuestionnaireAdapterItem.Navigation -> {
newItem is QuestionnaireAdapterItem.Navigation &&
oldItem.questionnairePageNavigationState == newItem.questionnairePageNavigationState
oldItem.questionnaireNavigationUIState == newItem.questionnaireNavigationUIState
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.annotation.VisibleForTesting
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.res.use
Expand Down Expand Up @@ -93,8 +92,9 @@ class QuestionnaireFragment : Fragment() {
view.findViewById<RecyclerView>(R.id.questionnaire_edit_recycler_view)
val questionnaireReviewRecyclerView =
view.findViewById<RecyclerView>(R.id.questionnaire_review_recycler_view)
val bottomNavGroup = view.findViewById<View>(R.id.bottom_nav_group)
view.findViewById<Button>(R.id.cancel_questionnaire).setOnClickListener {
val bottomNavGroup = view.findViewById<View>(R.id.questionnaire_bottom_view_container)

viewModel.setOnCancelButtonClickListener {
QuestionnaireCancelDialogFragment()
.show(requireActivity().supportFragmentManager, QuestionnaireCancelDialogFragment.TAG)
}
Expand All @@ -119,8 +119,6 @@ class QuestionnaireFragment : Fragment() {
QuestionnaireEditAdapter(questionnaireItemViewHolderFactoryMatchersProvider.get())
val questionnaireReviewAdapter = QuestionnaireReviewAdapter()

val cancelButton = requireView().findViewById<Button>(R.id.cancel_questionnaire)

val reviewModeEditButton =
view.findViewById<View>(R.id.review_mode_edit_button).apply {
setOnClickListener { viewModel.setReviewMode(false) }
Expand All @@ -138,20 +136,23 @@ class QuestionnaireFragment : Fragment() {
// Listen to updates from the view model.
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
viewModel.questionnaireStateFlow.collect { state ->
updateQuestionnairePageViewNavigation(bottomNavGroup, state.pageNavigationState)

when (val displayMode = state.displayMode) {
is DisplayMode.ReviewMode -> {
// Set items
questionnaireEditRecyclerView.visibility = View.GONE
val itemsData =
if (displayMode.showNavAsScroll) {
bottomNavGroup.visibility = View.GONE
state.items + QuestionnaireAdapterItem.Navigation(state.bottomNavigation)
} else {
updateNavigation(bottomNavGroup, state.bottomNavigation)
state.items
}
questionnaireReviewAdapter.submitList(
state.items,
itemsData,
)
questionnaireReviewRecyclerView.visibility = View.VISIBLE

// Set button visibility
cancelButton.visibility = if (displayMode.showCancelButton) View.VISIBLE else View.GONE

reviewModeEditButton.visibility =
if (displayMode.showEditButton) {
View.VISIBLE
Expand All @@ -165,12 +166,16 @@ class QuestionnaireFragment : Fragment() {
is DisplayMode.EditMode -> {
// Set items
questionnaireReviewRecyclerView.visibility = View.GONE
questionnaireEditAdapter.submitList(state.items)
val itemsData =
if (displayMode.showNavAsScroll) {
bottomNavGroup.visibility = View.GONE
state.items + QuestionnaireAdapterItem.Navigation(state.bottomNavigation)
} else {
updateNavigation(bottomNavGroup, state.bottomNavigation)
state.items
}
questionnaireEditAdapter.submitList(itemsData)
questionnaireEditRecyclerView.visibility = View.VISIBLE

// Set button visibility
cancelButton.visibility =
if (displayMode.pagination.showCancelButton) View.VISIBLE else View.GONE
reviewModeEditButton.visibility = View.GONE

// Set progress indicator
Expand Down Expand Up @@ -206,8 +211,8 @@ class QuestionnaireFragment : Fragment() {
questionnaireReviewRecyclerView.visibility = View.GONE
questionnaireEditRecyclerView.visibility = View.GONE
questionnaireProgressIndicator.visibility = View.GONE
cancelButton.visibility = View.GONE
reviewModeEditButton.visibility = View.GONE
bottomNavGroup.visibility = View.GONE
}
}
}
Expand Down Expand Up @@ -252,45 +257,49 @@ class QuestionnaireFragment : Fragment() {
}
}

internal fun updateQuestionnairePageViewNavigation(
internal fun updateNavigation(
navigationViewGroup: View,
questionnairePageNavigationState: QuestionnairePageNavigationState?,
questionnaireNavigationUIState: QuestionnaireNavigationUIState?,
) {
if (questionnairePageNavigationState == null) {
if (questionnaireNavigationUIState == null) {
navigationViewGroup.visibility = View.GONE
return
}

fun View.updateState(navigationViewState: QuestionnaireNavigationViewState) {
when (navigationViewState) {
QuestionnaireNavigationViewState.Disabled -> {
visibility = View.VISIBLE
isEnabled = false
}
is QuestionnaireNavigationViewState.Enabled -> {
visibility = View.VISIBLE
isEnabled = true
setOnClickListener { navigationViewState.onClickAction() }
}
QuestionnaireNavigationViewState.Hidden -> {
visibility = View.GONE
}
}
navigationViewGroup.findViewById<View>(R.id.cancel_questionnaire).apply {
updateState(questionnaireNavigationUIState.navCancel)
}

navigationViewGroup.findViewById<View>(R.id.pagination_previous_button).apply {
updateState(questionnairePageNavigationState.previousPageNavigationActionState)
updateState(questionnaireNavigationUIState.navPrevious)
}

navigationViewGroup.findViewById<View>(R.id.pagination_next_button).apply {
updateState(questionnairePageNavigationState.nextPageNavigationActionState)
updateState(questionnaireNavigationUIState.navNext)
}

navigationViewGroup.findViewById<View>(R.id.review_mode_button).apply {
updateState(questionnairePageNavigationState.reviewNavigationActionState)
updateState(questionnaireNavigationUIState.navReview)
}
navigationViewGroup.findViewById<View>(R.id.submit_questionnaire).apply {
updateState(questionnairePageNavigationState.submitNavigationActionState)
updateState(questionnaireNavigationUIState.navSubmit)
}
}

private fun View.updateState(navigationViewState: QuestionnaireNavigationViewUIState) {
when (navigationViewState) {
QuestionnaireNavigationViewUIState.Disabled -> {
visibility = View.VISIBLE
isEnabled = false
}
is QuestionnaireNavigationViewUIState.Enabled -> {
visibility = View.VISIBLE
isEnabled = true
setOnClickListener { navigationViewState.onClickAction() }
}
QuestionnaireNavigationViewUIState.Hidden -> {
visibility = View.GONE
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.fhir.datacapture

sealed class QuestionnaireNavigationViewUIState {
object Hidden : QuestionnaireNavigationViewUIState()

object Disabled : QuestionnaireNavigationViewUIState()

data class Enabled(val onClickAction: () -> Unit) : QuestionnaireNavigationViewUIState()
}

data class QuestionnaireNavigationUIState(
val navPrevious: QuestionnaireNavigationViewUIState = QuestionnaireNavigationViewUIState.Hidden,
val navNext: QuestionnaireNavigationViewUIState = QuestionnaireNavigationViewUIState.Hidden,
val navSubmit: QuestionnaireNavigationViewUIState = QuestionnaireNavigationViewUIState.Hidden,
val navCancel: QuestionnaireNavigationViewUIState = QuestionnaireNavigationViewUIState.Hidden,
val navReview: QuestionnaireNavigationViewUIState = QuestionnaireNavigationViewUIState.Hidden,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class QuestionnaireReviewAdapter :
}
is QuestionnaireAdapterItem.Navigation -> {
holder as NavigationViewHolder
holder.bind(item.questionnairePageNavigationState)
holder.bind(item.questionnaireNavigationUIState)
}
}
}
Expand Down
Loading

0 comments on commit 5ed31e7

Please sign in to comment.