2024-03-02 22:53:14 +03:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
2026-01-17 13:09:20 +03:00
|
|
|
import 'package:umbrix/features/config_option/data/config_option_repository.dart';
|
|
|
|
|
import 'package:umbrix/features/connection/data/connection_data_providers.dart';
|
|
|
|
|
import 'package:umbrix/features/connection/notifier/connection_notifier.dart';
|
|
|
|
|
import 'package:umbrix/utils/custom_loggers.dart';
|
2024-03-04 15:58:56 +03:30
|
|
|
import 'package:json_path/json_path.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
|
|
|
|
|
part 'config_option_notifier.g.dart';
|
|
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
|
class ConfigOptionNotifier extends _$ConfigOptionNotifier with AppLogger {
|
|
|
|
|
@override
|
2024-03-03 14:03:36 +03:30
|
|
|
Future<bool> build() async {
|
|
|
|
|
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
2024-07-04 21:04:44 +02:00
|
|
|
final serviceSingboxOptions = ref.read(connectionRepositoryProvider).configOptionsSnapshot;
|
2024-03-03 14:03:36 +03:30
|
|
|
ref.listen(
|
|
|
|
|
ConfigOptions.singboxConfigOptions,
|
|
|
|
|
(previous, next) async {
|
|
|
|
|
if (!serviceRunning || serviceSingboxOptions == null) return;
|
|
|
|
|
if (next case AsyncData(:final value) when next != previous) {
|
2024-07-04 21:04:44 +02:00
|
|
|
if (_lastUpdate == null || DateTime.now().difference(_lastUpdate!) > const Duration(milliseconds: 100)) {
|
2024-03-03 14:03:36 +03:30
|
|
|
_lastUpdate = DateTime.now();
|
|
|
|
|
state = AsyncData(value != serviceSingboxOptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fireImmediately: true,
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime? _lastUpdate;
|
2023-12-01 12:56:24 +03:30
|
|
|
|
2024-03-04 15:58:56 +03:30
|
|
|
Future<bool> exportJsonToClipboard({bool excludePrivate = true}) async {
|
|
|
|
|
try {
|
|
|
|
|
final options = await ref.read(ConfigOptions.singboxConfigOptions.future);
|
|
|
|
|
Map map = options.toJson();
|
|
|
|
|
if (excludePrivate) {
|
|
|
|
|
for (final key in ConfigOptions.privatePreferencesKeys) {
|
|
|
|
|
final query = key.split('.').map((e) => '["$e"]').join();
|
|
|
|
|
final res = JsonPath('\$$query').read(map).firstOrNull;
|
|
|
|
|
if (res != null) {
|
|
|
|
|
map = res.pointer.remove(map)! as Map;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const encoder = JsonEncoder.withIndent(' ');
|
|
|
|
|
final json = encoder.convert(map);
|
|
|
|
|
await Clipboard.setData(ClipboardData(text: json));
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e, st) {
|
|
|
|
|
loggy.warning("error exporting config options to clipboard", e, st);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> importFromClipboard() async {
|
|
|
|
|
try {
|
2024-07-04 21:04:44 +02:00
|
|
|
final input = await Clipboard.getData("text/plain").then((value) => value?.text);
|
2024-03-04 15:58:56 +03:30
|
|
|
if (input == null) return false;
|
|
|
|
|
if (jsonDecode(input) case final Map<String, dynamic> map) {
|
|
|
|
|
for (final option in ConfigOptions.preferences.entries) {
|
|
|
|
|
final query = option.key.split('.').map((e) => '["$e"]').join();
|
|
|
|
|
final res = JsonPath('\$$query').read(map).firstOrNull;
|
|
|
|
|
if (res?.value case final value?) {
|
|
|
|
|
try {
|
|
|
|
|
await ref.read(option.value.notifier).updateRaw(value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
loggy.debug("error updating [${option.key}]: $e", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e, st) {
|
|
|
|
|
loggy.warning("error importing config options to clipboard", e, st);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-12-01 12:56:24 +03:30
|
|
|
}
|
2023-12-12 18:07:02 +03:30
|
|
|
|
|
|
|
|
Future<void> resetOption() async {
|
2024-03-04 15:58:56 +03:30
|
|
|
for (final option in ConfigOptions.preferences.values) {
|
2024-03-02 22:53:14 +03:30
|
|
|
await ref.read(option.notifier).reset();
|
|
|
|
|
}
|
2023-12-12 18:07:02 +03:30
|
|
|
ref.invalidateSelf();
|
|
|
|
|
}
|
2023-12-01 12:56:24 +03:30
|
|
|
}
|