-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Todo view page design done - Added Todo Model - Great things coming!
- Loading branch information
Showing
4 changed files
with
184 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
); | ||
} | ||
} |
Oops, something went wrong.