Skip to content

Commit

Permalink
updated search screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelSena19 committed Oct 14, 2023
1 parent 335a003 commit 54ce1b8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 17 deletions.
77 changes: 70 additions & 7 deletions lib/screens/search_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../utilities/appbar_widget.dart';
import '../utilities/appointment_card.dart';
Expand All @@ -13,8 +14,13 @@ class SearchScreen extends StatefulWidget {
}

class _SearchScreenState extends State<SearchScreen> {
DateTime now = DateTime.now();
String patient = FirebaseAuth.instance.currentUser!.email.toString();
@override
Widget build(BuildContext context) {
Timestamp today =
Timestamp.fromDate(DateTime.utc(now.year, now.month, now.day));
print(today);
return Scaffold(
appBar: appbarWidget('Search'),
drawer: drawerWidget(context),
Expand Down Expand Up @@ -63,7 +69,60 @@ class _SearchScreenState extends State<SearchScreen> {
const SizedBox(
height: 20,
),
const AppointmentCard(),
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('appointments')
.where('patient', isEqualTo: patient)
.snapshots(),
builder: (context, snapshot) {
final appointments = snapshot.data!.docs;
if (snapshot.hasError) {
print('error');
return Text('Error: ${snapshot.error}');
} else if (snapshot.connectionState ==
ConnectionState.waiting) {
print('waiting');
return const Center(
child: SizedBox(
height: 10,
width: 10,
child: CircularProgressIndicator(),
),
);
} else if (appointments.isNotEmpty) {
print('has data');
return Column(
children: List.generate(appointments.length, (index) {
final appointment = appointments[index];
Timestamp firestoreTimestamp = appointment['date'];
if (firestoreTimestamp.toDate().isAtSameMomentAs(
DateTime(now.year, now.month, now.day, 0, 0, 0))) {
return AppointmentCard(
date: appointment['date'] ?? today,
patient: patient,
pharmacist: appointment['pharmacist'] ?? '',
pharmacy: appointment['pharmacy'] ?? '',
time: appointment['time'] ?? '',
);
}
else {
print('no appointments');
return const SizedBox();
}
}),
);
} else {
print('no appointments');
return const Center(
child: Text(
"You don't have any appointments today",
style:
TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
);
}
},
),
const SizedBox(
height: 40,
),
Expand All @@ -76,7 +135,8 @@ class _SearchScreenState extends State<SearchScreen> {
Expanded(child: Container()),
TextButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context){
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return const SeeMore();
}));
},
Expand Down Expand Up @@ -142,15 +202,19 @@ class SeeMore extends StatelessWidget {
elevation: 0,
backgroundColor: Colors.transparent,
centerTitle: true,
title: const Icon(Icons.local_pharmacy_outlined, color: Colors.lightBlue, size: 30,),
title: const Icon(
Icons.local_pharmacy_outlined,
color: Colors.lightBlue,
size: 30,
),
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('pharmacists').snapshots(),
stream:
FirebaseFirestore.instance.collection('pharmacists').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (snapshot.connectionState ==
ConnectionState.waiting) {
} else if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: SizedBox(
height: 10,
Expand All @@ -176,4 +240,3 @@ class SeeMore extends StatelessWidget {
);
}
}

69 changes: 59 additions & 10 deletions lib/utilities/appointment_card.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:health_watch/utilities/show_error_dialog.dart';

class AppointmentCard extends StatefulWidget {
const AppointmentCard({Key? key}) : super(key: key);
const AppointmentCard(
{Key? key,
required this.date,
required this.patient,
required this.pharmacist,
required this.pharmacy,
required this.time})
: super(key: key);

final Timestamp date;
final String patient;
final String pharmacist;
final String pharmacy;
final String time;

@override
State<AppointmentCard> createState() => _AppointmentCardState();
}

class _AppointmentCardState extends State<AppointmentCard> {
Future<void> updateStatus(String newStatus) async {
try{
final appointmentRef =
FirebaseFirestore.instance.collection('appointments');

QuerySnapshot querySnapshot = await appointmentRef
.where('patient', isEqualTo: widget.patient)
.where('pharmacy', isEqualTo: widget.pharmacy)
.where('pharmacist', isEqualTo: widget.pharmacist)
.where('date', isEqualTo: widget.date)
.where('time', isEqualTo: widget.time)
.get();

if(querySnapshot.docs.isNotEmpty){
final appointmentDoc = querySnapshot.docs.first;
await appointmentDoc.reference.update({'status': newStatus});
}
}
catch (error){
showError('$error');
}
}

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

@override
Widget build(BuildContext context) {
return Column(
Expand Down Expand Up @@ -36,17 +77,18 @@ class _AppointmentCardState extends State<AppointmentCard> {
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
children: [
Text(
'Dr. Emmanuel Doke',
style: TextStyle(color: Colors.white, fontSize: 18),
widget.pharmacist,
style: const TextStyle(
color: Colors.white, fontSize: 18),
),
SizedBox(
const SizedBox(
height: 5,
),
Text(
'Burma Camp Pharmacy',
style: TextStyle(color: Colors.black),
widget.pharmacy,
style: const TextStyle(color: Colors.black),
),
],
),
Expand All @@ -55,7 +97,10 @@ class _AppointmentCardState extends State<AppointmentCard> {
const SizedBox(
height: 10,
),
ScheduleCard(date: Timestamp.now(), time: '2:0',),
ScheduleCard(
date: widget.date,
time: widget.time,
),
const SizedBox(
height: 10,
),
Expand All @@ -70,7 +115,9 @@ class _AppointmentCardState extends State<AppointmentCard> {
'Cancel',
style: TextStyle(color: Colors.white),
),
onPressed: () {},
onPressed: () {
updateStatus('canceled');
},
),
),
const SizedBox(
Expand All @@ -85,7 +132,9 @@ class _AppointmentCardState extends State<AppointmentCard> {
'Completed',
style: TextStyle(color: Colors.white),
),
onPressed: () {},
onPressed: () {
updateStatus('completed');
},
),
),
],
Expand Down

0 comments on commit 54ce1b8

Please sign in to comment.