Convert CommentsController to Kotlin

pull/1066/head
Alex Baker 4 years ago
parent 6fd1cb3e44
commit bbf71bae38

@ -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<UserActivity> 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);
}
}

@ -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<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.addLinks(nameView, Linkify.ALL)
// date
val date = view.findViewById<TextView>(R.id.date)
date.text = DateUtilities.getLongDateStringWithTime(item.created!!, locale.locale)
// picture
val commentPictureView = view.findViewById<ImageView>(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
}
}
}
}

@ -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<UserActivity> = runBlocking {
dao.getCommentsForTask(taskUuid)
}
}

@ -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<Long> = runBlocking {
dao.getAll()
}
fun getAllOrdered(): List<Notification> = runBlocking {
dao.getAllOrdered()
}
fun insertAll(notifications: List<Notification>) = runBlocking {
dao.insertAll(notifications)
}
fun delete(taskId: Long) = runBlocking {
dao.delete(taskId)
}
fun deleteAll(taskIds: List<Long>) = runBlocking {
dao.deleteAll(taskIds)
}
fun latestTimestamp(): Long = runBlocking {
dao.latestTimestamp()
}
}
Loading…
Cancel
Save