Skip to content

Commit 8ea70e5

Browse files
Refactor MultipleChoiceTaskFragment to use a dedicated Compose-based screen
- Extract the multiple-choice task UI into a new `MultipleChoiceTaskScreen` Composable. - Migrate `MultipleChoiceTaskFragment` to use `createComposeView`, integrating `MultipleChoiceTaskScreen` for rendering. - Update the fragment to delegate action handling and footer position updates to the new screen component. - Move the task body rendering logic from `MultipleChoiceTaskFragment` to `MultipleChoiceTaskContent`. - Replace `MultipleChoiceTaskFragmentTest` with `MultipleChoiceTaskScreenTest`, migrating tests to use `composeTestRule`.
1 parent 506ca77 commit 8ea70e5

File tree

4 files changed

+343
-350
lines changed

4 files changed

+343
-350
lines changed

app/src/main/java/org/groundplatform/android/ui/datacollection/tasks/multiplechoice/MultipleChoiceTaskFragment.kt

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,12 @@
1515
*/
1616
package org.groundplatform.android.ui.datacollection.tasks.multiplechoice
1717

18-
import androidx.compose.foundation.layout.Box
19-
import androidx.compose.foundation.layout.padding
20-
import androidx.compose.foundation.lazy.LazyColumn
21-
import androidx.compose.foundation.lazy.items
22-
import androidx.compose.foundation.lazy.rememberLazyListState
23-
import androidx.compose.material3.MaterialTheme
24-
import androidx.compose.runtime.Composable
25-
import androidx.compose.runtime.getValue
26-
import androidx.compose.ui.Modifier
27-
import androidx.compose.ui.platform.testTag
28-
import androidx.lifecycle.compose.collectAsStateWithLifecycle
18+
import android.os.Bundle
19+
import android.view.LayoutInflater
20+
import android.view.ViewGroup
2921
import dagger.hilt.android.AndroidEntryPoint
3022
import org.groundplatform.android.ui.datacollection.tasks.AbstractTaskFragment
31-
import org.groundplatform.ui.theme.sizes
23+
import org.groundplatform.android.util.createComposeView
3224

3325
const val MULTIPLE_CHOICE_LIST_TEST_TAG = "multiple choice items test tag"
3426

@@ -39,22 +31,15 @@ const val MULTIPLE_CHOICE_LIST_TEST_TAG = "multiple choice items test tag"
3931
@AndroidEntryPoint
4032
class MultipleChoiceTaskFragment : AbstractTaskFragment<MultipleChoiceTaskViewModel>() {
4133

42-
@Composable
43-
override fun TaskBody() {
44-
val list by viewModel.items.collectAsStateWithLifecycle()
45-
val scrollState = rememberLazyListState()
46-
47-
Box(modifier = Modifier.padding(horizontal = MaterialTheme.sizes.taskViewPadding)) {
48-
LazyColumn(Modifier.testTag(MULTIPLE_CHOICE_LIST_TEST_TAG), state = scrollState) {
49-
items(list, key = { it.option.id }) { item ->
50-
MultipleChoiceItemView(
51-
item = item,
52-
isLastIndex = list.indexOf(item) == list.lastIndex,
53-
toggleItem = { viewModel.onItemToggled(it) },
54-
otherValueChanged = { viewModel.onOtherTextChanged(it) },
55-
)
56-
}
57-
}
58-
}
34+
override fun onCreateView(
35+
inflater: LayoutInflater,
36+
container: ViewGroup?,
37+
savedInstanceState: Bundle?,
38+
) = createComposeView {
39+
MultipleChoiceTaskScreen(
40+
viewModel = viewModel,
41+
onFooterPositionUpdated = { saveFooterPosition(it) },
42+
onAction = { handleTaskScreenAction(it) },
43+
)
5944
}
6045
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.groundplatform.android.ui.datacollection.tasks.multiplechoice
17+
18+
import androidx.compose.foundation.layout.Box
19+
import androidx.compose.foundation.layout.padding
20+
import androidx.compose.foundation.lazy.LazyColumn
21+
import androidx.compose.foundation.lazy.items
22+
import androidx.compose.foundation.lazy.rememberLazyListState
23+
import androidx.compose.material3.MaterialTheme
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.runtime.getValue
26+
import androidx.compose.ui.Modifier
27+
import androidx.compose.ui.platform.testTag
28+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
29+
import org.groundplatform.android.R
30+
import org.groundplatform.android.ui.datacollection.components.TaskHeader
31+
import org.groundplatform.android.ui.datacollection.tasks.TaskScreen
32+
import org.groundplatform.android.ui.datacollection.tasks.TaskScreenAction
33+
import org.groundplatform.ui.theme.sizes
34+
35+
@Composable
36+
fun MultipleChoiceTaskScreen(
37+
viewModel: MultipleChoiceTaskViewModel,
38+
onFooterPositionUpdated: (Float) -> Unit,
39+
onAction: (TaskScreenAction) -> Unit,
40+
) {
41+
val taskActionButtonsStates by viewModel.taskActionButtonStates.collectAsStateWithLifecycle()
42+
val list by viewModel.items.collectAsStateWithLifecycle()
43+
44+
TaskScreen(
45+
taskHeader =
46+
TaskHeader(label = viewModel.task.label, iconResId = R.drawable.ic_question_answer),
47+
taskActionButtonsStates = taskActionButtonsStates,
48+
onFooterPositionUpdated = onFooterPositionUpdated,
49+
onAction = onAction,
50+
taskBody = {
51+
MultipleChoiceTaskContent(
52+
list = list,
53+
onItemToggled = { viewModel.onItemToggled(it) },
54+
onOtherValueChanged = { viewModel.onOtherTextChanged(it) },
55+
)
56+
},
57+
)
58+
}
59+
60+
@Composable
61+
internal fun MultipleChoiceTaskContent(
62+
list: List<MultipleChoiceItem>,
63+
onItemToggled: (MultipleChoiceItem) -> Unit,
64+
onOtherValueChanged: (String) -> Unit,
65+
) {
66+
val scrollState = rememberLazyListState()
67+
68+
Box(modifier = Modifier.padding(horizontal = MaterialTheme.sizes.taskViewPadding)) {
69+
LazyColumn(Modifier.testTag(MULTIPLE_CHOICE_LIST_TEST_TAG), state = scrollState) {
70+
items(list, key = { it.option.id }) { item ->
71+
MultipleChoiceItemView(
72+
item = item,
73+
isLastIndex = list.indexOf(item) == list.lastIndex,
74+
toggleItem = onItemToggled,
75+
otherValueChanged = onOtherValueChanged,
76+
)
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)