flutter-freaccess-hub/lib/shared/helpers/license_helper.dart

81 lines
2.1 KiB
Dart

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<void> init() async {
await DatabaseStorage.instance.init();
}
@override
Future<String?> 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<void> set<T>(String key, T value) async {
log('setLicense($key, $value)');
value as Map<String, dynamic>;
await DatabaseStorage.database.insert(
tableLicense,
{
'key': key,
'display': value['display'],
'expirationDate': value['expirationDate'],
'startDate': value['startDate'],
'quantity': value['quantity'],
},
conflictAlgorithm: ConflictAlgorithm.replace);
}
static Future<void> insertLicenseFoo(final List<String> 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<void> delete(String key) async {
await DatabaseStorage.database.delete(tableLicense, where: 'key = ?', whereArgs: [key]);
}
@override
Future<void> clearAll() async {
await DatabaseStorage.database.delete(tableLicense);
}
}