Skip to content

Commit

Permalink
reworked appointment scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelSena19 committed Oct 13, 2023
1 parent 4b5e43f commit 11f3736
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 194 deletions.
175 changes: 1 addition & 174 deletions lib/screens/booking_screen.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import 'package:flutter/material.dart';
import 'package:health_watch/utilities/appbar_widget.dart';
import 'package:table_calendar/table_calendar.dart';
import '../constants/push_routes.dart';
import '../constants/routes.dart';
import '../utilities/show_error_dialog.dart';

class BookingScreen extends StatefulWidget {
const BookingScreen({super.key});
Expand All @@ -13,176 +8,8 @@ class BookingScreen extends StatefulWidget {
}

class _BookingScreenState extends State<BookingScreen> {
CalendarFormat _format = CalendarFormat.month;
DateTime _focusDay = DateTime.now();
DateTime _currentDay = DateTime.now();
int? _currentIndex;
bool _isWeekend = false;
bool _dateSelected = false;
bool _timeSelected = false;
final bool disable = true;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appbarWidget('Book Appointments'),
body: SafeArea(
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Column(
children: [
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
child: Text(
"Select Pharmacist",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
child: Text(
"Select Appointment Date",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
_tableCalendar(),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 25),
child: Text(
"Select Appointment Time",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
],
),
),
_isWeekend
? SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 30),
alignment: Alignment.center,
child: const Text(
'The weekend is not available, please select another date',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.grey),
),
),
)
: SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) {
return InkWell(
onTap: () {
setState(() {
_currentIndex = index;
_timeSelected = true;
});
},
splashColor: Colors.transparent,
child: Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
border: Border.all(
color: _currentIndex == index
? Colors.lightBlue
: Colors.black),
borderRadius: BorderRadius.circular(10),
color: _currentIndex == index
? Colors.lightBlue
: null),
alignment: Alignment.center,
child: Text(
'${index + 9}:00 ${index + 9 > 11 ? "PM" : "AM"}',
style: TextStyle(
fontWeight: FontWeight.bold,
color: _currentIndex == index
? Colors.white
: null,
),
),
),
);
},
childCount: 8,
),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1.5,
),
),
SliverToBoxAdapter(
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 50, vertical: 80),
child: ElevatedButton(
onPressed: () {
if (_timeSelected == false || _dateSelected == false) {
showErrorDialog(context,
"Select a date and time for the appointment");
} else {
pushReplacementRoute(context, successRoute);
}
},
child: const Text('Make Appointment'),
),
),
)
],
),
),
);
}

Widget _tableCalendar() {
return TableCalendar(
focusedDay: _focusDay,
firstDay: DateTime.now(),
lastDay: DateTime(2023, 12, 31),
calendarFormat: _format,
currentDay: _currentDay,
rowHeight: 50,
calendarStyle: const CalendarStyle(
todayDecoration: BoxDecoration(
color: Colors.lightBlue,
shape: BoxShape.circle,
),
),
availableCalendarFormats: const {
CalendarFormat.month: "Month",
},
onFormatChanged: (format) {
setState(() {
_format = format;
});
},
onDaySelected: ((selectedDay, focusedDay) {
setState(() {
_currentDay = selectedDay;
_focusDay = focusedDay;
_dateSelected = true;
if (selectedDay.weekday == 6 || selectedDay.weekday == 7) {
_isWeekend = true;
_timeSelected = false;
_currentIndex = null;
} else {
_isWeekend = false;
}
});
}),
);
return const Placeholder();
}
}
24 changes: 14 additions & 10 deletions lib/screens/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ class _LoginScreenState extends State<LoginScreen> {
? 'Enter a valid email'
: null;

void pushRoute(String route){
void pushRoute(String route) {
Navigator.pushNamed(context, route);
}

void pushReplacementRoute(String route){
void pushReplacementRoute(String route) {
Navigator.pushReplacementNamed(context, route);
}

void showError(String text) {
showErrorDialog(context, text);
}

@override
void dispose() {
emailController.dispose();
Expand Down Expand Up @@ -112,10 +116,10 @@ class _LoginScreenState extends State<LoginScreen> {
Container(
height: 60,
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 100),
const EdgeInsets.symmetric(vertical: 10, horizontal: 100),
child: ElevatedButton(
onPressed: () async{
try{
onPressed: () async {
try {
String email = emailController.text;
String password = passwordController.text;
final form = formKey.currentState!;
Expand All @@ -127,14 +131,14 @@ class _LoginScreenState extends State<LoginScreen> {
pushReplacementRoute(logicRoute);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
await showErrorDialog(context, 'User not found');
showError('User not found');
} else if (e.code == 'wrong-password') {
await showErrorDialog(context, 'Wrong password');
showError('Wrong password');
} else {
await showErrorDialog(context, 'Error: $e.code');
showError('Error: $e');
}
} catch (e) {
await showErrorDialog(context, e.toString());
showError('Error: $e');
}
},
child: Container(
Expand Down Expand Up @@ -184,4 +188,4 @@ class _LoginScreenState extends State<LoginScreen> {
),
);
}
}
}
75 changes: 73 additions & 2 deletions lib/screens/pharmacist_details_screen.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:health_watch/constants/push_routes.dart';
import 'package:health_watch/constants/routes.dart';
import 'package:health_watch/constants/user_data.dart';
import 'package:health_watch/utilities/appbar_widget.dart';
import 'package:health_watch/utilities/show_error_dialog.dart';

class PharmacistDetailsScreen extends StatefulWidget {
const PharmacistDetailsScreen({Key? key, required this.name})
Expand All @@ -18,9 +20,56 @@ class PharmacistDetailsScreen extends StatefulWidget {

class _PharmacistDetailsScreenState extends State<PharmacistDetailsScreen> {
bool isFav = false;
String _pharmacistName = '';
String _pharmacy = '';

@override
Widget build(BuildContext context) {
Future<DateTime?> pickDate() {
return showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.utc(2025),
);
}

Future<TimeOfDay?> pickTime() {
return showTimePicker(
context: context,
initialTime: TimeOfDay(
hour: TimeOfDay.now().hour,
minute: TimeOfDay.now().minute,
),
);
}

void showError(String error) {
showErrorDialog(context, error);
}

void push(String route){
pushRoute(context, route);
}

Future<void> bookAppointment(DateTime? date, String patient,
String pharmacist, String pharmacy, String status, String time) async {
final CollectionReference appointments =
FirebaseFirestore.instance.collection('appointments');
try {
await appointments.doc().set({
'date': date,
'patient': patient,
'pharmacist': pharmacist,
'pharmacy': pharmacy,
'status': status,
'time': time
});
} catch (error) {
showError('$error');
}
}

return Scaffold(
appBar: appbarWidget("Pharmacist Details"),
body: ListView(
Expand All @@ -46,6 +95,8 @@ class _PharmacistDetailsScreenState extends State<PharmacistDetailsScreen> {
pharmacistInfo['certification'] as String? ?? '';
final String pharmacy =
pharmacistInfo['pharmacy'] as String? ?? '';
_pharmacistName = widget.name;
_pharmacy = pharmacy;
final int id = pharmacistInfo['id'] as int? ?? 0;
return SizedBox(
width: double.infinity,
Expand Down Expand Up @@ -253,8 +304,28 @@ class _PharmacistDetailsScreenState extends State<PharmacistDetailsScreen> {
Padding(
padding: const EdgeInsets.all(20),
child: ElevatedButton(
onPressed: () {
pushRoute(context, bookingRoute);
onPressed: () async {
final date = await pickDate();
if (date == null) {
showError('Please enter a date');
}
else{
final time = await pickTime();
if (time == null) {
showError('Please enter a time');
}
else{
String selectedTime = '${time.hour}:${time.minute}';
String patient =
FirebaseAuth.instance.currentUser!.email.toString();
String pharmacist = _pharmacistName;
String pharmacy = _pharmacy;
String status = 'upcoming';
await bookAppointment(date, patient, pharmacist, pharmacy,
status, selectedTime);
push(successRoute);
}
}
},
style: ButtonStyle(
elevation: MaterialStateProperty.all(0),
Expand Down
Loading

0 comments on commit 11f3736

Please sign in to comment.