From e7b6c96576b7fe7f2d9ac2d72e0016d35197e18a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 25 Jul 2022 00:40:25 -0500 Subject: [PATCH] Add DescriptionRow composable --- .../org/tasks/compose/edit/DescriptionRow.kt | 112 ++++++++++++++++++ .../org/tasks/ui/DescriptionControlSet.kt | 78 +++--------- 2 files changed, 129 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/org/tasks/compose/edit/DescriptionRow.kt diff --git a/app/src/main/java/org/tasks/compose/edit/DescriptionRow.kt b/app/src/main/java/org/tasks/compose/edit/DescriptionRow.kt new file mode 100644 index 000000000..6e7a9c514 --- /dev/null +++ b/app/src/main/java/org/tasks/compose/edit/DescriptionRow.kt @@ -0,0 +1,112 @@ +package org.tasks.compose.edit + +import android.content.res.Configuration +import android.text.InputType +import android.util.TypedValue +import android.view.View +import android.view.inputmethod.EditorInfo +import android.widget.EditText +import androidx.compose.foundation.layout.* +import androidx.compose.runtime.Composable +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import androidx.core.widget.addTextChangedListener +import com.google.android.material.composethemeadapter.MdcTheme +import org.tasks.R +import org.tasks.compose.TaskEditRow +import org.tasks.dialogs.Linkify +import org.tasks.markdown.MarkdownProvider + +@Composable +fun DescriptionRow( + text: String?, + onChanged: (CharSequence?) -> Unit, + linkify: Linkify?, + markdownProvider: MarkdownProvider?, +) { + TaskEditRow( + iconRes = R.drawable.ic_outline_notes_24px, + content = { + Column(verticalArrangement = Arrangement.Center) { + Spacer(modifier = Modifier.height(11.dp)) + AndroidView( + modifier = Modifier + .fillMaxWidth() + .wrapContentHeight() + .padding(end = 16.dp), + factory = { context -> + EditText(context).apply { + setText(text) + val textWatcher = + markdownProvider?.markdown(linkify != null)?.textWatcher(this) + addTextChangedListener( + onTextChanged = { text, _, _, _ -> onChanged(text) }, + afterTextChanged = { editable -> textWatcher?.invoke(editable) } + ) + setBackgroundColor(context.getColor(android.R.color.transparent)) + textAlignment = View.TEXT_ALIGNMENT_VIEW_START + imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI + inputType = + InputType.TYPE_CLASS_TEXT or + InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or + InputType.TYPE_TEXT_FLAG_MULTI_LINE or + InputType.TYPE_TEXT_FLAG_AUTO_CORRECT + isSingleLine = false + maxLines = Int.MAX_VALUE + importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO + isVerticalScrollBarEnabled = true + freezesText = true + setHorizontallyScrolling(false) + isHorizontalScrollBarEnabled = false + setHint(R.string.TEA_note_label) + setHintTextColor(context.getColor(R.color.text_tertiary)) + setTextSize( + TypedValue.COMPLEX_UNIT_PX, + context.resources.getDimension(R.dimen.task_edit_text_size) + ) + linkify?.linkify(this) + } + }, + ) + Spacer(modifier = Modifier.height(11.dp)) + } + }, + ) +} + +@ExperimentalComposeUiApi +@Preview(showBackground = true, widthDp = 320) +@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES, widthDp = 320) +@Composable +fun EmptyDescriptionPreview() { + MdcTheme { + DescriptionRow( + text = null, + onChanged = {}, + linkify = null, + markdownProvider = null, + ) + } +} + +@ExperimentalComposeUiApi +@Preview(showBackground = true, widthDp = 320) +@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES, widthDp = 320) +@Composable +fun DescriptionPreview() { + MdcTheme { + DescriptionRow( + text = """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + + Eleifend quam adipiscing vitae proin sagittis. Faucibus a pellentesque sit amet porttitor eget dolor. + """.trimIndent(), + onChanged = {}, + linkify = null, + markdownProvider = null, + ) + } +} diff --git a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt index 6b0be7299..2a4dfedc1 100644 --- a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt +++ b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt @@ -1,18 +1,12 @@ package org.tasks.ui -import android.text.InputType -import android.util.TypedValue import android.view.View -import android.view.inputmethod.EditorInfo -import android.widget.EditText -import androidx.compose.foundation.layout.* -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.unit.dp -import androidx.compose.ui.viewinterop.AndroidView -import androidx.core.widget.addTextChangedListener +import android.view.ViewGroup +import androidx.compose.ui.platform.ComposeView +import com.google.android.material.composethemeadapter.MdcTheme import dagger.hilt.android.AndroidEntryPoint import org.tasks.R +import org.tasks.compose.edit.DescriptionRow import org.tasks.dialogs.Linkify import org.tasks.markdown.MarkdownProvider import org.tasks.preferences.Preferences @@ -28,61 +22,23 @@ class DescriptionControlSet : TaskEditControlComposeFragment() { private val linkifyEnabled: Boolean get() = preferences.getBoolean(R.string.p_linkify_task_edit, false) - @Composable - override fun Body() { - Column(verticalArrangement = Arrangement.Center) { - Spacer(modifier = Modifier.height(11.dp)) - AndroidView( - modifier = Modifier - .fillMaxWidth() - .wrapContentHeight() - .padding(end = 16.dp), - factory = { context -> - EditText(context).apply { - setText(viewModel.description.stripCarriageReturns()) - val textWatcher = - markdownProvider.markdown(linkifyEnabled).textWatcher(this) - addTextChangedListener( - onTextChanged = { text, _, _, _ -> textChanged(text) }, - afterTextChanged = { editable -> textWatcher?.invoke(editable) } - ) - setBackgroundColor(context.getColor(android.R.color.transparent)) - textAlignment = View.TEXT_ALIGNMENT_VIEW_START - imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI - inputType = - InputType.TYPE_CLASS_TEXT or - InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or - InputType.TYPE_TEXT_FLAG_MULTI_LINE or - InputType.TYPE_TEXT_FLAG_AUTO_CORRECT - isSingleLine = false - maxLines = Int.MAX_VALUE - importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO - isVerticalScrollBarEnabled = true - freezesText = true - setHorizontallyScrolling(false) - isHorizontalScrollBarEnabled = false - setHint(R.string.TEA_note_label) - setHintTextColor(context.getColor(R.color.text_tertiary)) - setTextSize(TypedValue.COMPLEX_UNIT_PX, context.resources.getDimension(R.dimen.task_edit_text_size)) - if (linkifyEnabled) { - linkify.linkify(this) - } - } - }, - ) - Spacer(modifier = Modifier.height(11.dp)) + override fun bind(parent: ViewGroup?): View = + (parent as ComposeView).apply { + setContent { + MdcTheme { + DescriptionRow( + text = viewModel.description.stripCarriageReturns(), + onChanged = { text -> viewModel.description = text.toString().trim { it <= ' ' } }, + linkify = if (linkifyEnabled) linkify else null, + markdownProvider = markdownProvider, + ) + } + } } - } - - override val icon = R.drawable.ic_outline_notes_24px override fun controlId() = TAG - private fun textChanged(text: CharSequence?) { - viewModel.description = text?.toString()?.trim { it <= ' ' } - } - companion object { const val TAG = R.string.TEA_ctrl_notes_pref } -} \ No newline at end of file +}