任务列表优化

This commit is contained in:
jyuesong
2022-05-30 10:41:09 +08:00
parent 9a27ae8e69
commit 2c64038fda
2 changed files with 344 additions and 263 deletions

View File

@ -3,95 +3,46 @@ import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:qinglong_app/base/base_state_widget.dart';
import 'package:qinglong_app/base/routes.dart';
import 'package:qinglong_app/base/single_account_page.dart';
import 'package:qinglong_app/base/theme.dart';
import 'package:qinglong_app/base/ui/abs_underline_tabindicator.dart';
import 'package:qinglong_app/base/ui/empty_widget.dart';
import 'package:qinglong_app/base/ui/menu.dart';
import 'package:qinglong_app/base/ui/ql_context_menu.dart';
import 'package:qinglong_app/module/task/intime_log/intime_log_page.dart';
import 'package:qinglong_app/module/task/task_bean.dart';
import 'package:qinglong_app/module/task/task_detail/task_detail_page.dart';
import 'package:qinglong_app/module/task/task_viewmodel.dart';
import 'package:qinglong_app/utils/utils.dart';
class TaskPage extends StatefulWidget {
class TaskPage extends ConsumerStatefulWidget {
const TaskPage({Key? key}) : super(key: key);
@override
_TaskPageState createState() => _TaskPageState();
}
class _TaskPageState extends State<TaskPage> {
class _TaskPageState extends ConsumerState<TaskPage> {
final TextEditingController _searchController = TextEditingController();
String currentState = TaskViewModel.allStr;
@override
void initState() {
super.initState();
_searchController.addListener(() {
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return BaseStateWidget<TaskViewModel>(
builder: (ref, model, child) {
return Column(
mainAxisSize: MainAxisSize.max,
children: [
searchCell(ref),
Expanded(
child: DefaultTabController(
length: 5,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TabBar(
tabs: const [
Tab(
text: "全部",
),
Tab(
text: "正在运行",
),
Tab(
text: "从未运行",
),
Tab(
text: "拉库脚本",
),
Tab(
text: "已禁用",
),
],
isScrollable: true,
indicator: AbsUnderlineTabIndicator(
wantWidth: 20,
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2,
)),
),
Expanded(
child: TabBarView(
children: [
body(model, model.list, ref),
body(model, model.running, ref),
body(model, model.neverRunning, ref),
body(model, model.notScripts, ref),
body(model, model.disabled, ref),
],
),
),
],
),
),
),
],
);
return body(model, getListByType(model), ref);
},
model: taskProvider,
onReady: (viewModel) {
viewModel.loadData();
viewModel.loadData(context);
},
);
}
@ -100,26 +51,51 @@ class _TaskPageState extends State<TaskPage> {
return RefreshIndicator(
color: Theme.of(context).primaryColor,
onRefresh: () async {
return model.loadData(false);
return model.loadData( false);
},
child: list.isEmpty
? const EmptyWidget()
: ListView.builder(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
itemBuilder: (context, index) {
TaskBean item = list[index];
if (_searchController.text.isEmpty ||
(item.name?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.command?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.schedule?.contains(_searchController.text.toLowerCase()) ?? false)) {
return TaskItemCell(item, ref);
} else {
return const SizedBox.shrink();
}
},
itemCount: list.length,
),
child: IconTheme(
data: const IconThemeData(
size: 25,
),
child: SlidableAutoCloseBehavior(
child: ListView.separated(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
itemBuilder: (context, index) {
if (index == 0) {
return searchCell(ref);
}
TaskBean item = list[index - 1];
if (_searchController.text.isEmpty ||
(item.name?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.command?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.schedule?.contains(_searchController.text.toLowerCase()) ?? false)) {
return TaskItemCell(item, ref);
} else {
return const SizedBox.shrink();
}
},
itemCount: list.length + 1,
separatorBuilder: (BuildContext context, int index) {
if (index == 0) return const SizedBox.shrink();
TaskBean item = list[index - 1];
if (_searchController.text.isEmpty ||
(item.name?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.command?.toLowerCase().contains(_searchController.text.toLowerCase()) ?? false) ||
(item.schedule?.contains(_searchController.text.toLowerCase()) ?? false)) {
return Container(
color: ref.watch(themeProvider).themeColor.settingBgColor(),
child: const Divider(
height: 1,
indent: 15,
),
);
} else {
return const SizedBox.shrink();
}
},
),
),
),
);
}
@ -129,41 +105,149 @@ class _TaskPageState extends State<TaskPage> {
horizontal: 15,
vertical: 10,
),
child: CupertinoSearchTextField(
onSubmitted: (value) {
setState(() {});
},
onSuffixTap: () {
_searchController.text = "";
setState(() {});
},
controller: _searchController,
borderRadius: BorderRadius.circular(
30,
),
padding: const EdgeInsets.symmetric(
horizontal: 5,
vertical: 5,
),
suffixInsets: const EdgeInsets.only(
right: 15,
),
prefixInsets: EdgeInsets.only(
top: Platform.isAndroid ? 10 : 6,
bottom: 6,
left: 15,
),
placeholderStyle: TextStyle(
fontSize: 16,
color: context.watch(themeProvider).themeColor.descColor(),
),
style: const TextStyle(
fontSize: 16,
),
placeholder: "搜索",
child: Row(
children: [
Expanded(
child: CupertinoSearchTextField(
onSubmitted: (value) {
setState(() {});
},
onSuffixTap: () {
_searchController.text = "";
setState(() {});
},
controller: _searchController,
borderRadius: BorderRadius.circular(
30,
),
padding: const EdgeInsets.symmetric(
horizontal: 5,
vertical: 5,
),
suffixInsets: const EdgeInsets.only(
right: 15,
),
prefixInsets: EdgeInsets.only(
top: Platform.isAndroid ? 10 : 6,
bottom: 6,
left: 15,
),
placeholderStyle: TextStyle(
fontSize: 16,
color: context.watch(themeProvider).themeColor.descColor(),
),
style: const TextStyle(
fontSize: 16,
),
placeholder: "搜索",
),
),
Material(
color: Colors.transparent,
child: PopupMenuButton<String>(
onSelected: (String result) {
currentState = result;
setState(() {});
},
itemBuilder: (BuildContext context) => [
PopupMenuItem(
child: Text(
TaskViewModel.allStr,
style: TextStyle(
color: currentState == TaskViewModel.allStr ? ref.watch(themeProvider).primaryColor : ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 14,
),
),
value: TaskViewModel.allStr,
),
PopupMenuItem(
child: Text(
TaskViewModel.runningStr,
style: TextStyle(
color:
currentState == TaskViewModel.runningStr ? ref.watch(themeProvider).primaryColor : ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 14,
),
),
value: TaskViewModel.runningStr,
),
PopupMenuItem(
child: Text(
TaskViewModel.neverStr,
style: TextStyle(
color: currentState == TaskViewModel.neverStr ? ref.watch(themeProvider).primaryColor : ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 14,
),
),
value: TaskViewModel.neverStr,
),
PopupMenuItem(
child: Row(
children: [
Text(
TaskViewModel.notScriptStr,
style: TextStyle(
color: currentState == TaskViewModel.notScriptStr
? ref.watch(themeProvider).primaryColor
: ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 14,
),
),
],
),
value: TaskViewModel.notScriptStr,
),
PopupMenuItem(
child: Text(
TaskViewModel.disableStr,
style: TextStyle(
fontSize: 14,
color:
currentState == TaskViewModel.disableStr ? ref.watch(themeProvider).primaryColor : ref.watch(themeProvider).themeColor.titleColor(),
),
),
value: TaskViewModel.disableStr,
),
],
child: Padding(
padding: const EdgeInsets.only(
left: 10,
),
child: Text(
"筛选",
style: TextStyle(
color: ref.watch(themeProvider).themeColor.descColor(),
fontSize: 14,
),
),
),
),
),
],
),
);
}
List<TaskBean> getListByType(TaskViewModel model) {
if (currentState == TaskViewModel.allStr) {
return model.list;
}
if (currentState == TaskViewModel.runningStr) {
return model.running;
}
if (currentState == TaskViewModel.neverStr) {
return model.neverRunning;
}
if (currentState == TaskViewModel.notScriptStr) {
return model.notScripts;
}
if (currentState == TaskViewModel.disableStr) {
return model.disabled;
}
return model.list;
}
}
class TaskItemCell extends StatelessWidget {
@ -176,93 +260,91 @@ class TaskItemCell extends StatelessWidget {
Widget build(BuildContext context) {
return ColoredBox(
color: ref.watch(themeProvider).themeColor.settingBgColor(),
child: QlCupertinoContextMenu(
bean: bean,
actions: [
QLCupertinoContextMenuAction(
child: Text(
bean.status! == 1 ? "运行" : "停止运行",
),
trailingIcon: bean.status! == 1 ? CupertinoIcons.memories : CupertinoIcons.stop_circle,
onPressed: () {
Navigator.of(context).pop();
if (bean.status! == 1) {
startCron(context, ref);
} else {
stopCron(context, ref);
}
},
),
QLCupertinoContextMenuAction(
child: const Text("查看日志"),
onPressed: () {
Navigator.of(context).pop();
logCron(context, ref);
},
trailingIcon: CupertinoIcons.clock,
),
QLCupertinoContextMenuAction(
child: const Text("编辑"),
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed(Routes.routeAddTask, arguments: bean);
},
trailingIcon: CupertinoIcons.pencil_outline,
),
QLCupertinoContextMenuAction(
child: Text(bean.isPinned! == 0 ? "置顶" : "取消置顶"),
onPressed: () {
Navigator.of(context).pop();
pinTask();
},
trailingIcon: bean.isPinned! == 0 ? CupertinoIcons.pin : CupertinoIcons.pin_slash,
),
QLCupertinoContextMenuAction(
child: Text(bean.isDisabled! == 0 ? "禁用" : "启用"),
onPressed: () {
Navigator.of(context).pop();
enableTask();
},
isDestructiveAction: true,
trailingIcon: bean.isDisabled! == 0 ? Icons.dnd_forwardslash : Icons.check_circle_outline_sharp,
),
QLCupertinoContextMenuAction(
child: const Text("删除"),
onPressed: () {
Navigator.of(context).pop();
delTask(context, ref);
},
isDestructiveAction: true,
trailingIcon: CupertinoIcons.delete,
),
],
previewBuilder: (context, anima, child) {
bool isDark = ref.watch(themeProvider).darkMode;
return ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Container(
color: isDark ? const Color(0xff333333) : Theme.of(context).scaffoldBackgroundColor,
padding: EdgeInsets.only(
left: 5,
right: 5,
top: 5,
bottom: isDark ? 5 : 20,
),
child: SizedBox(
height: 200,
child: TaskDetailPage(
bean,
hideAppbar: true,
),
),
),
);
},
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
child: Slidable(
key: ValueKey(bean.sId),
endActionPane: ActionPane(
motion: const StretchMotion(),
extentRatio: 0.7,
children: [
Container(
SlidableAction(
backgroundColor: const Color(0xff5D5E70),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
Navigator.of(context).pushNamed(Routes.routeAddTask, arguments: bean);
});
},
foregroundColor: Colors.white,
icon: CupertinoIcons.pencil_outline,
),
SlidableAction(
backgroundColor: const Color(0xffF19A39),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
pinTask(context);
});
},
foregroundColor: Colors.white,
icon: bean.isPinned! == 0 ? CupertinoIcons.pin : CupertinoIcons.pin_slash,
),
SlidableAction(
backgroundColor: const Color(0xffA356D6),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
enableTask(context);
});
},
foregroundColor: Colors.white,
icon: bean.isDisabled! == 0 ? Icons.dnd_forwardslash : Icons.check_circle_outline_sharp,
),
SlidableAction(
backgroundColor: const Color(0xffEA4D3E),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
delTask(context, ref);
});
},
foregroundColor: Colors.white,
icon: CupertinoIcons.delete,
),
],
),
startActionPane: ActionPane(
motion: const StretchMotion(),
extentRatio: 0.4,
children: [
SlidableAction(
backgroundColor: const Color(0xffD25535),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
if (bean.status! == 1) {
startCron(context, ref);
} else {
stopCron(context, ref);
}
});
},
foregroundColor: Colors.white,
icon: bean.status! == 1 ? CupertinoIcons.memories : CupertinoIcons.stop_circle,
),
SlidableAction(
backgroundColor: const Color(0xff606467),
onPressed: (_) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
logCron(context, ref);
});
},
foregroundColor: Colors.white,
icon: CupertinoIcons.text_justifyleft,
),
],
),
child: Material(
color: ref.watch(themeProvider).themeColor.settingBgColor(),
child: InkWell(
onTap: () {
Navigator.of(context).pushNamed(Routes.routeTaskDetail, arguments: bean);
},
child: Container(
width: MediaQuery.of(context).size.width,
color: bean.isPinned == 1 ? ref.watch(themeProvider).themeColor.pinColor() : Colors.transparent,
padding: const EdgeInsets.symmetric(
@ -282,11 +364,21 @@ class TaskItemCell extends StatelessWidget {
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
bean.isDisabled == 1
? const Icon(
Icons.dnd_forwardslash,
size: 18,
color: Color(0xffEA4D3E),
)
: const SizedBox.shrink(),
SizedBox(
width: bean.isDisabled == 1 ? 5 : 0,
),
bean.status == 1
? const SizedBox.shrink()
: SizedBox(
width: 15,
height: 15,
width: 13,
height: 13,
child: CircularProgressIndicator(
strokeWidth: 2,
color: ref.watch(themeProvider).primaryColor,
@ -305,7 +397,7 @@ class TaskItemCell extends StatelessWidget {
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 18,
fontSize: 16,
),
),
),
@ -328,37 +420,22 @@ class TaskItemCell extends StatelessWidget {
],
),
const SizedBox(
height: 8,
height: 5,
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
bean.isDisabled == 1
? const Icon(
Icons.dnd_forwardslash,
size: 12,
color: Colors.red,
)
: const SizedBox.shrink(),
const SizedBox(
width: 5,
Material(
color: Colors.transparent,
child: Text(
bean.schedule ?? "",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: ref.watch(themeProvider).themeColor.descColor(),
fontSize: 12,
),
Material(
color: Colors.transparent,
child: Text(
bean.schedule ?? "",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: ref.watch(themeProvider).themeColor.descColor(),
fontSize: 12,
),
),
),
],
),
),
const SizedBox(
height: 8,
height: 5,
),
Material(
color: Colors.transparent,
@ -376,30 +453,27 @@ class TaskItemCell extends StatelessWidget {
],
),
),
const Divider(
height: 1,
indent: 15,
),
],
),
),
),
);
}
startCron(BuildContext context, WidgetRef ref) async {
await ref.read(taskProvider).runCrons(bean.sId!);
await ref.read(taskProvider).runCrons( bean.sId!);
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
logCron(context, ref);
});
}
stopCron(BuildContext context, WidgetRef ref) {
ref.read(taskProvider).stopCrons(bean.sId!);
ref.read(taskProvider).stopCrons( bean.sId!);
}
logCron(BuildContext context, WidgetRef ref) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
showCupertinoDialog(
useRootNavigator: false,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text(
@ -416,7 +490,7 @@ class TaskItemCell extends StatelessWidget {
),
onPressed: () {
Navigator.of(context).pop();
ref.read(taskProvider).loadData(false);
ref.read(taskProvider).loadData( false);
},
),
],
@ -426,17 +500,18 @@ class TaskItemCell extends StatelessWidget {
});
}
void enableTask() {
ref.read(taskProvider).enableTask(bean.sId!, bean.isDisabled!);
void enableTask(BuildContext context) {
ref.read(taskProvider).enableTask( bean.sId!, bean.isDisabled!);
}
void pinTask() {
ref.read(taskProvider).pinTask(bean.sId!, bean.isPinned!);
void pinTask(BuildContext context) {
ref.read(taskProvider).pinTask( bean.sId!, bean.isPinned!);
}
void delTask(BuildContext context, WidgetRef ref) {
showCupertinoDialog(
context: context,
useRootNavigator: false,
builder: (context) => CupertinoAlertDialog(
title: const Text("确认删除"),
content: Text("确认删除定时任务 ${bean.name ?? ""}"),
@ -461,7 +536,7 @@ class TaskItemCell extends StatelessWidget {
),
onPressed: () {
Navigator.of(context).pop();
ref.read(taskProvider).delCron(bean.sId!);
ref.watch(taskProvider.notifier).delCron(bean.sId!);
},
),
],

View File

@ -15,6 +15,11 @@ Map<int, int> sort = {
};
class TaskViewModel extends BaseViewModel {
static const String allStr = "全部脚本";
static const String runningStr = "正在运行";
static const String neverStr = "从未运行";
static const String notScriptStr = "拉库脚本";
static const String disableStr = "禁用脚本";
List<TaskBean> list = [];
List<TaskBean> running = [];
List<TaskBean> neverRunning = [];
@ -41,12 +46,26 @@ class TaskViewModel extends BaseViewModel {
void sortList() {
list.sort((TaskBean a, TaskBean b) {
int? sortA = (a.isPinned == 1 && a.status != 0) ? 5 : ((a.isDisabled == 1 && a.status != 0) ? 4 : a.status);
int? sortB = (b.isPinned == 1 && b.status != 0) ? 5 : ((b.isDisabled == 1 && b.status != 0) ? 4 : b.status);
int? sortA = (a.isPinned == 1 && a.status != 0)
? 5
: (a.isDisabled == 1 && a.status != 0)
? 4
: a.status;
int? sortB = (b.isPinned == 1 && b.status != 0)
? 5
: (b.isDisabled == 1 && b.status != 0)
? 4
: b.status;
return sort[sortA!]! - sort[sortB!]!;
});
for (int i = 0; i < list.length; i++) {
if (list[i].isPinned == 1) {
list.insert(0, list.removeAt(i));
}
}
running.clear();
running.addAll(list.where((element) => element.status == 0));
neverRunning.clear();
@ -60,9 +79,7 @@ class TaskViewModel extends BaseViewModel {
Future<void> runCrons(String cron) async {
HttpResponse<NullResponse> result = await Api.startTasks([cron]);
if (result.success) {
list.firstWhere((element) => element.sId == cron).status = 0;
sortList();
notifyListeners();
loadData(false);
} else {
failToast(result.message, notify: true);
}
@ -71,9 +88,7 @@ class TaskViewModel extends BaseViewModel {
Future<void> stopCrons(String cron) async {
HttpResponse<NullResponse> result = await Api.stopTasks([cron]);
if (result.success) {
list.firstWhere((element) => element.sId == cron).status = 1;
sortList();
notifyListeners();
loadData(false);
} else {
failToast(result.message, notify: true);
}
@ -82,9 +97,8 @@ class TaskViewModel extends BaseViewModel {
Future<void> delCron(String id) async {
HttpResponse<NullResponse> result = await Api.delTask(id);
if (result.success) {
list.removeWhere((element) => element.sId == id);
"删除成功".toast();
notifyListeners();
loadData(false);
} else {
failToast(result.message, notify: true);
}
@ -108,9 +122,7 @@ class TaskViewModel extends BaseViewModel {
if (response.success) {
"取消置顶成功".toast();
list.firstWhere((element) => element.sId == sId).isPinned = 0;
sortList();
success();
loadData(false);
} else {
failToast(response.message, notify: true);
}
@ -119,9 +131,7 @@ class TaskViewModel extends BaseViewModel {
if (response.success) {
"置顶成功".toast();
list.firstWhere((element) => element.sId == sId).isPinned = 1;
sortList();
success();
loadData(false);
} else {
failToast(response.message, notify: true);
}
@ -134,9 +144,7 @@ class TaskViewModel extends BaseViewModel {
if (response.success) {
"禁用成功".toast();
list.firstWhere((element) => element.sId == sId).isDisabled = 1;
sortList();
success();
loadData(false);
} else {
failToast(response.message, notify: true);
}
@ -145,9 +153,7 @@ class TaskViewModel extends BaseViewModel {
if (response.success) {
"启用成功".toast();
list.firstWhere((element) => element.sId == sId).isDisabled = 0;
sortList();
success();
loadData(false);
} else {
failToast(response.message, notify: true);
}