save token and settings also in sqlite db
This commit is contained in:
@ -8,7 +8,7 @@ import '../log/log.dart';
|
||||
class Db {
|
||||
late Database _db;
|
||||
|
||||
void init() async {
|
||||
Future<void> init() async {
|
||||
if (kIsWeb) {
|
||||
Log.i("Database on web is not supported");
|
||||
return;
|
||||
@ -27,13 +27,20 @@ class Db {
|
||||
_db = await openDatabase(
|
||||
dbpath,
|
||||
onCreate: (db, version) {
|
||||
return db.execute(
|
||||
'CREATE TABLE previews(id INTEGER PRIMARY KEY, thumbnail BLOB)',
|
||||
final batch = db.batch();
|
||||
batch.execute(
|
||||
'CREATE TABLE previews(id INTEGER PRIMARY KEY, thumbnail BLOB);',
|
||||
);
|
||||
batch.execute(
|
||||
'CREATE TABLE settings(domain TEXT, token TEXT, videopath TEXT, tilewidth INTEGER);',
|
||||
);
|
||||
batch.insert("settings",
|
||||
{"domain": "", "token": "", "videopath": "", "tilewidth": 0});
|
||||
return batch.commit();
|
||||
},
|
||||
// Set the version. This executes the onCreate function and provides a
|
||||
// path to perform database upgrades and downgrades.
|
||||
version: 1,
|
||||
version: 2,
|
||||
);
|
||||
}
|
||||
|
||||
@ -46,10 +53,16 @@ class Db {
|
||||
|
||||
/// get db size in bytes
|
||||
Future<int> getDbSize() async {
|
||||
final int cnt = (await Db().db().rawQuery("pragma page_count;"))[0]
|
||||
["page_count"] as int;
|
||||
final batch = _db.batch();
|
||||
batch.rawQuery("pragma page_count;");
|
||||
batch.rawQuery("pragma page_size;");
|
||||
final result = (await batch.commit(noResult: false));
|
||||
print(result);
|
||||
|
||||
final int cnt =
|
||||
((result[0] as List<Map<String, dynamic>>)[0]["page_count"] as int);
|
||||
final int pagesize =
|
||||
(await Db().db().rawQuery("pragma page_size;"))[0]["page_size"] as int;
|
||||
(result[1] as List<Map<String, dynamic>>)[0]["page_size"] as int;
|
||||
return cnt * pagesize;
|
||||
}
|
||||
|
||||
|
51
lib/db/settings_db.dart
Normal file
51
lib/db/settings_db.dart
Normal file
@ -0,0 +1,51 @@
|
||||
import 'database.dart';
|
||||
|
||||
class SettingsT {
|
||||
String domain;
|
||||
String token;
|
||||
String videopath;
|
||||
int tilewidth;
|
||||
|
||||
SettingsT(this.domain, this.token, this.videopath, this.tilewidth);
|
||||
}
|
||||
|
||||
class SettingsDB {
|
||||
static final SettingsDB _instance = SettingsDB._();
|
||||
|
||||
SettingsT _settings = SettingsT("", "", "", 0);
|
||||
bool _initialized = false;
|
||||
|
||||
Future<SettingsT> getSettings() async {
|
||||
if (!_initialized) {
|
||||
final result = (await Db().db().query("settings",
|
||||
where: "1",
|
||||
columns: ["domain", "token", "videopath", "tilewidth"]))
|
||||
.first;
|
||||
|
||||
_settings = SettingsT(
|
||||
result["domain"] as String,
|
||||
result["token"] as String,
|
||||
result["videopath"] as String,
|
||||
result["tilewidth"] as int);
|
||||
}
|
||||
return _settings;
|
||||
}
|
||||
|
||||
Future<void> setSettings(SettingsT settings) async {
|
||||
await Db().db().update(
|
||||
"settings",
|
||||
{
|
||||
"domain": settings.domain,
|
||||
"token": settings.token,
|
||||
"videopath": settings.videopath,
|
||||
"tilewidth": settings.tilewidth
|
||||
},
|
||||
where: "1");
|
||||
}
|
||||
|
||||
static SettingsDB getInstance() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
SettingsDB._();
|
||||
}
|
Reference in New Issue
Block a user