-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slider Question functionality added #287
Changes from 6 commits
dd698ed
18fa9dd
35c7a44
b133777
c090554
b4f516a
6b7de2d
e720019
904d4d4
fc140b1
78fad29
1f39818
5944688
d44fe9d
1b02ebf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,119 @@ | ||||||
/* | ||||||
* Copyright 2020 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.views | ||||||
|
||||||
import android.widget.FrameLayout | ||||||
import android.widget.TextView | ||||||
import androidx.appcompat.view.ContextThemeWrapper | ||||||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||||||
import androidx.test.platform.app.InstrumentationRegistry | ||||||
import com.google.android.fhir.datacapture.R | ||||||
import com.google.android.material.slider.Slider | ||||||
import com.google.common.truth.Truth.assertThat | ||||||
import com.google.fhir.r4.core.Integer | ||||||
import com.google.fhir.r4.core.Questionnaire | ||||||
import com.google.fhir.r4.core.QuestionnaireResponse | ||||||
import org.junit.Before | ||||||
import org.junit.Test | ||||||
import org.junit.runner.RunWith | ||||||
|
||||||
@RunWith(AndroidJUnit4::class) | ||||||
class QuestionnaireItemSliderViewHolderFactoryInstrumentedTest { | ||||||
private lateinit var context: ContextThemeWrapper | ||||||
private lateinit var parent: FrameLayout | ||||||
private lateinit var viewHolder: QuestionnaireItemViewHolder | ||||||
|
||||||
@Before | ||||||
fun setUp() { | ||||||
context = ContextThemeWrapper( | ||||||
InstrumentationRegistry.getInstrumentation().targetContext, | ||||||
R.style.Theme_MaterialComponents | ||||||
) | ||||||
parent = FrameLayout(context) | ||||||
viewHolder = QuestionnaireItemSliderViewHolderFactory.create(parent) | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun shouldSetHeaderTextViewText() { | ||||||
viewHolder.bind( | ||||||
QuestionnaireItemViewItem( | ||||||
Questionnaire.Item.newBuilder().apply { | ||||||
text = com.google.fhir.r4.core.String.newBuilder().setValue("Question?").build() | ||||||
}.build(), | ||||||
QuestionnaireResponse.Item.newBuilder() | ||||||
) {} | ||||||
) | ||||||
|
||||||
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.slider_header).text) | ||||||
.isEqualTo("Question?") | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun singleAnswerOrNull_noAnswer_shouldReturnNull() { | ||||||
val questionnaireItemViewItem = QuestionnaireItemViewItem( | ||||||
Questionnaire.Item.getDefaultInstance(), | ||||||
QuestionnaireResponse.Item.newBuilder() | ||||||
) {} | ||||||
|
||||||
assertThat(questionnaireItemViewItem.singleAnswerOrNull).isNull() | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun singleAnswerOrNull_singleAnswer_shouldReturnSingleAnswer() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
viewHolder.bind( | ||||||
QuestionnaireItemViewItem( | ||||||
Questionnaire.Item.getDefaultInstance(), | ||||||
QuestionnaireResponse.Item.newBuilder().apply { | ||||||
addAnswer( | ||||||
QuestionnaireResponse.Item.Answer.newBuilder().apply { | ||||||
value = QuestionnaireResponse.Item.Answer.ValueX.newBuilder() | ||||||
.setInteger(Integer.newBuilder().setValue(10).build()) | ||||||
.build() | ||||||
} | ||||||
) | ||||||
} | ||||||
) {} | ||||||
) | ||||||
|
||||||
assertThat(viewHolder.itemView.findViewById<Slider>(R.id.slider).value).isEqualTo(10) | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun singleAnswerOrNull_multipleAnswers_shouldReturnNull() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
val questionnaireItemViewItem = QuestionnaireItemViewItem( | ||||||
Questionnaire.Item.getDefaultInstance(), | ||||||
QuestionnaireResponse.Item.newBuilder().apply { | ||||||
addAnswer( | ||||||
QuestionnaireResponse.Item.Answer.newBuilder().apply { | ||||||
value = QuestionnaireResponse.Item.Answer.ValueX.newBuilder() | ||||||
.setInteger(Integer.newBuilder().setValue(10).build()) | ||||||
.build() | ||||||
} | ||||||
) | ||||||
addAnswer( | ||||||
QuestionnaireResponse.Item.Answer.newBuilder().apply { | ||||||
value = QuestionnaireResponse.Item.Answer.ValueX.newBuilder() | ||||||
.setInteger(Integer.newBuilder().setValue(10).build()) | ||||||
.build() | ||||||
} | ||||||
) | ||||||
} | ||||||
) {} | ||||||
|
||||||
assertThat(questionnaireItemViewItem.singleAnswerOrNull).isNull() | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2020 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.views | ||
|
||
import android.view.View | ||
import android.widget.TextView | ||
import com.google.android.fhir.datacapture.R | ||
import com.google.android.material.slider.Slider | ||
import com.google.fhir.r4.core.Integer | ||
import com.google.fhir.r4.core.QuestionnaireResponse | ||
|
||
internal object QuestionnaireItemSliderViewHolderFactory : QuestionnaireItemViewHolderFactory( | ||
R.layout.questionnaire_item_slider | ||
) { | ||
override fun getQuestionnaireItemViewHolderDelegate(): QuestionnaireItemViewHolderDelegate = | ||
object : QuestionnaireItemViewHolderDelegate { | ||
private lateinit var sliderHeader: TextView | ||
private lateinit var slider: Slider | ||
private lateinit var questionnaireItemViewItem: QuestionnaireItemViewItem | ||
|
||
override fun init(itemView: View) { | ||
sliderHeader = itemView.findViewById(R.id.slider_header) | ||
slider = itemView.findViewById(R.id.slider) | ||
} | ||
|
||
override fun bind(questionnaireItemViewItem: QuestionnaireItemViewItem) { | ||
this.questionnaireItemViewItem = questionnaireItemViewItem | ||
val questionnaireItem = questionnaireItemViewItem.questionnaireItem | ||
val answer = questionnaireItemViewItem.singleAnswerOrNull | ||
sliderHeader.text = questionnaireItem.text.value | ||
slider.valueFrom = 0.0F | ||
slider.valueTo = 100.0F | ||
slider.stepSize = 10.0F | ||
val sliderValue = answer?.value?.integer?.value?.toString() ?: "0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cant be hardcoded.. imagine if the slider is from 5 to 10 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now, this is hard coded, will create a new issue for this as discussed with @jingtang10 to read values from extensions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes i think we'll have to do this in a follow up and especially considering the ongoing work in #224 |
||
slider.value = sliderValue.toFloat() | ||
|
||
slider.addOnChangeListener { _, newValue, _ -> | ||
// Responds to when slider's value is changed | ||
QuestionnaireResponse.Item.Answer.newBuilder() | ||
.apply { | ||
value = QuestionnaireResponse.Item.Answer.ValueX.newBuilder() | ||
.setInteger(Integer.newBuilder().setValue(newValue.toInt()) | ||
.build()).build() | ||
|
||
questionnaireItemViewItem.singleAnswerOrNull = this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, not understanding this. Can you explain this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of
You can refer to the other factories to find this pattern There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joiskash but this is already doing the same way as QuestionnaireResponse.Item.Answer.newBuilder().apply in the code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is the same thing. Just suggesting to maintain uniformity in the code. Also why is the callback called inside apply? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not set also keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
questionnaireItemViewItem.questionnaireResponseItemChangedCallback() | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginHorizontal="@dimen/item_margin_horizontal" | ||
android:layout_marginVertical="@dimen/item_margin_vertical" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/slider_header" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
|
||
<com.google.android.material.slider.Slider | ||
android:id="@+id/slider" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
</LinearLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to write tests verifying that the slider is working as it is supposed to . Check the
answerFalse_shouldSetCheckBoxUnchecked
unit test in QuestionnaireItemCheckBoxViewHolderFactoryInstrumentedTest to get a better understanding. The assert statement should use the value returned from the viewHolder.findViewById(R.id.slider)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please comment again once done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @jingtang10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see https://github.com/google/android-fhir/blob/master/datacapture/src/androidTest/java/com/google/android/fhir/datacapture/views/QuestionnaireItemEditTextSingleLineViewHolderFactoryInstrumentedTest.kt. So there're two types of test cases, you have the ones that update the UI, and you have the ones that change the UI and update the questionnaire respoonse.
so you'll need to add at least another test case that tests the answer is set after you modify the slider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done