fix proxy name resolution

This commit is contained in:
Fabian Freund
2026-05-24 09:29:02 +02:00
parent e34acf2777
commit 0b0ef38636
10 changed files with 233 additions and 15 deletions
@@ -27,6 +27,9 @@ import 'package:weblibre/features/proxy/domain/repositories/singbox_proxy_profil
part 'proxy_connection_options.g.dart';
const loadingProxyTitle = 'Loading proxy...';
const unknownProxyTitle = 'Unknown proxy';
class ProxyConnectionOption with FastEquatable {
final ProxyConnectionId id;
final String title;
@@ -72,13 +75,21 @@ List<ProxyConnectionOption> proxyConnectionOptions(Ref ref) {
String proxyConnectionTitle(
List<ProxyConnectionOption> options,
ProxyConnectionId proxyConnectionId,
) {
ProxyConnectionId proxyConnectionId, {
bool isLoading = false,
}) {
for (final option in options) {
if (option.id == proxyConnectionId) {
return option.title;
}
}
return 'Unknown proxy';
return isLoading ? loadingProxyTitle : unknownProxyTitle;
}
bool proxyConnectionOptionExists(
List<ProxyConnectionOption> options,
ProxyConnectionId proxyConnectionId,
) {
return options.any((option) => option.id == proxyConnectionId);
}
@@ -87,7 +87,7 @@ final class SingboxProxyRuntimeRepositoryProvider
}
String _$singboxProxyRuntimeRepositoryHash() =>
r'bb84ab57abd3b7261105da85d3e047cef1b31a44';
r'660543fd3f82a5fcc5c3c85b11d0a98d472638af';
abstract class _$SingboxProxyRuntimeRepository
extends $AsyncNotifier<SingboxProxyRuntimeState> {
@@ -24,6 +24,7 @@ import 'package:weblibre/core/logger.dart';
import 'package:weblibre/features/geckoview/features/tabs/data/models/container_data.dart';
import 'package:weblibre/features/proxy/data/proxy_connection.dart';
import 'package:weblibre/features/proxy/domain/providers/proxy_connection_options.dart';
import 'package:weblibre/features/proxy/domain/repositories/singbox_proxy_profiles.dart';
import 'package:weblibre/features/proxy/domain/repositories/singbox_proxy_runtime.dart';
import 'package:weblibre/features/tor/presentation/controllers/start_tor_proxy.dart';
import 'package:weblibre/features/tor/presentation/widgets/tor_dialog.dart';
@@ -112,8 +113,8 @@ Future<bool> _maybeStartSingboxProxy(
if (isRunning) return true;
final proxyTitle = proxyConnectionTitle(
ref.read(proxyConnectionOptionsProvider),
final proxyTitle = await _proxyConnectionTitleForPrompt(
ref,
proxyConnectionId,
);
final shouldStart = await showDialog<bool>(
@@ -157,6 +158,35 @@ Future<bool> _maybeStartSingboxProxy(
}
}
Future<String> _proxyConnectionTitleForPrompt(
WidgetRef ref,
ProxyConnectionId proxyConnectionId,
) async {
final options = ref.read(proxyConnectionOptionsProvider);
final title = proxyConnectionTitle(options, proxyConnectionId);
if (title != unknownProxyTitle) {
return title;
}
if (proxyConnectionId is SingboxProxyConnectionId) {
try {
final profile = await ref
.read(singboxProxyProfilesRepositoryProvider.notifier)
.findProfile(proxyConnectionId.profileId);
if (profile != null) return profile.name;
} catch (error, stackTrace) {
logger.w(
'Failed to resolve sing-box proxy profile ${proxyConnectionId.profileId} for prompt title',
error: error,
stackTrace: stackTrace,
);
}
}
return proxyConnectionTitle(options, proxyConnectionId);
}
Future<SingboxProxyRuntimeState> _resolveRuntimeStateForPrompt(
WidgetRef ref,
) async {
@@ -21,6 +21,7 @@ import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:weblibre/features/proxy/data/proxy_connection.dart';
import 'package:weblibre/features/proxy/domain/providers/proxy_connection_options.dart';
import 'package:weblibre/features/proxy/domain/repositories/singbox_proxy_profiles.dart';
import 'package:weblibre/features/settings/presentation/widgets/settings_detail.dart';
import 'package:weblibre/features/user/data/models/proxy_routing_settings.dart';
import 'package:weblibre/features/user/domain/repositories/proxy_routing_settings.dart';
@@ -129,8 +130,10 @@ class _GlobalRoutingProxySection extends ConsumerWidget {
}
final options = ref.watch(proxyConnectionOptionsProvider);
final optionsState = ref.watch(singboxProxyProfilesRepositoryProvider);
return _ProxyConnectionPicker(
options: options,
optionsLoaded: optionsState.hasValue,
selectedId: settings.regularTabsProxyConnectionId,
onChanged: (id) => ref
.read(proxyRoutingSettingsRepositoryProvider.notifier)
@@ -148,8 +151,10 @@ class _PrivateTabsProxySection extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final settings = ref.watch(proxyRoutingSettingsWithDefaultsProvider);
final options = ref.watch(proxyConnectionOptionsProvider);
final optionsState = ref.watch(singboxProxyProfilesRepositoryProvider);
return _ProxyConnectionPicker(
options: options,
optionsLoaded: optionsState.hasValue,
selectedId: settings.privateTabsProxyConnectionId,
onChanged: (id) => ref
.read(proxyRoutingSettingsRepositoryProvider.notifier)
@@ -162,11 +167,13 @@ class _PrivateTabsProxySection extends ConsumerWidget {
class _ProxyConnectionPicker extends StatelessWidget {
final List<ProxyConnectionOption> options;
final bool optionsLoaded;
final ProxyConnectionId? selectedId;
final ValueChanged<ProxyConnectionId?> onChanged;
const _ProxyConnectionPicker({
required this.options,
required this.optionsLoaded,
required this.selectedId,
required this.onChanged,
});
@@ -174,7 +181,9 @@ class _ProxyConnectionPicker extends StatelessWidget {
@override
Widget build(BuildContext context) {
final hasUnknownSelection =
selectedId != null && !options.any((option) => option.id == selectedId);
selectedId != null &&
optionsLoaded &&
!proxyConnectionOptionExists(options, selectedId!);
return RadioGroup<ProxyConnectionId?>(
groupValue: selectedId,