imporved app bar; added quick actions;

This commit is contained in:
Fabian Freund
2024-07-09 09:26:21 +02:00
parent 28197ef59f
commit a38f50488c
17 changed files with 446 additions and 169 deletions
@@ -1,4 +1,5 @@
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:bang_navigator/features/search_browser/domain/entities/modes.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:fast_equatable/fast_equatable.dart';
import 'package:flutter/material.dart';
@@ -16,6 +17,8 @@ class Settings with FastEquatable {
final bool blockHttpProtocol;
final Set<HostSource> enableHostList;
final ThemeMode themeMode;
final KagiTool? quickAction;
final bool quickActionVoiceInput;
Settings({
required this.kagiSession,
@@ -27,6 +30,8 @@ class Settings with FastEquatable {
required this.blockHttpProtocol,
required this.enableHostList,
required this.themeMode,
required this.quickAction,
required this.quickActionVoiceInput,
});
Settings.withDefaults({
@@ -39,6 +44,8 @@ class Settings with FastEquatable {
bool? blockHttpProtocol,
Set<HostSource>? enableHostList,
ThemeMode? themeMode,
this.quickAction,
bool? quickActionVoiceInput,
}) : showEarlyAccessFeatures = showEarlyAccessFeatures ?? true,
incognitoMode = incognitoMode ?? true,
enableJavascript = enableJavascript ?? true,
@@ -46,7 +53,8 @@ class Settings with FastEquatable {
enableContentBlocking = enableContentBlocking ?? true,
blockHttpProtocol = blockHttpProtocol ?? false,
enableHostList = enableHostList ?? {HostSource.stevenBlackUnified},
themeMode = themeMode ?? ThemeMode.dark;
themeMode = themeMode ?? ThemeMode.dark,
quickActionVoiceInput = quickActionVoiceInput ?? false;
@override
bool get cacheHash => true;
@@ -62,5 +70,7 @@ class Settings with FastEquatable {
blockHttpProtocol,
enableHostList,
themeMode,
quickAction,
quickActionVoiceInput,
];
}
@@ -25,6 +25,10 @@ abstract class _$SettingsCWProxy {
Settings themeMode(ThemeMode themeMode);
Settings quickAction(KagiTool? quickAction);
Settings quickActionVoiceInput(bool quickActionVoiceInput);
/// 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
@@ -41,6 +45,8 @@ abstract class _$SettingsCWProxy {
bool? blockHttpProtocol,
Set<HostSource>? enableHostList,
ThemeMode? themeMode,
KagiTool? quickAction,
bool? quickActionVoiceInput,
});
}
@@ -84,6 +90,13 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
@override
Settings themeMode(ThemeMode themeMode) => this(themeMode: themeMode);
@override
Settings quickAction(KagiTool? quickAction) => this(quickAction: quickAction);
@override
Settings quickActionVoiceInput(bool quickActionVoiceInput) =>
this(quickActionVoiceInput: quickActionVoiceInput);
@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.
@@ -102,6 +115,8 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
Object? blockHttpProtocol = const $CopyWithPlaceholder(),
Object? enableHostList = const $CopyWithPlaceholder(),
Object? themeMode = const $CopyWithPlaceholder(),
Object? quickAction = const $CopyWithPlaceholder(),
Object? quickActionVoiceInput = const $CopyWithPlaceholder(),
}) {
return Settings(
kagiSession: kagiSession == const $CopyWithPlaceholder()
@@ -149,6 +164,16 @@ class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
? _value.themeMode
// ignore: cast_nullable_to_non_nullable
: themeMode as ThemeMode,
quickAction: quickAction == const $CopyWithPlaceholder()
? _value.quickAction
// ignore: cast_nullable_to_non_nullable
: quickAction as KagiTool?,
quickActionVoiceInput:
quickActionVoiceInput == const $CopyWithPlaceholder() ||
quickActionVoiceInput == null
? _value.quickActionVoiceInput
// ignore: cast_nullable_to_non_nullable
: quickActionVoiceInput as bool,
);
}
}
@@ -1,7 +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:bang_navigator/features/settings/utils/preference_parser.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -10,34 +9,26 @@ part 'settings_repository.g.dart';
typedef UpdateSettingsFunc = Settings Function(Settings currentSettings);
Set<HostSource>? _parseHostSources(List<String>? input) => input
?.map(
(list) =>
HostSource.values.firstWhereOrNull((source) => source.name == list),
)
.whereNotNull()
.toSet();
enum _StorageKeys {
kagiSession('kagi_session'),
showEarlyAccessFeatures('show_early_access_features'),
incognito('enabe_incognito'),
javascript('enable_js'),
launchExternal('enable_launch_external'),
contentBlocking('enable_content_blocking'),
blockHttpProtocol('block_http'),
enableHostList('enable_host_lists'),
themeMode('theme_mode'),
quickAction('enable_quick_action'),
quickActionVoiceInput('enable_quick_action_voice_input');
ThemeMode? _parseThemeMode(int? index) {
if (index != null && index < ThemeMode.values.length) {
return ThemeMode.values[index];
}
final String key;
return null;
const _StorageKeys(this.key);
}
@Riverpod(keepAlive: true)
class SettingsRepository extends _$SettingsRepository {
static const _sessionStorageKey = 'b4ng_kagi_session';
static const _showEarlyAccessFeaturesKey = 'b4ng_show_early_access';
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 _blockHttpProtocolStorageKey = 'b4ng_settings_block_http';
static const _enableHostListStorageKey = 'b4ng_settings_host_lists';
static const _themeModeStorageKey = 'b4ng_settings_theme_mode';
final FlutterSecureStorage _flutterSecureStorage;
final Future<SharedPreferences> _sharedPreferences;
@@ -53,7 +44,7 @@ class SettingsRepository extends _$SettingsRepository {
if (oldSettings != newSettings) {
if (oldSettings.kagiSession != newSettings.kagiSession) {
await _flutterSecureStorage.write(
key: _sessionStorageKey,
key: _StorageKeys.kagiSession.key,
value: (newSettings.kagiSession?.isNotEmpty ?? false)
? newSettings.kagiSession
: null,
@@ -63,28 +54,28 @@ class SettingsRepository extends _$SettingsRepository {
if (newSettings.showEarlyAccessFeatures !=
oldSettings.showEarlyAccessFeatures) {
await sharedPreferences.setBool(
_showEarlyAccessFeaturesKey,
_StorageKeys.showEarlyAccessFeatures.key,
newSettings.showEarlyAccessFeatures,
);
}
if (newSettings.incognitoMode != oldSettings.incognitoMode) {
await sharedPreferences.setBool(
_incognitoStorageKey,
_StorageKeys.incognito.key,
newSettings.incognitoMode,
);
}
if (newSettings.enableJavascript != oldSettings.enableJavascript) {
await sharedPreferences.setBool(
_javascriptStorageKey,
_StorageKeys.javascript.key,
newSettings.enableJavascript,
);
}
if (newSettings.launchUrlExternal != oldSettings.launchUrlExternal) {
await sharedPreferences.setBool(
_launchExternalStorageKey,
_StorageKeys.launchExternal.key,
newSettings.launchUrlExternal,
);
}
@@ -92,14 +83,14 @@ class SettingsRepository extends _$SettingsRepository {
if (newSettings.enableContentBlocking !=
oldSettings.enableContentBlocking) {
await sharedPreferences.setBool(
_contentBlockingStorageKey,
_StorageKeys.contentBlocking.key,
newSettings.enableContentBlocking,
);
}
if (newSettings.blockHttpProtocol != oldSettings.blockHttpProtocol) {
await sharedPreferences.setBool(
_blockHttpProtocolStorageKey,
_StorageKeys.blockHttpProtocol.key,
newSettings.blockHttpProtocol,
);
}
@@ -109,18 +100,38 @@ class SettingsRepository extends _$SettingsRepository {
oldSettings.enableHostList,
)) {
await sharedPreferences.setStringList(
_enableHostListStorageKey,
_StorageKeys.enableHostList.key,
newSettings.enableHostList.map((list) => list.name).toList(),
);
}
if (newSettings.themeMode != oldSettings.themeMode) {
await sharedPreferences.setInt(
_themeModeStorageKey,
_StorageKeys.themeMode.key,
newSettings.themeMode.index,
);
}
if (newSettings.quickAction != oldSettings.quickAction) {
final index = newSettings.quickAction?.index;
if (index != null) {
await sharedPreferences.setInt(
_StorageKeys.quickAction.key,
index,
);
} else {
await sharedPreferences.remove(_StorageKeys.quickAction.key);
}
}
if (newSettings.quickActionVoiceInput !=
oldSettings.quickActionVoiceInput) {
await sharedPreferences.setBool(
_StorageKeys.quickActionVoiceInput.key,
newSettings.quickActionVoiceInput,
);
}
ref.invalidateSelf();
}
}
@@ -130,21 +141,27 @@ class SettingsRepository extends _$SettingsRepository {
final sharedPreferences = await _sharedPreferences;
return Settings.withDefaults(
kagiSession: await _flutterSecureStorage.read(key: _sessionStorageKey),
kagiSession:
await _flutterSecureStorage.read(key: _StorageKeys.kagiSession.key),
showEarlyAccessFeatures:
sharedPreferences.getBool(_showEarlyAccessFeaturesKey),
incognitoMode: sharedPreferences.getBool(_incognitoStorageKey),
enableJavascript: sharedPreferences.getBool(_javascriptStorageKey),
launchUrlExternal: sharedPreferences.getBool(_launchExternalStorageKey),
sharedPreferences.getBool(_StorageKeys.showEarlyAccessFeatures.key),
incognitoMode: sharedPreferences.getBool(_StorageKeys.incognito.key),
enableJavascript: sharedPreferences.getBool(_StorageKeys.javascript.key),
launchUrlExternal:
sharedPreferences.getBool(_StorageKeys.launchExternal.key),
enableContentBlocking:
sharedPreferences.getBool(_contentBlockingStorageKey),
sharedPreferences.getBool(_StorageKeys.contentBlocking.key),
blockHttpProtocol:
sharedPreferences.getBool(_blockHttpProtocolStorageKey),
enableHostList: _parseHostSources(
sharedPreferences.getStringList(_enableHostListStorageKey),
sharedPreferences.getBool(_StorageKeys.blockHttpProtocol.key),
enableHostList: parseHostSources(
sharedPreferences.getStringList(_StorageKeys.enableHostList.key),
),
themeMode:
_parseThemeMode(sharedPreferences.getInt(_themeModeStorageKey)),
parseThemeMode(sharedPreferences.getInt(_StorageKeys.themeMode.key)),
quickAction:
parseKagiTool(sharedPreferences.getInt(_StorageKeys.quickAction.key)),
quickActionVoiceInput:
sharedPreferences.getBool(_StorageKeys.quickActionVoiceInput.key),
);
}
}
@@ -7,7 +7,7 @@ part of 'settings_repository.dart';
// **************************************************************************
String _$settingsRepositoryHash() =>
r'ac2b1ecddd82e45e901425e9e5fcb9b4b61855fa';
r'34d3c771d6ebb17cb9aa41211e035b4d1cbf2183';
/// See also [SettingsRepository].
@ProviderFor(SettingsRepository)