data.dart 6.1 KB

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