first implementation finished
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:fast_equatable/fast_equatable.dart';
|
||||
|
||||
part 'settings.g.dart';
|
||||
|
||||
@CopyWith()
|
||||
class Settings with FastEquatable {
|
||||
final String? kagiSession;
|
||||
final bool incognitoMode;
|
||||
final bool enableJavascript;
|
||||
final bool launchUrlExternal;
|
||||
|
||||
Settings({
|
||||
required this.kagiSession,
|
||||
required this.incognitoMode,
|
||||
required this.enableJavascript,
|
||||
required this.launchUrlExternal,
|
||||
});
|
||||
|
||||
Settings.withDefaults({
|
||||
required this.kagiSession,
|
||||
bool? incognitoMode,
|
||||
bool? enableJavascript,
|
||||
bool? launchUrlExternal,
|
||||
}) : incognitoMode = incognitoMode ?? true,
|
||||
enableJavascript = enableJavascript ?? true,
|
||||
launchUrlExternal = launchUrlExternal ?? false;
|
||||
|
||||
@override
|
||||
bool get cacheHash => true;
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [
|
||||
kagiSession,
|
||||
incognitoMode,
|
||||
enableJavascript,
|
||||
launchUrlExternal,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class _$SettingsCWProxy {
|
||||
Settings kagiSession(String? kagiSession);
|
||||
|
||||
Settings incognitoMode(bool incognitoMode);
|
||||
|
||||
Settings enableJavascript(bool enableJavascript);
|
||||
|
||||
Settings launchUrlExternal(bool launchUrlExternal);
|
||||
|
||||
/// 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? incognitoMode,
|
||||
bool? enableJavascript,
|
||||
bool? launchUrlExternal,
|
||||
});
|
||||
}
|
||||
|
||||
/// 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 incognitoMode(bool incognitoMode) =>
|
||||
this(incognitoMode: incognitoMode);
|
||||
|
||||
@override
|
||||
Settings enableJavascript(bool enableJavascript) =>
|
||||
this(enableJavascript: enableJavascript);
|
||||
|
||||
@override
|
||||
Settings launchUrlExternal(bool launchUrlExternal) =>
|
||||
this(launchUrlExternal: launchUrlExternal);
|
||||
|
||||
@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? incognitoMode = const $CopyWithPlaceholder(),
|
||||
Object? enableJavascript = const $CopyWithPlaceholder(),
|
||||
Object? launchUrlExternal = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return Settings(
|
||||
kagiSession: kagiSession == const $CopyWithPlaceholder()
|
||||
? _value.kagiSession
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: kagiSession as String?,
|
||||
incognitoMode:
|
||||
incognitoMode == const $CopyWithPlaceholder() || incognitoMode == null
|
||||
? _value.incognitoMode
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: incognitoMode as bool,
|
||||
enableJavascript: enableJavascript == const $CopyWithPlaceholder() ||
|
||||
enableJavascript == null
|
||||
? _value.enableJavascript
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: enableJavascript as bool,
|
||||
launchUrlExternal: launchUrlExternal == const $CopyWithPlaceholder() ||
|
||||
launchUrlExternal == null
|
||||
? _value.launchUrlExternal
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: launchUrlExternal 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);
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/data/models/settings.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);
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class SettingsRepository extends _$SettingsRepository {
|
||||
static const _sessionStorageKey = 'b4ng_kagi_session';
|
||||
static const _incognitoStorageKey = 'b4ng_settings_incognito';
|
||||
static const _javascriptStorageKey = 'b4ng_settings_js';
|
||||
static const _launchExternalStorageKey = 'b4ng_settings_launch_external';
|
||||
|
||||
final FlutterSecureStorage _flutterSecureStorage;
|
||||
final Future<SharedPreferences> _sharedPreferences;
|
||||
|
||||
SettingsRepository()
|
||||
: _flutterSecureStorage = const FlutterSecureStorage(),
|
||||
_sharedPreferences = SharedPreferences.getInstance();
|
||||
|
||||
Future<void> updateSettings(UpdateSettingsFunc updateWithCurrent) async {
|
||||
final oldSettings = state.value!;
|
||||
final newSettings = updateWithCurrent(oldSettings);
|
||||
|
||||
if (oldSettings != newSettings) {
|
||||
if (oldSettings.kagiSession != newSettings.kagiSession) {
|
||||
await _flutterSecureStorage.write(
|
||||
key: _sessionStorageKey,
|
||||
value: newSettings.kagiSession,
|
||||
);
|
||||
}
|
||||
|
||||
if (newSettings.incognitoMode != oldSettings.incognitoMode) {
|
||||
await _sharedPreferences.then(
|
||||
(s) => s.setBool(_incognitoStorageKey, newSettings.incognitoMode),
|
||||
);
|
||||
}
|
||||
|
||||
if (newSettings.enableJavascript != oldSettings.enableJavascript) {
|
||||
await _sharedPreferences.then(
|
||||
(s) => s.setBool(_javascriptStorageKey, newSettings.enableJavascript),
|
||||
);
|
||||
}
|
||||
|
||||
if (newSettings.launchUrlExternal != oldSettings.launchUrlExternal) {
|
||||
await _sharedPreferences.then(
|
||||
(s) => s.setBool(
|
||||
_launchExternalStorageKey,
|
||||
newSettings.launchUrlExternal,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ref.invalidateSelf();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<Settings> build() async {
|
||||
final sharedPreferences = await _sharedPreferences;
|
||||
|
||||
return Settings.withDefaults(
|
||||
kagiSession: await _flutterSecureStorage.read(key: _sessionStorageKey),
|
||||
incognitoMode: sharedPreferences.getBool(_incognitoStorageKey),
|
||||
enableJavascript: sharedPreferences.getBool(_javascriptStorageKey),
|
||||
launchUrlExternal: sharedPreferences.getBool(_launchExternalStorageKey),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'settings_repository.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$settingsRepositoryHash() =>
|
||||
r'1e1dc6c76bdcaf742335bf8e11a584093ec13f33';
|
||||
|
||||
/// 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
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:kagi_bang_bang/features/settings/data/repositories/settings_repository.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'save_settings.g.dart';
|
||||
|
||||
@riverpod
|
||||
class SaveSettingsController extends _$SaveSettingsController {
|
||||
@override
|
||||
FutureOr<void> build() {}
|
||||
|
||||
Future<void> save(UpdateSettingsFunc updateSettings) async {
|
||||
state = const AsyncLoading();
|
||||
state = await AsyncValue.guard(
|
||||
() => ref
|
||||
.read(settingsRepositoryProvider.notifier)
|
||||
.updateSettings(updateSettings),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'save_settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$saveSettingsControllerHash() =>
|
||||
r'861664dc6e526737adff645597b141c2ba9d97c1';
|
||||
|
||||
/// See also [SaveSettingsController].
|
||||
@ProviderFor(SaveSettingsController)
|
||||
final saveSettingsControllerProvider =
|
||||
AutoDisposeAsyncNotifierProvider<SaveSettingsController, void>.internal(
|
||||
SaveSettingsController.new,
|
||||
name: r'saveSettingsControllerProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$saveSettingsControllerHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$SaveSettingsController = AutoDisposeAsyncNotifier<void>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
@@ -0,0 +1,146 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/data/models/settings.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/data/repositories/settings_repository.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/presentation/controllers/save_settings.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/utils/session_link_extractor.dart';
|
||||
import 'package:kagi_bang_bang/presentation/hooks/listenable_callback.dart';
|
||||
|
||||
class SettingsScreen extends HookConsumerWidget {
|
||||
const SettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final kagiSessionTextController = useTextEditingController(
|
||||
text: ref.read(
|
||||
settingsRepositoryProvider
|
||||
.select((value) => value.valueOrNull?.kagiSession),
|
||||
),
|
||||
);
|
||||
final hideSessionText = useState(true);
|
||||
|
||||
// final isSavingSettings = ref.watch(
|
||||
// saveSettingsControllerProvider.select((value) => value.isLoading),
|
||||
// );
|
||||
|
||||
final incognitoEnabled = ref.watch(
|
||||
settingsRepositoryProvider
|
||||
.select((value) => value.valueOrNull?.incognitoMode ?? false),
|
||||
);
|
||||
|
||||
final javacsriptEnabled = ref.watch(
|
||||
settingsRepositoryProvider
|
||||
.select((value) => value.valueOrNull?.enableJavascript ?? false),
|
||||
);
|
||||
|
||||
final launchUrlExternal = ref.watch(
|
||||
settingsRepositoryProvider
|
||||
.select((value) => value.valueOrNull?.launchUrlExternal ?? false),
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Settings')),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListView(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 16,
|
||||
right: 16,
|
||||
bottom: 16,
|
||||
),
|
||||
child: TextField(
|
||||
controller: kagiSessionTextController,
|
||||
obscureText: hideSessionText.value,
|
||||
decoration: InputDecoration(
|
||||
label: const Text('Kagi Session Token'),
|
||||
hintText: 'https://kagi.com/search?token=...',
|
||||
helperMaxLines: 2,
|
||||
helperText:
|
||||
'You can visit your Kagi Account settings to get your Session Link.',
|
||||
suffixIcon: IconButton(
|
||||
onPressed: () {
|
||||
hideSessionText.value = !hideSessionText.value;
|
||||
},
|
||||
icon: Icon(
|
||||
hideSessionText.value
|
||||
? Icons.visibility
|
||||
: Icons.visibility_off,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SwitchListTile.adaptive(
|
||||
title: const Text('Incognito Mode'),
|
||||
subtitle: const Text(
|
||||
'Deletes all browsing data upon app restart for enhanced privacy.',
|
||||
),
|
||||
value: incognitoEnabled,
|
||||
onChanged: (value) async {
|
||||
await ref.read(saveSettingsControllerProvider.notifier).save(
|
||||
(currentSettings) =>
|
||||
currentSettings.copyWith.incognitoMode(value),
|
||||
);
|
||||
},
|
||||
),
|
||||
SwitchListTile.adaptive(
|
||||
title: const Text('Enable JavaScript'),
|
||||
subtitle: const Text(
|
||||
'While turning off JavaScript boosts security, privacy, and speed, it may cause some sites to not work as intended.',
|
||||
),
|
||||
value: javacsriptEnabled,
|
||||
onChanged: (value) async {
|
||||
await ref.read(saveSettingsControllerProvider.notifier).save(
|
||||
(currentSettings) =>
|
||||
currentSettings.copyWith.enableJavascript(value),
|
||||
);
|
||||
},
|
||||
),
|
||||
SwitchListTile.adaptive(
|
||||
title: const Text('Launch Links Externally'),
|
||||
subtitle: const Text(
|
||||
'Opens all links (except for kagi.com) in your default browser.',
|
||||
),
|
||||
value: launchUrlExternal,
|
||||
onChanged: (value) async {
|
||||
await ref.read(saveSettingsControllerProvider.notifier).save(
|
||||
(currentSettings) =>
|
||||
currentSettings.copyWith.launchUrlExternal(value),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
String? extractKagiSession(Uri uri) {
|
||||
if (uri.authority == 'kagi.com') {
|
||||
return uri.queryParameters['token'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user