Skip to content

Commit

Permalink
Catalog, show questionnaire response when submit questionnaire button…
Browse files Browse the repository at this point in the history
… get clicked. (google#1097)

* Sdc gallery app landing page
1. Show Bottom navigation bar with Components and Layouts menus.
2. Implements components screen.
3. Implements layouts screen.
4. Handles bottom navigation clicks.
5. Update actionbar.

* Rename file name.

* Update components and layouts vector icons.

* Sdc gallery app single choice workflow.

* Update it as boolean choice.

* Address review comments.

* Boolean choice component icon.

* Spotless apply

* Build failures

* Delete gallery drawables.

* fix package name in nav graph

* Spotless apply.

* Delete gallery color resource

* theme attributes for color value.

* bottom view margin

* default layout workflow.

* apply layout theme

* Add filename field to the layout enum.

* Update catalog title in toolbar when user navigate back from defalult layout screen.

* Display questionnaire response.

* Review comment.

* Remove commented code

* Add submit menu to overflow menu.

* Address review comments.

* Address review comment.
  • Loading branch information
santosh-pingle committed Feb 24, 2022
1 parent b02f8d8 commit e6bb4c4
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ package com.google.android.fhir.catalog
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.add
import androidx.fragment.app.commit
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.fhir.catalog.QuestionnaireContainerFragment.Companion.QUESTIONNAIRE_FRAGMENT_TAG
import com.google.android.fhir.datacapture.QuestionnaireFragment
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -66,10 +71,20 @@ class DemoQuestionnaireFragment : Fragment() {
NavHostFragment.findNavController(this).navigateUp()
true
}
// TODO https://github.com/google/android-fhir/issues/1088
R.id.submit_questionnaire -> {
onSubmitQuestionnaireClick()
true
}
else -> super.onOptionsItemSelected(item)
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu, menu)
}

private fun setUpActionBar() {
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
Expand All @@ -87,13 +102,21 @@ class DemoQuestionnaireFragment : Fragment() {
}

private fun addQuestionnaireFragment() {
val fragment = QuestionnaireFragment()
viewLifecycleOwner.lifecycleScope.launch {
fragment.arguments =
bundleOf(
QuestionnaireFragment.EXTRA_QUESTIONNAIRE_JSON_STRING to viewModel.getQuestionnaireJson()
)
childFragmentManager.commit { add(R.id.container, fragment, QUESTIONNAIRE_FRAGMENT_TAG) }
if (childFragmentManager.findFragmentByTag(QUESTIONNAIRE_FRAGMENT_TAG) == null) {
childFragmentManager.commit {
setReorderingAllowed(true)
add<QuestionnaireFragment>(
R.id.container,
tag = QUESTIONNAIRE_FRAGMENT_TAG,
args =
bundleOf(
QuestionnaireFragment.EXTRA_QUESTIONNAIRE_JSON_STRING to
viewModel.getQuestionnaireJson()
)
)
}
}
}
}

Expand All @@ -104,6 +127,26 @@ class DemoQuestionnaireFragment : Fragment() {
}
}

private fun onSubmitQuestionnaireClick() {
// TODO https://github.com/google/android-fhir/issues/1088
val questionnaireFragment =
childFragmentManager.findFragmentByTag(
QuestionnaireContainerFragment.QUESTIONNAIRE_FRAGMENT_TAG
) as
QuestionnaireFragment
launchQuestionnaireResponseFragment(
viewModel.getQuestionnaireResponseJson(questionnaireFragment.getQuestionnaireResponse())
)
}

private fun launchQuestionnaireResponseFragment(response: String) {
findNavController()
.navigate(
DemoQuestionnaireFragmentDirections
.actionGalleryQuestionnaireFragmentToQuestionnaireResponseFragment(response)
)
}

companion object {
const val QUESTIONNAIRE_FRAGMENT_TAG = "questionnaire-fragment-tag"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import kotlinx.coroutines.withContext
import org.hl7.fhir.r4.model.QuestionnaireResponse

class DemoQuestionnaireViewModel(application: Application, private val state: SavedStateHandle) :
AndroidViewModel(application) {
private val backgroundContext = viewModelScope.coroutineContext
private var questionnaireJson: String? = null

fun getQuestionnaireResponseJson(response: QuestionnaireResponse) =
FhirContext.forCached(FhirVersionEnum.R4).newJsonParser().encodeResourceToString(response)

suspend fun getQuestionnaireJson(): String {
return withContext(backgroundContext) {
if (questionnaireJson == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2021 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:https://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.catalog

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.navArgs
import org.json.JSONObject

class QuestionnaireResponseFragment : Fragment() {
private val args: QuestionnaireResponseFragmentArgs by navArgs()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_questionnaire_response, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setCloseOnClickListener()
view.findViewById<TextView>(R.id.questionnaire_response_tv).text =
JSONObject(args.questionnaireResponse).toString(2)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
NavHostFragment.findNavController(this).navigateUp()
true
}
else -> super.onOptionsItemSelected(item)
}
}

override fun onResume() {
super.onResume()
setUpActionBar()
}

private fun setUpActionBar() {
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
}
(requireActivity() as MainActivity).setActionBar(
getString(R.string.questionnaire_response_title),
Gravity.START
)
setHasOptionsMenu(true)
}

private fun setCloseOnClickListener() {
view?.findViewById<Button>(R.id.close_button)?.setOnClickListener {
NavHostFragment.findNavController(this).navigateUp()
}
}
}
64 changes: 64 additions & 0 deletions catalog/src/main/res/layout/fragment_questionnaire_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8" ?>
<ScrollView
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QuestionnaireResponseFragment"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:paddingBottom="38dp"
>
<TextView
style="@style/TextAppearance.MyQuestionnaire.Header.Text"
android:id="@+id/title_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/questionnaire_submitted"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
style="@style/TextAppearance.MyQuestionnaire.Body1"
android:id="@+id/subTitle_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/questionnaire_response_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_textView"
/>
<View
android:id="@+id/line_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="@color/line_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/subTitle_tv"
/>
<TextView
style="@style/TextAppearance.MyQuestionnaire.Body1"
android:id="@+id/questionnaire_response_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/line_view"
/>
<Button
android:id="@+id/close_button"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:text="@string/close"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/questionnaire_response_tv"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
12 changes: 12 additions & 0 deletions catalog/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<menu
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/submit_questionnaire"
android:title="@string/submit"
app:showAsAction="never"
>
</item>
</menu>
12 changes: 12 additions & 0 deletions catalog/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,17 @@
>
<argument android:name="questionnaireTitleKey" app:argType="string" />
<argument android:name="questionnaireFilePathKey" app:argType="string" />
<action
android:id="@+id/action_galleryQuestionnaireFragment_to_questionnaireResponseFragment"
app:destination="@id/questionnaireResponseFragment"
/>
</fragment>
<fragment
android:id="@+id/questionnaireResponseFragment"
android:name="com.google.android.fhir.catalog.QuestionnaireResponseFragment"
android:label="fragment_questionnaire_response"
tools:layout="@layout/fragment_questionnaire_response"
>
<argument android:name="questionnaire_response" app:argType="string" />
</fragment>
</navigation>
1 change: 1 addition & 0 deletions catalog/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
<color name="red_600">#D93025</color>
<color name="red_300">#F28B82</color>
<color name="toolbar_text_color">#5F6368</color>
<color name="line_24">#DADCE0</color>
</resources>
7 changes: 7 additions & 0 deletions catalog/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@
<string name="layout_name_review">Review</string>
<string name="layout_name_read_only">Read only</string>
<string name="toolbar_text">Structured data capture \n Catalog</string>
<string
name="questionnaire_response_title"
>Default sample questionnaire</string>
<string name="close">Close</string>
<string name="questionnaire_submitted">Questionnaire submitted</string>
<string name="questionnaire_response_subtitle">Below is the JSON code</string>
<string name="submit">Submit</string>
</resources>
8 changes: 8 additions & 0 deletions catalog/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@
<style name="ThemeOverlay.App.Layout.Header.Text" parent="">
<item name="colorOnSurface">@color/grey_900</item>
</style>
<style
name="TextAppearance.MyQuestionnaire.Body1"
parent="TextAppearance.MaterialComponents.Body1"
>
<item name="android:textColor">?attr/colorOnSurface</item>
<item name="android:lineSpacingExtra">5sp</item>
<item name="android:letterSpacing">.01</item>
</style>
</resources>

0 comments on commit e6bb4c4

Please sign in to comment.