Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Proxy Configuration #544

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/models/settings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SettingsModel {
this.historyRetentionPeriod = HistoryRetentionPeriod.oneWeek,
this.workspaceFolderPath,
this.isSSLDisabled = false,
this.proxySettings = const ProxySettings(),
});

final bool isDark;
Expand All @@ -32,6 +33,9 @@ class SettingsModel {
final String? workspaceFolderPath;
final bool isSSLDisabled;

// Proxy settings
final ProxySettings? proxySettings;

SettingsModel copyWith({
bool? isDark,
bool? alwaysShowCollectionPaneScrollbar,
Expand All @@ -45,22 +49,24 @@ class SettingsModel {
HistoryRetentionPeriod? historyRetentionPeriod,
String? workspaceFolderPath,
bool? isSSLDisabled,
ProxySettings? proxySettings,
}) {
return SettingsModel(
isDark: isDark ?? this.isDark,
alwaysShowCollectionPaneScrollbar: alwaysShowCollectionPaneScrollbar ??
this.alwaysShowCollectionPaneScrollbar,
size: size ?? this.size,
offset: offset ?? this.offset,
defaultUriScheme: defaultUriScheme ?? this.defaultUriScheme,
defaultCodeGenLang: defaultCodeGenLang ?? this.defaultCodeGenLang,
offset: offset ?? this.offset,
saveResponses: saveResponses ?? this.saveResponses,
promptBeforeClosing: promptBeforeClosing ?? this.promptBeforeClosing,
activeEnvironmentId: activeEnvironmentId ?? this.activeEnvironmentId,
historyRetentionPeriod:
historyRetentionPeriod ?? this.historyRetentionPeriod,
workspaceFolderPath: workspaceFolderPath ?? this.workspaceFolderPath,
isSSLDisabled: isSSLDisabled ?? this.isSSLDisabled,
proxySettings: proxySettings,
);
}

Expand All @@ -71,15 +77,16 @@ class SettingsModel {
isDark: isDark,
alwaysShowCollectionPaneScrollbar: alwaysShowCollectionPaneScrollbar,
size: size,
offset: offset,
defaultUriScheme: defaultUriScheme,
defaultCodeGenLang: defaultCodeGenLang,
offset: offset,
saveResponses: saveResponses,
promptBeforeClosing: promptBeforeClosing,
activeEnvironmentId: activeEnvironmentId,
historyRetentionPeriod: historyRetentionPeriod,
workspaceFolderPath: workspaceFolderPath,
isSSLDisabled: isSSLDisabled,
proxySettings: proxySettings,
);
}

Expand All @@ -92,13 +99,16 @@ class SettingsModel {
final dx = data["dx"] as double?;
final dy = data["dy"] as double?;
Size? size;

if (width != null && height != null) {
size = Size(width, height);
}
Offset? offset;

if (dx != null && dy != null) {
offset = Offset(dx, dy);
}

final defaultUriSchemeStr = data["defaultUriScheme"] as String?;
SupportedUriSchemes? defaultUriScheme;
if (defaultUriSchemeStr != null) {
Expand All @@ -109,6 +119,7 @@ class SettingsModel {
// pass
}
}

final defaultCodeGenLangStr = data["defaultCodeGenLang"] as String?;
CodegenLanguage? defaultCodeGenLang;
if (defaultCodeGenLangStr != null) {
Expand All @@ -119,6 +130,7 @@ class SettingsModel {
// pass
}
}

final saveResponses = data["saveResponses"] as bool?;
final promptBeforeClosing = data["promptBeforeClosing"] as bool?;
final activeEnvironmentId = data["activeEnvironmentId"] as String?;
Expand All @@ -132,9 +144,19 @@ class SettingsModel {
// pass
}
}

final workspaceFolderPath = data["workspaceFolderPath"] as String?;
final isSSLDisabled = data["isSSLDisabled"] as bool?;

ProxySettings? proxySettings;
if (data["proxySettings"] != null) {
try {
proxySettings = ProxySettings.fromJson(Map<String, dynamic>.from(data["proxySettings"]));
} catch (e) {
// pass
}
}

const sm = SettingsModel();

return sm.copyWith(
Expand All @@ -151,6 +173,7 @@ class SettingsModel {
historyRetentionPeriod ?? HistoryRetentionPeriod.oneWeek,
workspaceFolderPath: workspaceFolderPath,
isSSLDisabled: isSSLDisabled,
proxySettings: proxySettings,
);
}

Expand All @@ -170,6 +193,7 @@ class SettingsModel {
"historyRetentionPeriod": historyRetentionPeriod.name,
"workspaceFolderPath": workspaceFolderPath,
"isSSLDisabled": isSSLDisabled,
"proxySettings": proxySettings?.toJson(),
};
}

Expand All @@ -194,7 +218,8 @@ class SettingsModel {
other.activeEnvironmentId == activeEnvironmentId &&
other.historyRetentionPeriod == historyRetentionPeriod &&
other.workspaceFolderPath == workspaceFolderPath &&
other.isSSLDisabled == isSSLDisabled;
other.isSSLDisabled == isSSLDisabled &&
other.proxySettings == proxySettings;
}

@override
Expand All @@ -213,6 +238,7 @@ class SettingsModel {
historyRetentionPeriod,
workspaceFolderPath,
isSSLDisabled,
proxySettings,
);
}
}
1 change: 1 addition & 0 deletions lib/providers/collection_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class CollectionStateNotifier
substitutedHttpRequestModel,
defaultUriScheme: defaultUriScheme,
noSSL: noSSL,
proxySettings: ref.read(settingsProvider).proxySettings,
);

late final RequestModel newRequestModel;
Expand Down
11 changes: 9 additions & 2 deletions lib/providers/settings_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class ThemeStateNotifier extends StateNotifier<SettingsModel> {
HistoryRetentionPeriod? historyRetentionPeriod,
String? workspaceFolderPath,
bool? isSSLDisabled,
ProxySettings? proxySettings,
}) async {
state = state.copyWith(

final newState = state.copyWith(
isDark: isDark,
alwaysShowCollectionPaneScrollbar: alwaysShowCollectionPaneScrollbar,
size: size,
Expand All @@ -47,7 +49,12 @@ class ThemeStateNotifier extends StateNotifier<SettingsModel> {
historyRetentionPeriod: historyRetentionPeriod,
workspaceFolderPath: workspaceFolderPath,
isSSLDisabled: isSSLDisabled,
proxySettings: proxySettings,
);
await setSettingsToSharedPrefs(state);

if (newState != state) {
state = newState;
await setSettingsToSharedPrefs(state);
}
}
}
18 changes: 18 additions & 0 deletions lib/screens/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ class SettingsPage extends ConsumerWidget {
ref.read(settingsProvider.notifier).update(isDark: value);
},
),
SwitchListTile(
title: const Text('Proxy Settings'),
subtitle: Text(ref.watch(settingsProvider).proxySettings != null
? 'Enabled - ${ref.watch(settingsProvider).proxySettings!.host}:${ref.watch(settingsProvider).proxySettings!.port}'
: 'Disabled'
),
value: ref.watch(settingsProvider).proxySettings != null,
onChanged: (bool value) {
if (!value) {
ref.read(settingsProvider.notifier).update(proxySettings: null);
} else {
showDialog(
context: context,
builder: (context) => const ProxySettingsDialog(),
);
}
},
),
SwitchListTile(
hoverColor: kColorTransparent,
title: const Text('Collection Pane Scrollbar Visiblity'),
Expand Down
109 changes: 109 additions & 0 deletions lib/widgets/dialog_proxy_settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash_core/models/models.dart';
import 'package:apidash/providers/settings_providers.dart';

class ProxySettingsDialog extends ConsumerStatefulWidget {
const ProxySettingsDialog({super.key});

@override
ConsumerState<ProxySettingsDialog> createState() =>
_ProxySettingsDialogState();
}

class _ProxySettingsDialogState extends ConsumerState<ProxySettingsDialog> {
late TextEditingController _hostController;
late TextEditingController _portController;
late TextEditingController _usernameController;
late TextEditingController _passwordController;

@override
void initState() {
super.initState();
final settings = ref.read(settingsProvider);
final proxy = settings.proxySettings;
_hostController = TextEditingController(text: proxy?.host ?? '');
_portController = TextEditingController(text: proxy?.port ?? '');
_usernameController = TextEditingController(text: proxy?.username ?? '');
_passwordController = TextEditingController(text: proxy?.password ?? '');
}

@override
void dispose() {
_hostController.dispose();
_portController.dispose();
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}

void _saveSettings() {
if (_hostController.text.isNotEmpty && _portController.text.isNotEmpty) {
final newProxy = ProxySettings(
host: _hostController.text,
port: _portController.text,
username: _usernameController.text.isEmpty ? null : _usernameController.text,
password: _passwordController.text.isEmpty ? null : _passwordController.text,
);
ref.read(settingsProvider.notifier).update(proxySettings: newProxy);
Navigator.of(context).pop();
}
else{
ref.read(settingsProvider.notifier).update(proxySettings: null);
}
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Proxy Settings'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: _hostController,
decoration: const InputDecoration(
labelText: 'Proxy Host',
hintText: 'e.g., localhost',
),
),
const SizedBox(height: 8),
TextField(
controller: _portController,
decoration: const InputDecoration(
labelText: 'Proxy Port',
hintText: 'e.g., 8080',
),
),
const SizedBox(height: 8),
TextField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username (Optional)',
),
),
const SizedBox(height: 8),
TextField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password (Optional)',
),
obscureText: true,
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
TextButton(
onPressed: _saveSettings,
child: const Text('Save'),
),
],
);
}
}
1 change: 1 addition & 0 deletions lib/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export 'dialog_about.dart';
export 'dialog_history_retention.dart';
export 'dialog_import.dart';
export 'dialog_ok_cancel.dart';
export 'dialog_proxy_settings.dart';
export 'dialog_rename.dart';
export 'dialog_text.dart';
export 'drag_and_drop_area.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/apidash_core/lib/models/models.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'http_request_model.dart';
export 'http_response_model.dart';
export 'proxy_settings_model.dart';
17 changes: 17 additions & 0 deletions packages/apidash_core/lib/models/proxy_settings_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'proxy_settings_model.freezed.dart';
part 'proxy_settings_model.g.dart';

@freezed
class ProxySettings with _$ProxySettings {
const factory ProxySettings({
@Default('') String host,
@Default('') String port,
String? username,
String? password,
}) = _ProxySettings;

factory ProxySettings.fromJson(Map<String, dynamic> json) =>
_$ProxySettingsFromJson(json);
}
Loading