From 785301d908a05014ac451ccbd13d94d8e3c04ab4 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Thu, 11 Apr 2024 17:05:57 +0200 Subject: [PATCH] Update categorie (id for filter and update) --- .../dropDown_input_container_categories.dart | 2 +- .../Map/category_input_container.dart | 31 ++++++-- .../Section/SubSection/Map/category_list.dart | 39 +++++++++- .../Section/SubSection/Map/map_config.dart | 73 ++++++++++++++++++- .../SubSection/Map/new_update_categorie.dart | 2 +- 5 files changed, 132 insertions(+), 15 deletions(-) diff --git a/lib/Components/dropDown_input_container_categories.dart b/lib/Components/dropDown_input_container_categories.dart index 7c9346d..1d0a010 100644 --- a/lib/Components/dropDown_input_container_categories.dart +++ b/lib/Components/dropDown_input_container_categories.dart @@ -27,7 +27,7 @@ class _DropDownInputContainerCategoriesState extends State element.order == widget.initialValue!.order); + selectedCategorieDTO = widget.categories.firstWhere((element) => element.id == widget.initialValue!.id); } List label = []; label.add(TranslationDTO(language: "FR", value: "Aucune catégorie")); diff --git a/lib/Screens/Configurations/Section/SubSection/Map/category_input_container.dart b/lib/Screens/Configurations/Section/SubSection/Map/category_input_container.dart index 37d2fed..e92ce83 100644 --- a/lib/Screens/Configurations/Section/SubSection/Map/category_input_container.dart +++ b/lib/Screens/Configurations/Section/SubSection/Map/category_input_container.dart @@ -23,9 +23,12 @@ class CategoryInputContainer extends StatefulWidget { } class _CategoryInputContainerState extends State { + late List middleCategories; @override void initState() { + middleCategories = widget.initialValue; + print("initStateinitStateinitState"); super.initState(); } @@ -47,10 +50,18 @@ class _CategoryInputContainerState extends State { child: InkWell( onTap: () { List newValues = []; - List initials = widget.initialValue; - showCreateOrUpdateCategories("Catégories", initials, newValues, (value) { - widget.onChanged(value); - widget.initialValue = value; + + print("HERE COMES THE FUCKKKERS"); + print(middleCategories); + + showCreateOrUpdateCategories("Catégories", middleCategories, newValues, (value) { + print("RESULT HERERERER"); + print(value); + setState(() { + widget.onChanged(value); + middleCategories = value; + widget.initialValue = value; + }); }, context); }, child: Container( @@ -78,7 +89,7 @@ class _CategoryInputContainerState extends State { } } -showCreateOrUpdateCategories(String modalLabel, List values, List newValues, Function onGetResult, BuildContext context) { +showCreateOrUpdateCategories(String modalLabel, List initials, List newValues, Function onGetResult, BuildContext context) { showDialog( builder: (BuildContext context) { Size size = MediaQuery.of(context).size; @@ -95,9 +106,10 @@ showCreateOrUpdateCategories(String modalLabel, List values, List< ), height: size.height * 0.7, width: size.width * 0.65, - child: CategoryList(categories: values, onChanged: (result) { + child: CategoryList(categories: initials, onChanged: (result) { + print("ON CHANGZED HERE"); newValues = result; - onGetResult(result); + //onGetResult(result); }), ) ), @@ -113,7 +125,9 @@ showCreateOrUpdateCategories(String modalLabel, List values, List< icon: Icons.undo, color: kSecond, press: () { - onGetResult(values); + onGetResult(initials); + print("INITALASSS"); + print(initials); Navigator.of(context).pop(); }, fontSize: 20, @@ -132,6 +146,7 @@ showCreateOrUpdateCategories(String modalLabel, List values, List< if (!deepEq(values, newValues)) { onGetResult(newValues); }*/ + onGetResult(newValues); Navigator.of(context).pop(); }, fontSize: 20, diff --git a/lib/Screens/Configurations/Section/SubSection/Map/category_list.dart b/lib/Screens/Configurations/Section/SubSection/Map/category_list.dart index cb1d46a..835d185 100644 --- a/lib/Screens/Configurations/Section/SubSection/Map/category_list.dart +++ b/lib/Screens/Configurations/Section/SubSection/Map/category_list.dart @@ -22,12 +22,47 @@ class CategoryList extends StatefulWidget { class _CategoryListState extends State { late List categoriesMiddle; + late int currentIndex; @override void initState() { + print("INISTASTETETTETET CATEGORY LIIST"); super.initState(); categoriesMiddle = new List.from(widget.categories); categoriesMiddle.sort((a, b) => a.order!.compareTo(b.order!)); + + currentIndex = getNextAvailableIndex(categoriesMiddle); + assignIdsToNull(categoriesMiddle); + print(currentIndex); + print(categoriesMiddle); + } + + int getNextAvailableIndex(List categoriesMiddle) { + List existingIds = []; + + for (var categorie in categoriesMiddle) { + if (categorie.id != null) { + existingIds.add(categorie.id!); + } + } + existingIds.sort(); + + if (existingIds.isEmpty) { + return 0; + } + + return existingIds.last + 1; + } + + void assignIdsToNull(List categoriesMiddle) { + int nextIndex = getNextAvailableIndex(categoriesMiddle); + + for (var categorie in categoriesMiddle) { + if (categorie.id == null) { + categorie.id = nextIndex; + nextIndex++; + } + } } void _resetOrder() { @@ -94,7 +129,7 @@ class _CategoryListState extends State { onTap: () async { CategorieDTO newCategory = CategorieDTO(order: null); - var result = await showNewOrUpdateCategory(newCategory, appContext, context, "Création catégorie"); + var result = await showNewOrUpdateCategory(newCategory, appContext, context, "Création catégorie", currentIndex); if (result != null) { setState(() { @@ -168,7 +203,7 @@ class _CategoryListState extends State { message: "Modifier", child: InkWell( onTap: () async { - var result = await showNewOrUpdateCategory(category, appContext, context, "Modification catégorie"); + var result = await showNewOrUpdateCategory(category, appContext, context, "Modification catégorie", currentIndex); if (result != null) { setState(() { diff --git a/lib/Screens/Configurations/Section/SubSection/Map/map_config.dart b/lib/Screens/Configurations/Section/SubSection/Map/map_config.dart index a4e03d8..410d71a 100644 --- a/lib/Screens/Configurations/Section/SubSection/Map/map_config.dart +++ b/lib/Screens/Configurations/Section/SubSection/Map/map_config.dart @@ -1,7 +1,8 @@ import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; -import 'package:manager_app/Components/category_input_container.dart'; +import 'package:manager_app/Components/multi_select_dropdown_language_container.dart'; +import 'package:manager_app/Screens/Configurations/Section/SubSection/Map/category_input_container.dart'; import 'package:manager_app/Components/dropDown_input_container.dart'; import 'package:manager_app/Components/fetch_section_icon.dart'; import 'package:manager_app/Components/resource_input_container.dart'; @@ -54,6 +55,24 @@ class _MapConfigState extends State { selectedCategories = mapDTO.categories!.map((categorie) => categorie.label!.firstWhere((element) => element.language == 'FR').value!).toList(); + pointsToShow.forEach((pts) { + if(pts.categorieId == null && pts.categorie != null && mapDTO.categories!.any((e) => e.order == pts.categorie!.order)) { + print("SET CATEGORIE ID FOR CAT"); + print(pts.categorie); + var testCat = mapDTO.categories!.where((c) => c.label![0].value == pts.categorie!.label![0].value); + if(testCat.isNotEmpty) { + pts.categorieId = testCat.first.id; + pts.categorie!.id = testCat.first.id; + } else { + pts.categorie = null; // Categorie no more existing so we can reset cat + } + + print(pts.categorieId); + } + }); + + print(pointsToShow); + if(mapDTO.mapType != null) { switch(mapDTO.mapType!.value) { case 0: @@ -219,7 +238,43 @@ class _MapConfigState extends State { color: kPrimaryColor, onChanged: (List? value) { if(value != null) { + //print("COUCOU CategoryInputContainerCategoryInputContainerCategoryInputContainer"); + //print(value); mapDTO.categories = value; + + //update point's category + if(mapDTO.points != null) { + mapDTO.points!.forEach((p) { + // Check if category still exist - Delete + if(p.categorieId != null && !mapDTO.categories!.map((c) => c.id).any((e) => e != null && e == p.categorieId)) + { + print("DELETE CAT REF"); + print(p.categorieId); + print(p.categorie); + // delete cat ref from point + p.categorieId = null; + p.categorie = null; + } + // update point category - Update + if(p.categorieId != null && mapDTO.categories!.map((c) => c.id).any((e) => e != null && e == p.categorieId)) + { + var categorie = mapDTO.categories!.firstWhere((c) => c.id == p.categorieId); + p.categorie = categorie; + print("UPDATE CAT REF"); + } + + print(p); + if(p.categorieId == null && p.categorie != null && p.categorie!.label != null) + { + // need to update with label + var categorie = mapDTO.categories!.firstWhere((c) => c.label![0].value == p.categorie!.label![0].value); + //p.categorie = categorie; + p.categorieId = categorie.id; + } + }); + } + /*print("mapDTO.points"); + print(mapDTO.points);*/ widget.onChanged(jsonEncode(mapDTO).toString()); } }, @@ -289,7 +344,8 @@ class _MapConfigState extends State { } }); }, - )), + ) + ), Positioned( top: 0, right: 150, @@ -322,6 +378,13 @@ class _MapConfigState extends State { setState(() { mapDTO.points!.add(geoPoint); mapDTO.points!.sort((a, b) => a.title!.firstWhere((t) => t.language == 'FR').value!.toLowerCase().compareTo(b.title!.firstWhere((t) => t.language == 'FR').value!.toLowerCase())); + + if(selectedCategories == null || selectedCategories!.length == 0) { + pointsToShow = mapDTO.points!.where((point) => point.categorie == null).toList(); + } else { + pointsToShow = mapDTO.points!.where((point) => selectedCategories!.any((tps) => point.categorie == null || point.categorie?.label?.firstWhere((lab) => lab.language == 'FR').value == tps)).toList(); + } + widget.onChanged(jsonEncode(mapDTO).toString()); }); }, @@ -419,8 +482,12 @@ class _MapConfigState extends State { child: InkWell( onTap: () { setState(() { - mapDTO.points!.removeAt(index); + var pointToRemove = pointsToShow[index]; + mapDTO.points = mapDTO.points!.where((p) => p.longitude != pointToRemove.longitude || p.latitude != p.latitude).toList(); + + pointsToShow.removeAt(index); }); + widget.onChanged(jsonEncode(mapDTO).toString()); }, child: Icon( diff --git a/lib/Screens/Configurations/Section/SubSection/Map/new_update_categorie.dart b/lib/Screens/Configurations/Section/SubSection/Map/new_update_categorie.dart index 2442189..588cdd2 100644 --- a/lib/Screens/Configurations/Section/SubSection/Map/new_update_categorie.dart +++ b/lib/Screens/Configurations/Section/SubSection/Map/new_update_categorie.dart @@ -9,7 +9,7 @@ import 'package:manager_app/app_context.dart'; import 'package:manager_app/constants.dart'; import 'package:manager_api_new/api.dart'; -Future showNewOrUpdateCategory(CategorieDTO? inputCategorieDTO, AppContext appContext, BuildContext context, String text) async { +Future showNewOrUpdateCategory(CategorieDTO? inputCategorieDTO, AppContext appContext, BuildContext context, String text, int currentIndex) async { CategorieDTO categorieDTO = new CategorieDTO(); if (inputCategorieDTO != null) {