feat: 添加自动化任务(automation_jobs)功能模块
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../data/models/automation_job_model.dart';
|
||||
import '../../data/services/automation_jobs_api.dart';
|
||||
|
||||
class AutomationJobsState extends Equatable {
|
||||
final List<AutomationJobModel> jobs;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const AutomationJobsState({
|
||||
this.jobs = const [],
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
List<AutomationJobModel> get systemJobs =>
|
||||
jobs.where((j) => j.isSystem).toList();
|
||||
List<AutomationJobModel> get userJobs =>
|
||||
jobs.where((j) => !j.isSystem).toList();
|
||||
List<AutomationJobModel> get dailyJobs =>
|
||||
jobs.where((j) => j.isDaily).toList();
|
||||
List<AutomationJobModel> get weeklyJobs =>
|
||||
jobs.where((j) => j.isWeekly).toList();
|
||||
bool get canCreateMore => userJobs.length < 3;
|
||||
|
||||
AutomationJobsState copyWith({
|
||||
List<AutomationJobModel>? jobs,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return AutomationJobsState(
|
||||
jobs: jobs ?? this.jobs,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [jobs, isLoading, error];
|
||||
}
|
||||
|
||||
class AutomationJobsCubit extends Cubit<AutomationJobsState> {
|
||||
final AutomationJobsApi _api;
|
||||
|
||||
AutomationJobsCubit(this._api) : super(AutomationJobsState());
|
||||
|
||||
Future<void> loadJobs() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final jobs = await _api.list();
|
||||
emit(state.copyWith(jobs: jobs, isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteJob(String id) async {
|
||||
try {
|
||||
await _api.delete(id);
|
||||
await loadJobs();
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateJobStatus({
|
||||
required String id,
|
||||
required bool enabled,
|
||||
}) async {
|
||||
try {
|
||||
final updated = await _api.update(
|
||||
id,
|
||||
AutomationJobUpdateRequest(status: enabled ? 'active' : 'disabled'),
|
||||
);
|
||||
final nextJobs = state.jobs
|
||||
.map((job) => job.id == id ? updated : job)
|
||||
.toList();
|
||||
emit(state.copyWith(jobs: nextJobs));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../data/models/automation_job_model.dart';
|
||||
import '../../data/services/automation_jobs_api.dart';
|
||||
|
||||
class JobDetailState extends Equatable {
|
||||
final AutomationJobModel? job;
|
||||
final bool isLoading;
|
||||
final bool isSaving;
|
||||
final String? error;
|
||||
|
||||
const JobDetailState({
|
||||
this.job,
|
||||
this.isLoading = false,
|
||||
this.isSaving = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
JobDetailState copyWith({
|
||||
AutomationJobModel? job,
|
||||
bool? isLoading,
|
||||
bool? isSaving,
|
||||
String? error,
|
||||
}) {
|
||||
return JobDetailState(
|
||||
job: job ?? this.job,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
isSaving: isSaving ?? this.isSaving,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [job, isLoading, isSaving, error];
|
||||
}
|
||||
|
||||
class JobDetailCubit extends Cubit<JobDetailState> {
|
||||
final AutomationJobsApi _api;
|
||||
|
||||
JobDetailCubit(this._api) : super(JobDetailState());
|
||||
|
||||
Future<void> loadJob(String id) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final job = await _api.get(id);
|
||||
emit(state.copyWith(job: job, isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> updateJob(String id, AutomationJobUpdateRequest request) async {
|
||||
emit(state.copyWith(isSaving: true));
|
||||
try {
|
||||
final job = await _api.update(id, request);
|
||||
emit(state.copyWith(job: job, isSaving: false));
|
||||
return true;
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isSaving: false, error: e.toString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> deleteJob(String id) async {
|
||||
emit(state.copyWith(isSaving: true, error: null));
|
||||
try {
|
||||
await _api.delete(id);
|
||||
emit(state.copyWith(isSaving: false));
|
||||
return true;
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isSaving: false, error: e.toString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> createJob(AutomationJobCreateRequest request) async {
|
||||
emit(state.copyWith(isSaving: true, error: null));
|
||||
try {
|
||||
final job = await _api.create(request);
|
||||
emit(state.copyWith(job: job, isSaving: false));
|
||||
return true;
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isSaving: false, error: e.toString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user