From bbf71bae380a1dfc359f5805da7f7721830b49c3 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 4 Aug 2020 15:19:08 -0500 Subject: [PATCH] Convert CommentsController to Kotlin --- .../astrid/notes/CommentsController.java | 126 ------------------ .../astrid/notes/CommentsController.kt | 115 ++++++++++++++++ .../org/tasks/data/UserActivityDaoBlocking.kt | 11 -- .../notifications/NotificationDaoBlocking.kt | 31 ----- 4 files changed, 115 insertions(+), 168 deletions(-) delete mode 100644 app/src/main/java/com/todoroo/astrid/notes/CommentsController.java create mode 100644 app/src/main/java/com/todoroo/astrid/notes/CommentsController.kt delete mode 100644 app/src/main/java/org/tasks/data/UserActivityDaoBlocking.kt delete mode 100644 app/src/main/java/org/tasks/notifications/NotificationDaoBlocking.kt diff --git a/app/src/main/java/com/todoroo/astrid/notes/CommentsController.java b/app/src/main/java/com/todoroo/astrid/notes/CommentsController.java deleted file mode 100644 index ffe23d2d4..000000000 --- a/app/src/main/java/com/todoroo/astrid/notes/CommentsController.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ - -package com.todoroo.astrid.notes; - -import static org.tasks.files.ImageHelper.sampleBitmap; - -import android.app.Activity; -import android.graphics.Color; -import android.net.Uri; -import android.text.Html; -import android.text.util.Linkify; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.TextView; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.astrid.data.Task; -import java.util.ArrayList; -import javax.inject.Inject; -import org.tasks.R; -import org.tasks.data.UserActivity; -import org.tasks.data.UserActivityDaoBlocking; -import org.tasks.files.FileHelper; -import org.tasks.locale.Locale; -import org.tasks.preferences.Preferences; - -public class CommentsController { - - private final UserActivityDaoBlocking userActivityDao; - private final ArrayList items = new ArrayList<>(); - private final Activity activity; - private final Preferences preferences; - private final Locale locale; - - private int commentItems = 10; - private Task task; - private ViewGroup commentsContainer; - - @Inject - public CommentsController( - UserActivityDaoBlocking userActivityDao, Activity activity, Preferences preferences, Locale locale) { - this.userActivityDao = userActivityDao; - this.activity = activity; - this.preferences = preferences; - this.locale = locale; - } - - private static void setupImagePopupForCommentView( - View view, ImageView commentPictureView, final Uri uri, final Activity activity) { - if (uri != null) { - commentPictureView.setVisibility(View.VISIBLE); - commentPictureView.setImageBitmap( - sampleBitmap( - activity, - uri, - commentPictureView.getLayoutParams().width, - commentPictureView.getLayoutParams().height)); - - view.setOnClickListener(v -> FileHelper.startActionView(activity, uri)); - } else { - commentPictureView.setVisibility(View.GONE); - } - } - - public void initialize(Task task, ViewGroup commentsContainer) { - this.task = task; - this.commentsContainer = commentsContainer; - } - - public void reloadView() { - if (!preferences.getBoolean(R.string.p_show_task_edit_comments, true)) { - return; - } - - items.clear(); - commentsContainer.removeAllViews(); - - items.addAll(userActivityDao.getCommentsForTask(task.getUuid())); - - for (int i = 0; i < Math.min(items.size(), commentItems); i++) { - View notesView = this.getUpdateNotes(items.get(i), commentsContainer); - commentsContainer.addView(notesView); - } - - if (items.size() > commentItems) { - Button loadMore = new Button(activity); - loadMore.setHint(R.string.TEA_load_more); - loadMore.setBackgroundColor(Color.alpha(0)); - loadMore.setOnClickListener( - v -> { - // Perform action on click - commentItems += 10; - reloadView(); - }); - commentsContainer.addView(loadMore); - } - } - - private View getUpdateNotes(UserActivity userActivity, ViewGroup parent) { - View convertView = - activity.getLayoutInflater().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 void bindView(View view, UserActivity item) { - // name - final TextView nameView = view.findViewById(R.id.title); - nameView.setText(Html.fromHtml(item.getMessage())); - Linkify.addLinks(nameView, Linkify.ALL); - - // date - final TextView date = view.findViewById(R.id.date); - date.setText(DateUtilities.getLongDateStringWithTime(item.getCreated(), locale.getLocale())); - - // picture - final ImageView commentPictureView = view.findViewById(R.id.comment_picture); - setupImagePopupForCommentView(view, commentPictureView, item.getPictureUri(), activity); - } -} diff --git a/app/src/main/java/com/todoroo/astrid/notes/CommentsController.kt b/app/src/main/java/com/todoroo/astrid/notes/CommentsController.kt new file mode 100644 index 000000000..6ee389c3b --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/notes/CommentsController.kt @@ -0,0 +1,115 @@ +/* + * 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.graphics.Color +import android.net.Uri +import android.text.Html +import android.text.util.Linkify +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.launch +import org.tasks.R +import org.tasks.data.UserActivity +import org.tasks.data.UserActivityDao +import org.tasks.files.FileHelper +import org.tasks.files.ImageHelper +import org.tasks.locale.Locale +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() + 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(R.id.title) + nameView.text = Html.fromHtml(item.message) + Linkify.addLinks(nameView, Linkify.ALL) + + // date + val date = view.findViewById(R.id.date) + date.text = DateUtilities.getLongDateStringWithTime(item.created!!, locale.locale) + + // picture + val commentPictureView = view.findViewById(R.id.comment_picture) + setupImagePopupForCommentView(view, commentPictureView, item.pictureUri, activity) + } + + 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 + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/data/UserActivityDaoBlocking.kt b/app/src/main/java/org/tasks/data/UserActivityDaoBlocking.kt deleted file mode 100644 index 8c7549811..000000000 --- a/app/src/main/java/org/tasks/data/UserActivityDaoBlocking.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.tasks.data - -import kotlinx.coroutines.runBlocking -import javax.inject.Inject - -@Deprecated("use coroutines") -class UserActivityDaoBlocking @Inject constructor(private val dao: UserActivityDao) { - fun getCommentsForTask(taskUuid: String): List = runBlocking { - dao.getCommentsForTask(taskUuid) - } -} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/notifications/NotificationDaoBlocking.kt b/app/src/main/java/org/tasks/notifications/NotificationDaoBlocking.kt deleted file mode 100644 index b7beaad42..000000000 --- a/app/src/main/java/org/tasks/notifications/NotificationDaoBlocking.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.tasks.notifications - -import kotlinx.coroutines.runBlocking -import javax.inject.Inject - -@Deprecated("use coroutines") -class NotificationDaoBlocking @Inject constructor(private val dao: NotificationDao) { - fun getAll(): List = runBlocking { - dao.getAll() - } - - fun getAllOrdered(): List = runBlocking { - dao.getAllOrdered() - } - - fun insertAll(notifications: List) = runBlocking { - dao.insertAll(notifications) - } - - fun delete(taskId: Long) = runBlocking { - dao.delete(taskId) - } - - fun deleteAll(taskIds: List) = runBlocking { - dao.deleteAll(taskIds) - } - - fun latestTimestamp(): Long = runBlocking { - dao.latestTimestamp() - } -} \ No newline at end of file