-
Notifications
You must be signed in to change notification settings - Fork 156
/
alarm_helper.dart
74 lines (63 loc) · 1.92 KB
/
alarm_helper.dart
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
import 'package:flutter_alarm_clock/app/data/models/alarm_info.dart';
import 'package:sqflite/sqflite.dart';
final String tableAlarm = 'alarm';
final String columnId = 'id';
final String columnTitle = 'title';
final String columnDateTime = 'alarmDateTime';
final String columnPending = 'isPending';
final String columnColorIndex = 'gradientColorIndex';
class AlarmHelper {
static Database? _database;
static AlarmHelper? _alarmHelper;
AlarmHelper._createInstance();
factory AlarmHelper() {
if (_alarmHelper == null) {
_alarmHelper = AlarmHelper._createInstance();
}
return _alarmHelper!;
}
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database!;
}
Future<Database> initializeDatabase() async {
var dir = await getDatabasesPath();
var path = dir + "alarm.db";
var database = await openDatabase(
path,
version: 1,
onCreate: (db, version) {
db.execute('''
create table $tableAlarm (
$columnId integer primary key autoincrement,
$columnTitle text not null,
$columnDateTime text not null,
$columnPending integer,
$columnColorIndex integer)
''');
},
);
return database;
}
void insertAlarm(AlarmInfo alarmInfo) async {
var db = await this.database;
var result = await db.insert(tableAlarm, alarmInfo.toMap());
print('result : $result');
}
Future<List<AlarmInfo>> getAlarms() async {
List<AlarmInfo> _alarms = [];
var db = await this.database;
var result = await db.query(tableAlarm);
result.forEach((element) {
var alarmInfo = AlarmInfo.fromMap(element);
_alarms.add(alarmInfo);
});
return _alarms;
}
Future<int> delete(int? id) async {
var db = await this.database;
return await db.delete(tableAlarm, where: '$columnId = ?', whereArgs: [id]);
}
}