prepare for multiple apps
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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:typed_data';
|
||||
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/features/geckoview/domain/providers.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'cache.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class CacheRepository extends _$CacheRepository {
|
||||
Future<void> clearCache() {
|
||||
return ref.read(userDatabaseProvider).cacheDao.clearIconCache();
|
||||
}
|
||||
|
||||
Future<void> cacheIcon(Uri url, Uint8List bytes) {
|
||||
return ref.read(userDatabaseProvider).cacheDao.cacheIcon(url.origin, bytes);
|
||||
}
|
||||
|
||||
Future<Uint8List?> getCachedIcon(String origin) {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.cacheDao
|
||||
.getCachedIcon(origin)
|
||||
.getSingleOrNull();
|
||||
}
|
||||
|
||||
@override
|
||||
void build() {
|
||||
final eventService = ref.watch(eventServiceProvider);
|
||||
|
||||
final db = ref.watch(userDatabaseProvider);
|
||||
|
||||
final sub = eventService.iconUpdateEvents.listen(
|
||||
(event) async {
|
||||
if (Uri.tryParse(event.url) case final Uri url) {
|
||||
await db.cacheDao.cacheIcon(url.origin, event.bytes);
|
||||
}
|
||||
},
|
||||
onError: (Object error, StackTrace stackTrace) {
|
||||
logger.e(
|
||||
'Error in icon update events',
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
ref.onDispose(() async {
|
||||
await sub.cancel();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'cache.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(CacheRepository)
|
||||
final cacheRepositoryProvider = CacheRepositoryProvider._();
|
||||
|
||||
final class CacheRepositoryProvider
|
||||
extends $NotifierProvider<CacheRepository, void> {
|
||||
CacheRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'cacheRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$cacheRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
CacheRepository create() => CacheRepository();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(void value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<void>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$cacheRepositoryHash() => r'e3cd7461aefe9e034a663169cd81ab7f69c2640e';
|
||||
|
||||
abstract class _$CacheRepository extends $Notifier<void> {
|
||||
void build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<void, void>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<void, void>,
|
||||
void,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
/*
|
||||
* 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:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:riverpod/riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/features/user/data/models/engine_settings.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'engine_settings.g.dart';
|
||||
|
||||
typedef UpdateEngineSettingsFunc =
|
||||
EngineSettings Function(EngineSettings currentSettings);
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class EngineSettingsRepository extends _$EngineSettingsRepository {
|
||||
final _partitionKey = 'engine';
|
||||
|
||||
EngineSettings _deserializeSettings(
|
||||
List<MapEntry<String, DriftAny?>> entries,
|
||||
) {
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
final settings = Map.fromEntries(entries);
|
||||
|
||||
return EngineSettings.fromJson({
|
||||
'incognitoMode': settings['incognitoMode']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'javascriptEnabled': settings['javascriptEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'trackingProtectionPolicy': settings['trackingProtectionPolicy']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'httpsOnlyMode': settings['httpsOnlyMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'globalPrivacyControlEnabled': settings['globalPrivacyControlEnabled']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'cookieBannerHandlingMode': settings['cookieBannerHandlingMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'cookieBannerHandlingModePrivateBrowsing':
|
||||
settings['cookieBannerHandlingModePrivateBrowsing']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'cookieBannerHandlingGlobalRules':
|
||||
settings['cookieBannerHandlingGlobalRules']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'cookieBannerHandlingGlobalRulesSubFrames':
|
||||
settings['cookieBannerHandlingGlobalRulesSubFrames']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'webContentIsolationStrategy': settings['webContentIsolationStrategy']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping),
|
||||
'userAgent': settings['userAgent']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'queryParameterStripping': settings['queryParameterStripping']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'bounceTrackingProtectionMode': settings['bounceTrackingProtectionMode']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping),
|
||||
'enterpriseRootsEnabled': settings['enterpriseRootsEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'addonCollection': settings['addonCollection']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'dohSettingsMode': settings['dohSettingsMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'dohProviderUrl': settings['dohProviderUrl']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'dohDefaultProviderUrl': settings['dohDefaultProviderUrl']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'dohExceptionsList': settings['dohExceptionsList']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping)
|
||||
.mapNotNull(jsonDecode),
|
||||
'fingerprintingProtectionOverrides':
|
||||
settings['fingerprintingProtectionOverrides']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'enablePdfJs': settings['enablePdfJs']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'locales': settings['locales']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping)
|
||||
.mapNotNull(jsonDecode),
|
||||
// Custom Tracking Protection
|
||||
'blockCookies': settings['blockCookies']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'customCookiePolicy': settings['customCookiePolicy']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'blockTrackingContent': settings['blockTrackingContent']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'trackingContentScope': settings['trackingContentScope']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'blockCryptominers': settings['blockCryptominers']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'blockFingerprinters': settings['blockFingerprinters']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'blockRedirectTrackers': settings['blockRedirectTrackers']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'blockSuspectedFingerprinters': settings['blockSuspectedFingerprinters']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'suspectedFingerprintersScope': settings['suspectedFingerprintersScope']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping),
|
||||
'allowListBaseline': settings['allowListBaseline']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'allowListConvenience': settings['allowListConvenience']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
// Web Content Settings
|
||||
'webFontsEnabled': settings['webFontsEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'automaticFontSizeAdjustment': settings['automaticFontSizeAdjustment']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'fontSizeFactor': settings['fontSizeFactor']?.readAs(
|
||||
DriftSqlType.double,
|
||||
db.typeMapping,
|
||||
),
|
||||
'fontInflationEnabled': settings['fontInflationEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'displayDensityOverride': settings['displayDensityOverride']?.readAs(
|
||||
DriftSqlType.double,
|
||||
db.typeMapping,
|
||||
),
|
||||
'screenWidthOverride': settings['screenWidthOverride']?.readAs(
|
||||
DriftSqlType.int,
|
||||
db.typeMapping,
|
||||
),
|
||||
'screenHeightOverride': settings['screenHeightOverride']?.readAs(
|
||||
DriftSqlType.int,
|
||||
db.typeMapping,
|
||||
),
|
||||
'inputAutoZoomEnabled': settings['inputAutoZoomEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
// Process Isolation Settings
|
||||
'fissionEnabled': settings['fissionEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'isolatedProcessEnabled': settings['isolatedProcessEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'appZygoteProcessEnabled': settings['appZygoteProcessEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'extensionsWebAPIEnabled': settings['extensionsWebAPIEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
// LNA Settings
|
||||
'lnaBlocking': settings['lnaBlocking']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'lnaBlockTrackers': settings['lnaBlockTrackers']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'lnaEnabled': settings['lnaEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateSettings(
|
||||
UpdateEngineSettingsFunc updateWithCurrent,
|
||||
) async {
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
|
||||
final current = await fetchSettings();
|
||||
|
||||
final oldJson = current.toJson();
|
||||
final newJson = updateWithCurrent(current).toJson();
|
||||
|
||||
return db.transaction(() async {
|
||||
for (final MapEntry(:key, :value) in newJson.entries) {
|
||||
if (oldJson[key] != value) {
|
||||
await db.settingDao.updateSetting(key, _partitionKey, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<EngineSettings> fetchSettings() {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.get()
|
||||
.then(_deserializeSettings);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<EngineSettings> build() {
|
||||
final db = ref.watch(userDatabaseProvider);
|
||||
|
||||
return db.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.watch()
|
||||
.map((entries) {
|
||||
return _deserializeSettings(entries);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod()
|
||||
EngineSettings engineSettingsWithDefaults(Ref ref) {
|
||||
return ref.watch(
|
||||
engineSettingsRepositoryProvider.select(
|
||||
(value) => value.value ?? EngineSettings.withDefaults(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'engine_settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(EngineSettingsRepository)
|
||||
final engineSettingsRepositoryProvider = EngineSettingsRepositoryProvider._();
|
||||
|
||||
final class EngineSettingsRepositoryProvider
|
||||
extends $StreamNotifierProvider<EngineSettingsRepository, EngineSettings> {
|
||||
EngineSettingsRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'engineSettingsRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$engineSettingsRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
EngineSettingsRepository create() => EngineSettingsRepository();
|
||||
}
|
||||
|
||||
String _$engineSettingsRepositoryHash() =>
|
||||
r'4abe41cfeba8e39484683033f11c54be644fa2b2';
|
||||
|
||||
abstract class _$EngineSettingsRepository
|
||||
extends $StreamNotifier<EngineSettings> {
|
||||
Stream<EngineSettings> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<EngineSettings>, EngineSettings>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<EngineSettings>, EngineSettings>,
|
||||
AsyncValue<EngineSettings>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
|
||||
@ProviderFor(engineSettingsWithDefaults)
|
||||
final engineSettingsWithDefaultsProvider =
|
||||
EngineSettingsWithDefaultsProvider._();
|
||||
|
||||
final class EngineSettingsWithDefaultsProvider
|
||||
extends $FunctionalProvider<EngineSettings, EngineSettings, EngineSettings>
|
||||
with $Provider<EngineSettings> {
|
||||
EngineSettingsWithDefaultsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'engineSettingsWithDefaultsProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$engineSettingsWithDefaultsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<EngineSettings> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
EngineSettings create(Ref ref) {
|
||||
return engineSettingsWithDefaults(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(EngineSettings value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<EngineSettings>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$engineSettingsWithDefaultsHash() =>
|
||||
r'd47fa79c0ad87a2357de58133585b4f6b097b068';
|
||||
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* 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:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:riverpod/riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/features/user/data/models/general_settings.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'general_settings.g.dart';
|
||||
|
||||
typedef UpdateGeneralSettingsFunc =
|
||||
GeneralSettings Function(GeneralSettings currentSettings);
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class GeneralSettingsRepository extends _$GeneralSettingsRepository {
|
||||
final _partitionKey = 'general';
|
||||
|
||||
GeneralSettings _deserializeSettings(
|
||||
List<MapEntry<String, DriftAny?>> entries,
|
||||
) {
|
||||
final settings = Map.fromEntries(entries);
|
||||
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
|
||||
return GeneralSettings.fromJson({
|
||||
'themeMode': settings['themeMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'uiScaleFactor': settings['uiScaleFactor']?.readAs(
|
||||
DriftSqlType.double,
|
||||
db.typeMapping,
|
||||
),
|
||||
'disableAnimations': settings['disableAnimations']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'showModalBarrier': settings['showModalBarrier']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'enableReadability': settings['enableReadability']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'enforceReadability': settings['enforceReadability']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'deleteBrowsingDataOnQuit': settings['deleteBrowsingDataOnQuit']
|
||||
?.readAs(DriftSqlType.string, db.typeMapping)
|
||||
.mapNotNull(jsonDecode),
|
||||
'defaultSearchProvider': settings['defaultSearchProvider']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'defaultSearchSuggestionsProvider':
|
||||
settings['defaultSearchSuggestionsProvider']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'createChildTabsOption': settings['createChildTabsOption']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'enableLocalAiFeatures': settings['enableLocalAiFeatures']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'showContainerUi': settings['showContainerUi']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'showIsolatedTabUi': settings['showIsolatedTabUi']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'defaultCreateTabType': settings['defaultCreateTabType']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'newTabPosition': settings['newTabPosition']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabIntentOpenSetting': settings['tabIntentOpenSetting']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'autoHideTabBar': settings['autoHideTabBar']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabBarSwipeAction': settings['tabBarSwipeAction']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'historyAutoCleanInterval': settings['historyAutoCleanInterval']?.readAs(
|
||||
DriftSqlType.int,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabViewBottomSheet': settings['tabViewBottomSheet']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabBarShowContextualBar': settings['tabBarShowContextualBar']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabBarShowQuickTabSwitcherBar': settings['tabBarShowQuickTabSwitcherBar']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'tabBarPosition': settings['tabBarPosition']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabBarLayout': settings['tabBarLayout']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'quickTabSwitcherMode': settings['quickTabSwitcherMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'pullToRefreshEnabled': settings['pullToRefreshEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'useExternalDownloadManager': settings['useExternalDownloadManager']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'doubleBackCloseTab': settings['doubleBackCloseTab']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'unassignedTabsAutoCleanInterval':
|
||||
settings['unassignedTabsAutoCleanInterval']?.readAs(
|
||||
DriftSqlType.int,
|
||||
db.typeMapping,
|
||||
),
|
||||
'maxSearchHistoryEntries': settings['maxSearchHistoryEntries']?.readAs(
|
||||
DriftSqlType.int,
|
||||
db.typeMapping,
|
||||
),
|
||||
'allowClipboardAccess': settings['allowClipboardAccess']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabListShowFavicons': settings['tabListShowFavicons']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'quickTabSwitcherShowTitles': settings['quickTabSwitcherShowTitles']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'quickTabSwitcherShowHistorySuggestions':
|
||||
settings['quickTabSwitcherShowHistorySuggestions']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'syncServerOverride': settings['syncServerOverride']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'syncTokenServerOverride': settings['syncTokenServerOverride']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerEnabled': settings['urlCleanerEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerAutoApply': settings['urlCleanerAutoApply']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerAllowReferralMarketing':
|
||||
settings['urlCleanerAllowReferralMarketing']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerCatalogUrl': settings['urlCleanerCatalogUrl']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerHashUrl': settings['urlCleanerHashUrl']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerAutoUpdate': settings['urlCleanerAutoUpdate']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'urlCleanerLastCheckEpochMs': settings['urlCleanerLastCheckEpochMs']
|
||||
?.readAs(DriftSqlType.int, db.typeMapping),
|
||||
'urlCleanerLastUpdateWasAuto': settings['urlCleanerLastUpdateWasAuto']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
'smallWebTabType': settings['smallWebTabType']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'tabBarLongPressUrlCopy': settings['tabBarLongPressUrlCopy']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'unshortenerEnabled': settings['unshortenerEnabled']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'unshortenerToken': settings['unshortenerToken']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'allowNonManifestPwaInstall': settings['allowNonManifestPwaInstall']
|
||||
?.readAs(DriftSqlType.bool, db.typeMapping),
|
||||
});
|
||||
}
|
||||
|
||||
//Eager fetch, when up to date settings are required
|
||||
Future<GeneralSettings> fetchSettings() {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.get()
|
||||
.then(_deserializeSettings);
|
||||
}
|
||||
|
||||
Future<void> updateSettings(
|
||||
UpdateGeneralSettingsFunc updateWithCurrent,
|
||||
) async {
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
|
||||
final current = await fetchSettings();
|
||||
|
||||
final oldJson = current.toJson();
|
||||
final newJson = updateWithCurrent(current).toJson();
|
||||
|
||||
return db.transaction(() async {
|
||||
for (final MapEntry(:key, :value) in newJson.entries) {
|
||||
if (oldJson[key] != value) {
|
||||
await db.settingDao.updateSetting(key, _partitionKey, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<GeneralSettings> build() {
|
||||
final db = ref.watch(userDatabaseProvider);
|
||||
|
||||
return db.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.watch()
|
||||
.map((event) {
|
||||
return _deserializeSettings(event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
GeneralSettings generalSettingsWithDefaults(Ref ref) {
|
||||
return ref.watch(
|
||||
generalSettingsRepositoryProvider.select(
|
||||
(value) => value.value ?? GeneralSettings.withDefaults(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'general_settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(GeneralSettingsRepository)
|
||||
final generalSettingsRepositoryProvider = GeneralSettingsRepositoryProvider._();
|
||||
|
||||
final class GeneralSettingsRepositoryProvider
|
||||
extends
|
||||
$StreamNotifierProvider<GeneralSettingsRepository, GeneralSettings> {
|
||||
GeneralSettingsRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'generalSettingsRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$generalSettingsRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
GeneralSettingsRepository create() => GeneralSettingsRepository();
|
||||
}
|
||||
|
||||
String _$generalSettingsRepositoryHash() =>
|
||||
r'afc63f4d929ea146f0b8a7c0f6936b06c5a41024';
|
||||
|
||||
abstract class _$GeneralSettingsRepository
|
||||
extends $StreamNotifier<GeneralSettings> {
|
||||
Stream<GeneralSettings> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<GeneralSettings>, GeneralSettings>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<GeneralSettings>, GeneralSettings>,
|
||||
AsyncValue<GeneralSettings>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
|
||||
@ProviderFor(generalSettingsWithDefaults)
|
||||
final generalSettingsWithDefaultsProvider =
|
||||
GeneralSettingsWithDefaultsProvider._();
|
||||
|
||||
final class GeneralSettingsWithDefaultsProvider
|
||||
extends
|
||||
$FunctionalProvider<GeneralSettings, GeneralSettings, GeneralSettings>
|
||||
with $Provider<GeneralSettings> {
|
||||
GeneralSettingsWithDefaultsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'generalSettingsWithDefaultsProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$generalSettingsWithDefaultsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<GeneralSettings> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
GeneralSettings create(Ref ref) {
|
||||
return generalSettingsWithDefaults(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(GeneralSettings value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<GeneralSettings>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$generalSettingsWithDefaultsHash() =>
|
||||
r'9da4a00a3500286fbf515ee319fa911bfacab40e';
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'onboarding.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class OnboardingRepository extends _$OnboardingRepository {
|
||||
static const targetRevision = 3;
|
||||
|
||||
Future<int?> getCurrentRevision() {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.onboardingDao
|
||||
.getLastRevision()
|
||||
.getSingleOrNull();
|
||||
}
|
||||
|
||||
Future<void> pushRevision(int revision) {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.onboardingDao
|
||||
.pushRevision(revision, DateTime.now());
|
||||
}
|
||||
|
||||
Future<bool> isOutdated() async {
|
||||
final current = await getCurrentRevision();
|
||||
return current == null || current < targetRevision;
|
||||
}
|
||||
|
||||
@override
|
||||
void build() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'onboarding.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(OnboardingRepository)
|
||||
final onboardingRepositoryProvider = OnboardingRepositoryProvider._();
|
||||
|
||||
final class OnboardingRepositoryProvider
|
||||
extends $NotifierProvider<OnboardingRepository, void> {
|
||||
OnboardingRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'onboardingRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$onboardingRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
OnboardingRepository create() => OnboardingRepository();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(void value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<void>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$onboardingRepositoryHash() =>
|
||||
r'5d583af3ae38b351357b16809ef32bb6807f5c05';
|
||||
|
||||
abstract class _$OnboardingRepository extends $Notifier<void> {
|
||||
void build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<void, void>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<void, void>,
|
||||
void,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:weblibre/core/filesystem.dart';
|
||||
import 'package:weblibre/domain/entities/profile.dart';
|
||||
import 'package:weblibre/features/user/data/models/auth_settings.dart';
|
||||
|
||||
part 'profile.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class ProfileRepository extends _$ProfileRepository {
|
||||
Future<List<Profile>> _readProfiles() {
|
||||
return filesystem.getAvailableProfileDirectories().then((dirs) async {
|
||||
final profiles = await Future.wait(
|
||||
dirs.map(filesystem.readProfileMetadata),
|
||||
);
|
||||
return profiles.nonNulls.toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> switchProfile(String id) async {
|
||||
await filesystem.setStartupProfile(UuidValue.withValidation(id));
|
||||
}
|
||||
|
||||
Future<Profile> createProfile({
|
||||
required String name,
|
||||
AuthSettings? authSettings,
|
||||
}) async {
|
||||
final profile = Profile.create(name: name, authSettings: authSettings);
|
||||
if (!await filesystem.createNewProfile(profile)) {
|
||||
throw Exception('Could not create profile');
|
||||
}
|
||||
|
||||
ref.invalidateSelf();
|
||||
|
||||
return profile;
|
||||
}
|
||||
|
||||
Future<void> updateProfileMetadata(Profile profile) async {
|
||||
await filesystem.updateProfileMetadata(profile);
|
||||
ref.invalidateSelf();
|
||||
}
|
||||
|
||||
Future<bool> deleteProfile(String id) async {
|
||||
final uuid = UuidValue.withValidation(id);
|
||||
if (filesystem.selectedProfile == uuid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await filesystem.getProfileDir(uuid).delete(recursive: true);
|
||||
|
||||
ref.invalidateSelf();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Profile>> build() {
|
||||
return _readProfiles();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'profile.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(ProfileRepository)
|
||||
final profileRepositoryProvider = ProfileRepositoryProvider._();
|
||||
|
||||
final class ProfileRepositoryProvider
|
||||
extends $AsyncNotifierProvider<ProfileRepository, List<Profile>> {
|
||||
ProfileRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'profileRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$profileRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
ProfileRepository create() => ProfileRepository();
|
||||
}
|
||||
|
||||
String _$profileRepositoryHash() => r'b770e7406e1602f808cc8076c1eda67b4fce6b2d';
|
||||
|
||||
abstract class _$ProfileRepository extends $AsyncNotifier<List<Profile>> {
|
||||
FutureOr<List<Profile>> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<List<Profile>>, List<Profile>>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<List<Profile>>, List<Profile>>,
|
||||
AsyncValue<List<Profile>>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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:riverpod/riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/features/user/data/models/tor_settings.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'tor_settings.g.dart';
|
||||
|
||||
typedef UpdateTorSettingsFunc =
|
||||
TorSettings Function(TorSettings currentSettings);
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class TorSettingsRepository extends _$TorSettingsRepository {
|
||||
final _partitionKey = 'tor';
|
||||
|
||||
TorSettings _deserializeSettings(List<MapEntry<String, DriftAny?>> entries) {
|
||||
final settings = Map.fromEntries(entries);
|
||||
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
|
||||
return TorSettings.fromJson({
|
||||
'proxyRegularTabsMode': settings['proxyRegularTabsMode']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'proxyPrivateTabsTor': settings['proxyPrivateTabsTor']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'config': settings['config']?.readAs(DriftSqlType.string, db.typeMapping),
|
||||
'requireBridge': settings['requireBridge']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'fetchRemoteBridges': settings['fetchRemoteBridges']?.readAs(
|
||||
DriftSqlType.bool,
|
||||
db.typeMapping,
|
||||
),
|
||||
'entryNodeCountry': settings['entryNodeCountry']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
'exitNodeCountry': settings['exitNodeCountry']?.readAs(
|
||||
DriftSqlType.string,
|
||||
db.typeMapping,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
//Eager fetch, when up to date settings are required
|
||||
Future<TorSettings> fetchSettings() {
|
||||
return ref
|
||||
.read(userDatabaseProvider)
|
||||
.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.get()
|
||||
.then(_deserializeSettings);
|
||||
}
|
||||
|
||||
Future<void> updateSettings(UpdateTorSettingsFunc updateWithCurrent) async {
|
||||
final db = ref.read(userDatabaseProvider);
|
||||
|
||||
final current = await fetchSettings();
|
||||
|
||||
final oldJson = current.toJson();
|
||||
final newJson = updateWithCurrent(current).toJson();
|
||||
|
||||
return db.transaction(() async {
|
||||
for (final MapEntry(:key, :value) in newJson.entries) {
|
||||
if (oldJson[key] != value) {
|
||||
await db.settingDao.updateSetting(key, _partitionKey, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<TorSettings> build() {
|
||||
final db = ref.watch(userDatabaseProvider);
|
||||
|
||||
return db.settingDao
|
||||
.getAllSettingsOfPartitionKey(_partitionKey)
|
||||
.watch()
|
||||
.map((event) {
|
||||
return _deserializeSettings(event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
TorSettings torSettingsWithDefaults(Ref ref) {
|
||||
return ref.watch(
|
||||
torSettingsRepositoryProvider.select(
|
||||
(value) => value.value ?? TorSettings.withDefaults(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'tor_settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(TorSettingsRepository)
|
||||
final torSettingsRepositoryProvider = TorSettingsRepositoryProvider._();
|
||||
|
||||
final class TorSettingsRepositoryProvider
|
||||
extends $StreamNotifierProvider<TorSettingsRepository, TorSettings> {
|
||||
TorSettingsRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'torSettingsRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$torSettingsRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
TorSettingsRepository create() => TorSettingsRepository();
|
||||
}
|
||||
|
||||
String _$torSettingsRepositoryHash() =>
|
||||
r'f771f23f17903bd192b24604bab522fb20571ffd';
|
||||
|
||||
abstract class _$TorSettingsRepository extends $StreamNotifier<TorSettings> {
|
||||
Stream<TorSettings> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<TorSettings>, TorSettings>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<TorSettings>, TorSettings>,
|
||||
AsyncValue<TorSettings>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
|
||||
@ProviderFor(torSettingsWithDefaults)
|
||||
final torSettingsWithDefaultsProvider = TorSettingsWithDefaultsProvider._();
|
||||
|
||||
final class TorSettingsWithDefaultsProvider
|
||||
extends $FunctionalProvider<TorSettings, TorSettings, TorSettings>
|
||||
with $Provider<TorSettings> {
|
||||
TorSettingsWithDefaultsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'torSettingsWithDefaultsProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$torSettingsWithDefaultsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<TorSettings> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
TorSettings create(Ref ref) {
|
||||
return torSettingsWithDefaults(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(TorSettings value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<TorSettings>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$torSettingsWithDefaultsHash() =>
|
||||
r'501a7ed7f14870d40b8f60303d3c385a45d9f542';
|
||||
Reference in New Issue
Block a user