From d3c00cc99ffc0810610e5188392d177a5bd9cd22 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Fri, 9 Feb 2024 19:44:58 +0300 Subject: [PATCH] Removed excess controllers --- .../bloc/diary_edit_bloc.dart | 192 +----------------- .../bloc/diary_edit_state.dart | 182 +---------------- 2 files changed, 5 insertions(+), 369 deletions(-) diff --git a/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_bloc.dart b/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_bloc.dart index 98d6b5e3..7254d633 100644 --- a/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_bloc.dart +++ b/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_bloc.dart @@ -24,7 +24,7 @@ class DiaryEditBloc extends Bloc { final box = await Hive.openBox('token'); final token = box.get('token'); final response = await http.get( - Uri.parse(dotenv.env['SERVER_ROOT']! + '/profile/index'), + Uri.parse(dotenv.env['SERVER_ROOT']! + '/diary/index'), headers: { 'Authorization': 'Bearer $token', }, @@ -33,201 +33,17 @@ class DiaryEditBloc extends Bloc { if (response.statusCode == 200) { final data = jsonDecode(response.body); // Проверка наличия ключевых полей в JSON - if (data is Map && - data.containsKey('user') && - data.containsKey('userDetails') && - data.containsKey('chartPoints') && - data.containsKey('circumferences')) { + if (data is Map && data.containsKey('id')) { // Все ключевые поля присутствуют, продолжаем обработку if (state is DiaryEditLoadedState) { // Если текущее состояние уже загружено, обновляем его var currentState = state as DiaryEditLoadedState; - var plans = [] - ..addAll(data["finishedPlans"]) - ..addAll(data["rejectedPlans"]) - ..addAll(data["startedPlan"]) - ..addAll(data["paidPlan"]) - ..addAll(data["requestedPlan"]) - ..addAll(data["initedPlan"]); - // Обновляем значения контроллеров только если они уже были созданы - emit((currentState).copyWith( - chartPoints: currentState.chartPoints, - chartDates: currentState.chartDates, - circumferences: currentState.circumferences, - user: data['user'], - userDetails: data['userDetails'], - plans: plans, - token: token, - chestCircumferenceController: TextEditingController(), - waistCircumferenceController: TextEditingController(), - hipCircumferenceController: TextEditingController(), - weightController: TextEditingController(), - chestCircumferenceFormController: - currentState.chestCircumferenceFormController, - waistCircumferenceFormController: - currentState.waistCircumferenceFormController, - hipCircumferenceFormController: - currentState.hipCircumferenceFormController, - firstNameFormController: currentState.firstNameFormController, - lastNameFormController: currentState.lastNameFormController, - countryFormController: currentState.countryFormController, - cityFormController: currentState.cityFormController, - heightFormController: currentState.heightFormController, - upPressureFormController: currentState.upPressureFormController, - downPressureFormController: - currentState.downPressureFormController, - allergiesFormController: currentState.allergiesFormController, - pillsFormController: currentState.pillsFormController, - favoriteFormController: currentState.favoriteFormController, - unfavoriteFormController: currentState.unfavoriteFormController, - additionalFormController: currentState.additionalFormController, - sportsFormController: currentState.sportsFormController, - dateFormController: currentState.dateFormController, - hypoFormController: currentState.hypoFormController, - hyperFormController: currentState.hyperFormController, - selectedCity: currentState.selectedCity, - selectedCountry: currentState.selectedCountry, - )); + emit((currentState).copyWith()); } else { - var plans = [] - ..addAll(data["finishedPlans"]) - ..addAll(data["rejectedPlans"]) - ..addAll(data["startedPlan"]) - ..addAll(data["paidPlan"]) - ..addAll(data["requestedPlan"]) - ..addAll(data["initedPlan"]); - - /*print(data["chartPoints"].length); - print(data["chartPoints"].runtimeType); - print(data["circumferences"].length); - print(data["circumferences"].runtimeType);*/ - if (data["chartPoints"].length == 1) { - data["chartPoints"] = []; - } - if (data["circumferences"].length == 1) { - data["circumferences"] = []; - } - var circumferences = data["circumferences"].isEmpty - ? data["circumferences"].asMap().map((index, value) { - return MapEntry(index.toString(), value); - }) - : data["circumferences"]; - var chartPoints = data["chartPoints"].isEmpty - ? data["chartPoints"].asMap().map((index, value) { - return MapEntry(index.toString(), value); - }) - : data["chartPoints"]; - - /*List weights = [0.0, 0.0, 0.0, 0.0]; - List dates = [ - "0000-00-00 00:00:00", - "0000-00-00 00:00:00", - "0000-00-00 00:00:00", - "0000-00-00 00:00:00" - ]; - for (int i = 0; i < data["chartPoints"].length; i++) { - weights[i] = data["chartPoints"][i]["weight"].toDouble(); - dates[i] = data["chartPoints"][i]["created_at"].toString(); - } - print(data);*/ - List dates = [ - "0000-00-00 00:00:00", - "0000-00-00 00:00:00", - "0000-00-00 00:00:00", - "0000-00-00 00:00:00" - ]; - for (int i = 0; i < data["circumferences"].length; i++) { - dates[i] = data["circumferences"]['$i']["created_at"]; - } - if (chartPoints.length < 4) { - for (int i = chartPoints.length; i < 4; i++) { - chartPoints["$i"] = { - "id": 0, - "user_id": 0, - "weight": 0, - "created_at": "0000-00-00 00:00:00", - "updated_at": "0000-00-00 00:00:00" - }; - } - } - - if (circumferences.length < 4) { - for (int i = circumferences.length; i < 4; i++) { - circumferences["$i"] = { - "id": 0, - "user_id": 0, - "chest_circumference": 0, - "waist_circumference": 0, - "hip_circumference": 0, - "created_at": "0000-00-00 00:00:00", - "updated_at": "0000-00-00 00:00:00" - }; - } - } - - /*print(chartPoints); - print(circumferences);*/ - /*print(weights); - print(weights.runtimeType); - print(circumferences); - print(circumferences.runtimeType);*/ // Если текущее состояние - начальное, создаем новое загруженное состояние - emit(DiaryEditLoadedState( - chartPoints: chartPoints, - chartDates: dates, - circumferences: circumferences, - user: data['user'], - userDetails: data['userDetails'], - plans: plans, - token: token, - chestCircumferenceController: TextEditingController(), - waistCircumferenceController: TextEditingController(), - hipCircumferenceController: TextEditingController(), - hypoFormController: data['userDetails']['has_hypo'].toString(), - hyperFormController: data['userDetails']['has_hyper'].toString(), - chestCircumferenceFormController: TextEditingController( - text: data['userDetails']['chest_circumference']?.toString()), - waistCircumferenceFormController: TextEditingController( - text: data['userDetails']['waist_circumference']?.toString()), - hipCircumferenceFormController: TextEditingController( - text: data['userDetails']['hip_circumference']?.toString()), - weightController: TextEditingController( - text: data['userDetails']['weight']?.toString()), - weightFormController: TextEditingController( - text: data['userDetails']['weight']?.toString()), - firstNameFormController: TextEditingController( - text: data['userDetails']['first_name']), - lastNameFormController: - TextEditingController(text: data['userDetails']['last_name']), - countryFormController: - TextEditingController(text: data['userDetails']['country']), - cityFormController: - TextEditingController(text: data['userDetails']['city']), - heightFormController: TextEditingController( - text: data['userDetails']['height']?.toString()), - upPressureFormController: TextEditingController( - text: data['userDetails']['blood_pressure_up']?.toString()), - downPressureFormController: TextEditingController( - text: data['userDetails']['blood_pressure_down']?.toString()), - allergiesFormController: TextEditingController( - text: data['userDetails']['about_alergy']), - pillsFormController: TextEditingController( - text: data['userDetails']['about_taking_pils']), - sportsFormController: TextEditingController( - text: data['userDetails']['about_sport_hobbies']), - favoriteFormController: TextEditingController( - text: data['userDetails']['favorite_foods']), - unfavoriteFormController: TextEditingController( - text: data['userDetails']['unfavorite_foods']), - additionalFormController: TextEditingController( - text: data['userDetails']['additional_info']), - dateFormController: - TextEditingController(text: data['userDetails']['birthdate']), - selectedCity: data['userDetails']['city'], - selectedCountry: data['userDetails']['country'], - )); + emit(DiaryEditLoadedState()); } } else { // Один или несколько ключевых полей отсутствуют, обрабатываем ошибку diff --git a/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_state.dart b/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_state.dart index a512813f..7b9da4ab 100644 --- a/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_state.dart +++ b/lib/presentation/auth/auth_screen/diary/diary_edit_screen/bloc/diary_edit_state.dart @@ -18,199 +18,19 @@ class DiaryEditInitialState extends DiaryEditState { /// Represents the state of DiaryEdit in the application. class DiaryEditLoadedState extends DiaryEditState { - DiaryEditLoadedState( - {this.chestCircumferenceController, - this.waistCircumferenceController, - this.hipCircumferenceController, - this.diaryEditModelObj, - this.weightController, - this.user, - this.userDetails, - this.chartPoints, - this.chartDates, - this.plans, - this.token, - this.selectedCountry, - this.selectedCity, - this.chestCircumferenceFormController, - this.waistCircumferenceFormController, - this.hipCircumferenceFormController, - this.firstNameFormController, - this.lastNameFormController, - this.countryFormController, - this.dateFormController, - this.cityFormController, - this.heightFormController, - this.weightFormController, - this.upPressureFormController, - this.downPressureFormController, - this.allergiesFormController, - this.pillsFormController, - this.sportsFormController, - this.favoriteFormController, - this.unfavoriteFormController, - this.additionalFormController, - this.hypoFormController, - this.hyperFormController, - this.circumferences}) + DiaryEditLoadedState({this.diaryEditModelObj}) : super(diaryEditModelObj: null); DiaryEditModel? diaryEditModelObj; - Map? user; - Map? userDetails; - Map? circumferences; - Map? chartPoints; - List? chartDates; - List? plans; - TextEditingController? weightController; - TextEditingController? chestCircumferenceController; - TextEditingController? waistCircumferenceController; - TextEditingController? hipCircumferenceController; - TextEditingController? weightFormController; - TextEditingController? chestCircumferenceFormController; - TextEditingController? waistCircumferenceFormController; - TextEditingController? hipCircumferenceFormController; - TextEditingController? firstNameFormController; - TextEditingController? lastNameFormController; - TextEditingController? countryFormController; - TextEditingController? dateFormController; - TextEditingController? cityFormController; - TextEditingController? heightFormController; - TextEditingController? upPressureFormController; - TextEditingController? downPressureFormController; - TextEditingController? allergiesFormController; - TextEditingController? pillsFormController; - TextEditingController? sportsFormController; - TextEditingController? favoriteFormController; - TextEditingController? unfavoriteFormController; - TextEditingController? additionalFormController; - String? token; - String? selectedCountry; - String? selectedCity; - String? hypoFormController; - String? hyperFormController; @override List get props => [ diaryEditModelObj, - user, - chartPoints, - chartDates, - circumferences, - plans, - weightController, - chestCircumferenceController, - waistCircumferenceController, - hipCircumferenceController, - token, - chestCircumferenceFormController, - waistCircumferenceFormController, - hipCircumferenceFormController, - firstNameFormController, - lastNameFormController, - countryFormController, - dateFormController, - cityFormController, - heightFormController, - weightFormController, - upPressureFormController, - downPressureFormController, - allergiesFormController, - pillsFormController, - sportsFormController, - favoriteFormController, - unfavoriteFormController, - additionalFormController, - selectedCountry, - selectedCity, - hypoFormController, - hyperFormController, ]; DiaryEditLoadedState copyWith({ DiaryEditModel? diaryEditModelObj, - Map? user, - Map? userDetails, - List? plans, - Map? chartPoints, - List? chartDates, - Map? circumferences, - TextEditingController? weightController, - TextEditingController? chestCircumferenceController, - TextEditingController? waistCircumferenceController, - TextEditingController? hipCircumferenceController, - TextEditingController? chestCircumferenceFormController, - TextEditingController? waistCircumferenceFormController, - TextEditingController? hipCircumferenceFormController, - TextEditingController? firstNameFormController, - TextEditingController? lastNameFormController, - TextEditingController? countryFormController, - TextEditingController? dateFormController, - TextEditingController? cityFormController, - TextEditingController? heightFormController, - TextEditingController? weightFormeFormController, - TextEditingController? allergiesFormController, - TextEditingController? pillsFormController, - TextEditingController? sportsFormController, - TextEditingController? favoriteFormController, - TextEditingController? unfavoriteFormController, - TextEditingController? additionalFormController, - TextEditingController? downPressureFormController, - TextEditingController? upPressureFormController, - String? token, - String? selectedCountry, - String? selectedCity, - String? hypoFormController, - String? hyperFormController, }) { return DiaryEditLoadedState( diaryEditModelObj: diaryEditModelObj ?? this.diaryEditModelObj, - user: user ?? this.user, - plans: plans ?? this.plans, - userDetails: userDetails ?? this.userDetails, - circumferences: circumferences ?? this.circumferences, - chartPoints: chartPoints ?? this.chartPoints, - chartDates: chartDates ?? this.chartDates, - weightController: weightController ?? this.weightController, - chestCircumferenceController: - chestCircumferenceController ?? this.chestCircumferenceController, - waistCircumferenceController: - waistCircumferenceController ?? this.waistCircumferenceController, - hipCircumferenceController: - hipCircumferenceController ?? this.hipCircumferenceController, - chestCircumferenceFormController: chestCircumferenceFormController ?? - this.chestCircumferenceFormController, - waistCircumferenceFormController: waistCircumferenceFormController ?? - this.waistCircumferenceFormController, - hipCircumferenceFormController: - hipCircumferenceFormController ?? this.hipCircumferenceFormController, - firstNameFormController: - firstNameFormController ?? this.firstNameFormController, - lastNameFormController: - lastNameFormController ?? this.lastNameFormController, - countryFormController: - countryFormController ?? this.countryFormController, - dateFormController: dateFormController ?? this.dateFormController, - cityFormController: cityFormController ?? this.cityFormController, - heightFormController: heightFormController ?? this.heightFormController, - weightFormController: weightFormController ?? this.weightFormController, - upPressureFormController: - upPressureFormController ?? this.upPressureFormController, - downPressureFormController: - downPressureFormController ?? this.downPressureFormController, - allergiesFormController: - allergiesFormController ?? this.allergiesFormController, - pillsFormController: pillsFormController ?? this.pillsFormController, - sportsFormController: sportsFormController ?? this.sportsFormController, - favoriteFormController: - favoriteFormController ?? this.favoriteFormController, - unfavoriteFormController: - unfavoriteFormController ?? this.unfavoriteFormController, - additionalFormController: - additionalFormController ?? this.additionalFormController, - token: token ?? this.token, - selectedCountry: selectedCountry ?? this.selectedCountry, - selectedCity: selectedCity ?? this.selectedCity, - hypoFormController: hypoFormController ?? this.hypoFormController, - hyperFormController: hyperFormController ?? this.hyperFormController, ); } }