From 012663eebee78f93cf4742ffe0f60774eccc9a19 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 29 Jun 2020 17:45:45 -0500 Subject: [PATCH] Create views in suspending function --- .../com/todoroo/astrid/alarms/AlarmService.kt | 24 ++++++----- .../todoroo/astrid/files/FilesControlSet.kt | 40 +++++++++++-------- .../astrid/repeats/RepeatControlSet.kt | 8 +--- .../com/todoroo/astrid/tags/TagsControlSet.kt | 30 ++++++-------- .../todoroo/astrid/timers/TimerControlSet.kt | 9 +---- .../todoroo/astrid/ui/HideUntilControlSet.kt | 8 +--- .../todoroo/astrid/ui/ReminderControlSet.kt | 11 +++-- app/src/main/java/org/tasks/data/TagDao.kt | 2 +- .../java/org/tasks/data/TagDaoBlocking.kt | 2 +- .../java/org/tasks/data/TagDataDaoBlocking.kt | 2 +- .../org/tasks/fragments/CommentBarFragment.kt | 5 ++- .../main/java/org/tasks/jobs/CleanupWork.kt | 5 ++- .../java/org/tasks/ui/CalendarControlSet.kt | 7 +--- .../java/org/tasks/ui/DeadlineControlSet.kt | 8 +--- .../org/tasks/ui/DescriptionControlSet.kt | 10 +---- .../main/java/org/tasks/ui/ListFragment.kt | 21 +++++----- .../java/org/tasks/ui/LocationControlSet.kt | 15 ++++--- .../java/org/tasks/ui/PriorityControlSet.kt | 5 +-- .../java/org/tasks/ui/SubtaskControlSet.kt | 27 +++++++------ .../org/tasks/ui/TaskEditControlFragment.kt | 9 +++++ 20 files changed, 118 insertions(+), 130 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/alarms/AlarmService.kt b/app/src/main/java/com/todoroo/astrid/alarms/AlarmService.kt index d8994e212..f45178ed7 100644 --- a/app/src/main/java/com/todoroo/astrid/alarms/AlarmService.kt +++ b/app/src/main/java/com/todoroo/astrid/alarms/AlarmService.kt @@ -5,8 +5,9 @@ */ package com.todoroo.astrid.alarms +import kotlinx.coroutines.runBlocking import org.tasks.data.Alarm -import org.tasks.data.AlarmDaoBlocking +import org.tasks.data.AlarmDao import org.tasks.jobs.AlarmEntry import org.tasks.jobs.NotificationQueue import java.util.* @@ -20,12 +21,13 @@ import javax.inject.Singleton */ @Singleton class AlarmService @Inject constructor( - private val alarmDao: AlarmDaoBlocking, + private val alarmDao: AlarmDao, private val jobs: NotificationQueue) { - fun rescheduleAlarms(taskId: Long, oldDueDate: Long, newDueDate: Long) { + // TODO: remove runBlocking + fun rescheduleAlarms(taskId: Long, oldDueDate: Long, newDueDate: Long) = runBlocking { if (newDueDate <= 0 || newDueDate <= oldDueDate) { - return + return@runBlocking } val alarms: MutableSet = LinkedHashSet() for (alarm in getAlarms(taskId)) { @@ -36,7 +38,7 @@ class AlarmService @Inject constructor( } } - fun getAlarms(taskId: Long): List { + suspend fun getAlarms(taskId: Long): List { return alarmDao.getAlarms(taskId) } @@ -45,7 +47,7 @@ class AlarmService @Inject constructor( * * @return true if data was changed */ - fun synchronizeAlarms(taskId: Long, timestamps: MutableSet): Boolean { + suspend fun synchronizeAlarms(taskId: Long, timestamps: MutableSet): Boolean { var changed = false for (item in alarmDao.getAlarms(taskId)) { if (!timestamps.remove(item.time)) { @@ -64,25 +66,25 @@ class AlarmService @Inject constructor( return changed } - private fun getActiveAlarmsForTask(taskId: Long): List { + private suspend fun getActiveAlarmsForTask(taskId: Long): List { return alarmDao.getActiveAlarms(taskId) } - /** Schedules all alarms */ - fun scheduleAllAlarms() { + // TODO: remove runBlocking + fun scheduleAllAlarms() = runBlocking { for (alarm in alarmDao.getActiveAlarms()) { scheduleAlarm(alarm) } } - fun cancelAlarms(taskId: Long) { + suspend fun cancelAlarms(taskId: Long) { for (alarm in getActiveAlarmsForTask(taskId)) { jobs.cancelAlarm(alarm.id) } } /** Schedules alarms for a single task */ - private fun scheduleAlarms(taskId: Long) { + private suspend fun scheduleAlarms(taskId: Long) { for (alarm in getActiveAlarmsForTask(taskId)) { scheduleAlarm(alarm) } diff --git a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt index 5be318117..8c18546b6 100644 --- a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt @@ -10,18 +10,20 @@ import android.app.Activity import android.content.Intent import android.net.Uri import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView +import androidx.lifecycle.lifecycleScope import butterknife.BindView import butterknife.OnClick import com.todoroo.astrid.data.Task import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.tasks.R import org.tasks.data.TaskAttachment -import org.tasks.data.TaskAttachmentDaoBlocking +import org.tasks.data.TaskAttachmentDao import org.tasks.dialogs.AddAttachmentDialog import org.tasks.dialogs.DialogBuilder import org.tasks.files.FileHelper @@ -33,7 +35,7 @@ import javax.inject.Inject @AndroidEntryPoint class FilesControlSet : TaskEditControlFragment() { @Inject lateinit var activity: Activity - @Inject lateinit var taskAttachmentDao: TaskAttachmentDaoBlocking + @Inject lateinit var taskAttachmentDao: TaskAttachmentDao @Inject lateinit var dialogBuilder: DialogBuilder @Inject lateinit var preferences: Preferences @@ -44,14 +46,9 @@ class FilesControlSet : TaskEditControlFragment() { lateinit var addAttachment: TextView private var taskUuid: String? = null - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + + override suspend fun createView(savedInstanceState: Bundle?) { taskUuid = task.uuid - for (attachment in taskAttachmentDao.getAttachments(taskUuid!!)) { - addAttachment(attachment) - } if (savedInstanceState == null) { if (task.hasTransitory(TaskAttachment.KEY)) { for (uri in (task.getTransitory>(TaskAttachment.KEY))!!) { @@ -59,7 +56,10 @@ class FilesControlSet : TaskEditControlFragment() { } } } - return view + + taskAttachmentDao + .getAttachments(task.uuid) + .forEach { addAttachment(it) } } @OnClick(R.id.add_attachment) @@ -118,9 +118,13 @@ class FilesControlSet : TaskEditControlFragment() { dialogBuilder .newDialog(R.string.premium_remove_file_confirm) .setPositiveButton(android.R.string.ok) { _, _ -> - taskAttachmentDao.delete(taskAttachment) - FileHelper.delete(context, taskAttachment.parseUri()) - attachmentContainer.removeView(fileRow) + lifecycleScope.launch { + withContext(NonCancellable) { + taskAttachmentDao.delete(taskAttachment) + FileHelper.delete(context, taskAttachment.parseUri()) + } + attachmentContainer.removeView(fileRow) + } } .setNegativeButton(android.R.string.cancel, null) .show() @@ -138,8 +142,10 @@ class FilesControlSet : TaskEditControlFragment() { private fun newAttachment(output: Uri) { val attachment = TaskAttachment(taskUuid!!, output, FileHelper.getFilename(context, output)!!) - taskAttachmentDao.createNew(attachment) - addAttachment(attachment) + lifecycleScope.launch { + taskAttachmentDao.createNew(attachment) + addAttachment(attachment) + } } companion object { diff --git a/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt b/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt index 6b1c55802..f8932844a 100644 --- a/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt @@ -7,7 +7,6 @@ package com.todoroo.astrid.repeats import android.app.Activity import android.os.Bundle -import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.AdapterView @@ -87,9 +86,7 @@ class RepeatControlSet : TaskEditControlFragment() { } } - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { if (savedInstanceState == null) { repeatAfterCompletion = task.repeatAfterCompletion() dueDate = task.dueDate @@ -129,7 +126,7 @@ class RepeatControlSet : TaskEditControlFragment() { if (parent is AdapterView<*>) { selectedItemPosition = parent.selectedItemPosition } - val tv = inflater.inflate(android.R.layout.simple_spinner_item, parent, false) as TextView + val tv = activity.layoutInflater.inflate(android.R.layout.simple_spinner_item, parent, false) as TextView tv.setPadding(0, 0, 0, 0) tv.text = repeatTypes[selectedItemPosition] return tv @@ -141,7 +138,6 @@ class RepeatControlSet : TaskEditControlFragment() { typeSpinner.adapter = typeAdapter typeSpinner.setSelection(if (repeatAfterCompletion) TYPE_COMPLETION_DATE else TYPE_DUE_DATE) refreshDisplayView() - return view } @OnItemSelected(R.id.repeatType) diff --git a/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt b/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt index 2a0b85c3a..e5af9a5f8 100644 --- a/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt @@ -8,9 +8,7 @@ package com.todoroo.astrid.tags import android.app.Activity import android.content.Intent import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import android.widget.TextView import butterknife.BindView import com.google.android.material.chip.ChipGroup @@ -18,9 +16,9 @@ import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.data.Task import dagger.hilt.android.AndroidEntryPoint import org.tasks.R -import org.tasks.data.TagDaoBlocking +import org.tasks.data.TagDao import org.tasks.data.TagData -import org.tasks.data.TagDataDaoBlocking +import org.tasks.data.TagDataDao import org.tasks.tags.TagPickerActivity import org.tasks.ui.ChipProvider import org.tasks.ui.TaskEditControlFragment @@ -34,8 +32,8 @@ import javax.inject.Inject */ @AndroidEntryPoint class TagsControlSet : TaskEditControlFragment() { - @Inject lateinit var tagDao: TagDaoBlocking - @Inject lateinit var tagDataDao: TagDataDaoBlocking + @Inject lateinit var tagDao: TagDao + @Inject lateinit var tagDataDao: TagDataDao @Inject lateinit var chipProvider: ChipProvider @BindView(R.id.no_tags) @@ -46,23 +44,21 @@ class TagsControlSet : TaskEditControlFragment() { private lateinit var originalTags: ArrayList private lateinit var selectedTags: ArrayList - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) - if (savedInstanceState != null) { - selectedTags = savedInstanceState.getParcelableArrayList(EXTRA_SELECTED_TAGS)!! - originalTags = savedInstanceState.getParcelableArrayList(EXTRA_ORIGINAL_TAGS)!! - } else { + + override suspend fun createView(savedInstanceState: Bundle?) { + if (savedInstanceState == null) { originalTags = ArrayList(if (task.isNew) { - task.tags.mapNotNull(tagDataDao::getTagByName) + task.tags.mapNotNull { tagDataDao.getTagByName(it) } } else { tagDataDao.getTagDataForTask(task.id) }) selectedTags = ArrayList(originalTags) + refreshDisplayView() + } else { + selectedTags = savedInstanceState.getParcelableArrayList(EXTRA_SELECTED_TAGS)!! + originalTags = savedInstanceState.getParcelableArrayList(EXTRA_ORIGINAL_TAGS)!! + refreshDisplayView() } - refreshDisplayView() - return view } override fun onSaveInstanceState(outState: Bundle) { diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt b/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt index 087b4c17b..8f6797ea7 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt @@ -10,9 +10,7 @@ import android.os.Bundle import android.os.SystemClock import android.text.format.DateFormat import android.text.format.DateUtils -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import android.widget.Chronometer import android.widget.Chronometer.OnChronometerTickListener import android.widget.ImageView @@ -58,9 +56,7 @@ class TimerControlSet : TaskEditControlFragment() { private lateinit var dialogView: View private lateinit var callback: TimerControlSetCallback - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { val elapsedSeconds: Int val estimatedSeconds: Int if (savedInstanceState == null) { @@ -72,13 +68,12 @@ class TimerControlSet : TaskEditControlFragment() { elapsedSeconds = savedInstanceState.getInt(EXTRA_ELAPSED) estimatedSeconds = savedInstanceState.getInt(EXTRA_ESTIMATED) } - dialogView = inflater.inflate(R.layout.control_set_timers_dialog, null) + dialogView = activity.layoutInflater.inflate(R.layout.control_set_timers_dialog, null) estimated = TimeDurationControlSet(activity, dialogView, R.id.estimatedDuration, theme) elapsed = TimeDurationControlSet(activity, dialogView, R.id.elapsedDuration, theme) estimated.setTimeDuration(estimatedSeconds) elapsed.setTimeDuration(elapsedSeconds) refresh() - return view } override fun onAttach(activity: Activity) { diff --git a/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt b/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt index eeb6290b1..d05a078ad 100644 --- a/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt @@ -8,7 +8,6 @@ package com.todoroo.astrid.ui import android.app.Activity import android.content.Intent import android.os.Bundle -import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.* @@ -71,9 +70,7 @@ class HideUntilControlSet : TaskEditControlFragment(), OnItemSelectedListener { override val isClickable: Boolean get() = true - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { adapter = object : HiddenTopArrayAdapter( activity, android.R.layout.simple_spinner_item, spinnerItems) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { @@ -81,7 +78,7 @@ class HideUntilControlSet : TaskEditControlFragment(), OnItemSelectedListener { if (parent is AdapterView<*>) { selectedItemPosition = parent.selectedItemPosition } - val tv = inflater.inflate(android.R.layout.simple_spinner_item, parent, false) as TextView + val tv = activity.layoutInflater.inflate(android.R.layout.simple_spinner_item, parent, false) as TextView tv.setPadding(0, 0, 0, 0) val value = getItem(selectedItemPosition) if (value!!.setting == Task.HIDE_UNTIL_NONE) { @@ -139,7 +136,6 @@ class HideUntilControlSet : TaskEditControlFragment(), OnItemSelectedListener { spinner.setSelection(selection) spinner.onItemSelectedListener = this refreshDisplayView() - return view } override val layout: Int diff --git a/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt b/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt index 20c086fa9..b30b91879 100644 --- a/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt @@ -16,12 +16,14 @@ import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import androidx.annotation.StringRes +import androidx.lifecycle.lifecycleScope import butterknife.BindView import butterknife.OnClick import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.alarms.AlarmService import com.todoroo.astrid.data.Task import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.R import org.tasks.activities.DateAndTimePickerActivity import org.tasks.data.Alarm @@ -61,10 +63,8 @@ class ReminderControlSet : TaskEditControlFragment() { private var randomControlSet: RandomReminderControlSet? = null private var whenDue = false private var whenOverdue = false - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + + override suspend fun createView(savedInstanceState: Bundle?) { mode.paintFlags = mode.paintFlags or Paint.UNDERLINE_TEXT_FLAG taskId = task.id if (savedInstanceState == null) { @@ -76,10 +76,9 @@ class ReminderControlSet : TaskEditControlFragment() { randomReminder = savedInstanceState.getLong(EXTRA_RANDOM_REMINDER) setup(savedInstanceState.getLongArray(EXTRA_ALARMS)!!.toList()) } - return view } - private fun currentAlarms(): List { + private suspend fun currentAlarms(): List { return if (taskId == Task.NO_ID) { emptyList() } else { diff --git a/app/src/main/java/org/tasks/data/TagDao.kt b/app/src/main/java/org/tasks/data/TagDao.kt index 37d6705e2..8aa0f9f66 100644 --- a/app/src/main/java/org/tasks/data/TagDao.kt +++ b/app/src/main/java/org/tasks/data/TagDao.kt @@ -30,7 +30,7 @@ abstract class TagDao { abstract suspend fun delete(tags: List) @Transaction - open suspend fun applyTags(task: Task, tagDataDao: TagDataDaoBlocking, current: List): Boolean { + open suspend fun applyTags(task: Task, tagDataDao: TagDataDao, current: List): Boolean { val taskId = task.id val existing = HashSet(tagDataDao.getTagDataForTask(taskId)) val selected = HashSet(current) diff --git a/app/src/main/java/org/tasks/data/TagDaoBlocking.kt b/app/src/main/java/org/tasks/data/TagDaoBlocking.kt index abac3112a..502538df2 100644 --- a/app/src/main/java/org/tasks/data/TagDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/TagDaoBlocking.kt @@ -39,7 +39,7 @@ class TagDaoBlocking @Inject constructor(private val dao: TagDao) { } fun applyTags(task: Task, tagDataDao: TagDataDaoBlocking, current: List): Boolean = runBlocking { - dao.applyTags(task, tagDataDao, current) + dao.applyTags(task, tagDataDao.dao, current) } fun insert(task: Task, tags: Collection) = runBlocking { diff --git a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt index c03f8e849..ac694176e 100644 --- a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt @@ -9,7 +9,7 @@ import org.tasks.time.DateTimeUtils.currentTimeMillis import javax.inject.Inject @Deprecated("use coroutines") -class TagDataDaoBlocking @Inject constructor(private val dao: TagDataDao) { +class TagDataDaoBlocking @Inject constructor(val dao: TagDataDao) { fun subscribeToTags(): LiveData> { return dao.subscribeToTags() } diff --git a/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt b/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt index d9d2c7fab..06a1fea19 100644 --- a/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt +++ b/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt @@ -62,6 +62,10 @@ class CommentBarFragment : TaskEditControlFragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(layout, container, false) ButterKnife.bind(this, view) + return view + } + + override suspend fun createView(savedInstanceState: Bundle?) { if (savedInstanceState != null) { val uri = savedInstanceState.getString(EXTRA_PICTURE) if (uri != null) { @@ -77,7 +81,6 @@ class CommentBarFragment : TaskEditControlFragment() { } commentBar.setBackgroundColor(themeColor.primaryColor) resetPictureButton() - return view } override val layout: Int diff --git a/app/src/main/java/org/tasks/jobs/CleanupWork.kt b/app/src/main/java/org/tasks/jobs/CleanupWork.kt index 5598a2ef0..67292d7fd 100644 --- a/app/src/main/java/org/tasks/jobs/CleanupWork.kt +++ b/app/src/main/java/org/tasks/jobs/CleanupWork.kt @@ -7,6 +7,7 @@ import androidx.work.WorkerParameters import com.todoroo.astrid.alarms.AlarmService import com.todoroo.astrid.reminders.ReminderService import com.todoroo.astrid.timers.TimerPlugin +import kotlinx.coroutines.runBlocking import org.tasks.analytics.Firebase import org.tasks.data.DeletionDaoBlocking import org.tasks.data.LocationDaoBlocking @@ -39,7 +40,9 @@ class CleanupWork @WorkerInject constructor( return Result.failure() } tasks.forEach { task -> - alarmService.cancelAlarms(task) + runBlocking { + alarmService.cancelAlarms(task) + } reminderService.cancelReminder(task) notificationManager.cancel(task) locationDao.getGeofencesForTask(task).forEach { diff --git a/app/src/main/java/org/tasks/ui/CalendarControlSet.kt b/app/src/main/java/org/tasks/ui/CalendarControlSet.kt index 9427b2d25..aae197a37 100644 --- a/app/src/main/java/org/tasks/ui/CalendarControlSet.kt +++ b/app/src/main/java/org/tasks/ui/CalendarControlSet.kt @@ -52,10 +52,8 @@ class CalendarControlSet : TaskEditControlFragment() { private var calendarId: String? = null private var eventUri: String? = null - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + + override suspend fun createView(savedInstanceState: Bundle?) { val canAccessCalendars = permissionChecker.canAccessCalendars() if (savedInstanceState != null) { eventUri = savedInstanceState.getString(EXTRA_URI) @@ -81,7 +79,6 @@ class CalendarControlSet : TaskEditControlFragment() { eventUri = null } refreshDisplayView() - return view } override val layout: Int diff --git a/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt b/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt index ec5612862..b27831769 100644 --- a/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt +++ b/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt @@ -3,9 +3,6 @@ package org.tasks.ui import android.app.Activity import android.content.Intent import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import android.widget.TextView import butterknife.BindView import com.todoroo.andlib.utility.DateUtilities @@ -40,12 +37,9 @@ class DeadlineControlSet : TaskEditControlFragment() { callback = activity as DueDateChangeListener } - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { date = savedInstanceState?.getLong(EXTRA_DATE) ?: task.dueDate refreshDisplayView() - return view } override fun onRowClick() { diff --git a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt index 8d7837af4..df473fd56 100644 --- a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt +++ b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt @@ -1,9 +1,6 @@ package org.tasks.ui import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import android.widget.EditText import butterknife.BindView import butterknife.OnTextChanged @@ -24,10 +21,8 @@ class DescriptionControlSet : TaskEditControlFragment() { lateinit var editText: EditText private var description: String? = null - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + + override suspend fun createView(savedInstanceState: Bundle?) { description = if (savedInstanceState == null) { stripCarriageReturns(task.notes) } else { @@ -39,7 +34,6 @@ class DescriptionControlSet : TaskEditControlFragment() { if (preferences.getBoolean(R.string.p_linkify_task_edit, false)) { linkify.linkify(editText) } - return view } override fun onSaveInstanceState(outState: Bundle) { diff --git a/app/src/main/java/org/tasks/ui/ListFragment.kt b/app/src/main/java/org/tasks/ui/ListFragment.kt index 681503390..502299581 100644 --- a/app/src/main/java/org/tasks/ui/ListFragment.kt +++ b/app/src/main/java/org/tasks/ui/ListFragment.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import butterknife.BindView import com.google.android.material.chip.ChipGroup import com.todoroo.astrid.api.CaldavFilter @@ -14,6 +15,7 @@ import com.todoroo.astrid.api.GtasksFilter import com.todoroo.astrid.data.Task import com.todoroo.astrid.service.TaskMover import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.R import org.tasks.activities.ListPicker import org.tasks.data.* @@ -25,9 +27,9 @@ class ListFragment : TaskEditControlFragment() { @BindView(R.id.chip_group) lateinit var chipGroup: ChipGroup - @Inject lateinit var googleTaskListDao: GoogleTaskListDaoBlocking - @Inject lateinit var googleTaskDao: GoogleTaskDaoBlocking - @Inject lateinit var caldavDao: CaldavDaoBlocking + @Inject lateinit var googleTaskListDao: GoogleTaskListDao + @Inject lateinit var googleTaskDao: GoogleTaskDao + @Inject lateinit var caldavDao: CaldavDao @Inject lateinit var defaultFilterProvider: DefaultFilterProvider @Inject lateinit var taskMover: TaskMover @Inject lateinit var chipProvider: ChipProvider @@ -45,13 +47,8 @@ class ListFragment : TaskEditControlFragment() { callback = activity as OnListChanged } - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) - if (savedInstanceState != null) { - originalList = savedInstanceState.getParcelable(EXTRA_ORIGINAL_LIST) - setSelected(savedInstanceState.getParcelable(EXTRA_SELECTED_LIST)!!) - } else { + override suspend fun createView(savedInstanceState: Bundle?) { + if (savedInstanceState == null) { if (task.isNew) { if (task.hasTransitory(GoogleTask.KEY)) { val listId = task.getTransitory(GoogleTask.KEY)!! @@ -84,8 +81,10 @@ class ListFragment : TaskEditControlFragment() { originalList = defaultFilterProvider.defaultList } setSelected(originalList!!) + } else { + originalList = savedInstanceState.getParcelable(EXTRA_ORIGINAL_LIST) + setSelected(savedInstanceState.getParcelable(EXTRA_SELECTED_LIST)!!) } - return view } private fun setSelected(filter: Filter) { diff --git a/app/src/main/java/org/tasks/ui/LocationControlSet.kt b/app/src/main/java/org/tasks/ui/LocationControlSet.kt index 133e57cda..55d70f506 100644 --- a/app/src/main/java/org/tasks/ui/LocationControlSet.kt +++ b/app/src/main/java/org/tasks/ui/LocationControlSet.kt @@ -14,18 +14,20 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.core.util.Pair +import androidx.lifecycle.lifecycleScope import butterknife.BindView import butterknife.OnClick import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.data.SyncFlags import com.todoroo.astrid.data.Task import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.PermissionUtil.verifyPermissions import org.tasks.R import org.tasks.Strings.isNullOrEmpty import org.tasks.data.Geofence import org.tasks.data.Location -import org.tasks.data.LocationDaoBlocking +import org.tasks.data.LocationDao import org.tasks.data.Place import org.tasks.dialogs.DialogBuilder import org.tasks.dialogs.GeofenceDialog @@ -40,7 +42,7 @@ class LocationControlSet : TaskEditControlFragment() { @Inject lateinit var preferences: Preferences @Inject lateinit var dialogBuilder: DialogBuilder @Inject lateinit var geofenceApi: GeofenceApi - @Inject lateinit var locationDao: LocationDaoBlocking + @Inject lateinit var locationDao: LocationDao @Inject lateinit var device: Device @Inject lateinit var permissionRequestor: FragmentPermissionRequestor @Inject lateinit var permissionChecker: PermissionChecker @@ -56,10 +58,8 @@ class LocationControlSet : TaskEditControlFragment() { private var original: Location? = null private var location: Location? = null - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + + override suspend fun createView(savedInstanceState: Bundle?) { if (savedInstanceState == null) { if (task.isNew) { if (task.hasTransitory(Place.KEY)) { @@ -72,13 +72,12 @@ class LocationControlSet : TaskEditControlFragment() { original = locationDao.getGeofences(task.id) } if (original != null) { - location = Location(original!!.geofence, original!!.place) + setLocation(Location(original!!.geofence, original!!.place)) } } else { original = savedInstanceState.getParcelable(EXTRA_ORIGINAL) location = savedInstanceState.getParcelable(EXTRA_LOCATION) } - return view } override fun onResume() { diff --git a/app/src/main/java/org/tasks/ui/PriorityControlSet.kt b/app/src/main/java/org/tasks/ui/PriorityControlSet.kt index e06bd6997..69fe8097b 100644 --- a/app/src/main/java/org/tasks/ui/PriorityControlSet.kt +++ b/app/src/main/java/org/tasks/ui/PriorityControlSet.kt @@ -38,9 +38,7 @@ class PriorityControlSet : TaskEditControlFragment() { priority = getPriority() } - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { priority = savedInstanceState?.getInt(EXTRA_PRIORITY) ?: task.priority when (priority) { 0 -> priorityHigh.isChecked = true @@ -52,7 +50,6 @@ class PriorityControlSet : TaskEditControlFragment() { tintRadioButton(priorityMedium, 1) tintRadioButton(priorityLow, 2) tintRadioButton(priorityNone, 3) - return view } override fun onSaveInstanceState(outState: Bundle) { diff --git a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt index 17ae41f14..c8d2c5f13 100644 --- a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt +++ b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt @@ -15,6 +15,7 @@ import android.widget.EditText import android.widget.LinearLayout import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -28,13 +29,14 @@ import com.todoroo.astrid.activity.MainActivity import com.todoroo.astrid.api.CaldavFilter import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.GtasksFilter -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.dao.TaskDao.TaskCriteria.activeAndVisible import com.todoroo.astrid.data.Task import com.todoroo.astrid.service.TaskCompleter import com.todoroo.astrid.service.TaskCreator import com.todoroo.astrid.ui.CheckableImageView import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.Strings.isNullOrEmpty @@ -57,11 +59,11 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks @Inject lateinit var activity: Activity @Inject lateinit var taskCompleter: TaskCompleter @Inject lateinit var localBroadcastManager: LocalBroadcastManager - @Inject lateinit var googleTaskDao: GoogleTaskDaoBlocking + @Inject lateinit var googleTaskDao: GoogleTaskDao @Inject lateinit var toaster: Toaster @Inject lateinit var taskCreator: TaskCreator - @Inject lateinit var caldavDao: CaldavDaoBlocking - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var caldavDao: CaldavDao + @Inject lateinit var taskDao: TaskDao @Inject lateinit var locale: Locale @Inject lateinit var checkBoxProvider: CheckBoxProvider @Inject lateinit var chipProvider: ChipProvider @@ -77,9 +79,7 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks outState.putParcelableArrayList(EXTRA_NEW_SUBTASKS, newSubtasks) } - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState) + override suspend fun createView(savedInstanceState: Bundle?) { viewModel = ViewModelProvider(this).get(TaskListViewModel::class.java) if (savedInstanceState != null) { for (task in savedInstanceState.getParcelableArrayList(EXTRA_NEW_SUBTASKS)!!) { @@ -96,7 +96,6 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks viewModel.observe(this, Observer { list: List? -> recyclerAdapter.submitList(list) }) recyclerView!!.adapter = recyclerAdapter } - return view } override val layout: Int @@ -162,8 +161,10 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks override fun onResume() { super.onResume() localBroadcastManager.registerRefreshReceiver(refreshReceiver) - googleTask = googleTaskDao.getByTaskId(task.id) - updateUI() + lifecycleScope.launch { + googleTask = googleTaskDao.getByTaskId(task.id) + updateUI() + } } override fun onPause() { @@ -254,8 +255,10 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks } override fun toggleSubtask(taskId: Long, collapsed: Boolean) { - taskDao.setCollapsed(taskId, collapsed) - localBroadcastManager.broadcastRefresh() + lifecycleScope.launch { + taskDao.setCollapsed(taskId, collapsed) + localBroadcastManager.broadcastRefresh() + } } override fun complete(task: Task, completed: Boolean) { diff --git a/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt b/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt index c427f016b..62fe64c7f 100644 --- a/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt +++ b/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt @@ -8,8 +8,10 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope import butterknife.ButterKnife import com.todoroo.astrid.data.Task +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.tasks.R @@ -30,9 +32,16 @@ abstract class TaskEditControlFragment : Fragment() { content.setOnClickListener { onRowClick() } } ButterKnife.bind(this, view) + + lifecycleScope.launch { + createView(savedInstanceState) + } + return view } + protected abstract suspend fun createView(savedInstanceState: Bundle?) + override fun onAttach(activity: Activity) { super.onAttach(activity) val args = requireArguments()