data.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import 'package:path/path.dart';
  2. import 'package:sqflite/sqflite.dart';
  3. import 'dart:math';
  4. class NotesDatabaseService {
  5. String path;
  6. NotesDatabaseService._();
  7. static final NotesDatabaseService db = NotesDatabaseService._();
  8. Database _database;
  9. Future<Database> get database async {
  10. if (_database != null) return _database;
  11. // if _database is null we instantiate it
  12. _database = await init();
  13. return _database;
  14. }
  15. init() async {
  16. String path = await getDatabasesPath();
  17. path = join(path, 'notes.db');
  18. print("Entered path $path");
  19. return await openDatabase(path, version: 1,
  20. onCreate: (Database db, int version) async {
  21. await db.execute(
  22. 'CREATE TABLE Notes (_id INTEGER PRIMARY KEY, content TEXT, date TEXT);');
  23. await db.execute(
  24. 'CREATE TABLE Notifications (_id INTEGER PRIMARY KEY, note INTEGER, date1 TEXT);');
  25. print('New table created at $path');
  26. });
  27. }
  28. Future<List<NotesModel>> getNotesFromDB() async {
  29. final db = await database;
  30. List<NotesModel> notesList = [];
  31. List<Map> maps =
  32. await db.query('Notes', columns: ['_id', 'content', 'date']);
  33. if (maps.length > 0) {
  34. maps.forEach((map) {
  35. notesList.add(NotesModel.fromMap(map));
  36. });
  37. }
  38. return notesList;
  39. }
  40. Future<List<NotificationModel>> getNotificationsFromDB() async {
  41. final db = await database;
  42. List<NotificationModel> notesList = [];
  43. List<Map> maps =
  44. await db.query('Notifications', columns: ['_id', 'note', 'date1']);
  45. if (maps.length > 0) {
  46. maps.forEach((map) {
  47. notesList.add(NotificationModel.fromMap(map));
  48. });
  49. }
  50. return notesList;
  51. }
  52. Future<List<NotificationModel>> getNotificationForNote(int id) async {
  53. final db = await database;
  54. List<NotificationModel> notesList = [];
  55. List<Map> maps = await db.query('Notifications',
  56. columns: ['_id', 'note', 'date1'], where: "note = ?", whereArgs: [id]);
  57. if (maps.length > 0) {
  58. maps.forEach((map) {
  59. notesList.add(NotificationModel.fromMap(map));
  60. });
  61. }
  62. return notesList;
  63. }
  64. updateNoteInDB(NotesModel updatedNote) async {
  65. final db = await database;
  66. await db.update('Notes', updatedNote.toMap(),
  67. where: '_id = ?', whereArgs: [updatedNote.id]);
  68. }
  69. updateNotificationInDB(NotificationModel updatedNotification) async {
  70. final db = await database;
  71. await db.update('Notifications', updatedNotification.toMap(),
  72. where: '_id = ?', whereArgs: [updatedNotification.id]);
  73. }
  74. Future<bool> deleteNoteInDB(NotesModel noteToDelete) async {
  75. final db = await database;
  76. final res = await db
  77. .delete('Notes', where: '_id = ?', whereArgs: [noteToDelete.id]);
  78. print('Note deleted');
  79. return true;
  80. }
  81. Future<bool> deleteNotificationInDB(NotificationModel noteToDelete) async {
  82. final db = await database;
  83. final res = await db.delete('Notifications',
  84. where: '_id = ?', whereArgs: [noteToDelete.id]);
  85. print('Notification deleted');
  86. return true;
  87. }
  88. Future<NotesModel> addNoteInDB(NotesModel newNote) async {
  89. final db = await database;
  90. int id = await db.transaction((transaction) {
  91. return transaction.rawInsert(
  92. 'INSERT into Notes(content, date) VALUES ( "${newNote.content}", "${newNote.date.toIso8601String()}");');
  93. });
  94. newNote.id = id;
  95. print('Note added: ${newNote.content}');
  96. return newNote;
  97. }
  98. Future<NotificationModel> addNotificationInDB(
  99. NotificationModel newNote) async {
  100. final db = await database;
  101. int id = await db.transaction((transaction) {
  102. return transaction.rawInsert(
  103. 'INSERT into Notifications(note, date1) VALUES ( "${newNote.note}", "${newNote.date1}");');
  104. });
  105. newNote.id = id;
  106. print('Notification added: ${newNote.date1}');
  107. return newNote;
  108. }
  109. }
  110. class NotesModel {
  111. int id;
  112. String content;
  113. DateTime date;
  114. NotesModel({this.id, this.content, this.date});
  115. NotesModel.fromMap(Map<String, dynamic> map) {
  116. this.id = map['_id'];
  117. this.content = map['content'];
  118. this.date = DateTime.parse(map['date']);
  119. }
  120. Map<String, dynamic> toMap() {
  121. return <String, dynamic>{
  122. '_id': this.id,
  123. 'content': this.content,
  124. 'date': this.date.toIso8601String(),
  125. };
  126. }
  127. String getTitleFromModel(int maxSymbols) {
  128. List<String> a = content.split("\n");
  129. return a.first.substring(0, min(a.first.length, maxSymbols));
  130. }
  131. String getShortDesc(int maxSymbols) {
  132. List<String> a = content.split("\n");
  133. if (a.length > 1) {
  134. return a[1].substring(0, min(a[1].length, maxSymbols));
  135. } else
  136. return "";
  137. }
  138. }
  139. class NotificationModel {
  140. int id;
  141. int note;
  142. String date1;
  143. NotificationModel({this.id, this.note, this.date1});
  144. NotificationModel.fromMap(Map<String, dynamic> map) {
  145. this.id = map['_id'];
  146. print(map['note']);
  147. this.note = map['note'];
  148. this.date1 = map['date1'];
  149. }
  150. Map<String, dynamic> toMap() {
  151. return <String, dynamic>{
  152. '_id': this.id,
  153. 'note': this.note,
  154. 'date1': this.date1,
  155. };
  156. }
  157. makeData(DateTime dateTime) {
  158. date1 = dateTime.toIso8601String();
  159. }
  160. String getString() {
  161. DateTime time = DateTime.parse(date1);
  162. String month = time.month.toString();
  163. if (month.length == 1) month = "0" + month;
  164. return "Notify ${time.day}.$month at ${time.hour}:${time.minute}";
  165. }
  166. }