From 2b83a735cb1b7bde042e34ca763c3a9658f35d9d Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 3 Feb 2025 13:42:50 +0800 Subject: [PATCH] fix: advanced options used by default when creating task (#901) --- .../app/modules/create/views/create_view.dart | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/ui/flutter/lib/app/modules/create/views/create_view.dart b/ui/flutter/lib/app/modules/create/views/create_view.dart index 80c0232d..27f93934 100644 --- a/ui/flutter/lib/app/modules/create/views/create_view.dart +++ b/ui/flutter/lib/app/modules/create/views/create_view.dart @@ -82,16 +82,7 @@ class CreateView extends GetView { _urlController.text = url; _urlController.selection = TextSelection.fromPosition( TextPosition(offset: _urlController.text.length)); - final uppercaseUrl = url.toUpperCase(); - Protocol? protocol; - if (uppercaseUrl.startsWith("HTTP:") || - uppercaseUrl.startsWith("HTTPS:")) { - protocol = Protocol.http; - } - if (uppercaseUrl.startsWith("MAGNET:") || - uppercaseUrl.endsWith(".TORRENT")) { - protocol = Protocol.bt; - } + final protocol = parseProtocol(url); if (protocol != null) { final extraHandlers = { Protocol.http: () { @@ -672,6 +663,20 @@ class CreateView extends GetView { ); } + // parse protocol from url + parseProtocol(String url) { + final uppercaseUrl = url.toUpperCase(); + Protocol? protocol; + if (uppercaseUrl.startsWith("HTTP:") || uppercaseUrl.startsWith("HTTPS:")) { + protocol = Protocol.http; + } + if (uppercaseUrl.startsWith("MAGNET:") || + uppercaseUrl.endsWith(".TORRENT")) { + protocol = Protocol.bt; + } + return protocol; + } + // recognize magnet uri, if length == 40, auto add magnet prefix recognizeMagnetUri(String text) { if (text.length != 40) { @@ -764,24 +769,23 @@ class CreateView extends GetView { Object? parseReqExtra(String url) { Object? reqExtra; - if (controller.showAdvanced.value) { - switch (controller.advancedTabController.index) { - case 0: - final header = Map.fromEntries(_httpHeaderControllers - .map((e) => MapEntry(e.name.text, e.value.text))); - header.removeWhere( - (key, value) => key.trim().isEmpty || value.trim().isEmpty); - if (header.isNotEmpty) { - reqExtra = ReqExtraHttp()..header = header; - } - break; - case 1: - if (_btTrackerController.text.trim().isNotEmpty) { - reqExtra = ReqExtraBt() - ..trackers = Util.textToLines(_btTrackerController.text); - } - break; - } + final protocol = parseProtocol(url); + switch (protocol) { + case Protocol.http: + final header = Map.fromEntries(_httpHeaderControllers + .map((e) => MapEntry(e.name.text, e.value.text))); + header.removeWhere( + (key, value) => key.trim().isEmpty || value.trim().isEmpty); + if (header.isNotEmpty) { + reqExtra = ReqExtraHttp()..header = header; + } + break; + case Protocol.bt: + if (_btTrackerController.text.trim().isNotEmpty) { + reqExtra = ReqExtraBt() + ..trackers = Util.textToLines(_btTrackerController.text); + } + break; } return reqExtra; }