You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/home/home_screen.dart

291 lines
9.3 KiB
Dart

import 'dart:developer';
3 years ago
import 'package:connectivity/connectivity.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
3 years ago
import 'package:queuing_system/core/api.dart';
4 years ago
import 'package:queuing_system/core/base/app_scaffold_widget.dart';
4 years ago
import 'package:queuing_system/core/config/size_config.dart';
3 years ago
import 'package:queuing_system/core/response_model/patient_call.dart';
import 'package:queuing_system/header/app_header.dart';
3 years ago
import 'package:queuing_system/home/home_screen_components.dart';
import 'package:queuing_system/utils/call_by_voice.dart';
import 'package:queuing_system/utils/call_type.dart';
4 years ago
import 'package:queuing_system/utils/signalR_utils.dart';
import 'package:queuing_system/utils/utils.dart';
import 'package:queuing_system/widget/data_display/app_texts_widget.dart';
3 years ago
var DEVICE_IP = "10.10.15.11"; // Testing IP
// var DEVICE_IP = "10.10.14.11"; // Testing IP
// var DEVICE_IP = "10.10.15.11";
3 years ago
// var DEVICE_IP = "10.70.249.21"; // (Make sure by Haroon before use it) Production IP
4 years ago
class MyHomePage extends StatefulWidget {
final String title = "MyHomePage";
const MyHomePage({Key key}) : super(key: key);
4 years ago
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
3 years ago
SignalRHelper signalRHelper = SignalRHelper();
List<Tickets> waitings = [];
List<Tickets> isQueuePatients = [];
4 years ago
bool isLoading = false;
4 years ago
@override
void dispose() {
super.dispose();
}
@override
void initState() {
3 years ago
listenNetworkConnectivity();
3 years ago
if (!signalRHelper.getConnectionState()) {
signalRHelper.startSignalRConnection(DEVICE_IP, onUpdateAvailable: onUpdateAvailable, onConnect: onConnect, onConnecting: onConnecting, onDisconnect: onDisconnect);
3 years ago
}
4 years ago
super.initState();
}
TextEditingController controller = TextEditingController();
4 years ago
@override
Widget build(BuildContext context) {
return AppScaffold(
appBar: AppHeader(),
3 years ago
body: content(),
bottomNavigationBar: Container(
color: Colors.grey.withOpacity(0.1),
4 years ago
height: Utils.getHeight(),
4 years ago
width: double.infinity,
child: Row(
3 years ago
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
4 years ago
children: [
Row(
children: [
const SizedBox(width: 20),
AppText(
"Powered By",
fontSize: SizeConfig.getWidthMultiplier() * 2.6,
fontFamily: 'Poppins-Medium.ttf',
),
const SizedBox(width: 20),
Image.asset(
"assets/images/cloud_logo.png",
height: SizeConfig.getHeightMultiplier() * 4,
),
],
),
// Row(
// children: [
// const SizedBox(width: 60),
// SizedBox(
// width: 200,
// child: TextField(
// controller: controller,
// )),
// const SizedBox(width: 30),
// isLoading
// ? const CircularProgressIndicator()
// : ElevatedButton(
// onPressed: onUpdateIPPressed,
// child: const Text(
// "Update IP",
// style: TextStyle(color: Colors.white),
// ),
// style: ElevatedButton.styleFrom(backgroundColor: AppGlobal.appRedColor),
// ),
// const SizedBox(width: 30),
// Text("IP: $DEVICE_IP", style: const TextStyle(fontWeight: FontWeight.w600)),
// const SizedBox(width: 20),
// ],
// ),
4 years ago
],
),
3 years ago
),
4 years ago
);
}
3 years ago
onUpdateIPPressed() async {
// if (controller.text.isNotEmpty) {
// isLoading = true;
// setState(() {});
// DEVICE_IP = controller.text;
//
// await signalRHelper.connection.stop();
// if (!signalRHelper.getConnectionState()) {
// await signalRHelper.startSignalRConnection(DEVICE_IP, onUpdateAvailable: onUpdateAvailable, onConnect: onConnect, onConnecting: onConnecting, onDisconnect: onDisconnect);
// }
//
// controller.clear();
// waitings.clear();
// isLoading = false;
// setState(() {});
// }
}
Widget content() {
// waitings.removeAt(0);
3 years ago
// waitings = waitings.sublist(0,3);
voiceCall();
if (waitings.isEmpty) {
3 years ago
// No Patient in Queue
return noPatientInQueue();
} else if (waitings.length > 3) {
3 years ago
// Return Content With Side List
return priorityTicketsWithSideList(waitings);
} else {
3 years ago
// Return Content In Center Aligned
return priorityTickets(waitings);
}
}
String getCallTypeText(Tickets ticket) {
final callType = ticket.getCallType();
switch (callType) {
case CallType.RECEPTION:
return "Please Visit Doctor";
break;
case CallType.NURSE:
return "Please Visit Nurse";
break;
case CallType.DOCTOR:
return "Please Visit Doctor";
break;
case CallType.NONE:
return "";
break;
default:
return "";
}
}
3 years ago
CallByVoice voiceCaller;
final AudioPlayer audioPlayer = AudioPlayer();
int callFlag = 0;
voiceCall() async {
//DONE: After calling this voice call, we should delay for milliseconds that is given by API. After that we will check if there are more patients in isQueuePatients we will remove the patient from waiting list and then update the state
if (waitings.isNotEmpty) {
if (waitings.first.isToneReq) {
audioPlayer.setAsset("assets/tones/call_tone.mp3");
await audioPlayer.play();
await Future.delayed(const Duration(seconds: 2));
}
if (waitings.first.isVoiceReq && voiceCaller == null) {
final postVoice = getCallTypeText(waitings.first);
voiceCaller = CallByVoice(waitings.first.queueNo.toString(), preVoice: "Ticket Number", postVoice: postVoice, lang: 'en');
await voiceCaller.startCalling();
voiceCaller = null;
}
}
if (isQueuePatients.isNotEmpty) {
await Future.delayed(Duration(milliseconds: int.parse(isQueuePatients.first.queueDuration) * 10)).whenComplete(() async {
if (isQueuePatients.isNotEmpty) {
isQueuePatients.removeAt(0);
}
if (waitings.isNotEmpty) {
Tickets ticket = waitings.elementAt(0);
waitings.removeAt(0);
waitings.add(ticket);
}
if (isQueuePatients.isNotEmpty) {
setState(() {});
}
});
} else {
// if (isQueuePatients.isEmpty && callFlag == 1) {
// callFlag == 0;
// await Future.delayed(const Duration(seconds: 3));
// waitings.clear();
// API.getCallRequestInfoByClinicInfo(DEVICE_IP, onSuccess: (waitingCalls, isQueuePatientsCalls) {
// setState(() {
// waitings = waitingCalls;
// isQueuePatients = isQueuePatientsCalls;
// // currents = currentInClinic;
// });
//
// log("--------------------");
// log("waiting: $waitings");
// log("isQueuePatients: $isQueuePatients");
// log("--------------------");
//
// updateTickets();
// }, onFailure: (error) {});
// }
3 years ago
}
}
onUpdateAvailable(data) async {
print("here is the data: $data");
if (isQueuePatients.isNotEmpty && callFlag == 0) {
callFlag = 1;
return;
}
waitings.clear();
API.getCallRequestInfoByClinicInfo(DEVICE_IP, onSuccess: (waitingCalls, isQueuePatientsCalls) {
setState(() {
waitings = waitingCalls;
isQueuePatients = isQueuePatientsCalls;
// currents = currentInClinic;
});
log("--------------------");
log("waiting: $waitings");
log("isQueuePatients: $isQueuePatients");
log("--------------------");
updateTickets();
}, onFailure: (error) {});
3 years ago
}
updateTickets() {
if (waitings != null && waitings.isNotEmpty) {
List<Tickets> _ticketsToUpdate = waitings.where((t) => t.callUpdated == false).toList();
API.callUpdateNotIsQueueRecordByIDAsync(DEVICE_IP, ticket: _ticketsToUpdate.first, onSuccess: (tickets_updated) {
log("[${tickets_updated.length}] Tickets Updated: $tickets_updated");
}, onFailure: (e) {
log("API UPDate Tickets Failed with : ${e.toString()}");
});
}
3 years ago
}
onConnect() {
log("SignalR: onConnect");
3 years ago
}
onDisconnect(exception) {
log("SignalR: onDisconnect");
signalRHelper.startSignalRConnection(DEVICE_IP, onUpdateAvailable: onUpdateAvailable, onConnect: onConnect, onConnecting: onConnecting, onDisconnect: onDisconnect);
3 years ago
}
onConnecting() {
log("SignalR: onConnecting");
3 years ago
}
listenNetworkConnectivity() async {
3 years ago
Connectivity().onConnectivityChanged.listen((event) {
switch (event) {
case ConnectivityResult.wifi:
signalRHelper.connection.start();
break;
case ConnectivityResult.none:
signalRHelper.closeConnection(context);
break;
case ConnectivityResult.mobile:
break;
3 years ago
}
});
}
4 years ago
}