2021-01-06 17:35:57 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
2022-05-04 16:58:47 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
2021-06-20 21:08:52 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
|
2021-08-29 15:02:51 +00:00
|
|
|
import 'package:selfprivacy/logic/models/job.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2021-08-18 09:36:40 +00:00
|
|
|
import 'package:selfprivacy/utils/password_generator.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-01-14 18:45:10 +00:00
|
|
|
class UserFormCubit extends FormCubit {
|
|
|
|
UserFormCubit({
|
2021-07-29 09:34:26 +00:00
|
|
|
required this.jobsCubit,
|
2022-05-04 16:58:47 +00:00
|
|
|
required FieldCubitFactory fieldFactory,
|
2021-03-15 15:39:44 +00:00
|
|
|
User? user,
|
2021-01-06 17:35:57 +00:00
|
|
|
}) {
|
|
|
|
var isEdit = user != null;
|
|
|
|
|
2022-05-04 16:58:47 +00:00
|
|
|
login = fieldFactory.createUserLoginField();
|
2022-05-16 20:30:14 +00:00
|
|
|
login.setValue(isEdit ? user.login : '');
|
2022-05-04 16:58:47 +00:00
|
|
|
password = fieldFactory.createUserPasswordField();
|
|
|
|
password.setValue(
|
2022-05-16 20:30:14 +00:00
|
|
|
isEdit ? (user.password ?? '') : StringGenerators.userPassword());
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-23 19:21:42 +00:00
|
|
|
super.addFields([login, password]);
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
FutureOr<void> onSubmit() {
|
|
|
|
var user = User(
|
|
|
|
login: login.state.value,
|
|
|
|
password: password.state.value,
|
|
|
|
);
|
2021-07-29 09:34:26 +00:00
|
|
|
jobsCubit.addJob(CreateUserJob(user: user));
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 15:39:44 +00:00
|
|
|
late FieldCubit<String> login;
|
|
|
|
late FieldCubit<String> password;
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-01-14 18:45:10 +00:00
|
|
|
void genNewPassword() {
|
2021-08-18 09:36:40 +00:00
|
|
|
password.externalSetValue(StringGenerators.userPassword());
|
2021-01-14 18:45:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 09:34:26 +00:00
|
|
|
final JobsCubit jobsCubit;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|