-
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.
- Now one can store a todo item in the db - Displaying todos in the agenda homescreen tab
- Loading branch information
Showing
12 changed files
with
191 additions
and
106 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,4 +1,5 @@ | ||
# Miscellaneous | ||
*.db | ||
*.class | ||
*.lock | ||
*.log | ||
|
Binary file not shown.
Binary file not shown.
Empty file.
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:academia/exports/barrel.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
class TodoController extends GetxController { | ||
RxList<Todo> todos = <Todo>[].obs; | ||
|
||
@override | ||
|
||
/// Opens the database | ||
/// Initializes the [todos] if they exist in the database | ||
void onInit() { | ||
// Read all todos | ||
TodoModelHelper().queryAll().then( | ||
(value) => todos.value = value.map((e) => Todo.fromJson(e)).toList()); | ||
super.onInit(); | ||
debugPrint("[+] Todo initialized successfully"); | ||
} | ||
|
||
/// Adds a todo specified by [newTodo] to the local db | ||
Future<void> createTodo(Todo newTodo) async { | ||
await TodoModelHelper().create(newTodo.toJson()); | ||
TodoModelHelper().queryAll().then( | ||
(value) => todos.value = value.map((e) => Todo.fromJson(e)).toList()); | ||
} | ||
|
||
/// Deletes both from db and [todos] a todo specified by [id] | ||
Future<void> deleteTodo(int id) async { | ||
todos.removeWhere((element) => element.id == id); | ||
await TodoModelHelper().delete(id); | ||
update(); | ||
} | ||
|
||
/// Deletes both from db and [todos] a todo specified by [id] | ||
Future<void> updateTodo(Todo update) async { | ||
todos.removeWhere((element) => element.id == update.id); | ||
await TodoModelHelper().update(update.toJson()); | ||
TodoModelHelper().queryAll().then( | ||
(value) => todos.value = value.map((e) => Todo.fromJson(e)).toList()); | ||
} | ||
} |
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
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,71 +1,72 @@ | ||
import 'package:academia/storage/storage.dart'; | ||
import 'todo_model.dart'; | ||
import 'package:academia/exports/barrel.dart'; | ||
import 'package:sqflite/sqflite.dart'; | ||
|
||
class TodoModelHelper { | ||
class TodoModelHelper implements DatabaseOperations { | ||
static final TodoModelHelper _instance = TodoModelHelper._internal(); | ||
|
||
factory TodoModelHelper() { | ||
return _instance; | ||
} | ||
|
||
TodoModelHelper._internal(); | ||
|
||
// Register the table schema | ||
void registerModel(Database db) { | ||
db.execute(''' | ||
CREATE TABLE IF NOT EXISTS todos ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
DatabaseHelper().registerModel('todos', ''' | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL, | ||
date TEXT NOT NULL, | ||
notificationTime TEXT NOT NULL, | ||
notificationFrequency TEXT NOT NULL, | ||
notification_time TEXT NOT NULL, | ||
notification_frequency TEXT NOT NULL, | ||
color TEXT NOT NULL, | ||
description TEXT, | ||
complete INTEGER NOT NULL, | ||
dateAdded TEXT NOT NULL | ||
) | ||
date_added TEXT NOT NULL, | ||
date_completed TEXT | ||
'''); | ||
|
||
return _instance; | ||
} | ||
|
||
// Insert a new Todo | ||
Future<int> insert(Todo todo) async { | ||
TodoModelHelper._internal(); | ||
|
||
@override | ||
|
||
/// Inserts a new todo on the local database specified by [data] | ||
Future<int> create(Map<String, dynamic> data) async { | ||
final db = await DatabaseHelper().database; | ||
return await db.insert( | ||
final id = await db.insert( | ||
'todos', | ||
todo.toJson(), | ||
data, | ||
conflictAlgorithm: ConflictAlgorithm.replace, | ||
); | ||
|
||
debugPrint("[+] Todo written successfully"); | ||
return id; | ||
} | ||
|
||
// Get all Todos | ||
Future<List<Todo>> getAllTodos() async { | ||
final db = await DatabaseHelper().database; | ||
final List<Map<String, dynamic>> maps = await db.query('todos'); | ||
@override | ||
|
||
return List.generate(maps.length, (i) { | ||
return Todo.fromJson(maps[i]); | ||
}); | ||
/// Queries for all todos stored in the local database | ||
Future<List<Map<String, dynamic>>> queryAll() async { | ||
final db = await DatabaseHelper().database; | ||
final users = await db.query('todos'); | ||
return users; | ||
} | ||
|
||
// Update a Todo | ||
Future<int> update(Todo todo) async { | ||
@override | ||
Future<int> update(Map<String, dynamic> data) async { | ||
final db = await DatabaseHelper().database; | ||
return await db.update( | ||
'todos', | ||
todo.toJson(), | ||
where: 'id =?', | ||
whereArgs: [todo.id], | ||
); | ||
return await db | ||
.update('todos', data, where: 'id =?', whereArgs: [data['id']]); | ||
} | ||
|
||
// Delete a Todo | ||
@override | ||
|
||
/// Deletes a todo specified with id [id] | ||
Future<int> delete(int id) async { | ||
final db = await DatabaseHelper().database; | ||
return await db.delete( | ||
'todos', | ||
where: 'id =?', | ||
whereArgs: [id], | ||
); | ||
return await db.delete('todos', where: 'id =?', whereArgs: [id]); | ||
} | ||
|
||
@override | ||
Future<void> truncate() async { | ||
final db = await DatabaseHelper().database; | ||
|
||
await db.execute('DROP TABLE todos'); | ||
} | ||
} |
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 +1,3 @@ | ||
export 'todo_home_screen.dart'; | ||
export 'models/models.dart'; | ||
export 'controllers/todo_controller.dart'; |
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
Oops, something went wrong.