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 dff77b6fe..c93e6b058 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 @@ -8,11 +8,12 @@ import android.view.MenuItem import android.view.WindowManager import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.extensions.config +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.helpers.NEW_EVENT_START_TS -import com.simplemobiletools.calendar.pro.helpers.TASK_ID import com.simplemobiletools.calendar.pro.models.Task import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import kotlinx.android.synthetic.main.activity_task.* import org.joda.time.DateTime import java.util.* @@ -50,7 +51,7 @@ class TaskActivity : SimpleActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.save -> saveCurrentTask() + R.id.save -> saveTask() R.id.delete -> deleteTask() R.id.duplicate -> duplicateTask() else -> return super.onOptionsItemSelected(item) @@ -88,7 +89,31 @@ class TaskActivity : SimpleActivity() { updateActionBarTitle(getString(R.string.new_task)) } - private fun saveCurrentTask() {} + private fun saveTask() { + val newTitle = task_title.value + if (newTitle.isEmpty()) { + toast(R.string.title_empty) + runOnUiThread { + task_title.requestFocus() + } + return + } + + mTask.apply { + startTS = mTaskDateTime.withSecondOfMinute(0).withMillisOfSecond(0).seconds() + title = newTitle + description = task_description.value + flags = mTask.flags.addBitIf(task_all_day.isChecked, FLAG_ALL_DAY) + lastUpdated = System.currentTimeMillis() + } + + ensureBackgroundThread { + TasksHelper(this).insertTask(mTask) { + hideKeyboard() + finish() + } + } + } private fun deleteTask() {} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index 7b61e246a..bb9a11b8b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -17,7 +17,7 @@ class EventsHelper(val context: Context) { private val eventsDB = context.eventsDB private val eventTypesDB = context.eventTypesDB - fun getEventTypes(activity: Activity, showWritableOnly: Boolean, callback: (notes: ArrayList) -> Unit) { + fun getEventTypes(activity: Activity, showWritableOnly: Boolean, callback: (eventTypes: ArrayList) -> Unit) { ensureBackgroundThread { var eventTypes = ArrayList() try { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/TasksHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/TasksHelper.kt new file mode 100644 index 000000000..033b201a9 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/TasksHelper.kt @@ -0,0 +1,35 @@ +package com.simplemobiletools.calendar.pro.helpers + +import android.app.Activity +import android.content.Context +import com.simplemobiletools.calendar.pro.extensions.eventTypesDB +import com.simplemobiletools.calendar.pro.extensions.tasksDB +import com.simplemobiletools.calendar.pro.extensions.updateWidgets +import com.simplemobiletools.calendar.pro.models.EventType +import com.simplemobiletools.calendar.pro.models.Task +import com.simplemobiletools.commons.helpers.ensureBackgroundThread + +class TasksHelper(val context: Context) { + private val tasksDB = context.tasksDB + private val eventTypesDB = context.eventTypesDB + + fun getEventTypes(activity: Activity, callback: (notes: ArrayList) -> Unit) { + ensureBackgroundThread { + var eventTypes = ArrayList() + try { + eventTypes = eventTypesDB.getEventTypes().toMutableList() as ArrayList + } catch (ignored: Exception) { + } + + activity.runOnUiThread { + callback(eventTypes) + } + } + } + + fun insertTask(task: Task, callback: () -> Unit) { + tasksDB.insertOrUpdate(task) + context.updateWidgets() + callback() + } +}