-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_file.tar.gz
128 lines (111 loc) · 10 KB
/
storage_file.tar.gz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
lib/database_helper.dart 0000644 0001750 0001750 00000006041 14215733464 014261 0 ustar miki miki import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'models/task.dart';
import 'models/todo.dart';
class DatabaseHelper {
static void init() {
sqfliteFfiInit();
}
Future<Database> database() async {
const String dbName = 'todo.db';
const int version = 1;
// ignore: prefer_function_declarations_over_variables
OnDatabaseCreateFn onCreate = (db, version) async {
await db.execute("CREATE TABLE tasks(id INTEGER PRIMARY KEY, title TEXT, description TEXT)");
await db.execute("CREATE TABLE todo(id INTEGER PRIMARY KEY, taskId INTEGER, title TEXT, isDone INTEGER)");
};
if (Platform.isAndroid || Platform.isIOS) {
return openDatabase(
join(await getDatabasesPath(), dbName),
version: version,
onCreate: onCreate,
);
} else {
return databaseFactoryFfi.openDatabase(
join(await databaseFactoryFfi.getDatabasesPath(), dbName),
options: OpenDatabaseOptions(
version: version,
onCreate: onCreate,
),
);
}
}
Future<int> insertTask(Task task) async {
int taskId = 0;
Database _db = await database();
await _db.insert('tasks', task.toMap(), conflictAlgorithm: ConflictAlgorithm.replace).then((value) {
taskId = value;
});
return taskId;
}
Future<void> updateTaskTitle(int id, String title) async {
Database _db = await database();
await _db.rawUpdate("UPDATE tasks SET title = '$title' WHERE id = '$id'");
}
Future<void> updateTaskDescription(int id, String description) async {
Database _db = await database();
await _db.rawUpdate("UPDATE tasks SET description = '$description' WHERE id = '$id'");
}
Future<void> insertTodo(Todo todo) async {
Database _db = await database();
await _db.insert('todo', todo.toMap(), conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<List<Task>> getTasks() async {
Database _db = await database();
List<Map<String, dynamic>> taskMap = await _db.query('tasks');
return List.generate(taskMap.length, (index) {
return Task(id: taskMap[index]['id'], title: taskMap[index]['title'], description: taskMap[index]['description']);
});
}
Future<List<Todo>> getTodo(int taskId) async {
Database _db = await database();
List<Map<String, dynamic>> todoMap = await _db.rawQuery("SELECT * FROM todo WHERE taskId = $taskId");
return List.generate(todoMap.length, (index) {
return Todo(id: todoMap[index]['id'], title: todoMap[index]['title'], taskId: todoMap[index]['taskId'], isDone: todoMap[index]['isDone']);
});
}
Future<void> updateTodoDone(int id, int isDone) async {
Database _db = await database();
await _db.rawUpdate("UPDATE todo SET isDone = '$isDone' WHERE id = '$id'");
}
Future<void> deleteTask(int id) async {
Database _db = await database();
await _db.rawDelete("DELETE FROM tasks WHERE id = '$id'");
await _db.rawDelete("DELETE FROM todo WHERE taskId = '$id'");
}
} lib/models/task.dart 0000644 0001750 0001750 00000000437 14215117774 013406 0 ustar miki miki class Task {
final int? id;
final String title;
final String description;
Task({
this.id,
required this.title,
this.description = ''});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
};
}
} lib/models/todo.dart 0000644 0001750 0001750 00000000522 14215117467 013403 0 ustar miki miki class Todo {
final int? id;
final int taskId;
final String title;
final int isDone;
Todo({
this.id,
required this.taskId,
required this.title,
required this.isDone});
Map<String, dynamic> toMap() {
return {
'id': id,
'taskId': taskId,
'title': title,
'isDone': isDone,
};
}
}