2024-05-28 21:59:44 +02:00
|
|
|
import 'package:cpd/database/todo_interface.dart';
|
2024-05-19 22:08:04 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-12 00:08:54 +02:00
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'package:cpd/database/db.dart';
|
|
|
|
import 'package:cpd/database/habit.dart';
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
class TodoDB implements ToDoInterface {
|
2024-05-12 00:08:54 +02:00
|
|
|
final tableName = 'todos';
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-12 00:08:54 +02:00
|
|
|
Future<void> createTable(Database database) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
await database.execute("""CREATE TABLE IF NOT EXISTS $tableName (
|
2024-06-09 17:45:17 +02:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
isComplete INTEGER NOT NULL,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
subtitle TEXT,
|
|
|
|
iconCodePoint INTEGER NOT NULL,
|
|
|
|
iconFontFamily TEXT NOT NULL
|
|
|
|
)""");
|
|
|
|
} catch (e) {
|
2024-06-08 14:22:18 +02:00
|
|
|
return Future.error(e);
|
2024-05-28 21:59:44 +02:00
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-24 00:01:08 +02:00
|
|
|
Future<int> create({required String title, required String subtitle, required IconData icon}) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
return await database.rawInsert(
|
2024-06-09 17:45:17 +02:00
|
|
|
'''INSERT INTO $tableName (isComplete, title, subtitle, iconCodePoint, iconFontFamily)
|
|
|
|
VALUES (?, ?, ?, ?, ?)''',
|
|
|
|
[0, title, subtitle, icon.codePoint, icon.fontFamily],
|
2024-05-28 21:59:44 +02:00
|
|
|
);
|
2024-06-09 17:45:17 +02:00
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
2024-05-24 00:01:08 +02:00
|
|
|
}
|
2024-05-19 22:08:04 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-24 00:01:08 +02:00
|
|
|
Future<List<Habit>> fetchAll() async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
final todos = await database.query(tableName);
|
2024-06-09 17:45:17 +02:00
|
|
|
return todos.map((todo) => Habit.fromSqfliteDatabase(todo)).toList();
|
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-19 22:08:04 +02:00
|
|
|
Future<Habit?> fetchById(int id) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
final todo = await database.query(tableName, where: 'id = ?', whereArgs: [id]);
|
2024-06-09 17:45:17 +02:00
|
|
|
if (todo.isNotEmpty) {
|
|
|
|
return Habit.fromSqfliteDatabase(todo.first);
|
2024-05-28 21:59:44 +02:00
|
|
|
} else {
|
2024-06-09 17:45:17 +02:00
|
|
|
return null;
|
2024-05-28 21:59:44 +02:00
|
|
|
}
|
2024-06-09 17:45:17 +02:00
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
2024-05-19 22:08:04 +02:00
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-24 00:01:08 +02:00
|
|
|
Future<int> update({required int id, required String title, required String subtitle, required IconData icon}) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
2024-06-09 17:45:17 +02:00
|
|
|
return await database.update(tableName, {
|
2024-05-28 21:59:44 +02:00
|
|
|
'title': title,
|
|
|
|
'subtitle': subtitle,
|
2024-06-09 17:45:17 +02:00
|
|
|
'iconCodePoint': icon.codePoint,
|
|
|
|
'iconFontFamily': icon.fontFamily,
|
2024-05-28 21:59:44 +02:00
|
|
|
},
|
|
|
|
where: 'id = ?',
|
|
|
|
conflictAlgorithm: ConflictAlgorithm.rollback,
|
|
|
|
whereArgs: [id],
|
|
|
|
);
|
2024-06-09 17:45:17 +02:00
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
2024-05-12 17:56:36 +02:00
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-12 00:08:54 +02:00
|
|
|
Future<void> delete(int id) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
await database.delete(tableName, where: 'id = ?', whereArgs: [id]);
|
2024-06-09 17:45:17 +02:00
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
|
|
|
}
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
2024-05-13 23:31:03 +02:00
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-06-09 17:45:17 +02:00
|
|
|
Future<int?> updateCompletionStatus(int id, bool isCompleted) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
try {
|
|
|
|
final database = await HabitDatabase().database;
|
2024-06-09 17:45:17 +02:00
|
|
|
return await database.update(tableName, {
|
2024-05-30 14:28:21 +02:00
|
|
|
'isComplete': isCompleted ? 1 : 0,
|
2024-05-28 21:59:44 +02:00
|
|
|
},
|
|
|
|
where: 'id = ?',
|
|
|
|
conflictAlgorithm: ConflictAlgorithm.rollback,
|
|
|
|
whereArgs: [id],
|
|
|
|
);
|
2024-06-09 17:45:17 +02:00
|
|
|
} catch (e) {
|
2024-05-28 21:59:44 +02:00
|
|
|
return Future.error(e);
|
|
|
|
}
|
2024-05-13 23:31:03 +02:00
|
|
|
}
|
2024-06-09 17:45:17 +02:00
|
|
|
}
|