Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@
*/
package org.groundplatform.android.ui.datacollection.tasks.multiplechoice

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import dagger.hilt.android.AndroidEntryPoint
import org.groundplatform.android.ui.datacollection.tasks.AbstractTaskFragment
import org.groundplatform.ui.theme.sizes
import org.groundplatform.android.util.createComposeView

const val MULTIPLE_CHOICE_LIST_TEST_TAG = "multiple choice items test tag"

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

@Composable
override fun TaskBody() {
val list by viewModel.items.collectAsStateWithLifecycle()
val scrollState = rememberLazyListState()

Box(modifier = Modifier.padding(horizontal = MaterialTheme.sizes.taskViewPadding)) {
LazyColumn(Modifier.testTag(MULTIPLE_CHOICE_LIST_TEST_TAG), state = scrollState) {
items(list, key = { it.option.id }) { item ->
MultipleChoiceItemView(
item = item,
isLastIndex = list.indexOf(item) == list.lastIndex,
toggleItem = { viewModel.onItemToggled(it) },
otherValueChanged = { viewModel.onOtherTextChanged(it) },
)
}
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
) = createComposeView {
MultipleChoiceTaskScreen(
viewModel = viewModel,
onFooterPositionUpdated = { saveFooterPosition(it) },
onAction = { handleTaskScreenAction(it) },
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2026 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
*
* 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 org.groundplatform.android.ui.datacollection.tasks.multiplechoice

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import org.groundplatform.android.R
import org.groundplatform.android.ui.common.ExcludeFromJacocoGeneratedReport
import org.groundplatform.android.ui.datacollection.components.TaskHeader
import org.groundplatform.android.ui.datacollection.tasks.TaskScreen
import org.groundplatform.android.ui.datacollection.tasks.TaskScreenAction
import org.groundplatform.domain.model.task.MultipleChoice.Cardinality
import org.groundplatform.domain.model.task.Option
import org.groundplatform.ui.theme.AppTheme
import org.groundplatform.ui.theme.sizes

@Composable
fun MultipleChoiceTaskScreen(
viewModel: MultipleChoiceTaskViewModel,
onFooterPositionUpdated: (Float) -> Unit,
onAction: (TaskScreenAction) -> Unit,
) {
val taskActionButtonsStates by viewModel.taskActionButtonStates.collectAsStateWithLifecycle()
val list by viewModel.items.collectAsStateWithLifecycle()

TaskScreen(
taskHeader =
TaskHeader(label = viewModel.task.label, iconResId = R.drawable.ic_question_answer),
taskActionButtonsStates = taskActionButtonsStates,
onFooterPositionUpdated = onFooterPositionUpdated,
onAction = onAction,
taskBody = {
MultipleChoiceTaskContent(
list = list,
onItemToggled = { viewModel.onItemToggled(it) },
onOtherValueChanged = { viewModel.onOtherTextChanged(it) },
)
},
)
}

@Composable
internal fun MultipleChoiceTaskContent(
list: List<MultipleChoiceItem>,
onItemToggled: (MultipleChoiceItem) -> Unit,
onOtherValueChanged: (String) -> Unit,
) {
val scrollState = rememberLazyListState()

Box(modifier = Modifier.padding(horizontal = MaterialTheme.sizes.taskViewPadding)) {
LazyColumn(Modifier.testTag(MULTIPLE_CHOICE_LIST_TEST_TAG), state = scrollState) {
items(list, key = { it.option.id }) { item ->
MultipleChoiceItemView(
item = item,
isLastIndex = list.indexOf(item) == list.lastIndex,
toggleItem = onItemToggled,
otherValueChanged = onOtherValueChanged,
)
}
}
}
}

@Preview(showBackground = true)
@Composable
@ExcludeFromJacocoGeneratedReport
private fun MultipleChoiceTaskContentPreview() {
AppTheme {
Surface {
MultipleChoiceTaskContent(
list =
listOf(
MultipleChoiceItem(
option = Option("option id 1", "code1", "Option 1"),
cardinality = Cardinality.SELECT_ONE,
isSelected = true,
),
MultipleChoiceItem(
option = Option("option id 2", "code2", "Option 2"),
cardinality = Cardinality.SELECT_ONE,
isSelected = false,
),
MultipleChoiceItem(
option = Option("option id 3", "code3", "Other"),
cardinality = Cardinality.SELECT_ONE,
isSelected = false,
isOtherOption = true,
),
),
onItemToggled = {},
onOtherValueChanged = {},
)
}
}
}
Loading
Loading