Skip to content

Commit

Permalink
Add support to create update tag translation task
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Oct 3, 2024
1 parent f7a000b commit c63da91
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/api/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ abstract class _EHApi {
@Part(name: "type") String t = "import",
@CancelRequest() CancelToken? cancel,
});
@PUT('/task')
@MultiPart()
Future<ApiResult<Task>> createUpdateTagTranslationTask({
@Part(name: "cfg") UpdateTagTranslationConfig? cfg,
@Part(name: "type") String t = "update_tag_translation",
@CancelRequest() CancelToken? cancel,
});
@GET('/task/download_cfg')
Future<ApiResult<DownloadConfig>> getDefaultDownloadConfig(
{@CancelRequest() CancelToken? cancel});
Expand Down
51 changes: 51 additions & 0 deletions lib/api/client.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/api/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,12 @@ class DefaultImportConfig {
_$DefaultImportConfigFromJson(json);
Map<String, dynamic> toJson() => _$DefaultImportConfigToJson(this);
}

@JsonSerializable()
class UpdateTagTranslationConfig {
UpdateTagTranslationConfig({this.file});
String? file;
factory UpdateTagTranslationConfig.fromJson(Map<String, dynamic> json) =>
_$UpdateTagTranslationConfigFromJson(json);
Map<String, dynamic> toJson() => _$UpdateTagTranslationConfigToJson(this);
}
12 changes: 12 additions & 0 deletions lib/api/task.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions lib/dialog/new_update_tag_translation_task_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:logging/logging.dart';
import '../api/task.dart';
import '../globals.dart';

final _log = Logger("NewUpdateTagTranslationTaskPage");

class NewUpdateTagTranslationTaskPage extends StatefulWidget {
const NewUpdateTagTranslationTaskPage({super.key});

static const routeName = "/dialog/new_update_tag_translation_task";

@override
State<NewUpdateTagTranslationTaskPage> createState() =>
_NewUpdateTagTranslationTaskPage();
}

class _NewUpdateTagTranslationTaskPage
extends State<NewUpdateTagTranslationTaskPage> {
final _formKey = GlobalKey<FormState>();
bool _ok = false;
bool _isCreating = false;
CancelToken? _cancel;
String _file = "";

@override
void dispose() {
_cancel?.cancel();
super.dispose();
}

Widget _buildWithVecticalPadding(Widget child) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 8),
child: child,
);
}

Future<void> create() async {
try {
_cancel = CancelToken();
setState(() {
_isCreating = true;
});
final cfg =
_file.isEmpty ? null : UpdateTagTranslationConfig(file: _file);
(await api.createUpdateTagTranslationTask(cfg: cfg, cancel: _cancel))
.unwrap();
_ok = true;
if (!_cancel!.isCancelled) {
setState(() {
_isCreating = false;
});
}
} catch (e) {
if (!_cancel!.isCancelled) {
_log.warning("Failed to create import task:", e);
setState(() {
_isCreating = false;
});
}
}
}

@override
Widget build(BuildContext context) {
tryInitApi(context);
if (_ok) {
WidgetsBinding.instance!.addPostFrameCallback((_) {
context.canPop() ? context.pop() : context.go("/task_manager");
});
_ok = false;
}
final i18n = AppLocalizations.of(context)!;
final maxWidth = MediaQuery.of(context).size.width;
return Container(
padding: maxWidth < 400
? const EdgeInsets.symmetric(vertical: 20, horizontal: 5)
: const EdgeInsets.all(20),
width: maxWidth < 810 ? null : 800,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
children: [
Stack(
alignment: Alignment.center,
children: [
Text(
i18n.createUpdateTagTranslationTask,
style: Theme.of(context).textTheme.headlineSmall,
),
Align(
alignment: Alignment.centerRight,
child: IconButton(
onPressed: () => context.canPop()
? context.pop()
: context.go("/task_manager"),
icon: const Icon(Icons.close),
)),
],
),
_buildWithVecticalPadding(TextFormField(
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: i18n.translationFile,
helperText: i18n.translationFileHelp,
helperMaxLines: 3,
),
onChanged: (value) {
_file = value;
},
)),
_buildWithVecticalPadding(ElevatedButton(
onPressed: _isCreating
? null
: () {
create();
},
child: Text(i18n.create))),
],
))));
}
}
5 changes: 4 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,8 @@
"share": "Share",
"failedChangeExpireTime": "Failed to change expired time: ",
"failedShareGallery": "Failed to share gallery: ",
"updateTagTranslation": "Update tag's translation"
"updateTagTranslation": "Update tag's translation",
"createUpdateTagTranslationTask": "Create update tag's translation task",
"translationFile": "Tnanslation file",
"translationFileHelp": "The path to json file contains tag's translation. Will download from website if this is empty."
}
5 changes: 4 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,8 @@
"share": "分享",
"failedChangeExpireTime": "修改过期时间失败:",
"failedShareGallery": "分享画廊失败:",
"updateTagTranslation": "更新标签的翻译"
"updateTagTranslation": "更新标签的翻译",
"createUpdateTagTranslationTask": "创建更新标签翻译的任务",
"translationFile": "翻译文件",
"translationFileHelp": "包含标签翻译的JSON文件路径。未指定时将从网站上下载。"
}
10 changes: 10 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'dialog/gallery_share_page.dart';
import 'dialog/new_download_task_page.dart';
import 'dialog/new_export_zip_task_page.dart';
import 'dialog/new_import_task_page.dart';
import 'dialog/new_update_tag_translation_task_page.dart';
import 'dialog/new_user_page.dart';
import 'dialog/task_page.dart';
import 'globals.dart';
Expand Down Expand Up @@ -327,6 +328,15 @@ final _router = GoRouter(
return "/";
}
}),
GoRoute(
path: NewUpdateTagTranslationTaskPage.routeName,
pageBuilder: (context, state) {
return DialogPage(
key: state.pageKey,
builder: (context) {
return NewUpdateTagTranslationTaskPage();
});
}),
],
observers: [
_NavigatorObserver(),
Expand Down
6 changes: 6 additions & 0 deletions lib/pages/task_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ class _TaskManagerPage extends State<TaskManagerPage>
value: TaskType.import,
child: Text(i18n.createImportTask),
),
PopupMenuItem(
value: TaskType.updateTagTranslation,
child: Text(i18n.createUpdateTagTranslationTask),
),
];
},
onSelected: (TaskType type) {
Expand All @@ -230,6 +234,8 @@ class _TaskManagerPage extends State<TaskManagerPage>
context.push("/dialog/new_export_zip_task");
} else if (type == TaskType.import) {
context.push("/dialog/new_import_task");
} else if (type == TaskType.updateTagTranslation) {
context.push("/dialog/new_update_tag_translation_task");
}
});
}
Expand Down

0 comments on commit c63da91

Please sign in to comment.