mirror of https://github.com/tasks/tasks
Convert comments row to compose
parent
7b9b71dd67
commit
60820ab3c9
@ -1,146 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.notes
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.AlertDialog
|
||||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.text.Html
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.todoroo.andlib.utility.DateUtilities
|
||||
import com.todoroo.astrid.data.Task
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.tasks.R
|
||||
import org.tasks.data.UserActivity
|
||||
import org.tasks.data.UserActivityDao
|
||||
import org.tasks.dialogs.Linkify
|
||||
import org.tasks.files.FileHelper
|
||||
import org.tasks.files.ImageHelper
|
||||
import org.tasks.preferences.Preferences
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.min
|
||||
|
||||
class CommentsController @Inject constructor(
|
||||
private val userActivityDao: UserActivityDao,
|
||||
private val activity: Activity,
|
||||
private val preferences: Preferences,
|
||||
private val locale: Locale
|
||||
) {
|
||||
private val items = ArrayList<UserActivity>()
|
||||
private var commentItems = 10
|
||||
private var task: Task? = null
|
||||
private var commentsContainer: ViewGroup? = null
|
||||
|
||||
fun initialize(task: Task?, commentsContainer: ViewGroup?) {
|
||||
this.task = task
|
||||
this.commentsContainer = commentsContainer
|
||||
}
|
||||
|
||||
fun reloadView() {
|
||||
if (!preferences.getBoolean(R.string.p_show_task_edit_comments, true)) {
|
||||
return
|
||||
}
|
||||
(activity as AppCompatActivity).lifecycleScope.launch {
|
||||
items.clear()
|
||||
commentsContainer!!.removeAllViews()
|
||||
items.addAll(userActivityDao.getCommentsForTask(task!!.uuid))
|
||||
for (i in 0 until min(items.size, commentItems)) {
|
||||
val notesView = getUpdateNotes(items[i], commentsContainer)
|
||||
commentsContainer!!.addView(notesView)
|
||||
}
|
||||
if (items.size > commentItems) {
|
||||
val loadMore = Button(activity)
|
||||
loadMore.setHint(R.string.TEA_load_more)
|
||||
loadMore.setBackgroundColor(Color.alpha(0))
|
||||
loadMore.setOnClickListener {
|
||||
// Perform action on click
|
||||
commentItems += 10
|
||||
reloadView()
|
||||
}
|
||||
commentsContainer!!.addView(loadMore)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUpdateNotes(userActivity: UserActivity, parent: ViewGroup?): View {
|
||||
val convertView = activity.layoutInflater.inflate(R.layout.comment_adapter_row, parent, false)
|
||||
bindView(convertView, userActivity)
|
||||
return convertView
|
||||
}
|
||||
|
||||
/** Helper method to set the contents and visibility of each field */
|
||||
private fun bindView(view: View, item: UserActivity) {
|
||||
// name
|
||||
val nameView = view.findViewById<TextView>(R.id.title)
|
||||
nameView.text = Html.fromHtml(item.message)
|
||||
Linkify.safeLinkify(nameView)
|
||||
|
||||
// date
|
||||
val date = view.findViewById<TextView>(R.id.date)
|
||||
date.text = DateUtilities.getLongDateStringWithTime(item.created!!, locale)
|
||||
|
||||
// picture
|
||||
val commentPictureView = view.findViewById<ImageView>(R.id.comment_picture)
|
||||
setupImagePopupForCommentView(view, commentPictureView, item.pictureUri, activity)
|
||||
|
||||
// delete button
|
||||
val deleteBtn = view.findViewById<ImageView>(R.id.clear)
|
||||
deleteBtn.setOnClickListener {
|
||||
val builder = AlertDialog.Builder(commentsContainer!!.context)
|
||||
|
||||
// Display a message on alert dialog
|
||||
builder.setMessage(R.string.delete_comment)
|
||||
|
||||
// Set a positive button and its click listener on alert dialog
|
||||
builder.setPositiveButton(R.string.delete){dialog, which ->
|
||||
|
||||
(activity as AppCompatActivity).lifecycleScope.launch {
|
||||
withContext(NonCancellable) {
|
||||
userActivityDao.delete(item)
|
||||
}
|
||||
reloadView()
|
||||
}
|
||||
}
|
||||
|
||||
// Display a negative button on alert dialog
|
||||
builder.setNegativeButton(R.string.cancel){dialog,which ->
|
||||
}
|
||||
// Finally, make the alert dialog using builder
|
||||
val dialog: AlertDialog = builder.create()
|
||||
|
||||
// Display the alert dialog on app interface
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun setupImagePopupForCommentView(
|
||||
view: View, commentPictureView: ImageView, uri: Uri?, activity: Activity) {
|
||||
if (uri != null) {
|
||||
commentPictureView.visibility = View.VISIBLE
|
||||
commentPictureView.setImageBitmap(
|
||||
ImageHelper.sampleBitmap(
|
||||
activity,
|
||||
uri,
|
||||
commentPictureView.layoutParams.width,
|
||||
commentPictureView.layoutParams.height))
|
||||
view.setOnClickListener { FileHelper.startActionView(activity, uri) }
|
||||
} else {
|
||||
commentPictureView.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package org.tasks.compose
|
||||
|
||||
import androidx.compose.material.ContentAlpha
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.IconButton
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Delete
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import org.tasks.R
|
||||
|
||||
@Composable
|
||||
fun DeleteButton(onClick: () -> Unit) {
|
||||
IconButton(onClick = onClick) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Delete,
|
||||
modifier = Modifier.alpha(ContentAlpha.medium),
|
||||
contentDescription = stringResource(id = R.string.delete)
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
package org.tasks.compose.edit
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import coil.compose.AsyncImage
|
||||
import com.todoroo.andlib.utility.DateUtilities
|
||||
import org.tasks.R
|
||||
import org.tasks.compose.DeleteButton
|
||||
import org.tasks.compose.TaskEditRow
|
||||
import org.tasks.data.UserActivity
|
||||
import java.util.*
|
||||
|
||||
@Composable
|
||||
fun CommentsRow(
|
||||
comments: List<UserActivity>,
|
||||
deleteComment: (UserActivity) -> Unit,
|
||||
openImage: (Uri) -> Unit,
|
||||
) {
|
||||
if (comments.isEmpty()) {
|
||||
return
|
||||
}
|
||||
TaskEditRow(
|
||||
iconRes = R.drawable.ic_outline_chat_bubble_outline_24px,
|
||||
content = {
|
||||
Column(
|
||||
modifier = Modifier.padding(vertical = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
comments.forEach {
|
||||
Comment(
|
||||
comment = it,
|
||||
deleteComment = deleteComment,
|
||||
openImage = openImage,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Comment(
|
||||
comment: UserActivity,
|
||||
deleteComment: (UserActivity) -> Unit,
|
||||
openImage: (Uri) -> Unit,
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.Top) {
|
||||
Column(
|
||||
modifier = Modifier.weight(1f).padding(top = 8.dp),
|
||||
) {
|
||||
comment.message?.let {
|
||||
// TODO: linkify text
|
||||
Text(text = it)
|
||||
}
|
||||
comment.pictureUri?.let {
|
||||
AsyncImage(
|
||||
model = it,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.clickable { openImage(it) }.size(100.dp)
|
||||
)
|
||||
}
|
||||
Text(
|
||||
text = DateUtilities.getLongDateStringWithTime(comment.created!!, Locale.getDefault()),
|
||||
style = MaterialTheme.typography.caption,
|
||||
)
|
||||
}
|
||||
DeleteButton(
|
||||
onClick = {
|
||||
// TODO: add confirmation dialog
|
||||
deleteComment(comment)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
** Copyright (c) 2012 Todoroo Inc
|
||||
**
|
||||
** See the file "LICENSE" for the full license governing this code.
|
||||
-->
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/comment_row"
|
||||
style="@style/CommentsRow"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:padding="@dimen/keyline_first">
|
||||
|
||||
<include layout="@layout/control_set_clear_button" />
|
||||
|
||||
<LinearLayout
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_toStartOf="@id/delete"
|
||||
android:paddingEnd="@dimen/keyline_first"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"/>
|
||||
|
||||
<!-- add Edit button here -->
|
||||
|
||||
<TextView
|
||||
android:id="@+id/date"
|
||||
android:textStyle="normal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:ellipsize="end"
|
||||
android:singleLine="true"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
android:textSize="12sp"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/comment_picture"
|
||||
android:layout_width="50dip"
|
||||
android:layout_height="50dip"
|
||||
android:scaleType="fitCenter"
|
||||
android:visibility="gone"
|
||||
tools:ignore="ContentDescription"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
Loading…
Reference in New Issue