prepare for multiple apps
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:weblibre/data/database/extensions/database_table_size.dart';
|
||||
import 'package:weblibre/features/user/data/database/daos/cache.drift.dart';
|
||||
import 'package:weblibre/features/user/data/database/database.dart';
|
||||
import 'package:weblibre/features/user/data/database/definitions.drift.dart';
|
||||
|
||||
@DriftAccessor()
|
||||
class CacheDao extends DatabaseAccessor<UserDatabase> with $CacheDaoMixin {
|
||||
CacheDao(super.attachedDatabase);
|
||||
|
||||
SingleSelectable<double> getIconCacheSize() {
|
||||
return db.tableSize(db.iconCache);
|
||||
}
|
||||
|
||||
Future<int> clearIconCache() {
|
||||
return db.iconCache.deleteAll();
|
||||
}
|
||||
|
||||
SingleOrNullSelectable<Uint8List?> getCachedIcon(String origin) {
|
||||
final query = selectOnly(db.iconCache)
|
||||
..addColumns([db.iconCache.iconData])
|
||||
..where(db.iconCache.origin.equals(origin));
|
||||
|
||||
return query.map((row) => row.read(db.iconCache.iconData));
|
||||
}
|
||||
|
||||
Future<int> cacheIcon(String origin, Uint8List bytes) {
|
||||
return db.iconCache.insertOne(
|
||||
IconCacheCompanion.insert(
|
||||
origin: origin,
|
||||
iconData: bytes,
|
||||
fetchDate: DateTime.now(),
|
||||
),
|
||||
onConflict: DoUpdate(
|
||||
(old) => IconCacheCompanion(
|
||||
iconData: Value(bytes),
|
||||
fetchDate: Value(DateTime.now()),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// dart format width=80
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:weblibre/features/user/data/database/database.dart' as i1;
|
||||
|
||||
mixin $CacheDaoMixin on i0.DatabaseAccessor<i1.UserDatabase> {
|
||||
CacheDaoManager get managers => CacheDaoManager(this);
|
||||
}
|
||||
|
||||
class CacheDaoManager {
|
||||
final $CacheDaoMixin _db;
|
||||
CacheDaoManager(this._db);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:weblibre/features/user/data/database/database.dart';
|
||||
import 'package:weblibre/features/user/data/database/definitions.drift.dart';
|
||||
|
||||
@DriftAccessor()
|
||||
class OnboardingDao extends DatabaseAccessor<UserDatabase> {
|
||||
OnboardingDao(super.attachedDatabase);
|
||||
|
||||
SingleOrNullSelectable<int?> getLastRevision() {
|
||||
final maxRevision = db.onboarding.revision.max();
|
||||
|
||||
final query = selectOnly(db.onboarding)..addColumns([maxRevision]);
|
||||
|
||||
return query.map((row) => row.read(maxRevision));
|
||||
}
|
||||
|
||||
Future<void> pushRevision(int revision, DateTime completionDate) {
|
||||
return db.onboarding.insertOne(
|
||||
OnboardingCompanion.insert(
|
||||
revision: revision,
|
||||
completionDate: completionDate,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// dart format width=80
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:weblibre/features/user/data/database/database.dart' as i1;
|
||||
|
||||
mixin $OnboardingDaoMixin on i0.DatabaseAccessor<i1.UserDatabase> {
|
||||
OnboardingDaoManager get managers => OnboardingDaoManager(this);
|
||||
}
|
||||
|
||||
class OnboardingDaoManager {
|
||||
final $OnboardingDaoMixin _db;
|
||||
OnboardingDaoManager(this._db);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:weblibre/features/user/data/database/daos/setting.drift.dart';
|
||||
import 'package:weblibre/features/user/data/database/database.dart';
|
||||
import 'package:weblibre/features/user/data/database/definitions.drift.dart';
|
||||
|
||||
@DriftAccessor()
|
||||
class SettingDao extends DatabaseAccessor<UserDatabase> with $SettingDaoMixin {
|
||||
SettingDao(super.attachedDatabase);
|
||||
|
||||
Future<int> updateSetting(String key, String? partitionKey, Object? value) {
|
||||
final normalizedValue = (value is Iterable) ? jsonEncode(value) : value;
|
||||
|
||||
final driftvalue = normalizedValue.mapNotNull(
|
||||
(normalizedValue) => DriftAny(normalizedValue),
|
||||
);
|
||||
|
||||
return db.setting.insertOne(
|
||||
SettingCompanion.insert(
|
||||
key: key,
|
||||
partitionKey: Value(partitionKey),
|
||||
value: Value(driftvalue),
|
||||
),
|
||||
onConflict: DoUpdate(
|
||||
(old) => SettingCompanion(
|
||||
partitionKey: Value(partitionKey),
|
||||
value: Value(driftvalue),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Selectable<MapEntry<String, DriftAny?>> getAllSettingsOfPartitionKey(
|
||||
String? partitionKey,
|
||||
) {
|
||||
final query = db.setting.select()
|
||||
..where((r) => r.partitionKey.equalsNullable(partitionKey));
|
||||
|
||||
return query.map((row) => MapEntry(row.key, row.value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// dart format width=80
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:weblibre/features/user/data/database/database.dart' as i1;
|
||||
|
||||
mixin $SettingDaoMixin on i0.DatabaseAccessor<i1.UserDatabase> {
|
||||
SettingDaoManager get managers => SettingDaoManager(this);
|
||||
}
|
||||
|
||||
class SettingDaoManager {
|
||||
final $SettingDaoMixin _db;
|
||||
SettingDaoManager(this._db);
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:weblibre/features/user/data/database/daos/toolbar_button_config.drift.dart';
|
||||
import 'package:weblibre/features/user/data/database/database.dart';
|
||||
import 'package:weblibre/features/user/data/database/definitions.drift.dart';
|
||||
|
||||
@DriftAccessor()
|
||||
class ToolbarButtonConfigDao extends DatabaseAccessor<UserDatabase>
|
||||
with $ToolbarButtonConfigDaoMixin {
|
||||
ToolbarButtonConfigDao(super.attachedDatabase);
|
||||
|
||||
Selectable<ToolbarButtonConfig> selectAll() =>
|
||||
db.toolbarButtonConfigs.select()
|
||||
..orderBy([(t) => OrderingTerm.asc(t.orderKey)]);
|
||||
|
||||
Stream<List<ToolbarButtonConfig>> watchAll() => selectAll().watch();
|
||||
|
||||
Future<List<ToolbarButtonConfig>> getAll() => selectAll().get();
|
||||
|
||||
Future<void> upsert(ToolbarButtonConfig config) =>
|
||||
into(db.toolbarButtonConfigs).insertOnConflictUpdate(config);
|
||||
|
||||
Future<void> assignOrderKey(String buttonId, {required String orderKey}) =>
|
||||
(update(db.toolbarButtonConfigs)
|
||||
..where((t) => t.buttonId.equals(buttonId)))
|
||||
.write(ToolbarButtonConfigsCompanion(orderKey: Value(orderKey)));
|
||||
|
||||
Future<void> assignVisibility(String buttonId, {required bool visible}) =>
|
||||
(update(db.toolbarButtonConfigs)
|
||||
..where((t) => t.buttonId.equals(buttonId)))
|
||||
.write(ToolbarButtonConfigsCompanion(isVisible: Value(visible)));
|
||||
|
||||
Future<void> assignFallback(String buttonId, String? fallbackId) =>
|
||||
(update(db.toolbarButtonConfigs)
|
||||
..where((t) => t.buttonId.equals(buttonId)))
|
||||
.write(ToolbarButtonConfigsCompanion(fallbackId: Value(fallbackId)));
|
||||
|
||||
Future<void> replaceAll(List<ToolbarButtonConfig> configs) =>
|
||||
transaction(() async {
|
||||
await delete(db.toolbarButtonConfigs).go();
|
||||
await _insertWithDeferredFallbacks(configs);
|
||||
});
|
||||
|
||||
Future<void> seedMissing(
|
||||
List<({String buttonId, bool defaultVisible, String? defaultFallback})>
|
||||
defaults,
|
||||
) async {
|
||||
final existing = await getAll();
|
||||
final existingIds = {for (final r in existing) r.buttonId};
|
||||
|
||||
final missing = defaults
|
||||
.where((d) => !existingIds.contains(d.buttonId))
|
||||
.toList();
|
||||
if (missing.isEmpty) return;
|
||||
|
||||
await transaction(() async {
|
||||
final inserted = <ToolbarButtonConfig>[];
|
||||
for (final def in missing) {
|
||||
final orderKey = await generateTrailingOrderKey().getSingle();
|
||||
inserted.add(
|
||||
ToolbarButtonConfig(
|
||||
buttonId: def.buttonId,
|
||||
orderKey: orderKey,
|
||||
isVisible: def.defaultVisible,
|
||||
fallbackId: def.defaultFallback,
|
||||
),
|
||||
);
|
||||
await into(db.toolbarButtonConfigs).insert(
|
||||
ToolbarButtonConfig(
|
||||
buttonId: def.buttonId,
|
||||
orderKey: orderKey,
|
||||
isVisible: def.defaultVisible,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await _assignFallbacks(inserted);
|
||||
});
|
||||
}
|
||||
|
||||
SingleSelectable<String> generateLeadingOrderKey({int bucket = 0}) =>
|
||||
db.definitionsDrift.toolbarLeadingOrderKey(bucket: bucket);
|
||||
|
||||
SingleSelectable<String> generateTrailingOrderKey({int bucket = 0}) =>
|
||||
db.definitionsDrift.toolbarTrailingOrderKey(bucket: bucket);
|
||||
|
||||
SingleOrNullSelectable<String> generateOrderKeyAfterButtonId(
|
||||
String buttonId,
|
||||
) => db.definitionsDrift.toolbarOrderKeyAfterButton(buttonId: buttonId);
|
||||
|
||||
SingleSelectable<String> generateOrderKeyBeforeButtonId(String buttonId) =>
|
||||
db.definitionsDrift.toolbarOrderKeyBeforeButton(buttonId: buttonId);
|
||||
|
||||
Future<void> _insertWithDeferredFallbacks(
|
||||
List<ToolbarButtonConfig> configs,
|
||||
) async {
|
||||
for (final config in configs) {
|
||||
await into(db.toolbarButtonConfigs).insert(
|
||||
ToolbarButtonConfig(
|
||||
buttonId: config.buttonId,
|
||||
orderKey: config.orderKey,
|
||||
isVisible: config.isVisible,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await _assignFallbacks(configs);
|
||||
}
|
||||
|
||||
Future<void> _assignFallbacks(List<ToolbarButtonConfig> configs) async {
|
||||
for (final config in configs) {
|
||||
if (config.fallbackId == null) continue;
|
||||
await assignFallback(config.buttonId, config.fallbackId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// dart format width=80
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:weblibre/features/user/data/database/database.dart' as i1;
|
||||
|
||||
mixin $ToolbarButtonConfigDaoMixin on i0.DatabaseAccessor<i1.UserDatabase> {
|
||||
ToolbarButtonConfigDaoManager get managers =>
|
||||
ToolbarButtonConfigDaoManager(this);
|
||||
}
|
||||
|
||||
class ToolbarButtonConfigDaoManager {
|
||||
final $ToolbarButtonConfigDaoMixin _db;
|
||||
ToolbarButtonConfigDaoManager(this._db);
|
||||
}
|
||||
Reference in New Issue
Block a user