diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt index 122f3fd47..4812a50ae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt @@ -10,13 +10,19 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.dialogs.SelectEventTypeDialog import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.eventTypesDB +import com.simplemobiletools.calendar.pro.extensions.eventsDB import com.simplemobiletools.calendar.pro.extensions.seconds import com.simplemobiletools.calendar.pro.helpers.* import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import kotlinx.android.synthetic.main.activity_event.* import kotlinx.android.synthetic.main.activity_task.* +import kotlinx.android.synthetic.main.activity_task.event_type +import kotlinx.android.synthetic.main.activity_task.event_type_color +import kotlinx.android.synthetic.main.activity_task.event_type_holder +import kotlinx.android.synthetic.main.activity_task.event_type_image import org.joda.time.DateTime import java.util.* @@ -38,7 +44,20 @@ class TaskActivity : SimpleActivity() { mDialogTheme = getDialogTheme() updateColors() val taskId = intent.getLongExtra(EVENT_ID, 0L) - gotTask(savedInstanceState, null) + ensureBackgroundThread { + val task = eventsDB.getTaskWithId(taskId) + if (taskId != 0L && task == null) { + hideKeyboard() + finish() + return@ensureBackgroundThread + } + + runOnUiThread { + if (!isDestroyed && !isFinishing) { + gotTask(savedInstanceState, task) + } + } + } } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -97,8 +116,14 @@ class TaskActivity : SimpleActivity() { private fun gotTask(savedInstanceState: Bundle?, task: Event?) { if (task != null) { mTask = task + if (savedInstanceState == null) { + setupEditTask() + } } else { mTask = Event(null) + if (savedInstanceState == null) { + setupNewTask() + } } mEventTypeId = if (config.defaultEventTypeId == -1L) config.lastUsedLocalEventTypeId else config.defaultEventTypeId @@ -112,8 +137,6 @@ class TaskActivity : SimpleActivity() { task_time.setOnClickListener { setupTime() } event_type_holder.setOnClickListener { showEventTypeDialog() } - setupNewTask() - if (savedInstanceState == null) { updateEventType() updateDateText() @@ -121,6 +144,15 @@ class TaskActivity : SimpleActivity() { } } + private fun setupEditTask() { + mTaskDateTime = Formatter.getDateTimeFromTS(mTask.startTS) + window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN) + updateActionBarTitle(getString(R.string.edit_task)) + + task_title.setText(mTask.title) + task_description.setText(mTask.description) + } + private fun setupNewTask() { val startTS = intent.getLongExtra(NEW_EVENT_START_TS, 0L) val dateTime = Formatter.getDateTimeFromTS(startTS) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index c0af83cce..dc689b7fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -18,6 +18,9 @@ interface EventsDao { @Query("SELECT * FROM events WHERE id = :id AND type = $TYPE_EVENT") fun getEventWithId(id: Long): Event? + @Query("SELECT * FROM events WHERE id = :id AND type = $TYPE_TASK") + fun getTaskWithId(id: Long): Event? + @Query("SELECT * FROM events WHERE import_id = :importId AND type = $TYPE_EVENT") fun getEventWithImportId(importId: String): Event? diff --git a/app/src/main/res/menu/menu_task.xml b/app/src/main/res/menu/menu_task.xml index 3c1c02e21..8bea7b952 100644 --- a/app/src/main/res/menu/menu_task.xml +++ b/app/src/main/res/menu/menu_task.xml @@ -14,6 +14,6 @@