From 73009d5a7b1346b4d724b4fddcaa3cd711f47c6f Mon Sep 17 00:00:00 2001 From: maxxkim Date: Thu, 1 Feb 2024 16:40:45 +0300 Subject: [PATCH] Chart point validation --- .../bloc/my_profile_bloc.dart | 77 +++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/lib/presentation/profile/my_profile_screen/bloc/my_profile_bloc.dart b/lib/presentation/profile/my_profile_screen/bloc/my_profile_bloc.dart index a9118446..499ff8f8 100644 --- a/lib/presentation/profile/my_profile_screen/bloc/my_profile_bloc.dart +++ b/lib/presentation/profile/my_profile_screen/bloc/my_profile_bloc.dart @@ -50,11 +50,40 @@ class MyProfileBloc extends Bloc { ..addAll(data["paidPlan"]) ..addAll(data["requestedPlan"]) ..addAll(data["initedPlan"]); + + Map chartPoints = data["chartPoints"] ?? {}; + Map circumferences = data["circumferences"] ?? {}; + + if (chartPoints.length < 4) { + for (int i = chartPoints.length; i < 4; i++) { + chartPoints["$i"] = { + "id": 0, + "user_id": 0, + "weight": 0, + "created_at": "", + "updated_at": "" + }; + } + } + + 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": "", + "updated_at": "" + }; + } + } // Обновляем значения контроллеров только если они уже были созданы emit((currentState).copyWith( - chartPoints: data['chartPoints'], - circumferences: data['circumferences'], + chartPoints: chartPoints, + circumferences: circumferences, user: data['user'], userDetails: data['userDetails'], plans: plans, @@ -88,14 +117,50 @@ class MyProfileBloc extends Bloc { hyperFormController: currentState.hyperFormController, )); } else { - print(data['userDetails']); + var plans = [] + ..addAll(data["finishedPlans"]) + ..addAll(data["rejectedPlans"]) + ..addAll(data["startedPlan"]) + ..addAll(data["paidPlan"]) + ..addAll(data["requestedPlan"]) + ..addAll(data["initedPlan"]); + + Map chartPoints = data["chartPoints"] ?? {}; + Map circumferences = data["circumferences"] ?? {}; + + if (chartPoints.length < 4) { + for (int i = chartPoints.length; i < 4; i++) { + chartPoints["$i"] = { + "id": 0, + "user_id": 0, + "weight": 0, + "created_at": "", + "updated_at": "" + }; + } + } + + 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": "", + "updated_at": "" + }; + } + } + print(chartPoints); // Если текущее состояние - начальное, создаем новое загруженное состояние emit(MyProfileLoadedState( - chartPoints: data['chartPoints'], - circumferences: data['circumferences'], + chartPoints: chartPoints, + circumferences: circumferences, user: data['user'], userDetails: data['userDetails'], - plans: data['plans'], + plans: plans, token: token, hypoFormController: data['userDetails']['has_hypo'].toString(), hyperFormController: data['userDetails']['has_hyper'].toString(),