refactored settings & added content blocking

This commit is contained in:
Fabian Freund
2024-06-19 21:09:54 +02:00
parent 2bacbbffd2
commit d3e30613c9
11 changed files with 385 additions and 215 deletions
@@ -1,3 +1,4 @@
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:fast_equatable/fast_equatable.dart';
@@ -10,6 +11,8 @@ class Settings with FastEquatable {
final bool incognitoMode;
final bool enableJavascript;
final bool launchUrlExternal;
final bool enableContentBlocking;
final Set<HostSource> enableHostList;
Settings({
required this.kagiSession,
@@ -17,6 +20,8 @@ class Settings with FastEquatable {
required this.incognitoMode,
required this.enableJavascript,
required this.launchUrlExternal,
required this.enableContentBlocking,
required this.enableHostList,
});
Settings.withDefaults({
@@ -25,10 +30,14 @@ class Settings with FastEquatable {
bool? incognitoMode,
bool? enableJavascript,
bool? launchUrlExternal,
bool? enableContentBlocking,
Set<HostSource>? enableHostList,
}) : showEarlyAccessFeatures = showEarlyAccessFeatures ?? true,
incognitoMode = incognitoMode ?? true,
enableJavascript = enableJavascript ?? true,
launchUrlExternal = launchUrlExternal ?? false;
launchUrlExternal = launchUrlExternal ?? false,
enableContentBlocking = enableContentBlocking ?? true,
enableHostList = enableHostList ?? {HostSource.stevenBlackUnified};
@override
bool get cacheHash => true;
@@ -40,5 +49,7 @@ class Settings with FastEquatable {
incognitoMode,
enableJavascript,
launchUrlExternal,
enableContentBlocking,
enableHostList,
];
}
@@ -17,6 +17,10 @@ abstract class _$SettingsCWProxy {
Settings launchUrlExternal(bool launchUrlExternal);
Settings enableContentBlocking(bool enableContentBlocking);
Settings enableHostList(Set<HostSource> enableHostList);
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Settings(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
@@ -29,6 +33,8 @@ abstract class _$SettingsCWProxy {
bool? incognitoMode,
bool? enableJavascript,
bool? launchUrlExternal,
bool? enableContentBlocking,
Set<HostSource>? enableHostList,
});
}
@@ -57,6 +63,14 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
Settings launchUrlExternal(bool launchUrlExternal) =>
this(launchUrlExternal: launchUrlExternal);
@override
Settings enableContentBlocking(bool enableContentBlocking) =>
this(enableContentBlocking: enableContentBlocking);
@override
Settings enableHostList(Set<HostSource> enableHostList) =>
this(enableHostList: enableHostList);
@override
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Settings(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
@@ -71,6 +85,8 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
Object? incognitoMode = const $CopyWithPlaceholder(),
Object? enableJavascript = const $CopyWithPlaceholder(),
Object? launchUrlExternal = const $CopyWithPlaceholder(),
Object? enableContentBlocking = const $CopyWithPlaceholder(),
Object? enableHostList = const $CopyWithPlaceholder(),
}) {
return Settings(
kagiSession: kagiSession == const $CopyWithPlaceholder()
@@ -98,6 +114,17 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
? _value.launchUrlExternal
// ignore: cast_nullable_to_non_nullable
: launchUrlExternal as bool,
enableContentBlocking:
enableContentBlocking == const $CopyWithPlaceholder() ||
enableContentBlocking == null
? _value.enableContentBlocking
// ignore: cast_nullable_to_non_nullable
: enableContentBlocking as bool,
enableHostList: enableHostList == const $CopyWithPlaceholder() ||
enableHostList == null
? _value.enableHostList
// ignore: cast_nullable_to_non_nullable
: enableHostList as Set<HostSource>,
);
}
}
@@ -1,4 +1,6 @@
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:bang_navigator/features/settings/data/models/settings.dart';
import 'package:collection/collection.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -14,6 +16,8 @@ class SettingsRepository extends _$SettingsRepository {
static const _incognitoStorageKey = 'b4ng_settings_incognito';
static const _javascriptStorageKey = 'b4ng_settings_js';
static const _launchExternalStorageKey = 'b4ng_settings_launch_external';
static const _contentBlockingStorageKey = 'b4ng_settings_content_blocking';
static const _enableHostListStorageKey = 'b4ng_settings_host_lists';
final FlutterSecureStorage _flutterSecureStorage;
final Future<SharedPreferences> _sharedPreferences;
@@ -23,6 +27,7 @@ class SettingsRepository extends _$SettingsRepository {
_sharedPreferences = SharedPreferences.getInstance();
Future<void> updateSettings(UpdateSettingsFunc updateWithCurrent) async {
final sharedPreferences = await _sharedPreferences;
final oldSettings = state.value!;
final newSettings = updateWithCurrent(oldSettings);
@@ -38,32 +43,48 @@ class SettingsRepository extends _$SettingsRepository {
if (newSettings.showEarlyAccessFeatures !=
oldSettings.showEarlyAccessFeatures) {
await _sharedPreferences.then(
(s) => s.setBool(
_showEarlyAccessFeaturesKey,
newSettings.showEarlyAccessFeatures,
),
await sharedPreferences.setBool(
_showEarlyAccessFeaturesKey,
newSettings.showEarlyAccessFeatures,
);
}
if (newSettings.incognitoMode != oldSettings.incognitoMode) {
await _sharedPreferences.then(
(s) => s.setBool(_incognitoStorageKey, newSettings.incognitoMode),
await sharedPreferences.setBool(
_incognitoStorageKey,
newSettings.incognitoMode,
);
}
if (newSettings.enableJavascript != oldSettings.enableJavascript) {
await _sharedPreferences.then(
(s) => s.setBool(_javascriptStorageKey, newSettings.enableJavascript),
await sharedPreferences.setBool(
_javascriptStorageKey,
newSettings.enableJavascript,
);
}
if (newSettings.launchUrlExternal != oldSettings.launchUrlExternal) {
await _sharedPreferences.then(
(s) => s.setBool(
_launchExternalStorageKey,
newSettings.launchUrlExternal,
),
await sharedPreferences.setBool(
_launchExternalStorageKey,
newSettings.launchUrlExternal,
);
}
if (newSettings.enableContentBlocking !=
oldSettings.enableContentBlocking) {
await sharedPreferences.setBool(
_contentBlockingStorageKey,
newSettings.enableContentBlocking,
);
}
if (!const DeepCollectionEquality.unordered().equals(
newSettings.enableHostList,
oldSettings.enableHostList,
)) {
await sharedPreferences.setStringList(
_enableHostListStorageKey,
newSettings.enableHostList.map((list) => list.name).toList(),
);
}
@@ -82,6 +103,16 @@ class SettingsRepository extends _$SettingsRepository {
incognitoMode: sharedPreferences.getBool(_incognitoStorageKey),
enableJavascript: sharedPreferences.getBool(_javascriptStorageKey),
launchUrlExternal: sharedPreferences.getBool(_launchExternalStorageKey),
enableContentBlocking:
sharedPreferences.getBool(_contentBlockingStorageKey),
enableHostList: sharedPreferences
.getStringList(_enableHostListStorageKey)
?.map(
(list) => HostSource.values
.firstWhereOrNull((source) => source.name == list),
)
.whereNotNull()
.toSet(),
);
}
}
@@ -7,7 +7,7 @@ part of 'settings_repository.dart';
// **************************************************************************
String _$settingsRepositoryHash() =>
r'0beffd6449e5488e61beb181fefefd4aa24361b0';
r'9d0e8b8452f85ca33b8f13a4bbcb69212e7d29bd';
/// See also [SettingsRepository].
@ProviderFor(SettingsRepository)