|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 New Vector Ltd |
| 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 | + * http://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 | + |
| 17 | +package io.element.android.libraries.designsystem.components.dialogs |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.padding |
| 20 | +import androidx.compose.foundation.lazy.LazyColumn |
| 21 | +import androidx.compose.foundation.lazy.itemsIndexed |
| 22 | +import androidx.compose.material3.AlertDialog |
| 23 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.remember |
| 26 | +import androidx.compose.runtime.toMutableStateList |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.res.stringResource |
| 29 | +import androidx.compose.ui.unit.dp |
| 30 | +import com.airbnb.android.showkase.annotation.ShowkaseComposable |
| 31 | +import io.element.android.libraries.designsystem.components.list.CheckboxListItem |
| 32 | +import io.element.android.libraries.designsystem.preview.DayNightPreviews |
| 33 | +import io.element.android.libraries.designsystem.preview.ElementPreview |
| 34 | +import io.element.android.libraries.designsystem.preview.PreviewGroup |
| 35 | +import io.element.android.libraries.designsystem.theme.components.DialogPreview |
| 36 | +import io.element.android.libraries.designsystem.theme.components.ListSupportingText |
| 37 | +import io.element.android.libraries.designsystem.theme.components.SimpleAlertDialogContent |
| 38 | +import io.element.android.libraries.ui.strings.CommonStrings |
| 39 | +import kotlinx.collections.immutable.ImmutableList |
| 40 | +import kotlinx.collections.immutable.persistentListOf |
| 41 | + |
| 42 | +@OptIn(ExperimentalMaterial3Api::class) |
| 43 | +@Composable |
| 44 | +fun MultipleSelectionDialog( |
| 45 | + options: ImmutableList<ListOption>, |
| 46 | + onConfirmClicked: (List<Int>) -> Unit, |
| 47 | + onDismissRequest: () -> Unit, |
| 48 | + modifier: Modifier = Modifier, |
| 49 | + confirmButtonTitle: String = stringResource(CommonStrings.action_confirm), |
| 50 | + dismissButtonTitle: String = stringResource(CommonStrings.action_cancel), |
| 51 | + title: String? = null, |
| 52 | + subtitle: String? = null, |
| 53 | + initialSelection: ImmutableList<Int> = persistentListOf(), |
| 54 | +) { |
| 55 | + val decoratedSubtitle: @Composable (() -> Unit)? = subtitle?.let { |
| 56 | + @Composable { |
| 57 | + ListSupportingText( |
| 58 | + text = it, |
| 59 | + modifier = Modifier.padding(start = 8.dp) |
| 60 | + ) |
| 61 | + } |
| 62 | + } |
| 63 | + AlertDialog( |
| 64 | + modifier = modifier, |
| 65 | + onDismissRequest = onDismissRequest, |
| 66 | + ) { |
| 67 | + MultipleSelectionDialogContent( |
| 68 | + title = title, |
| 69 | + subtitle = decoratedSubtitle, |
| 70 | + options = options, |
| 71 | + confirmButtonTitle = confirmButtonTitle, |
| 72 | + onConfirmClicked = onConfirmClicked, |
| 73 | + dismissButtonTitle = dismissButtonTitle, |
| 74 | + onDismissRequest = onDismissRequest, |
| 75 | + initialSelected = initialSelection, |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Composable |
| 81 | +internal fun MultipleSelectionDialogContent( |
| 82 | + options: ImmutableList<ListOption>, |
| 83 | + confirmButtonTitle: String, |
| 84 | + onConfirmClicked: (List<Int>) -> Unit, |
| 85 | + dismissButtonTitle: String, |
| 86 | + onDismissRequest: () -> Unit, |
| 87 | + modifier: Modifier = Modifier, |
| 88 | + title: String? = null, |
| 89 | + initialSelected: ImmutableList<Int> = persistentListOf(), |
| 90 | + subtitle: @Composable (() -> Unit)? = null, |
| 91 | +) { |
| 92 | + val selectedOptionIndexes = remember { initialSelected.toMutableStateList() } |
| 93 | + |
| 94 | + fun isSelected(index: Int) = selectedOptionIndexes.any { it == index } |
| 95 | + |
| 96 | + SimpleAlertDialogContent( |
| 97 | + title = title, |
| 98 | + subtitle = subtitle, |
| 99 | + modifier = modifier, |
| 100 | + submitText = confirmButtonTitle, |
| 101 | + onSubmitClicked = { |
| 102 | + onConfirmClicked(selectedOptionIndexes.toList()) |
| 103 | + }, |
| 104 | + cancelText = dismissButtonTitle, |
| 105 | + onCancelClicked = onDismissRequest, |
| 106 | + applyPaddingToContents = false, |
| 107 | + ) { |
| 108 | + LazyColumn { |
| 109 | + itemsIndexed(options) { index, option -> |
| 110 | + CheckboxListItem( |
| 111 | + headline = option.title, |
| 112 | + checked = isSelected(index), |
| 113 | + onChange = { |
| 114 | + if (isSelected(index)) { |
| 115 | + selectedOptionIndexes.remove(index) |
| 116 | + } else { |
| 117 | + selectedOptionIndexes.add(index) |
| 118 | + } |
| 119 | + }, |
| 120 | + supportingText = option.subtitle, |
| 121 | + compactLayout = true, |
| 122 | + modifier = Modifier.padding(start = 8.dp) |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +@DayNightPreviews |
| 130 | +@ShowkaseComposable(group = PreviewGroup.Dialogs) |
| 131 | +@Composable |
| 132 | +internal fun MultipleSelectionDialogContentPreview() { |
| 133 | + ElementPreview(showBackground = false) { |
| 134 | + DialogPreview { |
| 135 | + val options = persistentListOf( |
| 136 | + ListOption("Option 1", "Supporting line text lorem ipsum dolor sit amet, consectetur."), |
| 137 | + ListOption("Option 2"), |
| 138 | + ListOption("Option 3"), |
| 139 | + ) |
| 140 | + MultipleSelectionDialogContent( |
| 141 | + title = "Dialog title", |
| 142 | + options = options, |
| 143 | + onConfirmClicked = {}, |
| 144 | + onDismissRequest = {}, |
| 145 | + confirmButtonTitle = "Save", |
| 146 | + dismissButtonTitle = "Cancel", |
| 147 | + initialSelected = persistentListOf(0), |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments