import 'dart:developer'; import 'package:hub/shared/helpers/base_storage.dart'; import 'package:hub/shared/helpers/database_helper.dart'; import 'package:sqflite/sqflite.dart'; class LicenseDelegate implements BaseStorage { LicenseDelegate._(); static final LicenseDelegate instance = LicenseDelegate._(); static const String tableLicense = 'license'; static String get createTableQuery => ''' CREATE TABLE $tableLicense ( key TEXT UNIQUE, display TEXT, expirationDate TEXT, startDate TEXT, quantity TEXT ); '''; @override Future init() async { await DatabaseStorage.instance.init(); } @override Future get(String key) async { var response = await DatabaseStorage.database.query(tableLicense, where: 'key = ?', whereArgs: [key]); if (response.isEmpty) { return null; } return response.first['value'].toString(); } @override Future set(String key, T value) async { log('setLicense($key, $value)'); value as Map; await DatabaseStorage.database.insert( tableLicense, { 'key': key, 'display': value['display'], 'expirationDate': value['expirationDate'], 'startDate': value['startDate'], 'quantity': value['quantity'], }, conflictAlgorithm: ConflictAlgorithm.replace); } static Future insertLicenseFoo(final List key, final String display) async { for (var element in key) { DatabaseStorage.database.insert( LicenseDelegate.tableLicense, { 'key': element, 'display': display, 'expirationDate': '', 'startDate': '', 'quantity': '', }, conflictAlgorithm: ConflictAlgorithm.ignore, ); } } @override Future delete(String key) async { await DatabaseStorage.database.delete(tableLicense, where: 'key = ?', whereArgs: [key]); } @override Future clearAll() async { await DatabaseStorage.database.delete(tableLicense); } }