diff --git a/ui/flutter/lib/app/modules/create/controllers/create_controller.dart b/ui/flutter/lib/app/modules/create/controllers/create_controller.dart index 4c4ddc424..b1206624f 100644 --- a/ui/flutter/lib/app/modules/create/controllers/create_controller.dart +++ b/ui/flutter/lib/app/modules/create/controllers/create_controller.dart @@ -13,6 +13,7 @@ class CreateController extends GetxController final isResolving = false.obs; final showAdvanced = false.obs; late TabController advancedTabController; + final oldUrl = "".obs; final fileDataUri = "".obs; @override 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 ac804aa9a..febf5eee1 100644 --- a/ui/flutter/lib/app/modules/create/views/create_view.dart +++ b/ui/flutter/lib/app/modules/create/views/create_view.dart @@ -55,7 +55,10 @@ class CreateView extends GetView { _urlController.text = value!.text!; _urlController.selection = TextSelection.fromPosition( TextPosition(offset: _urlController.text.length)); + return; } + + recognizeMagnetUri(value!.text!); } }); } @@ -117,6 +120,10 @@ class CreateView extends GetView { }, onChanged: (v) async { controller.clearFileDataUri(); + if (controller.oldUrl.value.isEmpty) { + recognizeMagnetUri(v); + } + controller.oldUrl.value = v; }, ), ), @@ -237,6 +244,20 @@ class CreateView extends GetView { ); } + // recognize magnet uri, if length == 40, auto add magnet prefix + recognizeMagnetUri(String text) { + if (text.length != 40) { + return; + } + final exp = RegExp(r"[0-9a-fA-F]+"); + if (exp.hasMatch(text)) { + final uri = "magnet:?xt=urn:btih:$text"; + _urlController.text = uri; + _urlController.selection = TextSelection.fromPosition( + TextPosition(offset: _urlController.text.length)); + } + } + Future _doResolve() async { if (controller.isResolving.value) { return;