made task creation flow

This commit is contained in:
Kima
2024-05-05 21:11:13 +02:00
parent 7488c9abdd
commit d915200faa
10 changed files with 284 additions and 3 deletions

View File

@@ -7,17 +7,23 @@ import 'package:provider/provider.dart';
class SelfNoteProvider with ChangeNotifier {
late List<SelfNote> _notes;
late List<TodoItem> _todoItems;
late BuildContext _context;
List<SelfNote> get notes => _notes;
List<TodoItem> get todos => _todoItems;
SelfNoteProvider({
List<SelfNote> initialNotes = const [],
List<TodoItem> initialTodoItems = const [],
required BuildContext context,
}) {
_notes = List.castFrom(initialNotes);
_todoItems = List.castFrom(initialTodoItems);
_context = context;
if (_notes.isEmpty) restore();
if (_todoItems.isEmpty) restoreTodo();
}
// restore self notes from db
@@ -38,6 +44,24 @@ class SelfNoteProvider with ChangeNotifier {
}
}
// restore todo items from db
Future<void> restoreTodo() async {
String? userId = Provider.of<UserProvider>(_context, listen: false).id;
// await Provider.of<DatabaseProvider>(_context, listen: false)
// .userStore
// .storeSelfNotes([], userId: userId!);
// load self notes from db
if (userId != null) {
var dbTodo = await Provider.of<DatabaseProvider>(_context, listen: false)
.userQuery
.getTodoItems(userId: userId);
_todoItems = dbTodo;
notifyListeners();
}
}
// fetches fresh data from api (not needed, cuz no api for that)
// Future<void> fetch() async {
// }
@@ -54,4 +78,17 @@ class SelfNoteProvider with ChangeNotifier {
_notes = notes;
notifyListeners();
}
// store todo items in db
Future<void> storeTodo(List<TodoItem> todos) async {
User? user = Provider.of<UserProvider>(_context, listen: false).user;
if (user == null) throw "Cannot store Self Notes for User null";
String userId = user.id;
await Provider.of<DatabaseProvider>(_context, listen: false)
.userStore
.storeSelfTodoItems(todos, userId: userId);
_todoItems = todos;
notifyListeners();
}
}

View File

@@ -84,7 +84,7 @@ const userDataDB = DatabaseStruct("user_data", {
"goal_befores": String,
"goal_pin_dates": String,
// todo and notes
"todo_items": String, "self_notes": String,
"todo_items": String, "self_notes": String, "self_todo": String,
// v5 shit
"roundings": String,
"grade_rarities": String,
@@ -152,7 +152,7 @@ Future<Database> initDB(DatabaseProvider database) async {
"goal_befores": "{}",
"goal_pin_dates": "{}",
// todo and notes
"todo_items": "{}", "self_notes": "[]",
"todo_items": "{}", "self_notes": "[]", "self_todo": "[]",
// v5 shit
"roundings": "{}",
"grade_rarities": "{}",

View File

@@ -317,6 +317,18 @@ class UserDatabaseQuery {
return selfNotes;
}
Future<List<TodoItem>> getTodoItems({required String userId}) async {
List<Map> userData =
await db.query("user_data", where: "id = ?", whereArgs: [userId]);
if (userData.isEmpty) return [];
String? todoItemsJson = userData.elementAt(0)["self_todo"] as String?;
if (todoItemsJson == null) return [];
List<TodoItem> todoItems = (jsonDecode(todoItemsJson) as List)
.map((e) => TodoItem.fromJson(e))
.toList();
return todoItems;
}
// v5
Future<Map<String, String>> getRoundings({required String userId}) async {
List<Map> userData =

View File

@@ -196,6 +196,13 @@ class UserDatabaseStore {
where: "id = ?", whereArgs: [userId]);
}
Future<void> storeSelfTodoItems(List<TodoItem> todoItems,
{required String userId}) async {
String todoItemsJson = jsonEncode(todoItems.map((e) => e.json).toList());
await db.update("user_data", {"self_todo": todoItemsJson},
where: "id = ?", whereArgs: [userId]);
}
// v5
Future<void> storeRoundings(Map<String, String> roundings,
{required String userId}) async {

View File

@@ -33,3 +33,37 @@ class SelfNote {
'note_type': noteType == NoteType.image ? 'image' : 'text',
};
}
class TodoItem {
String id;
String title;
String content;
bool done;
Map? json;
TodoItem({
required this.id,
required this.title,
required this.content,
required this.done,
this.json,
});
factory TodoItem.fromJson(Map json) {
return TodoItem(
id: json['id'],
title: json['title'],
content: json['content'],
done: json['done'],
json: json,
);
}
get toJson => {
'id': id,
'title': title,
'content': content,
'done': done,
};
}