selfprivacy.org.app/lib/logic/cubit/jobs/jobs_cubit.dart

54 lines
1.4 KiB
Dart
Raw Normal View History

2021-05-25 21:53:54 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2021-06-08 18:52:44 +00:00
import 'package:selfprivacy/logic/api_maps/server.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
2021-06-08 18:52:44 +00:00
import 'package:selfprivacy/logic/models/jobs/job.dart';
2021-05-25 21:53:54 +00:00
import 'package:equatable/equatable.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/logic/models/user.dart';
2021-05-25 21:53:54 +00:00
export 'package:provider/provider.dart';
part 'jobs_state.dart';
class JobsCubit extends Cubit<JobsState> {
2021-07-29 05:24:42 +00:00
JobsCubit(this.usersCubit) : super(JobsStateEmpty());
2021-05-25 21:53:54 +00:00
2021-06-08 18:52:44 +00:00
final api = ServerApi();
2021-07-29 05:24:42 +00:00
final UsersCubit usersCubit;
2021-05-25 21:53:54 +00:00
void addJob(Job job) {
2021-07-29 05:24:42 +00:00
var newJobsList = <Job>[];
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).jobList);
}
newJobsList.add(job);
emit(JobsStateWithJobs(newJobsList));
2021-05-25 21:53:54 +00:00
}
void removeJob(String id) {
2021-07-29 05:24:42 +00:00
final newState = (state as JobsStateWithJobs).removeById(id);
2021-05-25 21:53:54 +00:00
emit(newState);
}
2021-06-08 18:52:44 +00:00
Future<void> applyAll() async {
2021-07-29 05:24:42 +00:00
if (state is JobsStateWithJobs) {
var jobs = (state as JobsStateWithJobs).jobList;
emit(JobsStateLoading());
var newUsers = <User>[];
for (var job in jobs) {
if (job is CreateUserJob) {
newUsers.add(job.user);
await api.createUser(job.user);
}
2021-06-08 18:52:44 +00:00
}
2021-07-29 05:24:42 +00:00
usersCubit.addUsers(newUsers);
await api.apply();
emit(JobsStateEmpty());
getIt<NavigationService>().navigator!.pop();
2021-06-08 18:52:44 +00:00
}
2021-05-25 21:53:54 +00:00
}
}