Skip to content

Commit

Permalink
chore: completed todo view page
Browse files Browse the repository at this point in the history
- Todo view page design done
- Added Todo Model
- Great things coming!
  • Loading branch information
IamMuuo committed May 5, 2024
1 parent 59a46be commit 5843e53
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 102 deletions.
47 changes: 28 additions & 19 deletions lib/tools/todo/models/todo_model.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
class Todo {
final int? id;
final String title;
final String name;
final DateTime date;
final DateTime remindDate;
final String notificationTime;
final String notificationFrequency;
final String color;
final String pictureAttachment;
final String note;
final String description;
final bool complete;
final DateTime dateAdded;

Todo({
this.id,
required this.title,
required this.name,
required this.date,
required this.remindDate,
required this.notificationTime,
required this.notificationFrequency,
required this.color,
required this.pictureAttachment,
required this.note,
});
required this.description,
required this.complete,
DateTime? dateAdded,
}) : dateAdded = dateAdded ?? DateTime.now();

factory Todo.fromJson(Map<String, dynamic> json) {
return Todo(
id: json['id'],
title: json['title'],
id: json['id'] as int?,
name: json['name'] as String,
date: DateTime.parse(json['date']),
remindDate: DateTime.parse(json['remindDate']),
color: json['color'],
pictureAttachment: json['pictureAttachment'],
note: json['note'],
notificationTime: json['notificationTime'],
notificationFrequency: json['notificationFrequency'] as String,
color: json['color'] as String,
description: json['description'] as String,
complete: json['complete'] as bool,
dateAdded: DateTime.parse(json['dateAdded']),
);
}

Map<String, dynamic> toJson() {
return {
'title': title,
'id': id,
'name': name,
'date': date.toIso8601String(),
'remindDate': remindDate.toIso8601String(),
'notificationTime': notificationTime,
'notificationFrequency': notificationFrequency,
'color': color,
'pictureAttachment': pictureAttachment,
'note': note,
'description': description,
'complete': complete,
'dateAdded': dateAdded.toIso8601String(),
};
}
}
70 changes: 43 additions & 27 deletions lib/tools/todo/models/todo_model_helper.dart
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
import 'package:academia/storage/storage.dart';
import 'todo_model.dart';
import 'package:sqflite/sqflite.dart';

class TodoModelHelper {
static final TodoModelHelper _instance = TodoModelHelper._internal();

factory TodoModelHelper() {
DatabaseHelper().registerModel('todos', '''
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
date TEXT NOT NULL,
remindDate TEXT NOT NULL,
color TEXT NOT NULL,
pictureAttachment TEXT,
note TEXT,
''');

return _instance;
}

TodoModelHelper._internal();

Future<int> create(Map<String, dynamic> data) async {
// Register the table schema
void registerModel(Database db) {
db.execute('''
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
date TEXT NOT NULL,
notificationTime TEXT NOT NULL,
notificationFrequency TEXT NOT NULL,
color TEXT NOT NULL,
description TEXT,
complete INTEGER NOT NULL,
dateAdded TEXT NOT NULL
)
''');
}

// Insert a new Todo
Future<int> insert(Todo todo) async {
final db = await DatabaseHelper().database;
final id = await db.insert(
return await db.insert(
'todos',
data,
todo.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);

return id;
}

Future<List<Map<String, dynamic>>> queryAll() async {
// Get all Todos
Future<List<Todo>> getAllTodos() async {
final db = await DatabaseHelper().database;
final todos = await db.query('todos');
return todos;
}
final List<Map<String, dynamic>> maps = await db.query('todos');

Future<int> delete(int id) async {
final db = await DatabaseHelper().database;
return await db.delete('todos', where: 'id =?', whereArgs: [id]);
return List.generate(maps.length, (i) {
return Todo.fromJson(maps[i]);
});
}

Future<int> update(Map<String, dynamic> data) async {
// Update a Todo
Future<int> update(Todo todo) async {
final db = await DatabaseHelper().database;
return await db
.update('todos', data, where: 'id =?', whereArgs: [data['id']]);
return await db.update(
'todos',
todo.toJson(),
where: 'id =?',
whereArgs: [todo.id],
);
}

Future<void> truncate() async {
// Delete a Todo
Future<int> delete(int id) async {
final db = await DatabaseHelper().database;
await db.execute('DELETE FROM todos');
return await db.delete(
'todos',
where: 'id =?',
whereArgs: [id],
);
}
}
Loading

0 comments on commit 5843e53

Please sign in to comment.