mirror of
https://github.com/qinglong-app/qinglong_app.git
synced 2025-10-09 16:48:19 +08:00
add task
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:qinglong_app/base/common_dialog.dart';
|
||||
import 'base_viewmodel.dart';
|
||||
|
||||
class BaseStateWidget<T extends BaseViewModel> extends ConsumerStatefulWidget {
|
||||
@@ -36,6 +37,12 @@ class _BaseStateWidgetState<T extends BaseViewModel> extends ConsumerState<BaseS
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var viewModel = ref.watch<T>(widget.model);
|
||||
if (viewModel.failReason != null) {
|
||||
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
|
||||
failDialog(context, viewModel.failReason!);
|
||||
viewModel.clearToast();
|
||||
});
|
||||
}
|
||||
if (viewModel.currentState == PageState.CONTENT) {
|
||||
return widget.builder(ref, viewModel, widget.child);
|
||||
}
|
||||
@@ -50,7 +57,7 @@ class _BaseStateWidgetState<T extends BaseViewModel> extends ConsumerState<BaseS
|
||||
if (viewModel.currentState == PageState.FAILED) {
|
||||
return Container(
|
||||
alignment: Alignment.center,
|
||||
child: Text(viewModel.failReason ??""),
|
||||
child: Text(viewModel.failReason ?? ""),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ class BaseViewModel extends ViewModel {
|
||||
String? failReason;
|
||||
|
||||
void loading({bool notify = false}) {
|
||||
failReason = null;
|
||||
currentState = PageState.LOADING;
|
||||
if (notify) {
|
||||
notifyListeners();
|
||||
@@ -14,6 +15,7 @@ class BaseViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
void success({bool notify = true}) {
|
||||
failReason = null;
|
||||
currentState = PageState.CONTENT;
|
||||
if (notify) {
|
||||
notifyListeners();
|
||||
@@ -27,8 +29,20 @@ class BaseViewModel extends ViewModel {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
void failToast(String? reason, {bool notify = false}) {
|
||||
currentState = PageState.CONTENT;
|
||||
failReason = reason;
|
||||
if (notify) {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void clearToast(){
|
||||
failReason = null;
|
||||
}
|
||||
|
||||
void empty({bool notify = false}) {
|
||||
failReason = null;
|
||||
currentState = PageState.EMPTY;
|
||||
if (notify) {
|
||||
notifyListeners();
|
||||
|
||||
@@ -39,6 +39,16 @@ class Api {
|
||||
}
|
||||
|
||||
static Future<HttpResponse<TaskDetailBean>> addTask(String name, String command, String cron, {String? id}) async {
|
||||
return await Http.put<TaskDetailBean>(Url.ADD_TASK, {"name": name, "command": command, "schedule": cron, "_id": id});
|
||||
var data = {"name": name, "command": command, "schedule": cron};
|
||||
|
||||
if (id != null) {
|
||||
data["_id"] = id;
|
||||
return await Http.put<TaskDetailBean>(Url.ADD_TASK, data);
|
||||
}
|
||||
return await Http.post<TaskDetailBean>(Url.ADD_TASK, data);
|
||||
}
|
||||
|
||||
static Future<HttpResponse<NullResponse>> delTask(String cron) async {
|
||||
return await Http.delete<NullResponse>(Url.ADD_TASK, [cron]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,31 +36,46 @@ class Http {
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> get<T>(String uri, Map<String, String>? json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.get(uri, queryParameters: json);
|
||||
try {
|
||||
_init();
|
||||
var response = await _dio!.get(uri, queryParameters: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
return decodeResponse<T>(response, compute);
|
||||
} on DioError catch (e) {
|
||||
return HttpResponse(success: false, message: e.message, code: 0);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> post<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.post(uri, data: json);
|
||||
try {
|
||||
_init();
|
||||
var response = await _dio!.post(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
return decodeResponse<T>(response, compute);
|
||||
} on DioError catch (e) {
|
||||
return HttpResponse(success: false, message: e.message, code: 0);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> delete<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.delete(uri, data: json);
|
||||
try {
|
||||
_init();
|
||||
var response = await _dio!.delete(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
return decodeResponse<T>(response, compute);
|
||||
} on DioError catch (e) {
|
||||
return HttpResponse(success: false, message: e.message, code: 0);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<HttpResponse<T>> put<T>(String uri, dynamic json, {bool compute = true}) async {
|
||||
_init();
|
||||
var response = await _dio!.put(uri, data: json);
|
||||
|
||||
return decodeResponse<T>(response, compute);
|
||||
try {
|
||||
_init();
|
||||
var response = await _dio!.put(uri, data: json);
|
||||
return decodeResponse<T>(response, compute);
|
||||
} on DioError catch (e) {
|
||||
return HttpResponse(success: false, message: e.message, code: 0);
|
||||
}
|
||||
}
|
||||
|
||||
static HttpResponse<T> decodeResponse<T>(
|
||||
@@ -173,4 +188,4 @@ void decode<T>() async {
|
||||
compute(DeserializeAction.invokeJson, DeserializeAction<T>({}));
|
||||
}
|
||||
|
||||
class NullResponse{}
|
||||
class NullResponse {}
|
||||
|
||||
@@ -7,7 +7,7 @@ class TokenInterceptor extends Interceptor {
|
||||
if (userInfoViewModel.token != null) {
|
||||
options.headers["Authorization"] = "Bearer " + userInfoViewModel.token!;
|
||||
}
|
||||
options.queryParameters["t"] = DateTime.now().millisecondsSinceEpoch;
|
||||
options.queryParameters["t"] = (DateTime.now().millisecondsSinceEpoch~/1000).toString();
|
||||
return handler.next(options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ class Routes {
|
||||
builder: (context) => AddTaskPage(
|
||||
taskBean: settings.arguments as TaskBean,
|
||||
));
|
||||
} else {
|
||||
return CupertinoPageRoute(builder: (context) => AddTaskPage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user