adding some initial task related code

This commit is contained in:
tibbi 2022-02-20 19:16:22 +01:00
parent 565c0439a3
commit 6113ab55b8
6 changed files with 85 additions and 7 deletions

View file

@ -13,13 +13,15 @@ import com.simplemobiletools.calendar.pro.helpers.Converters
import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID
import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao
import com.simplemobiletools.calendar.pro.interfaces.EventsDao
import com.simplemobiletools.calendar.pro.interfaces.TasksDao
import com.simplemobiletools.calendar.pro.interfaces.WidgetsDao
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.calendar.pro.models.Task
import com.simplemobiletools.calendar.pro.models.Widget
import java.util.concurrent.Executors
@Database(entities = [Event::class, EventType::class, Widget::class], version = 5)
@Database(entities = [Event::class, EventType::class, Widget::class, Task::class], version = 6)
@TypeConverters(Converters::class)
abstract class EventsDatabase : RoomDatabase() {
@ -29,6 +31,8 @@ abstract class EventsDatabase : RoomDatabase() {
abstract fun WidgetsDao(): WidgetsDao
abstract fun TasksDao(): TasksDao
companion object {
private var db: EventsDatabase? = null
@ -47,6 +51,7 @@ abstract class EventsDatabase : RoomDatabase() {
.addMigrations(MIGRATION_2_3)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.build()
db!!.openHelper.setWriteAheadLoggingEnabled(true)
}
@ -103,5 +108,14 @@ abstract class EventsDatabase : RoomDatabase() {
}
}
}
private val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `start_ts` INTEGER NOT NULL, `title` TEXT NOT NULL, `description` TEXT NOT NULL, `import_id` TEXT NOT NULL, `time_zone` TEXT NOT NULL, `flags` INTEGER NOT NULL, `event_type` INTEGER NOT NULL, `last_updated` INTEGER NOT NULL, `source` TEXT NOT NULL, `color` INTEGER NOT NULL)")
execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_id` ON `tasks` (`id`)")
}
}
}
}
}

View file

@ -32,6 +32,7 @@ import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao
import com.simplemobiletools.calendar.pro.interfaces.EventsDao
import com.simplemobiletools.calendar.pro.interfaces.TasksDao
import com.simplemobiletools.calendar.pro.interfaces.WidgetsDao
import com.simplemobiletools.calendar.pro.models.*
import com.simplemobiletools.calendar.pro.receivers.CalDAVSyncReceiver
@ -48,6 +49,7 @@ val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.eventsDB: EventsDao get() = EventsDatabase.getInstance(applicationContext).EventsDao()
val Context.eventTypesDB: EventTypesDao get() = EventsDatabase.getInstance(applicationContext).EventTypesDao()
val Context.widgetsDB: WidgetsDao get() = EventsDatabase.getInstance(applicationContext).WidgetsDao()
val Context.tasksDB: TasksDao get() = EventsDatabase.getInstance(applicationContext).TasksDao()
val Context.eventsHelper: EventsHelper get() = EventsHelper(this)
val Context.calDAVHelper: CalDAVHelper get() = CalDAVHelper(this)

View file

@ -113,7 +113,7 @@ const val REPEAT_ORDER_WEEKDAY = 4 // i.e. every 4th sunday
// special event flags
const val FLAG_ALL_DAY = 1
const val FLAG_IS_PAST_EVENT = 2
const val FLAG_IS_IN_PAST = 2
const val FLAG_MISSING_YEAR = 4
// constants related to ICS file exporting / importing

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.calendar.pro.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.calendar.pro.models.Task
@Dao
interface TasksDao {
@Query("SELECT * FROM tasks")
fun getAllTasks(): List<Task>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(task: Task): Long
}

View file

@ -39,7 +39,7 @@ data class Event(
@ColumnInfo(name = "last_updated") var lastUpdated: Long = 0L,
@ColumnInfo(name = "source") var source: String = SOURCE_SIMPLE_CALENDAR,
@ColumnInfo(name = "availability") var availability: Int = 0,
var color: Int = 0,
@ColumnInfo(name = "color") var color: Int = 0
) : Serializable {
companion object {
@ -48,8 +48,7 @@ data class Event(
fun addIntervalTime(original: Event) {
val oldStart = Formatter.getDateTimeFromTS(startTS)
val newStart: DateTime
newStart = when (repeatInterval) {
val newStart = when (repeatInterval) {
DAY -> oldStart.plusDays(1)
else -> {
when {
@ -192,9 +191,9 @@ data class Event(
}
var isPastEvent: Boolean
get() = flags and FLAG_IS_PAST_EVENT != 0
get() = flags and FLAG_IS_IN_PAST != 0
set(isPastEvent) {
flags = flags.addBitIf(isPastEvent, FLAG_IS_PAST_EVENT)
flags = flags.addBitIf(isPastEvent, FLAG_IS_IN_PAST)
}
fun getTimeZoneString(): String {

View file

@ -0,0 +1,47 @@
package com.simplemobiletools.calendar.pro.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.calendar.pro.extensions.seconds
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.commons.extensions.addBitIf
import java.io.Serializable
@Entity(tableName = "tasks", indices = [(Index(value = ["id"], unique = true))])
data class Task(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "start_ts") var startTS: Long = 0L,
@ColumnInfo(name = "title") var title: String = "",
@ColumnInfo(name = "description") var description: String = "",
@ColumnInfo(name = "import_id") var importId: String = "",
@ColumnInfo(name = "time_zone") var timeZone: String = "",
@ColumnInfo(name = "flags") var flags: Int = 0,
@ColumnInfo(name = "event_type") var eventType: Long = REGULAR_EVENT_TYPE_ID,
@ColumnInfo(name = "last_updated") var lastUpdated: Long = 0L,
@ColumnInfo(name = "source") var source: String = SOURCE_SIMPLE_CALENDAR,
@ColumnInfo(name = "color") var color: Int = 0
) : Serializable {
companion object {
private const val serialVersionUID = -32456795132345616L
}
fun getIsAllDay() = flags and FLAG_ALL_DAY != 0
// properly return the start time of all-day tasks as midnight
fun getTaskStartTS(): Long {
return if (getIsAllDay()) {
Formatter.getDateTimeFromTS(startTS).withTime(0, 0, 0, 0).seconds()
} else {
startTS
}
}
var isPastTask: Boolean
get() = flags and FLAG_IS_IN_PAST != 0
set(isPastTask) {
flags = flags.addBitIf(isPastTask, FLAG_IS_IN_PAST)
}
}