feat: auto get create url from clipboard (#193)

This commit is contained in:
Levi authored and GitHub committed 2023-09-02 11:32:28 +08:00
1 parent 0f0c8f27bd
commit 06766eea56
1 file changed
+171 -146
@@ -30,6 +30,8 @@ class CreateView extends GetView<CreateController> {
final _httpRefererController = TextEditingController(); final _httpRefererController = TextEditingController();
final _btTrackerController = TextEditingController(); final _btTrackerController = TextEditingController();
final _availableSchemes = ["http:", "https:", "magnet:"];
CreateView({Key? key}) : super(key: key); CreateView({Key? key}) : super(key: key);
@override @override
@@ -37,8 +39,24 @@ class CreateView extends GetView<CreateController> {
final String? filePath = Get.rootDelegate.arguments(); final String? filePath = Get.rootDelegate.arguments();
if (_urlController.text.isEmpty && filePath != null) { if (_urlController.text.isEmpty && filePath != null) {
_urlController.text = filePath; _urlController.text = filePath;
WidgetsBinding.instance.addPostFrameCallback((_) async { _urlController.selection = TextSelection.fromPosition(
await _doResolve(); TextPosition(offset: _urlController.text.length));
}
// if no file path, read from clipboard
if (filePath?.isEmpty ?? true) {
Clipboard.getData('text/plain').then((value) {
if (value?.text?.isNotEmpty ?? false) {
if (_availableSchemes
.where((e) =>
value!.text!.startsWith(e) ||
value.text!.startsWith(e.toUpperCase()))
.isNotEmpty) {
_urlController.text = value!.text!;
_urlController.selection = TextSelection.fromPosition(
TextPosition(offset: _urlController.text.length));
}
}
}); });
} }
@@ -60,154 +78,161 @@ class CreateView extends GetView<CreateController> {
final bytes = await details.files[0].readAsBytes(); final bytes = await details.files[0].readAsBytes();
controller.setFileDataUri(bytes); controller.setFileDataUri(bytes);
}, },
child: Padding( child: GestureDetector(
padding: behavior: HitTestBehavior.opaque,
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0), onTap: () {
child: Form( FocusScope.of(context).requestFocus(FocusNode());
key: _resolveFormKey, },
autovalidateMode: AutovalidateMode.onUserInteraction, child: Padding(
child: Column(children: [ padding:
Row(children: [ const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
Expanded( child: Form(
child: TextFormField( key: _resolveFormKey,
autofocus: true, autovalidateMode: AutovalidateMode.onUserInteraction,
controller: _urlController, child: Column(children: [
minLines: 1, Row(children: [
maxLines: 5, Expanded(
decoration: InputDecoration( child: TextFormField(
hintText: _hitText(), autofocus: true,
hintStyle: const TextStyle(fontSize: 12), controller: _urlController,
labelText: 'downloadLink'.tr, minLines: 1,
icon: const Icon(Icons.link), maxLines: 5,
suffixIcon: IconButton( decoration: InputDecoration(
onPressed: () { hintText: _hitText(),
_urlController.clear(); hintStyle: const TextStyle(fontSize: 12),
controller.clearFileDataUri(); labelText: 'downloadLink'.tr,
}, icon: const Icon(Icons.link),
icon: const Icon(Icons.clear), suffixIcon: IconButton(
), onPressed: () {
), _urlController.clear();
validator: (v) { controller.clearFileDataUri();
return v!.trim().isNotEmpty },
? null icon: const Icon(Icons.clear),
: 'downloadLinkValid'.tr;
},
onChanged: (v) async {
controller.clearFileDataUri();
},
),
),
IconButton(
icon: const Icon(Icons.folder_open),
onPressed: () async {
var pr = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ["torrent"]);
if (pr != null) {
if (!Util.isWeb()) {
_urlController.text = pr.files[0].path ?? "";
return;
}
_urlController.text = pr.files[0].name;
controller.setFileDataUri(pr.files[0].bytes!);
}
}),
]
//.where((e) => e != null).map((e) => e!).toList(),
),
Obx(() => Visibility(
visible: controller.showAdvanced.value,
child: Padding(
padding: const EdgeInsets.only(left: 40, top: 15),
child: Column(
children: [
TabBar(
controller: controller.advancedTabController,
tabs: const [
Tab(
text: 'HTTP',
),
Tab(
text: 'BitTorrent',
)
],
), ),
AutoScaleTabBarView(
controller: controller.advancedTabController,
children: [
Column(
children: [
TextFormField(
controller: _httpUaController,
decoration: const InputDecoration(
labelText: 'User-Agent',
)),
TextFormField(
controller: _httpCookieController,
decoration: const InputDecoration(
labelText: 'Cookie',
)),
TextFormField(
controller: _httpRefererController,
decoration: const InputDecoration(
labelText: 'Referer',
)),
],
),
Column(
children: [
TextFormField(
controller: _btTrackerController,
maxLines: 5,
decoration: InputDecoration(
labelText: 'Trakers',
hintText: 'addTrackerHit'.tr,
)),
],
)
],
)
],
),
))),
Center(
child: Padding(
padding: const EdgeInsets.only(top: 15),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(right: 10),
child: TextButton(
onPressed: () {
controller.showAdvanced.value =
!controller.showAdvanced.value;
},
child:
Row(mainAxisSize: MainAxisSize.min, children: [
Obx(() => Checkbox(
value: controller.showAdvanced.value,
onChanged: (bool? value) {
controller.showAdvanced.value =
value ?? false;
},
)),
Text('advancedOptions'.tr),
]),
), ),
validator: (v) {
return v!.trim().isNotEmpty
? null
: 'downloadLinkValid'.tr;
},
onChanged: (v) async {
controller.clearFileDataUri();
},
), ),
SizedBox( ),
width: 150, IconButton(
child: RoundedLoadingButton( icon: const Icon(Icons.folder_open),
color: Get.theme.colorScheme.secondary, onPressed: () async {
onPressed: _doResolve, var pr = await FilePicker.platform.pickFiles(
controller: _confirmController, type: FileType.custom,
child: Text('confirm'.tr), allowedExtensions: ["torrent"]);
if (pr != null) {
if (!Util.isWeb()) {
_urlController.text = pr.files[0].path ?? "";
return;
}
_urlController.text = pr.files[0].name;
controller.setFileDataUri(pr.files[0].bytes!);
}
}),
]
//.where((e) => e != null).map((e) => e!).toList(),
),
Obx(() => Visibility(
visible: controller.showAdvanced.value,
child: Padding(
padding: const EdgeInsets.only(left: 40, top: 15),
child: Column(
children: [
TabBar(
controller: controller.advancedTabController,
tabs: const [
Tab(
text: 'HTTP',
),
Tab(
text: 'BitTorrent',
)
],
),
AutoScaleTabBarView(
controller: controller.advancedTabController,
children: [
Column(
children: [
TextFormField(
controller: _httpUaController,
decoration: const InputDecoration(
labelText: 'User-Agent',
)),
TextFormField(
controller: _httpCookieController,
decoration: const InputDecoration(
labelText: 'Cookie',
)),
TextFormField(
controller: _httpRefererController,
decoration: const InputDecoration(
labelText: 'Referer',
)),
],
),
Column(
children: [
TextFormField(
controller: _btTrackerController,
maxLines: 5,
decoration: InputDecoration(
labelText: 'Trakers',
hintText: 'addTrackerHit'.tr,
)),
],
)
],
)
],
), ),
), ))),
], Center(
), child: Padding(
)), padding: const EdgeInsets.only(top: 15),
]))), child: Column(
children: [
Padding(
padding: const EdgeInsets.only(right: 10),
child: TextButton(
onPressed: () {
controller.showAdvanced.value =
!controller.showAdvanced.value;
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Obx(() => Checkbox(
value: controller.showAdvanced.value,
onChanged: (bool? value) {
controller.showAdvanced.value =
value ?? false;
},
)),
Text('advancedOptions'.tr),
]),
),
),
SizedBox(
width: 150,
child: RoundedLoadingButton(
color: Get.theme.colorScheme.secondary,
onPressed: _doResolve,
controller: _confirmController,
child: Text('confirm'.tr),
),
),
],
),
)),
]))),
),
), ),
); );
} }