// Dart imports: import 'dart:io'; // Package imports: import 'package:sqflite/sqflite.dart'; // Project imports: import 'package:openlib/services/files.dart'; class MyBook { final String id; final String title; final String? author; final String? thumbnail; final String link; final String? publisher; final String? info; final String? description; final String? format; MyBook( {required this.id, required this.title, required this.author, required this.thumbnail, required this.link, required this.publisher, required this.info, required this.format, required this.description}); Map toMap() { return { 'id': id, 'title': title, 'author': author, 'thumbnail': thumbnail, 'link': link, 'publisher': publisher, 'info': info, 'format': format, 'description': description }; } @override String toString() { return 'MyBook{id: $id,title: $title,author: $author,thumbnail: $thumbnail,link: $link,publisher: $publisher,info: $info,format: $format,description:$description}'; } } class MyLibraryDb { Database dbInstance; String tableName = 'mybooks'; MyLibraryDb({required this.dbInstance}); Future insert(MyBook book) async { await dbInstance.insert( tableName, book.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } Future delete(String id) async { await dbInstance.delete( tableName, where: 'id = ?', whereArgs: [id], ); } Future getId(String id) async { List> data = await dbInstance.query(tableName, where: 'id = ?', whereArgs: [id]); List book = listMapToMyBook(data); if (book.isNotEmpty) { return book.first; } return null; } Future checkIdExists(String id) async { List> data = await dbInstance.query(tableName, where: 'id = ?', whereArgs: [id]); List book = listMapToMyBook(data); if (book.isNotEmpty) { return true; } return false; } Future> getAll() async { final List> maps = await dbInstance.query(tableName); return listMapToMyBook(maps); } List listMapToMyBook(List> maps) { List myBookList = List.generate(maps.length, (i) { return MyBook( id: maps[i]['id'], title: maps[i]['title'], author: maps[i]['author'], thumbnail: maps[i]['thumbnail'], link: maps[i]['link'], publisher: maps[i]['publisher'], info: maps[i]['info'], format: maps[i]['format'], description: maps[i]['description']); }); return myBookList.reversed.toList(); } Future saveBookState(String fileName, String position) async { await dbInstance.insert( 'bookposition', {'fileName': fileName, 'position': position}, conflictAlgorithm: ConflictAlgorithm.replace, ); } Future deleteBookState(String fileName) async { await dbInstance.delete( 'bookposition', where: 'fileName = ?', whereArgs: [fileName], ); } Future getBookState(String fileName) async { List> data = await dbInstance .query('bookposition', where: 'fileName = ?', whereArgs: [fileName]); List dataList = List.generate(data.length, (i) { return {'fileName': data[i]['fileName'], 'position': data[i]['position']}; }); if (dataList.isNotEmpty) { return dataList[0]['position']; } else { return null; } } Future savePreference(String name, bool value) async { int boolInt = value ? 1 : 0; await dbInstance.insert( 'preferences', {'name': name, 'value': boolInt}, conflictAlgorithm: ConflictAlgorithm.replace, ); } Future getPreference(String name) async { List> data = await dbInstance .query('preferences', where: 'name = ?', whereArgs: [name]); List dataList = List.generate(data.length, (i) { return {'name': data[i]['name'], 'value': data[i]['value']}; }); if (dataList.isNotEmpty) { return dataList[0]['value'] == 0 ? false : true; } else { return false; } } Future setBrowserOptions(String name, String value) async { await dbInstance.insert( 'browserOptions', {'name': name, 'value': value}, conflictAlgorithm: ConflictAlgorithm.replace, ); } Future getBrowserOptions(String name) async { List> data = await dbInstance .query('browserOptions', where: 'name = ?', whereArgs: [name]); List dataList = List.generate(data.length, (i) { return {'name': data[i]['name'], 'value': data[i]['value']}; }); if (dataList.isNotEmpty) { return dataList[0]['value']; } else { return ""; } } }