feat: auto recognize magnet uri (#230)

This commit is contained in:
Levi authored and GitHub committed 2023-10-08 15:29:19 +08:00
1 parent fe55f1bd54
commit c0338c3d8e
2 files changed
+22

No files matched your search

@@ -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
@@ -55,7 +55,10 @@ class CreateView extends GetView<CreateController> {
_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<CreateController> {
},
onChanged: (v) async {
controller.clearFileDataUri();
if (controller.oldUrl.value.isEmpty) {
recognizeMagnetUri(v);
}
controller.oldUrl.value = v;
},
),
),
@@ -237,6 +244,20 @@ class CreateView extends GetView<CreateController> {
);
}
// 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<void> _doResolve() async {
if (controller.isResolving.value) {
return;