diff --git a/lib/Screens/Configurations/Section/SubSection/Menu/listView_card_subSection.dart b/lib/Screens/Configurations/Section/SubSection/Menu/listView_card_subSection.dart index 029e78b..6aa2d92 100644 --- a/lib/Screens/Configurations/Section/SubSection/Menu/listView_card_subSection.dart +++ b/lib/Screens/Configurations/Section/SubSection/Menu/listView_card_subSection.dart @@ -1,6 +1,8 @@ import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/material.dart'; +import 'package:manager_app/Components/confirmation_dialog.dart'; import 'package:manager_app/Components/fetch_section_icon.dart'; +import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Models/managerContext.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Menu/showEditSubSection.dart'; import 'package:manager_app/app_context.dart'; @@ -14,13 +16,15 @@ class ListViewCardSubSection extends StatefulWidget { final List listItems; final AppContext appContext; final ValueChanged> onChanged; + final Object rawData; ListViewCardSubSection( this.listItems, this.index, this.key, this.appContext, - this.onChanged + this.onChanged, + this.rawData ); @override @@ -58,15 +62,23 @@ class _ListViewCardSubSection extends State { onTap: () { showEditSubSection( widget.listItems[widget.index], - (SectionDTO value) async { - var result = await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!.sectionUpdate(value); - setState(() { - widget.listItems[widget.index] = value; - widget.onChanged(widget.listItems); - }); + (Object value) async { + try { + var test = updateSectionDetail(widget.listItems[widget.index], value); + // update sub section + await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!.sectionUpdate(test); + showNotification(kSuccess, kWhite, "La sous section a été mis à jour avec succès", context, null); + setState(() { + //widget.listItems[widget.index] = value; + widget.onChanged(widget.listItems); // For resfreh ui + }); + } catch(e) { + showNotification(kError, kWhite, "Une erreur est survenue lors de la mise à jour de la sous section", context, null); + } }, widget.appContext, - context + context, + widget.rawData ); }, child: Padding( @@ -79,9 +91,24 @@ class _ListViewCardSubSection extends State { ) ), InkWell( - onTap: () { - widget.listItems.removeAt(widget.index); - widget.onChanged(widget.listItems); + onTap: () async { + showConfirmationDialog( + "Êtes-vous sûr de vouloir supprimer cette sous section ?", + () {}, + () async { + try { + var sectionToDelete = widget.listItems[widget.index]; + ManagerAppContext managerAppContext = appContext.getContext(); + await managerAppContext.clientAPI!.sectionApi!.sectionDelete(sectionToDelete.id!); + showNotification(kSuccess, kWhite, 'La sous section a été supprimée avec succès', context, null); + // refresh UI + widget.onChanged(widget.listItems); + } catch(e) { + showNotification(kError, kWhite, 'Une erreur est survenue lors de la suppression de la sous section', context, null); + } + }, + context + ); }, child: Padding( padding: const EdgeInsets.all(6.0), @@ -160,6 +187,214 @@ class _ListViewCardSubSection extends State { ), ); } + + updateSectionDetail(SectionDTO sectionDTO, Object sectionDetailDTO) { + switch(sectionDTO.type) + { + case SectionType.Map: + (sectionDetailDTO as MapDTO).id = sectionDTO.id; + (sectionDetailDTO as MapDTO).order = sectionDTO.order; + (sectionDetailDTO as MapDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as MapDTO).type = sectionDTO.type; + (sectionDetailDTO as MapDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as MapDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as MapDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as MapDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as MapDTO).label = sectionDTO.label; + (sectionDetailDTO as MapDTO).title = sectionDTO.title; + (sectionDetailDTO as MapDTO).description = sectionDTO.description; + (sectionDetailDTO as MapDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as MapDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as MapDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as MapDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as MapDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as MapDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as MapDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Slider: + (sectionDetailDTO as SliderDTO).id = sectionDTO.id; + (sectionDetailDTO as SliderDTO).order = sectionDTO.order; + (sectionDetailDTO as SliderDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as SliderDTO).type = sectionDTO.type; + (sectionDetailDTO as SliderDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as SliderDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as SliderDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as SliderDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as SliderDTO).label = sectionDTO.label; + (sectionDetailDTO as SliderDTO).title = sectionDTO.title; + (sectionDetailDTO as SliderDTO).description = sectionDTO.description; + (sectionDetailDTO as SliderDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as SliderDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as SliderDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as SliderDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as SliderDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as SliderDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as SliderDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Video: + (sectionDetailDTO as VideoDTO).id = sectionDTO.id; + (sectionDetailDTO as VideoDTO).order = sectionDTO.order; + (sectionDetailDTO as VideoDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as VideoDTO).type = sectionDTO.type; + (sectionDetailDTO as VideoDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as VideoDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as VideoDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as VideoDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as VideoDTO).label = sectionDTO.label; + (sectionDetailDTO as VideoDTO).title = sectionDTO.title; + (sectionDetailDTO as VideoDTO).description = sectionDTO.description; + (sectionDetailDTO as VideoDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as VideoDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as VideoDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as VideoDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as VideoDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as VideoDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as VideoDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Web: + (sectionDetailDTO as WebDTO).id = sectionDTO.id; + (sectionDetailDTO as WebDTO).order = sectionDTO.order; + (sectionDetailDTO as WebDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as WebDTO).type = sectionDTO.type; + (sectionDetailDTO as WebDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as WebDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as WebDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as WebDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as WebDTO).label = sectionDTO.label; + (sectionDetailDTO as WebDTO).title = sectionDTO.title; + (sectionDetailDTO as WebDTO).description = sectionDTO.description; + (sectionDetailDTO as WebDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as WebDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as WebDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as WebDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as WebDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as WebDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as WebDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Menu: + (sectionDetailDTO as MenuDTO).id = sectionDTO.id; + (sectionDetailDTO as MenuDTO).order = sectionDTO.order; + (sectionDetailDTO as MenuDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as MenuDTO).type = sectionDTO.type; + (sectionDetailDTO as MenuDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as MenuDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as MenuDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as MenuDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as MenuDTO).label = sectionDTO.label; + (sectionDetailDTO as MenuDTO).title = sectionDTO.title; + (sectionDetailDTO as MenuDTO).description = sectionDTO.description; + (sectionDetailDTO as MenuDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as MenuDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as MenuDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as MenuDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as MenuDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as MenuDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as MenuDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Quiz: + (sectionDetailDTO as QuizDTO).id = sectionDTO.id; + (sectionDetailDTO as QuizDTO).order = sectionDTO.order; + (sectionDetailDTO as QuizDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as QuizDTO).type = sectionDTO.type; + (sectionDetailDTO as QuizDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as QuizDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as QuizDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as QuizDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as QuizDTO).label = sectionDTO.label; + (sectionDetailDTO as QuizDTO).title = sectionDTO.title; + (sectionDetailDTO as QuizDTO).description = sectionDTO.description; + (sectionDetailDTO as QuizDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as QuizDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as QuizDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as QuizDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as QuizDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as QuizDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as QuizDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Pdf: + (sectionDetailDTO as PdfDTO).id = sectionDTO.id; + (sectionDetailDTO as PdfDTO).order = sectionDTO.order; + (sectionDetailDTO as PdfDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as PdfDTO).type = sectionDTO.type; + (sectionDetailDTO as PdfDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as PdfDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as PdfDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as PdfDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as PdfDTO).label = sectionDTO.label; + (sectionDetailDTO as PdfDTO).title = sectionDTO.title; + (sectionDetailDTO as PdfDTO).description = sectionDTO.description; + (sectionDetailDTO as PdfDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as PdfDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as PdfDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as PdfDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as PdfDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as PdfDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as PdfDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Puzzle: + (sectionDetailDTO as PuzzleDTO).id = sectionDTO.id; + (sectionDetailDTO as PuzzleDTO).order = sectionDTO.order; + (sectionDetailDTO as PuzzleDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as PuzzleDTO).type = sectionDTO.type; + (sectionDetailDTO as PuzzleDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as PuzzleDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as PuzzleDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as PuzzleDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as PuzzleDTO).label = sectionDTO.label; + (sectionDetailDTO as PuzzleDTO).title = sectionDTO.title; + (sectionDetailDTO as PuzzleDTO).description = sectionDTO.description; + (sectionDetailDTO as PuzzleDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as PuzzleDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as PuzzleDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as PuzzleDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as PuzzleDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as PuzzleDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as PuzzleDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Agenda: + (sectionDetailDTO as AgendaDTO).id = sectionDTO.id; + (sectionDetailDTO as AgendaDTO).order = sectionDTO.order; + (sectionDetailDTO as AgendaDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as AgendaDTO).type = sectionDTO.type; + (sectionDetailDTO as AgendaDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as AgendaDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as AgendaDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as AgendaDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as AgendaDTO).label = sectionDTO.label; + (sectionDetailDTO as AgendaDTO).title = sectionDTO.title; + (sectionDetailDTO as AgendaDTO).description = sectionDTO.description; + (sectionDetailDTO as AgendaDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as AgendaDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as AgendaDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as AgendaDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as AgendaDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as AgendaDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as AgendaDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + case SectionType.Weather: + (sectionDetailDTO as WeatherDTO).id = sectionDTO.id; + (sectionDetailDTO as WeatherDTO).order = sectionDTO.order; + (sectionDetailDTO as WeatherDTO).dateCreation = sectionDTO.dateCreation; + (sectionDetailDTO as WeatherDTO).type = sectionDTO.type; + (sectionDetailDTO as WeatherDTO).instanceId = sectionDTO.instanceId; + (sectionDetailDTO as WeatherDTO).configurationId = sectionDTO.configurationId; + (sectionDetailDTO as WeatherDTO).isSubSection = sectionDTO.isSubSection; + (sectionDetailDTO as WeatherDTO).parentId = sectionDTO.parentId; + (sectionDetailDTO as WeatherDTO).label = sectionDTO.label; + (sectionDetailDTO as WeatherDTO).title = sectionDTO.title; + (sectionDetailDTO as WeatherDTO).description = sectionDTO.description; + (sectionDetailDTO as WeatherDTO).imageId = sectionDTO.imageId; + (sectionDetailDTO as WeatherDTO).imageSource = sectionDTO.imageSource; + (sectionDetailDTO as WeatherDTO).isBeacon = sectionDTO.isBeacon; + (sectionDetailDTO as WeatherDTO).beaconId = sectionDTO.beaconId; + (sectionDetailDTO as WeatherDTO).latitude = sectionDTO.latitude; + (sectionDetailDTO as WeatherDTO).longitude = sectionDTO.longitude; + (sectionDetailDTO as WeatherDTO).meterZoneGPS = sectionDTO.meterZoneGPS; + break; + } + + return sectionDetailDTO; + } } boxDecoration(SectionDTO sectionDTO, appContext) { diff --git a/lib/Screens/Configurations/Section/SubSection/Menu/menu_config.dart b/lib/Screens/Configurations/Section/SubSection/Menu/menu_config.dart index 0a8932d..34575a9 100644 --- a/lib/Screens/Configurations/Section/SubSection/Menu/menu_config.dart +++ b/lib/Screens/Configurations/Section/SubSection/Menu/menu_config.dart @@ -1,10 +1,16 @@ +import 'dart:js_interop'; + import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/material.dart'; +import 'package:manager_app/Components/common_loader.dart'; +import 'package:manager_app/Components/confirmation_dialog.dart'; import 'package:manager_app/Components/fetch_section_icon.dart'; +import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Models/managerContext.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Menu/listView_card_subSection.dart'; import 'package:manager_app/Screens/Configurations/new_section_popup.dart'; import 'package:manager_app/app_context.dart'; +import 'package:manager_app/client.dart'; import 'package:manager_app/constants.dart'; import 'package:manager_api_new/api.dart'; import 'dart:convert'; @@ -51,6 +57,9 @@ class _MenuConfigState extends State { Widget bodyGrid(Size size, AppContext appContext) { void _onReorder(int oldIndex, int newIndex) { + + // TODO Edit order (copy quiz question order lofic) + setState( () { if (newIndex > oldIndex) { @@ -69,7 +78,6 @@ class _MenuConfigState extends State { }, ); } - return SingleChildScrollView( child: Stack( children: [ @@ -78,28 +86,53 @@ class _MenuConfigState extends State { constraints: BoxConstraints(maxHeight: 300, minHeight: 250), child: Padding( padding: const EdgeInsets.all(8.0), - child : ReorderableListView( - onReorder: _onReorder, - scrollDirection: Axis.horizontal, - padding: const EdgeInsets.symmetric(vertical: 15.0), - children: List.generate( - menuDTO.sections!.length, - (index) { - return ListViewCardSubSection( - menuDTO.sections!, - index, - Key('$index'), - appContext, - (sections) { - setState(() { - List test = new List.from(sections); - menuDTO.sections = test; - widget.onChanged(jsonEncode(menuDTO).toString()); - }); - } - ); - }, - ), + child : FutureBuilder( + future: getSubsections((appContext.getContext() as ManagerAppContext).clientAPI!), + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return Center(child: CommonLoader()); + } else { + if (snapshot.connectionState == ConnectionState.done) { + var rawList = snapshot.data; + var rawSectionDTOList = jsonDecode(jsonEncode(snapshot.data)); + rawSectionDTOList = rawSectionDTOList.map((json) => SectionDTO.fromJson(json)).toList(); + List sectionList = rawSectionDTOList.whereType().toList(); + + menuDTO.sections = sectionList; + menuDTO.sections!.sort((a, b) => a.order!.compareTo(b.order!)); + + return ReorderableListView( + onReorder: _onReorder, + scrollDirection: Axis.horizontal, + padding: const EdgeInsets.symmetric(vertical: 15.0), + children: List.generate( + menuDTO.sections!.length, + (index) { + return ListViewCardSubSection( + menuDTO.sections!, + index, + Key('$index'), + appContext, + (sections) { + setState(() { + List test = new List< + SectionDTO>.from(sections); + menuDTO.sections = test; + widget.onChanged( + jsonEncode(menuDTO).toString()); + }); + }, + rawList[index] + ); + }, + ), + ); + } else { + return Center(child: Text( + "Une erreur est survenue lors de la récupération des sous sections")); + } + } + } ), ), ), @@ -107,22 +140,30 @@ class _MenuConfigState extends State { top: 0, right: 0, child: InkWell( - onTap: () { - showNewSection( + onTap: () async { + var newSubsection = await showNewSection( (appContext.getContext() as ManagerAppContext).selectedConfiguration!.id!, appContext, context, true, - (SectionDTO newSubsection) { - setState(() { - //print("RECEIVED new swubssection"); - //print(newSubsection); - menuDTO.sections!.add(newSubsection); - widget.onChanged(jsonEncode(menuDTO).toString()); - }); - }, false ); + try { + if(newSubsection != null) + { + newSubsection.instanceId = (appContext.getContext() as ManagerAppContext).instanceId; + newSubsection.isBeacon = false; + await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!.sectionCreate(newSubsection); + showNotification(kSuccess, kWhite, 'La sous section a été créée avec succès', context, null); + setState(() { + // refresh ui + print("Refresh UI"); + }); + } + } catch(e) { + showNotification(kError, kWhite, 'Une erreur est survenue lors de la création de la sous section', context, null); + } + }, child: Container( height: MediaQuery.of(context).size.width * 0.04, @@ -153,65 +194,10 @@ class _MenuConfigState extends State { ); } - getElement(int index, SectionDTO sectionDTO, Size size, AppContext appContext) { - return Container( - width: double.infinity, - height: double.infinity, - child: Stack( - children: [ - Align( - alignment: Alignment.center, - child: AutoSizeText( - sectionDTO.label!, - style: new TextStyle(fontSize: 15), - maxLines: 2, - textAlign: TextAlign.center, - ), - ), - Positioned( - top: 0, - left: 0, - child: Icon( - getSectionIcon(sectionDTO.type), - color: kSecond, - size: 18.0, - ), - ), - Positioned( - bottom: 0, - left: 0, - child: InkWell( - onTap: () { - /*showNewSection(appContext.getContext().selectedConfiguration.id, appContext, context, true);*/ - // TODO ? - }, - child: Icon( - Icons.edit, - color: kPrimaryColor, - size: 20.0, - ) - ), - ), - Positioned( - bottom: 0, - right: 0, - child: InkWell( - onTap: () { - setState(() { - menuDTO.sections!.removeAt(index); - widget.onChanged(jsonEncode(menuDTO).toString()); - }); - }, - child: Icon( - Icons.delete, - color: kError, - size: 20.0, - ) - ), - ) - ], - ), - ); + Future?> getSubsections(Client client) async { + final rawList = await client.sectionApi!.sectionGetAllSectionSubSections(menuDTO.id!); + var sections = rawList.map((json) => SectionDTO.fromJson(json)).toList(); + return rawList ?? []; } } diff --git a/lib/Screens/Configurations/Section/SubSection/Menu/showEditSubSection.dart b/lib/Screens/Configurations/Section/SubSection/Menu/showEditSubSection.dart index fb14e74..bcb4c35 100644 --- a/lib/Screens/Configurations/Section/SubSection/Menu/showEditSubSection.dart +++ b/lib/Screens/Configurations/Section/SubSection/Menu/showEditSubSection.dart @@ -4,10 +4,12 @@ import 'package:manager_app/Components/multi_string_input_container.dart'; import 'package:manager_app/Components/rounded_button.dart'; import 'package:manager_app/Components/string_input_container.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Agenda/agenda_config.dart'; +import 'package:manager_app/Screens/Configurations/Section/SubSection/Article/article_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/PDF/PDF_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Puzzle/puzzle_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Quizz/quizz_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Slider/slider_config.dart'; +import 'package:manager_app/Screens/Configurations/Section/SubSection/Video/video_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Weather/weather_config.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/WebOrVideo/web_config.dart'; import 'package:manager_app/app_context.dart'; @@ -17,7 +19,8 @@ import 'package:manager_api_new/api.dart'; import '../Map/map_config.dart'; import 'menu_config.dart'; -void showEditSubSection(SectionDTO subSectionDTO, Function getResult, AppContext appContext, BuildContext context) { +void showEditSubSection(SectionDTO subSectionDTO, Function getResult, AppContext appContext, BuildContext context, Object rawSubSectionData) { + Object updatedRawSubSectionData = rawSubSectionData; Size size = MediaQuery.of(context).size; showDialog( @@ -119,7 +122,14 @@ void showEditSubSection(SectionDTO subSectionDTO, Function getResult, AppContext ), child: Padding( padding: const EdgeInsets.all(10.0), - child: getSpecificData(subSectionDTO, context, appContext), + child: getSpecificData( + subSectionDTO, + rawSubSectionData, + rawSubSectionData, + appContext, + (updatedData) { + updatedRawSubSectionData = updatedData; + }), ), ), ], @@ -159,7 +169,7 @@ void showEditSubSection(SectionDTO subSectionDTO, Function getResult, AppContext color: kPrimaryColor, textColor: kWhite, press: () { - getResult(subSectionDTO); + getResult(updatedRawSubSectionData); Navigator.of(context).pop(); }, fontSize: 20, @@ -173,85 +183,100 @@ void showEditSubSection(SectionDTO subSectionDTO, Function getResult, AppContext ); } -getSpecificData(SectionDTO sectionDTO, BuildContext context, AppContext appContext) { - switch(sectionDTO.type) { +getSpecificData(SectionDTO subSectionDTO, Object? rawSectionData, Object sectionDetailDTO, AppContext appContext, Function(Object) onChanged) { + switch(subSectionDTO.type) { case SectionType.Map: - return Text("TODO"); - /*return MapConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - //print("Received info in parent"); - //print(data); - sectionDTO.data = data; + MapDTO mapDTO = MapDTO.fromJson(rawSectionData)!; + sectionDetailDTO = mapDTO; + return MapConfig( + initialValue: mapDTO, + onChanged: (MapDTO changedMap) { + onChanged(changedMap); + }, + ); + case SectionType.Slider: + SliderDTO sliderDTO = SliderDTO.fromJson(rawSectionData)!; + sectionDetailDTO = sliderDTO; + return SliderConfig( + initialValue: sliderDTO, + onChanged: (SliderDTO changedSlider) { + onChanged(changedSlider); }, - );*/ - /*case SectionType.Slider: - return Container( - width: MediaQuery.of(context).size.width * 0.5, - height: MediaQuery.of(context).size.height * 0.5, - child: SliderConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - //print("Received info in parent"); - //print(data); - sectionDTO.data = data; - }, - ), ); case SectionType.Video: + VideoDTO videoDTO = VideoDTO.fromJson(rawSectionData)!; + sectionDetailDTO = videoDTO; + return VideoConfig( + label: "Url de la vidéo:", + initialValue: videoDTO, + onChanged: (VideoDTO updatedWebDTO) { + onChanged(updatedWebDTO); + }, + ); case SectionType.Web: - return WebOrVideoConfig( - label: sectionDTO.type == SectionType.Video ? "Url de la vidéo:": "Url du site web:", - initialValue: sectionDTO.data!, - onChanged: (String data) { - sectionDTO.data = data; + WebDTO webDTO = WebDTO.fromJson(rawSectionData)!; + sectionDetailDTO = webDTO; + return WebConfig( + label: "Url du site web:", + initialValue: webDTO, + onChanged: (WebDTO updatedWebDTO) { + onChanged(updatedWebDTO); }, ); - case SectionType.Menu: - return MenuConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - //print("Received info in parent"); - //print(data); - sectionDTO.data = data; - }, - ); - case SectionType.Quizz: + case SectionType.Quiz: + QuizDTO quizDTO = QuizDTO.fromJson(rawSectionData)!; + sectionDetailDTO = quizDTO; return QuizzConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - //print("Received info in parent"); - //print(data); - sectionDTO.data = data; + initialValue: quizDTO, + onChanged: (QuizDTO updatedQuiz) { + onChanged(updatedQuiz); + }, + ); + case SectionType.Article: + ArticleDTO articleDTO = ArticleDTO.fromJson(rawSectionData)!; + sectionDetailDTO = articleDTO; + return ArticleConfig( + initialValue: articleDTO, + onChanged: (ArticleDTO changedArticle) + { + onChanged(changedArticle); }, ); case SectionType.Pdf: + PdfDTO pdfDTO = PdfDTO.fromJson(rawSectionData)!; + sectionDetailDTO = pdfDTO; return PDFConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - sectionDTO.data = data; + initialValue: pdfDTO, + onChanged: (PdfDTO changedPDF) { + onChanged(changedPDF); }, ); case SectionType.Puzzle: + PuzzleDTO puzzleDTO = PuzzleDTO.fromJson(rawSectionData)!; + sectionDetailDTO = puzzleDTO; return PuzzleConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - sectionDTO.data = data; + initialValue: puzzleDTO, + onChanged: (PuzzleDTO updatedPuzzle) { + onChanged(updatedPuzzle); }, ); case SectionType.Agenda: + AgendaDTO agendaDTO = AgendaDTO.fromJson(rawSectionData)!; + sectionDetailDTO = agendaDTO; return AgendaConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - sectionDTO.data = data; + initialValue: agendaDTO, + onChanged: (AgendaDTO updatedAgenda) { + onChanged(updatedAgenda); }, ); case SectionType.Weather: + WeatherDTO weatherDTO = WeatherDTO.fromJson(rawSectionData)!; + sectionDetailDTO = weatherDTO; return WeatherConfig( - initialValue: sectionDTO.data!, - onChanged: (String data) { - sectionDTO.data = data; + initialValue: weatherDTO, + onChanged: (WeatherDTO updatedWeather) { + onChanged(updatedWeather); }, - );*/ + ); } } diff --git a/lib/Screens/Configurations/Section/SubSection/Quizz/new_update_question_quizz.dart b/lib/Screens/Configurations/Section/SubSection/Quizz/new_update_question_quizz.dart index f72dc36..0557772 100644 --- a/lib/Screens/Configurations/Section/SubSection/Quizz/new_update_question_quizz.dart +++ b/lib/Screens/Configurations/Section/SubSection/Quizz/new_update_question_quizz.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 showNewOrUpdateQuestionQuizz(QuestionDTO? inputQuestionDTO, AppContext appContext, BuildContext context, String text) async { +Future showNewOrUpdateQuestionQuizz(QuestionDTO? inputQuestionDTO, AppContext appContext, BuildContext context, String text) async { QuestionDTO questionDTO = new QuestionDTO(); if (inputQuestionDTO != null) { diff --git a/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_answer_list.dart b/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_answer_list.dart index c6b5206..b2c7d07 100644 --- a/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_answer_list.dart +++ b/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_answer_list.dart @@ -127,7 +127,7 @@ class _QuizzResponseListState extends State { widget.onChanged(responsesMiddle); }); } - }, 1, [], context); + }, 1, [ResourceType.Image, ResourceType.ImageUrl, ResourceType.Video, ResourceType.VideoUrl, ResourceType.Audio], context); }, child: Container( height: MediaQuery.of(context).size.width * 0.04, diff --git a/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_config.dart b/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_config.dart index bbb20ed..51a6224 100644 --- a/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_config.dart +++ b/lib/Screens/Configurations/Section/SubSection/Quizz/quizz_config.dart @@ -1,11 +1,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; +import 'package:manager_app/Components/confirmation_dialog.dart'; +import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Components/multi_string_input_html_modal.dart'; import 'package:manager_app/Components/rounded_button.dart'; import 'package:manager_app/Models/managerContext.dart'; import 'package:manager_app/app_context.dart'; +import 'package:manager_app/client.dart'; import 'package:manager_app/constants.dart'; import 'package:manager_api_new/api.dart'; +import 'package:manager_app/Components/common_loader.dart'; import 'dart:convert'; import 'package:provider/provider.dart'; @@ -31,6 +35,7 @@ class QuizzConfig extends StatefulWidget { class _QuizzConfigState extends State { late QuizDTO quizzDTO; + late List questions = []; @override void initState() { @@ -46,24 +51,39 @@ class _QuizzConfigState extends State { final appContext = Provider.of(context); Size size = MediaQuery.of(context).size; - void _onReorder(int oldIndex, int newIndex) { - setState( - () { - if (newIndex > oldIndex) { - newIndex -= 1; - } - final QuestionDTO item = quizzDTO.questions!.removeAt(oldIndex); - quizzDTO.questions!.insert(newIndex, item); + void _onReorder(int oldIndex, int newIndex) async { + + if (newIndex > oldIndex) { + newIndex -= 1; + } + final QuestionDTO item = quizzDTO.questions![oldIndex]; + //quizzDTO.questions!.insert(newIndex, item); + + item.order = newIndex; + try { + await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionQuizApi!.sectionQuizUpdate(item); + showNotification(kSuccess, kWhite, "L'ordre des questions a été mis à jour avec succès", context, null); + setState(() { + // refresh ui + print("Refresh UI"); + }); + } catch(e) { + showNotification(kError, kWhite, "Une erreur est survenue lors de la mise à jour de l'ordre des questions", context, null); + } + + /*var i = 0; + quizzDTO.questions!.forEach((question) { + question.order = i; + i++; + });*/ + + /*setState( + () async { - var i = 0; - quizzDTO.questions!.forEach((question) { - question.order = i; - i++; - }); widget.onChanged(quizzDTO); }, - ); + );*/ } return @@ -143,88 +163,139 @@ class _QuizzConfigState extends State { ], ), ), - Padding( - padding: const EdgeInsets.all(8.0), - child: Container( - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(25), - border: Border.all(width: 1.5, color: kSecond) - ), - child: Stack( - children: [ - Padding( - padding: const EdgeInsets.only(top: 40, left: 10, right: 10, bottom: 10), + FutureBuilder( + future: getQuestions((appContext.getContext() as ManagerAppContext).clientAPI!), + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return Center(child: CommonLoader()); + } else { + if (snapshot.connectionState == ConnectionState.done) { + + quizzDTO.questions = snapshot.data; + quizzDTO.questions!.sort((a, b) => a.order!.compareTo(b.order!)); + + questions = quizzDTO.questions!; + + return Padding( + padding: const EdgeInsets.all(8.0), child: Container( - height: quizzDTO.questions!.length == 0 ? 75 : null, - child: ReorderableListView.builder( - shrinkWrap: true, - padding: const EdgeInsets.only(right: 125), - itemBuilder: (BuildContext context, int index) { - return Container( - key: ValueKey(index), - decoration: boxDecoration(), - padding: const EdgeInsets.all(2), - margin: EdgeInsets.symmetric(vertical: 3, horizontal: 3), - child: getElement(index, quizzDTO.questions![index], size, appContext), - ); - }, - itemCount: quizzDTO.questions!.length, - onReorder: _onReorder + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(25), + border: Border.all(width: 1.5, color: kSecond) ), - ) - ), - Positioned( - top: 10, - left: 10, - child: Text( - "Questions", - style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500), - ), - ), - Positioned( - bottom: 10, - right: 10, - child: InkWell( - onTap: () async { - QuestionDTO? result = await showNewOrUpdateQuestionQuizz(null, appContext, context, "Question"); - setState(() { - result.order = quizzDTO.questions!.length; - quizzDTO.questions!.add(result); - widget.onChanged(quizzDTO); - }); - }, - child: Container( - height: MediaQuery.of(context).size.width * 0.04, - width: MediaQuery.of(context).size.width * 0.04, - child: Icon( - Icons.add, - color: kTextLightColor, - size: 30.0, - ), - decoration: BoxDecoration( - color: kSuccess, - shape: BoxShape.rectangle, - borderRadius: BorderRadius.circular(20.0), - boxShadow: [ - BoxShadow( - color: kSecond, - spreadRadius: 0.5, - blurRadius: 5, - offset: Offset(0, 1.5), // changes position of shadow + child: Stack( + children: [ + Padding( + padding: const EdgeInsets.only( + top: 40, left: 10, right: 10, bottom: 10), + child: Container( + height: quizzDTO.questions!.length == 0 + ? 75 + : null, + child: ReorderableListView.builder( + shrinkWrap: true, + padding: const EdgeInsets.only( + right: 125), + itemBuilder: (BuildContext context, + int index) { + return Container( + key: ValueKey(index), + decoration: boxDecoration(), + padding: const EdgeInsets.all(2), + margin: EdgeInsets.symmetric( + vertical: 3, horizontal: 3), + child: getElement(index, + quizzDTO.questions![index], + size, appContext), + ); + }, + itemCount: quizzDTO.questions!.length, + onReorder: _onReorder + ), + ) ), - ], - ), - ), + Positioned( + top: 10, + left: 10, + child: Text( + "Questions", + style: TextStyle(fontSize: 15, + fontWeight: FontWeight.w500), + ), + ), + Positioned( + bottom: 10, + right: 10, + child: InkWell( + onTap: () async { + QuestionDTO? result = await showNewOrUpdateQuestionQuizz( + null, appContext, context, "Question"); + + try { + if(result != null) + { + await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionQuizApi!.sectionQuizCreate(quizzDTO.id!, result); + showNotification(kSuccess, kWhite, 'La question a été créée avec succès', context, null); + setState(() { + // refresh ui + print("Refresh UI"); + }); + } + } catch(e) { + showNotification(kError, kWhite, 'Une erreur est survenue lors de la création de la question', context, null); + } + + /*setState(() { + result.order = quizzDTO.questions!.length; + quizzDTO.questions!.add(result); + widget.onChanged(quizzDTO); + });*/ + }, + child: Container( + height: MediaQuery.of(context).size.width * 0.04, + width: MediaQuery.of(context).size.width * 0.04, + child: Icon( + Icons.add, + color: kTextLightColor, + size: 30.0, + ), + decoration: BoxDecoration( + color: kSuccess, + shape: BoxShape.rectangle, + borderRadius: BorderRadius.circular(20.0), + boxShadow: [ + BoxShadow( + color: kSecond, + spreadRadius: 0.5, + blurRadius: 5, + offset: Offset(0, + 1.5), // changes position of shadow + ), + ], + ), + ), + ), + ) + ]), ), - ) - ]), - ), + ); + } else { + return Center(child: Text( + "Une erreur est survenue lors de la récupération des questions")); + } + } + } ), ], ), ); } + Future?> getQuestions(Client client) async { + List? questions = await client.sectionQuizApi!.sectionQuizGetAllQuizQuestionFromSection(widget.initialValue.id!); + return questions ?? []; + } + getElement(int index, QuestionDTO question, Size size, AppContext appContext) { return Stack( children: [ @@ -260,18 +331,32 @@ class _QuizzConfigState extends State { message: "Modifier", child: InkWell( onTap: () async { - var result = await showNewOrUpdateQuestionQuizz( + QuestionDTO? result = await showNewOrUpdateQuestionQuizz( question, appContext, context, "Modifier la question" ); - setState(() { + try { + if(result != null) + { + await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionQuizApi!.sectionQuizUpdate(result); + showNotification(kSuccess, kWhite, 'La question a été mis à jour avec succès', context, null); + setState(() { + // refresh ui + print("Refresh UI"); + }); + } + } catch(e) { + showNotification(kError, kWhite, 'Une erreur est survenue lors de la mise à jour de la question', context, null); + } + + /*setState(() { quizzDTO.questions![question.order!] = result; widget.onChanged(quizzDTO); - }); - }, + });*/ + }, child: Padding( padding: const EdgeInsets.all(8.0), child: Icon( @@ -286,10 +371,25 @@ class _QuizzConfigState extends State { message: "Supprimer", child: InkWell( onTap: () { - setState(() { - quizzDTO.questions!.removeAt(question.order!); - widget.onChanged(quizzDTO); - }); + showConfirmationDialog( + "Êtes-vous sûr de vouloir supprimer cette question ?", + () {}, + () async { + + try { + var questionToRemove = questions[index]; + (appContext.getContext() as ManagerAppContext).clientAPI!.sectionQuizApi!.sectionQuizDeleteWithHttpInfo(questionToRemove.id!); + showNotification(kSuccess, kWhite, 'La question a été supprimée avec succès', context, null); + // refresh UI + setState(() { + print("Refresh UI"); + }); + } catch(e) { + showNotification(kError, kWhite, 'Une erreur est survenue lors de la suppression de la question', context, null); + } + }, + context + ); }, child: Padding( padding: const EdgeInsets.all(8.0), diff --git a/lib/Screens/Configurations/Section/section_detail_screen.dart b/lib/Screens/Configurations/Section/section_detail_screen.dart index 97ec6df..f874c28 100644 --- a/lib/Screens/Configurations/Section/section_detail_screen.dart +++ b/lib/Screens/Configurations/Section/section_detail_screen.dart @@ -409,7 +409,10 @@ class _SectionDetailScreenState extends State { Future save(bool isTraduction, AppContext appContext) async { updateSectionDetail(); - SectionDTO? section = await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!.sectionUpdate(sectionDetailDTO); + + var sectionResult = await (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!.sectionUpdate(sectionDetailDTO); + SectionDTO? section = SectionDTO.fromJson(sectionResult); + ManagerAppContext managerAppContext = appContext.getContext(); managerAppContext.selectedSection = section; appContext.setContext(managerAppContext); diff --git a/lib/Screens/Configurations/new_section_popup.dart b/lib/Screens/Configurations/new_section_popup.dart index b6f8d9a..36675ec 100644 --- a/lib/Screens/Configurations/new_section_popup.dart +++ b/lib/Screens/Configurations/new_section_popup.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 showNewSection(String configurationId, AppContext appContext, BuildContext contextBuild, bool isSubSection, Function? sendSubSection, bool isMobile) { +Future showNewSection(String configurationId, AppContext appContext, BuildContext contextBuild, bool isSubSection, bool isMobile) { SectionDTO sectionDTO = new SectionDTO(); sectionDTO.label = ""; sectionDTO.configurationId = configurationId; diff --git a/lib/Screens/Configurations/section_reorderList.dart b/lib/Screens/Configurations/section_reorderList.dart index 8f6944b..2e30df8 100644 --- a/lib/Screens/Configurations/section_reorderList.dart +++ b/lib/Screens/Configurations/section_reorderList.dart @@ -147,7 +147,7 @@ class _SectionReorderListState extends State { right: 10, child: InkWell( onTap: () async { - var sectionToCreate = await showNewSection(widget.configurationId, appContext, context, false, null, currentConfiguration.isMobile!); + var sectionToCreate = await showNewSection(widget.configurationId, appContext, context, false, currentConfiguration.isMobile!); if(sectionToCreate != null) { ManagerAppContext managerAppContext = appContext.getContext(); diff --git a/manager_api_new/lib/api/section_api.dart b/manager_api_new/lib/api/section_api.dart index 92eda7f..9039607 100644 --- a/manager_api_new/lib/api/section_api.dart +++ b/manager_api_new/lib/api/section_api.dart @@ -374,7 +374,7 @@ class SectionApi { /// Parameters: /// /// * [String] id (required): - Future?> sectionGetAllSectionSubSections( + Future sectionGetAllSectionSubSections( String id, ) async { final response = await sectionGetAllSectionSubSectionsWithHttpInfo( @@ -388,11 +388,13 @@ class SectionApi { // FormatException when trying to decode an empty string. if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { - final responseBody = await _decodeBodyBytes(response); + /*final responseBody = await _decodeBodyBytes(response); return (await apiClient.deserializeAsync(responseBody, 'List') as List) .cast() - .toList(growable: false); + .toList(growable: false);*/ + final decoded = json.decode(await _decodeBodyBytes(response)); + return decoded; } return null; } @@ -1017,7 +1019,7 @@ class SectionApi { /// Parameters: /// /// * [Object] body (required): - Future sectionUpdate( + Future sectionUpdate( Object body, ) async { final response = await sectionUpdateWithHttpInfo( @@ -1031,10 +1033,12 @@ class SectionApi { // FormatException when trying to decode an empty string. if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { - return await apiClient.deserializeAsync( + final decoded = json.decode(await _decodeBodyBytes(response)); + return decoded; + /*return await apiClient.deserializeAsync( await _decodeBodyBytes(response), 'SectionDTO', - ) as SectionDTO; + ) as SectionDTO*/; } return null; }