Untitled

 avatar
unknown
plain_text
5 months ago
3.1 kB
3
Indexable
package com.example.todolistapplicationtwo


class TodoApplicationRepository(context: Context)
    : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

    companion object {
        private const val DATABASE_NAME = "todoList.db"
        private const val DATABASE_VERSION = 1

        private const val TABLE_TODO = "Todo"
        private const val COLUMN_ID = "id"
        private const val COLUMN_TITLE = "title"
        private const val COLUMN_DESCRIPTION = "description"
        private const val COLUMN_PRIORITY = "priority"
        private const val COLUMN_IS_DONE = "is_done"
    }

    override fun onCreate(db: SQLiteDatabase?) {
        val createTableQuery = """
            CREATE TABLE $TABLE_TODO (
                $COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT,
                $COLUMN_TITLE TEXT NOT NULL,
                $COLUMN_DESCRIPTION TEXT,
                $COLUMN_PRIORITY TEXT NOT NULL,
                $COLUMN_IS_DONE INTEGER DEFAULT 0
            )
        """.trimIndent()
        db?.execSQL(createTableQuery)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        db?.execSQL("DROP TABLE IF EXISTS $TABLE_TODO")
        onCreate(db)
    }

    fun addTodo(todo: TodoItem): Long {
        val db = writableDatabase
        val values = ContentValues().apply {
            put(COLUMN_TITLE, todo.title)
            put(COLUMN_DESCRIPTION, todo.description)
            put(COLUMN_PRIORITY, todo.priority)
            put(COLUMN_IS_DONE, if (todo.isDone) 1 else 0)
        }
        val id = db.insert(TABLE_TODO, null, values)
        db.close()
        return id
    }

    fun getAllTodos(): List<TodoItem> {
        val todoList = mutableListOf<TodoItem>()
        val db = readableDatabase
        val cursor = db.query(TABLE_TODO, null, null, null, null, null, null)

        if (cursor.moveToFirst()) {
            do {
                val id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))
                val title = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TITLE))
                val description = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DESCRIPTION))
                val priority = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PRIORITY))
                val isDone = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_DONE)) == 1

                todoList.add(TodoItem(id, title, description, priority, isDone))
            } while (cursor.moveToNext())
        }
        cursor.close()
        db.close()
        return todoList
    }

    fun updateTodoStatus(id: Int, isDone: Boolean): Int {
        val db = writableDatabase
        val values = ContentValues().apply {
            put(COLUMN_IS_DONE, if (isDone) 1 else 0)
        }
        val rowsAffected = db.update(TABLE_TODO, values, "$COLUMN_ID=?", arrayOf(id.toString()))
        db.close()
        return rowsAffected
    }

    fun deleteTodo(id: Int): Int {
        val db = writableDatabase
        val rowsDeleted = db.delete(TABLE_TODO, "$COLUMN_ID=?", arrayOf(id.toString()))
        db.close()
        return rowsDeleted
    }
}
Editor is loading...
Leave a Comment