diff --git a/covas_mobile/lib/classes/events.dart b/covas_mobile/lib/classes/events.dart index 5879a30..c0c4fba 100644 --- a/covas_mobile/lib/classes/events.dart +++ b/covas_mobile/lib/classes/events.dart @@ -7,17 +7,36 @@ class Events { String? description; double? latitude; double? longitude; - - Events({this.place, this.id, this.name, this.startDate}); + List? tags; + List? organizers; + String? imgUrl; + Events( + {this.place, + this.id, + this.name, + this.startDate, + this.description, + this.endDate, + this.tags, + this.latitude, + this.longitude, + this.organizers, + this.imgUrl}); Events.fromJson(Map json) { - id = json['id']; - name = json['name']; - place = json['place']; - startDate = json["start_date"]; - endDate = json['end_date']; - description = json['description']; - latitude = json['latitude']; - longitude = json['longitude']; + id = json['id'] as String?; + name = json['name'] as String?; + place = json['place'] as String?; + startDate = json['start_date'] as String?; + endDate = json['end_date'] as String?; + description = json['description'] as String?; + latitude = (json['latitude'] as num?)?.toDouble(); // Safely cast to double + longitude = + (json['longitude'] as num?)?.toDouble(); // Safely cast to double + tags = (json['tags'] as List?) + ?.cast(); // Convert List to List + organizers = (json['organizers'] as List?) + ?.cast(); // Convert List to List + imgUrl = json['imgUrl'] as String?; } } diff --git a/covas_mobile/lib/pages/EditEvent.dart b/covas_mobile/lib/pages/EditEvent.dart index 001b4b4..85adc11 100644 --- a/covas_mobile/lib/pages/EditEvent.dart +++ b/covas_mobile/lib/pages/EditEvent.dart @@ -7,6 +7,7 @@ import 'package:textfield_tags/textfield_tags.dart'; import 'dart:convert'; import 'dart:io'; +import '../classes/events.dart'; import 'ItemMenu.dart'; import '../classes/alert.dart'; @@ -19,29 +20,20 @@ void main() { } class MyApp extends StatelessWidget { - Map events = {}; - String id = ""; - List organizers = []; - List tags = []; + Events? events; + @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, - home: EditEvent(id: id, organizers: organizers, tags: tags), + home: EditEvent(events: events), ); } } class EditEvent extends StatefulWidget { - const EditEvent( - {Key? key, - required this.id, - required this.tags, - required this.organizers}) - : super(key: key); - final String id; - final List tags; - final List organizers; + const EditEvent({Key? key, required this.events}) : super(key: key); + final Events? events; @override _EditEventState createState() => _EditEventState(); @@ -67,7 +59,6 @@ class _EditEventState extends State List> suggestions = []; String geographicalZone = ""; String imgUrl = ""; - Map events = {}; List initialTags = []; @@ -154,97 +145,6 @@ class _EditEventState extends State return "${year}-${month}-${day}"; } - Future _getEventInfos() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - var accessToken = prefs.getString("access_token") ?? ""; - String formerName = ""; - String formerMap = ""; - String formerImage = ""; - String formerDesc = ""; - - if (accessToken.isNotEmpty) { - var urlGet = Uri.parse("${globals.api}/events/${widget.id}"); - - var responseGet = await http.get(urlGet, - headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'}); - stderr.writeln('Response Get status: ${responseGet.statusCode}'); - if (responseGet.statusCode == 200) { - stderr.writeln('Username : ${responseGet.body}'); - events = jsonDecode(utf8.decode(responseGet.bodyBytes)); - formerName = convertNulltoEmptyString(events["name"]); - formerMap = convertNulltoEmptyString(events["place"]); - formerDesc = convertNulltoEmptyString(events["description"]); - - startDate = DateTime.parse(events["start_date"]); - - endDate = DateTime.parse(events["end_date"]); - - if (events["imgUrl"] != null) { - formerImage = events["imgUrl"]; - } - } else { - var text = ""; - switch (responseGet.statusCode) { - case 400: - { - text = "Requête mal construite"; - } - break; - case 406: - { - text = "Mot de passe incorrect"; - } - break; - case 404: - { - text = "Utilisateur inconnu"; - } - break; - case 403: - { - text = "Vous n'avez pas l'autorisation de faire cette action"; - } - break; - case 410: - { - text = "Token invalide"; - } - break; - case 500: - { - text = "Probleme interne du serveur"; - } - break; - default: - { - text = "Probleme d'authentification inconnu"; - } - break; - } - showErrorDialog(context, text); - } - } else { - showErrorDialog(context, "Cache invalide"); - } - - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - inputName.text = formerName; - startDatepicker.text = - DateFormat("dd/MM/yyyy").format(startDate).toString(); - startTimepicker.text = DateFormat("HH:mm").format(startDate).toString(); - endDatepicker.text = DateFormat("dd/MM/yyyy").format(endDate).toString(); - endTimepicker.text = DateFormat("HH:mm").format(endDate).toString(); - inputGeo.text = formerMap; - imgUrl = formerImage; - inputDesc.text = formerDesc; - }); - } - Future _updateEvent(BuildContext context) async { var name = inputName.text; var place = inputGeo.text; @@ -431,10 +331,30 @@ class _EditEventState extends State @override void initState() { - initialTags = List.from(widget.tags as List); - initialOrga = List.from(widget.organizers as List); + inputName.text = widget.events!.name ?? ""; + startDatepicker.text = DateFormat("dd/MM/yyyy") + .format(DateTime.parse( + widget.events!.startDate ?? DateTime.now().toString())) + .toString(); + startTimepicker.text = DateFormat("HH:mm") + .format(DateTime.parse( + widget.events!.startDate ?? DateTime.now().toString())) + .toString(); + endDatepicker.text = DateFormat("dd/MM/yyyy") + .format( + DateTime.parse(widget.events!.endDate ?? DateTime.now().toString())) + .toString(); + endTimepicker.text = DateFormat("HH:mm") + .format( + DateTime.parse(widget.events!.endDate ?? DateTime.now().toString())) + .toString(); + inputGeo.text = widget.events!.place ?? ""; + + imgUrl = widget.events!.imgUrl ?? ""; + inputDesc.text = widget.events!.description ?? ""; + initialTags = List.from(widget.events!.tags as List); + initialOrga = List.from(widget.events!.organizers as List); super.initState(); - _getEventInfos(); } final _formKey = GlobalKey(); diff --git a/covas_mobile/lib/pages/ItemMenu.dart b/covas_mobile/lib/pages/ItemMenu.dart index 22e5978..ce4498d 100644 --- a/covas_mobile/lib/pages/ItemMenu.dart +++ b/covas_mobile/lib/pages/ItemMenu.dart @@ -106,23 +106,27 @@ class _ItemMenuState extends State with ShowErrorDialog { stderr.writeln('Response Get status: ${responseGet.statusCode}'); if (responseGet.statusCode == 200) { stderr.writeln('Username : ${responseGet.body}'); - var events = jsonDecode(utf8.decode(responseGet.bodyBytes)); - id = events["id"]; - formerName = events["name"]; - formerMap = "${events["place"]}"; - formerDesc = events["description"]; - formerTags = List.from(events['tags'] as List); - formerOrga = List.from(events['organizers'] as List); - final startDate = DateTime.parse(events["start_date"]); + events = + Events.fromJson(jsonDecode(utf8.decode(responseGet.bodyBytes))); + id = events!.id ?? ""; + formerName = events!.name ?? ""; + formerMap = "${events!.place}" ?? ""; + formerDesc = events!.description ?? ""; + + formerTags = List.from(events!.tags as List); + formerOrga = List.from(events!.organizers as List); + final startDate = + DateTime.parse(events!.startDate ?? DateTime.now().toString()); final date = DateFormat.yMd().format(startDate); final time = DateFormat.Hm().format(startDate); - final endDate = DateTime.parse(events["end_date"]); + final endDate = + DateTime.parse(events!.endDate ?? DateTime.now().toString()); final dateE = DateFormat.yMd().format(endDate); final timeE = DateFormat.Hm().format(endDate); - if (events["imgUrl"] != null) { - formerImage = events["imgUrl"]; + if (events!.imgUrl != null) { + formerImage = events!.imgUrl ?? ""; } formerDate = "${date} ${time} à ${dateE} ${timeE}"; @@ -404,9 +408,7 @@ class _ItemMenuState extends State with ShowErrorDialog { onPressed: () { Navigator.push( context, - MaterialPageRoute( - builder: (_) => - EditEvent(id: id, tags: tags, organizers: organizers)), + MaterialPageRoute(builder: (_) => EditEvent(events: events)), ); }, backgroundColor: Colors.blue,