Files
Tanami_App/lib/features/deleteAccount/presentation/bloc/delete_account_bloc.dart
2024-06-06 18:40:58 +05:30

66 lines
1.8 KiB
Dart

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:tanami_app/features/deleteAccount/presentation/bloc/delete_account_event.dart';
import 'package:tanami_app/features/deleteAccount/presentation/bloc/delete_account_state.dart';
class DeleteAccountBloc extends Bloc<DeleteAccountEvent, DeleteAccountState> {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController descriptionTextField = TextEditingController();
GlobalKey<FormState> getFormKey() {
return formKey;
}
DeleteAccountBloc() : super(DeleteAccountInitial()) {
descriptionTextField.addListener(_onFormFieldChanged);
on<DeleteAccountFormChanged>(_onLoginFormChanged);
on<DeleteAccountSubmitted>((event, emit) async {
if (!formKey.currentState!.validate()) {
return;
}
emit(DeleteAccountLoading());
try {
// Simulate API call
await Future.delayed(const Duration(seconds: 2));
// Replace the next line with actual API call
final isSuccess = _mockApiCheck();
if (isSuccess) {
emit(DeleteAccountSuccess());
} else {
emit(const DeleteAccountFailure("Failed."));
}
} catch (e) {
emit(DeleteAccountFailure(e.toString()));
}
});
}
bool _mockApiCheck() {
return true;
}
void _onFormFieldChanged() {
add(DeleteAccountFormChanged(
descriptionTextField.text,
));
}
void _onLoginFormChanged(
DeleteAccountFormChanged event, Emitter<DeleteAccountState> emit) {
final areFieldsFilled = event.description.isNotEmpty;
emit(DeleteAccountFieldsState(areFieldsFilled));
}
// Method to reset text fields
void resetFields() {
descriptionTextField.clear();
}
@override
Future<void> close() {
descriptionTextField.dispose();
return super.close();
}
}