setup task editing

This commit is contained in:
tibbi 2022-02-24 16:45:33 +01:00
parent baeb0e2c44
commit c3e1f4b5d8
3 changed files with 39 additions and 4 deletions

View file

@ -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)

View file

@ -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?

View file

@ -14,6 +14,6 @@
<item
android:id="@+id/duplicate"
android:icon="@drawable/ic_copy_vector"
android:title="@string/duplicate_event"
android:title="@string/duplicate_task"
app:showAsAction="always" />
</menu>