Skip to content

Commit

Permalink
custom repeat keep setting
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghongenpin committed Jun 26, 2024
1 parent 13b6d8f commit fc66f57
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"repeatDelay": "Delay(ms)",
"fixed": "fixed",
"random": "random",
"keepCustomSettings": "Keep custom settings",
"editRequest": "Edit and Request",
"reSendRequest": "The request has been resent",
"viewExport": "View Export",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"repeatDelay": "延时(ms)",
"fixed": "固定",
"random": "重放",
"keepCustomSettings": "保持自定义设置",
"editRequest": "编辑请求",
"reSendRequest": "已重新发送请求",
"viewExport": "视图导出",
Expand Down
2 changes: 1 addition & 1 deletion lib/network/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HttpClients {
await connectRequest(hostAndPort, channel);

if (hostAndPort.isSsl()) {
await channel.secureSocket(channelContext);
await channel.secureSocket(channelContext, host: hostAndPort.host);
}

return channel;
Expand Down
8 changes: 6 additions & 2 deletions lib/ui/desktop/left/favorite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:network_proxy/ui/content/panel.dart';
import 'package:network_proxy/ui/desktop/left/repeat.dart';
import 'package:network_proxy/utils/curl.dart';
import 'package:network_proxy/utils/python.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';

/// @author wanghongen
Expand Down Expand Up @@ -165,11 +166,14 @@ class _FavoriteItemState extends State<_FavoriteItem> {
}

//显示高级重发
showCustomRepeat(HttpRequest request) {
showCustomRepeat(HttpRequest request) async {
var prefs = await SharedPreferences.getInstance();
if (!mounted) return;

showDialog(
context: context,
builder: (BuildContext context) {
return CustomRepeatDialog(onRepeat: () => onRepeat(request));
return CustomRepeatDialog(onRepeat: () => onRepeat(request), prefs: prefs);
});
}

Expand Down
86 changes: 69 additions & 17 deletions lib/ui/desktop/left/repeat.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:shared_preferences/shared_preferences.dart';

///高级重放
/// @author wang
class CustomRepeatDialog extends StatefulWidget {
final Function onRepeat;
final SharedPreferences prefs;

const CustomRepeatDialog({super.key, required this.onRepeat});
const CustomRepeatDialog({super.key, required this.onRepeat, required this.prefs});

@override
State<StatefulWidget> createState() => _CustomRepeatState();
Expand All @@ -24,9 +27,27 @@ class _CustomRepeatState extends State<CustomRepeatDialog> {
TextEditingController delay = TextEditingController(text: '0');

bool fixed = true;
bool keepSetting = true;

AppLocalizations get localizations => AppLocalizations.of(context)!;

@override
void initState() {
super.initState();

var customerRepeat = widget.prefs.getString('customerRepeat');
keepSetting = customerRepeat != null;
if (customerRepeat != null) {
Map<String, dynamic> data = jsonDecode(customerRepeat);
count.text = data['count'];
interval.text = data['interval'];
minInterval.text = data['minInterval'];
maxInterval.text = data['maxInterval'];
delay.text = data['delay'];
fixed = data['fixed'] == true;
}
}

@override
void dispose() {
count.dispose();
Expand All @@ -46,7 +67,9 @@ class _CustomRepeatState extends State<CustomRepeatDialog> {
child: ListBody(
children: <Widget>[
field(localizations.repeatCount, count), //次数
Row( //间隔
const SizedBox(height: 8),
Row(
//间隔
children: [
SizedBox(width: 75, child: Text(localizations.repeatInterval)),
const SizedBox(height: 5),
Expand Down Expand Up @@ -95,8 +118,21 @@ class _CustomRepeatState extends State<CustomRepeatDialog> {
]),
],
),
const SizedBox(height: 5),
const SizedBox(height: 8),
field(localizations.repeatDelay, delay), //延时
const SizedBox(height: 8),
//记录选择
Row(mainAxisAlignment: MainAxisAlignment.start, children: [
Text(localizations.keepCustomSettings),
Expanded(
child: Checkbox(
value: keepSetting,
onChanged: (val) {
setState(() {
keepSetting = val == true;
});
})),
])
],
),
)),
Expand All @@ -111,6 +147,20 @@ class _CustomRepeatState extends State<CustomRepeatDialog> {
if (!formKey.currentState!.validate()) {
return;
}
if (keepSetting) {
widget.prefs.setString(
'customerRepeat',
jsonEncode({
'count': count.text,
'interval': interval.text,
'minInterval': minInterval.text,
'maxInterval': maxInterval.text,
'delay': delay.text,
'fixed': fixed
}));
} else {
widget.prefs.remove('customerRepeat');
}

//定时发起请求
Future.delayed(Duration(milliseconds: int.parse(delay.text)), () => submitTask(int.parse(count.text)));
Expand Down Expand Up @@ -152,21 +202,23 @@ class _CustomRepeatState extends State<CustomRepeatDialog> {
);
}

FormField textField(TextEditingController? controller, {TextStyle? style}) {
Widget textField(TextEditingController? controller, {TextStyle? style}) {
Color color = Theme.of(context).colorScheme.primary;

return TextFormField(
controller: controller,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
style: style,
decoration: InputDecoration(
errorStyle: const TextStyle(height: 2, fontSize: 0),
contentPadding: const EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
border: OutlineInputBorder(borderSide: BorderSide(width: 1, color: color.withOpacity(0.3))),
enabledBorder: OutlineInputBorder(borderSide: BorderSide(width: 1.5, color: color.withOpacity(0.5))),
focusedBorder: OutlineInputBorder(borderSide: BorderSide(width: 2, color: color))),
validator: (val) => val == null || val.isEmpty ? localizations.cannotBeEmpty : null,
);
return ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 42),
child: TextFormField(
controller: controller,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
style: style,
decoration: InputDecoration(
errorStyle: const TextStyle(height: 2, fontSize: 0),
contentPadding: const EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
border: OutlineInputBorder(borderSide: BorderSide(width: 1, color: color.withOpacity(0.3))),
enabledBorder: OutlineInputBorder(borderSide: BorderSide(width: 1.5, color: color.withOpacity(0.5))),
focusedBorder: OutlineInputBorder(borderSide: BorderSide(width: 2, color: color))),
validator: (val) => val == null || val.isEmpty ? localizations.cannotBeEmpty : null,
));
}
}
8 changes: 6 additions & 2 deletions lib/ui/desktop/left/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:network_proxy/ui/desktop/left/repeat.dart';
import 'package:network_proxy/utils/curl.dart';
import 'package:network_proxy/utils/lang.dart';
import 'package:network_proxy/utils/python.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';

/// 请求 URI
Expand Down Expand Up @@ -247,11 +248,14 @@ class _RequestWidgetState extends State<RequestWidget> {
}

//显示高级重发
showCustomRepeat(HttpRequest request) {
showCustomRepeat(HttpRequest request) async {
var prefs = await SharedPreferences.getInstance();
if (!mounted) return;

showDialog(
context: context,
builder: (BuildContext context) {
return CustomRepeatDialog(onRepeat: () => onRepeat(request));
return CustomRepeatDialog(onRepeat: () => onRepeat(request), prefs: prefs);
});
}

Expand Down
6 changes: 4 additions & 2 deletions lib/ui/mobile/request/favorite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:network_proxy/ui/mobile/request/repeat.dart';
import 'package:network_proxy/ui/mobile/request/request_editor.dart';
import 'package:network_proxy/utils/curl.dart';
import 'package:network_proxy/utils/python.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MobileFavorites extends StatefulWidget {
final ProxyServer proxyServer;
Expand Down Expand Up @@ -181,8 +182,9 @@ class _FavoriteItemState extends State<_FavoriteItem> {
//显示高级重发
showCustomRepeat(HttpRequest request) {
Navigator.of(context).pop();
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MobileCustomRepeat(onRepeat: () => onRepeat(request))));
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => futureWidget(SharedPreferences.getInstance(),
(prefs) => MobileCustomRepeat(onRepeat: () => onRepeat(request), prefs: prefs))));
}

onRepeat(HttpRequest request) {
Expand Down
51 changes: 50 additions & 1 deletion lib/ui/mobile/request/repeat.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:shared_preferences/shared_preferences.dart';

///高级重放
/// @author wang
class MobileCustomRepeat extends StatefulWidget {
final Function onRepeat;
final SharedPreferences prefs;

const MobileCustomRepeat({super.key, required this.onRepeat});
const MobileCustomRepeat({super.key, required this.onRepeat, required this.prefs});

@override
State<StatefulWidget> createState() => _CustomRepeatState();
Expand All @@ -24,9 +27,27 @@ class _CustomRepeatState extends State<MobileCustomRepeat> {
TextEditingController delay = TextEditingController(text: '0');

bool fixed = true;
bool keepSetting = true;

AppLocalizations get localizations => AppLocalizations.of(context)!;

@override
void initState() {
super.initState();

var customerRepeat = widget.prefs.getString('customerRepeat');
keepSetting = customerRepeat != null;
if (customerRepeat != null) {
Map<String, dynamic> data = jsonDecode(customerRepeat);
count.text = data['count'];
interval.text = data['interval'];
minInterval.text = data['minInterval'];
maxInterval.text = data['maxInterval'];
delay.text = data['delay'];
fixed = data['fixed'] == true;
}
}

@override
void dispose() {
count.dispose();
Expand All @@ -49,6 +70,21 @@ class _CustomRepeatState extends State<MobileCustomRepeat> {
if (!formKey.currentState!.validate()) {
return;
}
if (keepSetting) {
widget.prefs.setString(
'customerRepeat',
jsonEncode({
'count': count.text,
'interval': interval.text,
'minInterval': minInterval.text,
'maxInterval': maxInterval.text,
'delay': delay.text,
'fixed': fixed
}));
} else {
widget.prefs.remove('customerRepeat');
}

Future.delayed(Duration(milliseconds: int.parse(delay.text)), () => submitTask(int.parse(count.text)));
Navigator.of(context).pop();
},
Expand All @@ -66,6 +102,19 @@ class _CustomRepeatState extends State<MobileCustomRepeat> {
intervalWidget(), //间隔
const SizedBox(height: 6),
field(localizations.repeatDelay, delay), //延时
const SizedBox(height: 6),
//记录选择
Row(children: [
Text(localizations.keepCustomSettings),
Expanded(
child: Checkbox(
value: keepSetting,
onChanged: (val) {
setState(() {
keepSetting = val == true;
});
})),
])
],
),
)));
Expand Down
6 changes: 4 additions & 2 deletions lib/ui/mobile/request/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:network_proxy/ui/mobile/widgets/highlight.dart';
import 'package:network_proxy/utils/curl.dart';
import 'package:network_proxy/utils/lang.dart';
import 'package:network_proxy/utils/navigator.dart';
import 'package:shared_preferences/shared_preferences.dart';

///请求行
class RequestRow extends StatefulWidget {
Expand Down Expand Up @@ -212,8 +213,9 @@ class RequestRowState extends State<RequestRow> {
//显示高级重发
showCustomRepeat(HttpRequest request) {
NavigatorHelper.pop();
NavigatorHelper.push(
MaterialPageRoute(builder: (context) => MobileCustomRepeat(onRepeat: () => onRepeat(request))));
NavigatorHelper.push(MaterialPageRoute(
builder: (context) => futureWidget(SharedPreferences.getInstance(),
(prefs) => MobileCustomRepeat(onRepeat: () => onRepeat(request), prefs: prefs))));
}

onRepeat(HttpRequest request) {
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path_provider_foundation
import proxy_manager
import screen_retriever
import share_plus
import shared_preferences_foundation
import url_launcher_macos
import window_manager

Expand All @@ -27,6 +28,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ProxyManagerPlugin.register(with: registry.registrar(forPlugin: "ProxyManagerPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
flutter_desktop_context_menu: ^0.2.0
win32audio: ^1.3.1
device_info_plus: ^10.1.0
shared_preferences: ^2.2.3

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit fc66f57

Please sign in to comment.