intermediate

This commit is contained in:
Fabian Freund
2025-01-07 00:06:31 +01:00
parent ea0880b97a
commit 4a2191b01d
84 changed files with 2655 additions and 1383 deletions
@@ -1,70 +0,0 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:fast_equatable/fast_equatable.dart';
import 'package:flutter/material.dart';
import 'package:lensai/features/kagi/data/entities/modes.dart';
part 'settings.g.dart';
@CopyWith()
class Settings with FastEquatable {
final String? kagiSession;
final bool showEarlyAccessFeatures;
final bool incognitoMode;
final bool enableJavascript;
final bool launchUrlExternal;
final bool blockHttpProtocol;
final ThemeMode themeMode;
final KagiTool? quickAction;
final bool quickActionVoiceInput;
final bool enableReadability;
Settings({
required this.kagiSession,
required this.showEarlyAccessFeatures,
required this.incognitoMode,
required this.enableJavascript,
required this.launchUrlExternal,
required this.blockHttpProtocol,
required this.themeMode,
required this.quickAction,
required this.quickActionVoiceInput,
required this.enableReadability,
});
Settings.withDefaults({
this.kagiSession,
bool? showEarlyAccessFeatures,
bool? incognitoMode,
bool? enableJavascript,
bool? launchUrlExternal,
bool? blockHttpProtocol,
ThemeMode? themeMode,
this.quickAction,
bool? quickActionVoiceInput,
bool? enableReadability,
}) : showEarlyAccessFeatures = showEarlyAccessFeatures ?? true,
incognitoMode = incognitoMode ?? true,
enableJavascript = enableJavascript ?? true,
launchUrlExternal = launchUrlExternal ?? false,
blockHttpProtocol = blockHttpProtocol ?? false,
themeMode = themeMode ?? ThemeMode.dark,
quickActionVoiceInput = quickActionVoiceInput ?? false,
enableReadability = enableReadability ?? true;
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [
kagiSession,
showEarlyAccessFeatures,
incognitoMode,
enableJavascript,
launchUrlExternal,
blockHttpProtocol,
themeMode,
quickAction,
quickActionVoiceInput,
enableReadability,
];
}
@@ -1,164 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'settings.dart';
// **************************************************************************
// CopyWithGenerator
// **************************************************************************
abstract class _$SettingsCWProxy {
Settings kagiSession(String? kagiSession);
Settings showEarlyAccessFeatures(bool showEarlyAccessFeatures);
Settings incognitoMode(bool incognitoMode);
Settings enableJavascript(bool enableJavascript);
Settings launchUrlExternal(bool launchUrlExternal);
Settings blockHttpProtocol(bool blockHttpProtocol);
Settings themeMode(ThemeMode themeMode);
Settings quickAction(KagiTool? quickAction);
Settings quickActionVoiceInput(bool quickActionVoiceInput);
Settings enableReadability(bool enableReadability);
/// 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
/// ```dart
/// Settings(...).copyWith(id: 12, name: "My name")
/// ````
Settings call({
String? kagiSession,
bool showEarlyAccessFeatures,
bool incognitoMode,
bool enableJavascript,
bool launchUrlExternal,
bool blockHttpProtocol,
ThemeMode themeMode,
KagiTool? quickAction,
bool quickActionVoiceInput,
bool enableReadability,
});
}
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfSettings.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfSettings.copyWith.fieldName(...)`
class _$SettingsCWProxyImpl implements _$SettingsCWProxy {
const _$SettingsCWProxyImpl(this._value);
final Settings _value;
@override
Settings kagiSession(String? kagiSession) => this(kagiSession: kagiSession);
@override
Settings showEarlyAccessFeatures(bool showEarlyAccessFeatures) =>
this(showEarlyAccessFeatures: showEarlyAccessFeatures);
@override
Settings incognitoMode(bool incognitoMode) =>
this(incognitoMode: incognitoMode);
@override
Settings enableJavascript(bool enableJavascript) =>
this(enableJavascript: enableJavascript);
@override
Settings launchUrlExternal(bool launchUrlExternal) =>
this(launchUrlExternal: launchUrlExternal);
@override
Settings blockHttpProtocol(bool blockHttpProtocol) =>
this(blockHttpProtocol: blockHttpProtocol);
@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
Settings enableReadability(bool enableReadability) =>
this(enableReadability: enableReadability);
@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.
///
/// Usage
/// ```dart
/// Settings(...).copyWith(id: 12, name: "My name")
/// ````
Settings call({
Object? kagiSession = const $CopyWithPlaceholder(),
Object? showEarlyAccessFeatures = const $CopyWithPlaceholder(),
Object? incognitoMode = const $CopyWithPlaceholder(),
Object? enableJavascript = const $CopyWithPlaceholder(),
Object? launchUrlExternal = const $CopyWithPlaceholder(),
Object? blockHttpProtocol = const $CopyWithPlaceholder(),
Object? themeMode = const $CopyWithPlaceholder(),
Object? quickAction = const $CopyWithPlaceholder(),
Object? quickActionVoiceInput = const $CopyWithPlaceholder(),
Object? enableReadability = const $CopyWithPlaceholder(),
}) {
return Settings(
kagiSession: kagiSession == const $CopyWithPlaceholder()
? _value.kagiSession
// ignore: cast_nullable_to_non_nullable
: kagiSession as String?,
showEarlyAccessFeatures:
showEarlyAccessFeatures == const $CopyWithPlaceholder()
? _value.showEarlyAccessFeatures
// ignore: cast_nullable_to_non_nullable
: showEarlyAccessFeatures as bool,
incognitoMode: incognitoMode == const $CopyWithPlaceholder()
? _value.incognitoMode
// ignore: cast_nullable_to_non_nullable
: incognitoMode as bool,
enableJavascript: enableJavascript == const $CopyWithPlaceholder()
? _value.enableJavascript
// ignore: cast_nullable_to_non_nullable
: enableJavascript as bool,
launchUrlExternal: launchUrlExternal == const $CopyWithPlaceholder()
? _value.launchUrlExternal
// ignore: cast_nullable_to_non_nullable
: launchUrlExternal as bool,
blockHttpProtocol: blockHttpProtocol == const $CopyWithPlaceholder()
? _value.blockHttpProtocol
// ignore: cast_nullable_to_non_nullable
: blockHttpProtocol as bool,
themeMode: themeMode == const $CopyWithPlaceholder()
? _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()
? _value.quickActionVoiceInput
// ignore: cast_nullable_to_non_nullable
: quickActionVoiceInput as bool,
enableReadability: enableReadability == const $CopyWithPlaceholder()
? _value.enableReadability
// ignore: cast_nullable_to_non_nullable
: enableReadability as bool,
);
}
}
extension $SettingsCopyWith on Settings {
/// Returns a callable class that can be used as follows: `instanceOfSettings.copyWith(...)` or like so:`instanceOfSettings.copyWith.fieldName(...)`.
// ignore: library_private_types_in_public_api
_$SettingsCWProxy get copyWith => _$SettingsCWProxyImpl(this);
}
@@ -1,151 +0,0 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:lensai/features/settings/data/models/settings.dart';
import 'package:lensai/features/settings/utils/preference_parser.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'settings_repository.g.dart';
typedef UpdateSettingsFunc = Settings Function(Settings currentSettings);
enum _StorageKeys {
kagiSession('kagi_session'),
showEarlyAccessFeatures('show_early_access_features'),
incognito('enabe_incognito'),
javascript('enable_js'),
launchExternal('enable_launch_external'),
blockHttpProtocol('block_http'),
themeMode('theme_mode'),
quickAction('enable_quick_action'),
quickActionVoiceInput('enable_quick_action_voice_input'),
enableReadability('enable_readability');
final String key;
const _StorageKeys(this.key);
}
@Riverpod(keepAlive: true)
class SettingsRepository extends _$SettingsRepository {
final FlutterSecureStorage _flutterSecureStorage;
final Future<SharedPreferences> _sharedPreferences;
SettingsRepository()
: _flutterSecureStorage = const FlutterSecureStorage(),
_sharedPreferences = SharedPreferences.getInstance();
Future<void> updateSettings(UpdateSettingsFunc updateWithCurrent) async {
final sharedPreferences = await _sharedPreferences;
final oldSettings = state.value!;
final newSettings = updateWithCurrent(oldSettings);
if (oldSettings != newSettings) {
if (oldSettings.kagiSession != newSettings.kagiSession) {
await _flutterSecureStorage.write(
key: _StorageKeys.kagiSession.key,
value: (newSettings.kagiSession?.isNotEmpty ?? false)
? newSettings.kagiSession
: null,
);
}
if (newSettings.showEarlyAccessFeatures !=
oldSettings.showEarlyAccessFeatures) {
await sharedPreferences.setBool(
_StorageKeys.showEarlyAccessFeatures.key,
newSettings.showEarlyAccessFeatures,
);
}
if (newSettings.incognitoMode != oldSettings.incognitoMode) {
await sharedPreferences.setBool(
_StorageKeys.incognito.key,
newSettings.incognitoMode,
);
}
if (newSettings.enableJavascript != oldSettings.enableJavascript) {
await sharedPreferences.setBool(
_StorageKeys.javascript.key,
newSettings.enableJavascript,
);
}
if (newSettings.launchUrlExternal != oldSettings.launchUrlExternal) {
await sharedPreferences.setBool(
_StorageKeys.launchExternal.key,
newSettings.launchUrlExternal,
);
}
if (newSettings.blockHttpProtocol != oldSettings.blockHttpProtocol) {
await sharedPreferences.setBool(
_StorageKeys.blockHttpProtocol.key,
newSettings.blockHttpProtocol,
);
}
if (newSettings.themeMode != oldSettings.themeMode) {
await sharedPreferences.setInt(
_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,
);
}
if (newSettings.enableReadability != oldSettings.enableReadability) {
await sharedPreferences.setBool(
_StorageKeys.enableReadability.key,
newSettings.enableReadability,
);
}
ref.invalidateSelf();
}
}
@override
FutureOr<Settings> build() async {
final sharedPreferences = await _sharedPreferences;
return Settings.withDefaults(
kagiSession:
await _flutterSecureStorage.read(key: _StorageKeys.kagiSession.key),
showEarlyAccessFeatures:
sharedPreferences.getBool(_StorageKeys.showEarlyAccessFeatures.key),
incognitoMode: sharedPreferences.getBool(_StorageKeys.incognito.key),
enableJavascript: sharedPreferences.getBool(_StorageKeys.javascript.key),
launchUrlExternal:
sharedPreferences.getBool(_StorageKeys.launchExternal.key),
blockHttpProtocol:
sharedPreferences.getBool(_StorageKeys.blockHttpProtocol.key),
themeMode:
parseThemeMode(sharedPreferences.getInt(_StorageKeys.themeMode.key)),
quickAction:
parseKagiTool(sharedPreferences.getInt(_StorageKeys.quickAction.key)),
quickActionVoiceInput:
sharedPreferences.getBool(_StorageKeys.quickActionVoiceInput.key),
enableReadability:
sharedPreferences.getBool(_StorageKeys.enableReadability.key),
);
}
}
@@ -1,27 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'settings_repository.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$settingsRepositoryHash() =>
r'b0a498899546b6a0c8566458ed8848fef410c537';
/// See also [SettingsRepository].
@ProviderFor(SettingsRepository)
final settingsRepositoryProvider =
AsyncNotifierProvider<SettingsRepository, Settings>.internal(
SettingsRepository.new,
name: r'settingsRepositoryProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$settingsRepositoryHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$SettingsRepository = AsyncNotifier<Settings>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -1,4 +1,4 @@
import 'package:lensai/features/settings/data/repositories/settings_repository.dart';
import 'package:lensai/features/user/domain/repositories/settings.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'save_settings.g.dart';
@@ -1,21 +1,16 @@
import 'package:fading_scroll/fading_scroll.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/features/bangs/data/models/bang.dart';
import 'package:lensai/features/bangs/domain/providers/bangs.dart';
import 'package:lensai/features/bangs/domain/repositories/data.dart';
import 'package:lensai/features/kagi/data/entities/modes.dart';
import 'package:lensai/features/settings/data/models/settings.dart';
import 'package:lensai/features/settings/data/repositories/settings_repository.dart';
import 'package:lensai/features/settings/presentation/controllers/save_settings.dart';
import 'package:lensai/features/settings/presentation/widgets/bang_group_list_tile.dart';
import 'package:lensai/features/settings/presentation/widgets/custom_list_tile.dart';
import 'package:lensai/features/settings/utils/session_link_extractor.dart';
import 'package:lensai/presentation/hooks/listenable_callback.dart';
import 'package:lensai/utils/ui_helper.dart' as ui_helper;
import 'package:lensai/features/user/data/models/settings.dart';
import 'package:lensai/features/user/domain/providers/cache.dart';
import 'package:lensai/features/user/domain/repositories/cache.dart';
import 'package:lensai/features/user/domain/repositories/settings.dart';
class SettingsScreen extends HookConsumerWidget {
const SettingsScreen({super.key});
@@ -44,40 +39,7 @@ class SettingsScreen extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final settings = ref.watch(
settingsRepositoryProvider.select(
(value) => value.valueOrNull ?? Settings.withDefaults(),
),
);
final kagiSessionTextController = useTextEditingController(
text: settings.kagiSession,
);
final hideSessionText = useState(true);
useListenableCallback(kagiSessionTextController, () async {
var text = kagiSessionTextController.text;
if (Uri.tryParse(text) case final Uri uri) {
if (extractKagiSession(uri) case final String session) {
text = session;
}
}
await ref.read(saveSettingsControllerProvider.notifier).save(
(currentSettings) => currentSettings.copyWith.kagiSession(text),
);
});
ref.listen(
settingsRepositoryProvider.select(
(settings) => settings.valueOrNull?.kagiSession,
), (previous, next) {
if (next != null &&
next.isNotEmpty &&
kagiSessionTextController.text != next) {
kagiSessionTextController.text = next;
}
});
final settings = ref.watch(settingsRepositoryProvider);
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
@@ -87,67 +49,6 @@ class SettingsScreen extends HookConsumerWidget {
return ListView(
controller: controller,
children: [
_buildSection(theme, 'Kagi'),
Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
bottom: 8,
),
child: TextField(
enableIMEPersonalizedLearning: false,
autocorrect: false,
controller: kagiSessionTextController,
obscureText: hideSessionText.value,
decoration: InputDecoration(
label: const Text('Kagi Session Token'),
hintText: 'https://kagi.com/search?token=...',
helperMaxLines: 2,
helper: Markdown(
shrinkWrap: true,
padding: EdgeInsets.zero,
data:
'You can visit your [Kagi Account Settings](user_details) to get your Session Link.',
onTapLink: (text, href, title) async {
if (href == 'user_details') {
await ui_helper.launchUrlFeedback(
context,
Uri.parse(
'https://kagi.com/settings?p=user_details',
),
);
}
},
),
suffixIcon: IconButton(
onPressed: () {
hideSessionText.value = !hideSessionText.value;
},
icon: Icon(
hideSessionText.value
? Icons.visibility
: Icons.visibility_off,
),
),
),
),
),
SwitchListTile.adaptive(
title: const Text('Show Early Access Features'),
subtitle: const Text(
"Displays Kagi's early access features in the UI. As an Ultimate subscriber, you will likely want to have this enabled.",
),
value: settings.showEarlyAccessFeatures,
onChanged: (value) async {
await ref.read(saveSettingsControllerProvider.notifier).save(
(currentSettings) => currentSettings.copyWith
.showEarlyAccessFeatures(value),
);
},
),
const SizedBox(
height: 16,
),
_buildSection(theme, 'General'),
Padding(
padding:
@@ -232,19 +133,6 @@ class SettingsScreen extends HookConsumerWidget {
);
},
),
SwitchListTile.adaptive(
title: const Text('Launch Links Externally'),
subtitle: const Text(
'Opens all links (except for kagi.com) in your default browser.',
),
value: settings.launchUrlExternal,
onChanged: (value) async {
await ref.read(saveSettingsControllerProvider.notifier).save(
(currentSettings) =>
currentSettings.copyWith.launchUrlExternal(value),
);
},
),
SwitchListTile.adaptive(
title: const Text('Enable Reader Mode'),
subtitle: const Text(
@@ -261,77 +149,6 @@ class SettingsScreen extends HookConsumerWidget {
const SizedBox(
height: 16,
),
_buildSection(theme, 'Appearance'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const CustomListTile(
title: 'Quick Action',
subtitle:
'Appears in the browser app bar between website title and tab count.',
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Center(
child: SegmentedButton<KagiTool>(
emptySelectionAllowed: true,
segments: [
ButtonSegment(
value: KagiTool.search,
icon: Icon(KagiTool.search.icon),
label: const Text('Search'),
),
ButtonSegment(
value: KagiTool.summarizer,
icon: Icon(KagiTool.summarizer.icon),
label: const Text('Summarizer'),
),
ButtonSegment(
value: KagiTool.assistant,
icon: Icon(KagiTool.assistant.icon),
label: const Text('Assistant'),
),
],
selected: {
if (settings.quickAction != null)
settings.quickAction!,
},
onSelectionChanged: (value) async {
await ref
.read(saveSettingsControllerProvider.notifier)
.save(
(currentSettings) =>
currentSettings.copyWith.quickAction(
value.isNotEmpty ? value.first : null,
),
);
},
),
),
),
],
),
),
SwitchListTile.adaptive(
title: const Text('Quick Action - STT'),
subtitle: const Text(
'Quick option will open with speech-to-text input.',
),
value: settings.quickActionVoiceInput,
onChanged: (settings.quickAction != null)
? (value) async {
await ref
.read(saveSettingsControllerProvider.notifier)
.save(
(currentSettings) => currentSettings.copyWith
.quickActionVoiceInput(value),
);
}
: null,
),
_buildSection(theme, 'Content Blocking'),
_buildSubSection(theme, 'Lists'),
const SizedBox(
@@ -354,7 +171,7 @@ class SettingsScreen extends HookConsumerWidget {
Consumer(
builder: (context, ref, child) {
final size = ref.watch(
bangIconCacheSizeMegabytesProvider.select(
iconCacheSizeMegabytesProvider.select(
(value) => value.valueOrNull,
),
);
@@ -384,8 +201,8 @@ class SettingsScreen extends HookConsumerWidget {
suffix: FilledButton.icon(
onPressed: () async {
await ref
.read(bangDataRepositoryProvider.notifier)
.clearIconData();
.read(cacheRepositoryProvider.notifier)
.clearCache();
},
icon: const Icon(Icons.delete),
label: const Text('Clear'),
@@ -1,18 +0,0 @@
import 'package:flutter/material.dart';
import 'package:lensai/features/kagi/data/entities/modes.dart';
ThemeMode? parseThemeMode(int? index) {
if (index != null && index < ThemeMode.values.length) {
return ThemeMode.values[index];
}
return null;
}
KagiTool? parseKagiTool(int? index) {
if (index != null && index < ThemeMode.values.length) {
return KagiTool.values[index];
}
return null;
}
@@ -1,7 +0,0 @@
String? extractKagiSession(Uri uri) {
if (uri.authority == 'kagi.com') {
return uri.queryParameters['token'];
}
return null;
}