Files
qzl 19aa33a609 fix: correct test failures and error propagation
- Add CacheScope provider in UserProfileCacheRepository tests
- Remove catch blocks that swallowed errors in _loadHistory/_loadMoreHistory
- Errors now properly propagate to switchUser() caller
2026-04-01 15:11:49 +08:00

104 lines
2.9 KiB
Dart

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../core/logging/logger.dart';
import '../../data/models/automation_job_model.dart';
import '../../data/apis/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;
final Logger _logger = getLogger('features.settings.automation_jobs');
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, stackTrace) {
_logger.error(
message: 'Failed to load automation jobs',
error: e,
stackTrace: stackTrace,
);
emit(state.copyWith(isLoading: false, error: e.toString()));
}
}
Future<void> deleteJob(String id) async {
try {
await _api.delete(id);
await loadJobs();
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to delete automation job',
error: e,
stackTrace: stackTrace,
extra: {'job_id': id},
);
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, stackTrace) {
_logger.error(
message: 'Failed to update automation job status',
error: e,
stackTrace: stackTrace,
extra: {'job_id': id, 'enabled': enabled},
);
emit(state.copyWith(error: e.toString()));
}
}
}