123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import 'package:path/path.dart';
- import 'package:sqflite/sqflite.dart';
- import 'dart:math';
- class NotesDatabaseService {
- String path;
- NotesDatabaseService._();
- static final NotesDatabaseService db = NotesDatabaseService._();
- Database _database;
- Future<Database> get database async {
- if (_database != null) return _database;
- // if _database is null we instantiate it
- _database = await init();
- return _database;
- }
- init() async {
- String path = await getDatabasesPath();
- path = join(path, 'notes.db');
- print("Entered path $path");
- return await openDatabase(path, version: 1,
- onCreate: (Database db, int version) async {
- await db.execute(
- 'CREATE TABLE Notes (_id INTEGER PRIMARY KEY, content TEXT, date TEXT);');
- await db.execute(
- 'CREATE TABLE Notifications (_id INTEGER PRIMARY KEY, note INTEGER, date1 TEXT);');
- print('New table created at $path');
- });
- }
- Future<List<NotesModel>> getNotesFromDB() async {
- final db = await database;
- List<NotesModel> notesList = [];
- List<Map> maps =
- await db.query('Notes', columns: ['_id', 'content', 'date']);
- if (maps.length > 0) {
- maps.forEach((map) {
- notesList.add(NotesModel.fromMap(map));
- });
- }
- return notesList;
- }
- Future<List<NotificationModel>> getNotificationsFromDB() async {
- final db = await database;
- List<NotificationModel> notesList = [];
- List<Map> maps =
- await db.query('Notifications', columns: ['_id', 'note', 'date1']);
- if (maps.length > 0) {
- maps.forEach((map) {
- notesList.add(NotificationModel.fromMap(map));
- });
- }
- return notesList;
- }
- Future<List<NotificationModel>> getNotificationForNote(int id) async {
- final db = await database;
- List<NotificationModel> notesList = [];
- List<Map> maps = await db.query('Notifications',
- columns: ['_id', 'note', 'date1'], where: "note = ?", whereArgs: [id]);
- if (maps.length > 0) {
- maps.forEach((map) {
- notesList.add(NotificationModel.fromMap(map));
- });
- }
- return notesList;
- }
- updateNoteInDB(NotesModel updatedNote) async {
- final db = await database;
- await db.update('Notes', updatedNote.toMap(),
- where: '_id = ?', whereArgs: [updatedNote.id]);
- }
- updateNotificationInDB(NotificationModel updatedNotification) async {
- final db = await database;
- await db.update('Notifications', updatedNotification.toMap(),
- where: '_id = ?', whereArgs: [updatedNotification.id]);
- }
- Future<bool> deleteNoteInDB(NotesModel noteToDelete) async {
- final db = await database;
- final res = await db
- .delete('Notes', where: '_id = ?', whereArgs: [noteToDelete.id]);
- print('Note deleted');
- return true;
- }
- Future<bool> deleteNotificationInDB(NotificationModel noteToDelete) async {
- final db = await database;
- final res = await db.delete('Notifications',
- where: '_id = ?', whereArgs: [noteToDelete.id]);
- print('Notification deleted');
- return true;
- }
- Future<NotesModel> addNoteInDB(NotesModel newNote) async {
- final db = await database;
- int id = await db.transaction((transaction) {
- return transaction.rawInsert(
- 'INSERT into Notes(content, date) VALUES ( "${newNote.content}", "${newNote.date.toIso8601String()}");');
- });
- newNote.id = id;
- print('Note added: ${newNote.content}');
- return newNote;
- }
- Future<NotificationModel> addNotificationInDB(
- NotificationModel newNote) async {
- final db = await database;
- int id = await db.transaction((transaction) {
- return transaction.rawInsert(
- 'INSERT into Notifications(note, date1) VALUES ( "${newNote.note}", "${newNote.date1}");');
- });
- newNote.id = id;
- print('Notification added: ${newNote.date1}');
- return newNote;
- }
- }
- class NotesModel {
- int id;
- String content;
- DateTime date;
- NotesModel({this.id, this.content, this.date});
- NotesModel.fromMap(Map<String, dynamic> map) {
- this.id = map['_id'];
- this.content = map['content'];
- this.date = DateTime.parse(map['date']);
- }
- Map<String, dynamic> toMap() {
- return <String, dynamic>{
- '_id': this.id,
- 'content': this.content,
- 'date': this.date.toIso8601String(),
- };
- }
- String getTitleFromModel(int maxSymbols) {
- List<String> a = content.split("\n");
- return a.first.substring(0, min(a.first.length, maxSymbols));
- }
- String getShortDesc(int maxSymbols) {
- List<String> a = content.split("\n");
- if (a.length > 1) {
- return a[1].substring(0, min(a[1].length, maxSymbols));
- } else
- return "";
- }
- }
- class NotificationModel {
- int id;
- int note;
- String date1;
- NotificationModel({this.id, this.note, this.date1});
- NotificationModel.fromMap(Map<String, dynamic> map) {
- this.id = map['_id'];
- print(map['note']);
- this.note = map['note'];
- this.date1 = map['date1'];
- }
- Map<String, dynamic> toMap() {
- return <String, dynamic>{
- '_id': this.id,
- 'note': this.note,
- 'date1': this.date1,
- };
- }
- makeData(DateTime dateTime) {
- date1 = dateTime.toIso8601String();
- }
- String getString() {
- DateTime time = DateTime.parse(date1);
- String month = time.month.toString();
- if (month.length == 1) month = "0" + month;
- return "Notify ${time.day}.$month at ${time.hour}:${time.minute}";
- }
- }
|