From c70e29c47a72396ac2bba4813e19a01f23cf3c2f Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Mon, 24 Apr 2023 18:43:48 -0500 Subject: [PATCH] citas --- lib/src/models/event_model.dart | 63 ++++++++- lib/src/screens/calendar.dart | 233 +++++++++++++++++++++++++++++++- 2 files changed, 289 insertions(+), 7 deletions(-) diff --git a/lib/src/models/event_model.dart b/lib/src/models/event_model.dart index 05549b1..eaf29d2 100644 --- a/lib/src/models/event_model.dart +++ b/lib/src/models/event_model.dart @@ -4,15 +4,76 @@ import 'package:prosappco/src/authentication/authentication_repository.dart'; final uid = AuthenticationRepository.instance.getCurrentUserUid(); class EventoService { - Future createEvent(String title, String description, String day) async { + Future createEvent( + String title, + String description, + String day, + String range1Hour1, + String range1Hour2, + ) async { try { await FirebaseFirestore.instance.collection('services').add({ + 'profesional_id': uid, 'title': title, 'description': description, 'day': day, + 'range1Hour1': range1Hour1, + 'range1Hour2': range1Hour2 + }).then((value) { + FirebaseFirestore.instance.collection('users').doc(uid).update({ + 'services': FieldValue.arrayUnion([value.id]) + }); }); } catch (e) { print('Evento $e'); } } } + +Future> getByProId() async { + try { + var snapshot = await FirebaseFirestore.instance + .collection('services') + .where('profesional_id', isEqualTo: uid) + .get(); + + List eventos = []; + for (var element in snapshot.docs) { + eventos.add(Event.fromJson(element.data())); + } + print('Error getByProId $eventos'); + return eventos; + } catch (e) { + print('Error getByProId $e'); + return []; + } +} + +class Event { + String? title; + String? description; + String? day; + String? range1Hour1; + String? range1Hour2; + String? profesionalId; + + Event({ + this.title, + this.description, + this.day, + this.range1Hour1, + this.range1Hour2, + this.profesionalId, + }); + + factory Event.fromJson(Map json) { + return Event( + title: json['title'], + description: json['description'], + day: json['day'], + range1Hour1: json['range1Hour1'], + range1Hour2: json['range1Hour2'], + profesionalId: json['profesional_id'], + ); + } +} diff --git a/lib/src/screens/calendar.dart b/lib/src/screens/calendar.dart index b6a43de..e84a160 100644 --- a/lib/src/screens/calendar.dart +++ b/lib/src/screens/calendar.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; import 'package:prosappco/src/components/pop_appbar.dart'; +import 'package:prosappco/src/components/primary_btn.dart'; import 'package:prosappco/src/models/event_model.dart'; import 'package:table_calendar/table_calendar.dart'; +import 'package:intl/intl.dart'; class CalendarScreen extends StatefulWidget { const CalendarScreen({super.key}); @@ -11,9 +13,40 @@ class CalendarScreen extends StatefulWidget { } class _CalendarScreenState extends State { + final _titleController = TextEditingController(); + final _descriptionController = TextEditingController(); + EventoService eventoService = EventoService(); CalendarFormat _calendarFormat = CalendarFormat.month; DateTime today = DateTime.now(); + TimeOfDay? _selectedTime1; + TimeOfDay? _selectedTime2; + + Future _selectTime1(BuildContext context) async { + final TimeOfDay? pickedTime1 = await showTimePicker( + context: context, + initialTime: TimeOfDay.now(), + ); + if (pickedTime1 != null) { + setState(() { + _selectedTime1 = pickedTime1; + }); + } + return pickedTime1; + } + + Future _selectTime2(BuildContext context) async { + final TimeOfDay? pickedTime2 = await showTimePicker( + context: context, + initialTime: TimeOfDay.now(), + ); + if (pickedTime2 != null) { + setState(() { + _selectedTime2 = pickedTime2; + }); + } + return pickedTime2; + } @override void initState() { @@ -45,7 +78,7 @@ class _CalendarScreenState extends State { }, label: 'Calendario', ), - body: SingleChildScrollView( + body: Container( child: Column( children: [ Container( @@ -67,18 +100,206 @@ class _CalendarScreenState extends State { }, ), ), - Text(today.toString()) + _eventList() ], ), ), floatingActionButton: FloatingActionButton( - onPressed: () async { - await eventoService.createEvent( - 'Hola', 'Saludarnois', today.toString()); - }, + onPressed: _showDialog, child: const Icon(Icons.add), ), ), ); } + + void _showDialog() { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(15), + ), + content: StatefulBuilder( + builder: (BuildContext context, StateSetter setStateDialog) { + return SizedBox( + height: 350, + child: Column( + children: [ + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 0, vertical: 15), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text( + _selectedTime1 == null + ? '' + : _selectedTime1!.format(context), + style: TextStyle( + color: Colors.grey[500], + fontSize: 12, + ), + ), + _selectedTime2 != null && _selectedTime1 != null + ? Text( + ' - ', + style: TextStyle( + color: Colors.grey[500], + fontSize: 12, + ), + ) + : const SizedBox(), + Text( + _selectedTime2 == null + ? '' + : _selectedTime2!.format(context), + style: TextStyle( + color: Colors.grey[500], + fontSize: 12, + ), + ), + Text( + ' | ', + style: TextStyle( + color: Colors.grey[500], + ), + ), + Text( + DateFormat('dd MMMM yyyy', 'es').format(today), + style: const TextStyle( + fontSize: 13, + ), + ) + ], + ), + ), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 10), + child: TextFormField( + controller: _titleController, + decoration: const InputDecoration(hintText: 'Titulo'), + ), + ), + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 10, vertical: 20), + child: TextFormField( + controller: _descriptionController, + decoration: + const InputDecoration(hintText: 'Descripción'), + ), + ), + Padding( + padding: const EdgeInsets.only(bottom: 40), + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SizedBox( + width: 80, + child: TextFormField( + textAlign: TextAlign.center, + onTap: () async { + var value = await _selectTime1(context); + setStateDialog(() { + _selectedTime1 = value; + }); + }, + readOnly: true, + decoration: const InputDecoration( + hintText: 'Hora', + ), + controller: TextEditingController( + text: _selectedTime1 == null + ? '' + : ' ${_selectedTime1!.format(context)}'), + style: const TextStyle(fontSize: 15), + ), + ), + const Text(' - '), + SizedBox( + width: 80, + child: TextFormField( + textAlign: TextAlign.center, + onTap: () async { + var value = await _selectTime2(context); + setStateDialog(() { + _selectedTime2 = value; + }); + }, + readOnly: true, + decoration: const InputDecoration( + hintText: 'Hora', + ), + controller: TextEditingController( + text: _selectedTime2 == null + ? '' + : ' ${_selectedTime2!.format(context)}'), + style: const TextStyle(fontSize: 15), + ), + ), + ], + ), + ), + PrimaryButtom( + onPressed: () async { + await eventoService + .createEvent( + _titleController.text, + _descriptionController.text, + today.toString(), + _selectedTime1!.format(context).toString(), + _selectedTime2!.format(context).toString(), + ) + .then((value) { + Navigator.pop(context); + _titleController.text = ''; + _descriptionController.text = ''; + }); + }, + label: 'Añadir evento', + ), + ], + ), + ); + }, + ), + ); + }, + ); + } + + Widget _eventList() { + return FutureBuilder( + future: getByProId(), + builder: (BuildContext context, AsyncSnapshot> snapshot) { + if (!snapshot.hasData) { + return const Text('No tiene citas'); + } + + List eventos = []; + + try { + eventos.addAll(snapshot.data!); + } catch (e) { + print("SAPOAAAA" + e.toString()); + } + + if (eventos.isEmpty) { + return const Text('No tiene citas'); + } + return Column( + children: [ + ...eventos.map( + (e) => ListTile( + leading: Text('${e.range1Hour1}'), + title: Text('${e.title}'), + trailing: Text('${e.day}'), + ), + ), + ], + ); + }, + ); + } }