|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:hmg_qline/config/dependency_injection.dart';
|
|
|
|
|
import 'package:hmg_qline/constants/app_constants.dart';
|
|
|
|
|
import 'package:hmg_qline/models/generic_response_model.dart';
|
|
|
|
|
import 'package:hmg_qline/models/global_config_model.dart';
|
|
|
|
|
import 'package:hmg_qline/models/prayers_widget_model.dart';
|
|
|
|
|
import 'package:hmg_qline/models/rss_feed_model.dart';
|
|
|
|
|
import 'package:hmg_qline/models/weathers_widget_model.dart';
|
|
|
|
|
import 'package:hmg_qline/repositories/screen_details_repo.dart';
|
|
|
|
|
import 'package:hmg_qline/services/cache_service.dart';
|
|
|
|
|
import 'package:hmg_qline/services/connectivity_service.dart';
|
|
|
|
|
import 'package:hmg_qline/utilities/enums.dart';
|
|
|
|
|
import 'package:hmg_qline/utilities/extensions.dart';
|
|
|
|
|
import 'package:hmg_qline/view_models/queuing_view_model.dart';
|
|
|
|
|
|
|
|
|
|
class ScreenConfigViewModel extends ChangeNotifier {
|
|
|
|
|
final ScreenDetailsRepo screenDetailsRepo;
|
|
|
|
|
final CacheService cacheService;
|
|
|
|
|
final ConnectivityService connectivityService;
|
|
|
|
|
|
|
|
|
|
ScreenConfigViewModel({
|
|
|
|
|
required this.screenDetailsRepo,
|
|
|
|
|
required this.cacheService,
|
|
|
|
|
required this.connectivityService,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Future<void> initializeScreenConfigVM() async {
|
|
|
|
|
await getCurrentScreenIP();
|
|
|
|
|
await getGlobalConfigurationsByIP();
|
|
|
|
|
await getInfoWidgetsDetailsFromServer();
|
|
|
|
|
await getLastTimeUpdatedFromCache();
|
|
|
|
|
listenNetworkConnectivity();
|
|
|
|
|
getTheWidgetsConfigurationsEveryMidnight();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isInternetConnected = true;
|
|
|
|
|
|
|
|
|
|
updateIsInternetConnected(bool value) {
|
|
|
|
|
isInternetConnected = value;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isHubConnected = true;
|
|
|
|
|
|
|
|
|
|
updateIsHubConnected(bool value) {
|
|
|
|
|
isHubConnected = value;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewState state = ViewState.idle;
|
|
|
|
|
|
|
|
|
|
void updateViewState(ViewState viewState) {
|
|
|
|
|
state = viewState;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScreenOrientationEnum currentScreenRotation = ScreenOrientationEnum.portraitUp;
|
|
|
|
|
|
|
|
|
|
updateCurrentScreenRotation(ScreenOrientationEnum value) {
|
|
|
|
|
currentScreenRotation = value;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScreenTypeEnum currentScreenTypeEnum = ScreenTypeEnum.waitingAreaScreen;
|
|
|
|
|
|
|
|
|
|
updateCurrentScreenTypeEnum(ScreenTypeEnum value) {
|
|
|
|
|
currentScreenTypeEnum = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTypeEnum currentQTypeEnum = QTypeEnum.lab;
|
|
|
|
|
|
|
|
|
|
updateCurrentQTypeEnum(QTypeEnum value) {
|
|
|
|
|
currentQTypeEnum = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String currentScreenIP = "";
|
|
|
|
|
|
|
|
|
|
Future<void> getCurrentScreenIP() async {
|
|
|
|
|
if (useTestIP) {
|
|
|
|
|
currentScreenIP = AppConstants.testIP;
|
|
|
|
|
} else {
|
|
|
|
|
currentScreenIP = await connectivityService.getCurrentScreenIP();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void listenNetworkConnectivity() {
|
|
|
|
|
return connectivityService.subscribeToConnectivityChange(onInternetDisConnected: () {
|
|
|
|
|
updateIsInternetConnected(false);
|
|
|
|
|
updateIsHubConnected(false);
|
|
|
|
|
}, onInternetConnected: () {
|
|
|
|
|
updateIsInternetConnected(true);
|
|
|
|
|
QueuingViewModel queuingViewModel = getIt.get<QueuingViewModel>();
|
|
|
|
|
queuingViewModel.startHubConnection();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalConfigurationsModel globalConfigurationsModel = GlobalConfigurationsModel();
|
|
|
|
|
|
|
|
|
|
Future<void> getGlobalConfigurationsByIP() async {
|
|
|
|
|
GlobalConfigurationsModel? response = await screenDetailsRepo.getGlobalScreenConfigurations(ipAddress: currentScreenIP);
|
|
|
|
|
if (response == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
updateGlobalConfigurationsModel(value: response);
|
|
|
|
|
updateCurrentScreenTypeEnum(globalConfigurationsModel.screenTypeEnum);
|
|
|
|
|
updateCurrentQTypeEnum(globalConfigurationsModel.qTypeEnum);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateGlobalConfigurationsModel({required var value, bool needNotify = false}) {
|
|
|
|
|
globalConfigurationsModel = value;
|
|
|
|
|
if (needNotify) {
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> getInfoWidgetsDetailsFromServer() async {
|
|
|
|
|
if (globalConfigurationsModel.isWeatherReq) {
|
|
|
|
|
await getWeatherDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
if (globalConfigurationsModel.isPrayerTimeReq) {
|
|
|
|
|
await getPrayerDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
if (globalConfigurationsModel.isRssFeedReq) {
|
|
|
|
|
await getRssFeedDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int currentDate = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
|
await cacheService.setLastTimeUpdatedInCache(lasTimeUpdated: currentDate.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RssFeedModel rssFeedModel = RssFeedModel();
|
|
|
|
|
|
|
|
|
|
Future<void> getRssFeedDetailsFromServer() async {
|
|
|
|
|
RssFeedModel? response = await screenDetailsRepo.getRssFeedDetailsByLanguageID(languageId: 0);
|
|
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rssFeedModel = response;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrayersWidgetModel prayersWidgetModel = PrayersWidgetModel();
|
|
|
|
|
|
|
|
|
|
Future<void> getPrayerDetailsFromServer() async {
|
|
|
|
|
PrayersWidgetModel? response = await screenDetailsRepo.getPrayerDetailsByLatLong(
|
|
|
|
|
latitude: globalConfigurationsModel.projectLatitude ?? 0.0,
|
|
|
|
|
longitude: globalConfigurationsModel.projectLongitude ?? 0.0,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prayersWidgetModel = response;
|
|
|
|
|
getNextPrayerToShow();
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WeathersWidgetModel weathersWidgetModel = WeathersWidgetModel();
|
|
|
|
|
|
|
|
|
|
Future<void> getWeatherDetailsFromServer() async {
|
|
|
|
|
WeathersWidgetModel? response = await screenDetailsRepo.getWeatherDetailsByCity(
|
|
|
|
|
cityId: globalConfigurationsModel.cityKey.toString(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
weathersWidgetModel = response;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String nextPrayerToShowWithTime = '';
|
|
|
|
|
|
|
|
|
|
void getNextPrayerToShow() {
|
|
|
|
|
final current = DateTime.now();
|
|
|
|
|
log("Checking Namaz time Locally at ${current.toString()} and ${current.timeZoneName} ");
|
|
|
|
|
|
|
|
|
|
if (prayersWidgetModel.fajr!.toDateTimeFromInt().isAfter(current)) {
|
|
|
|
|
final namazTime = prayersWidgetModel.fajr!.toFormattedDateTimeFromInt();
|
|
|
|
|
nextPrayerToShowWithTime = "${globalConfigurationsModel.fajarText} at $namazTime";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prayersWidgetModel.dhuhr!.toDateTimeFromInt().isAfter(current)) {
|
|
|
|
|
final namazTime = prayersWidgetModel.dhuhr!.toFormattedDateTimeFromInt();
|
|
|
|
|
nextPrayerToShowWithTime = "${globalConfigurationsModel.dhuhrText} at $namazTime";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prayersWidgetModel.asr!.toDateTimeFromInt().isAfter(current)) {
|
|
|
|
|
final namazTime = prayersWidgetModel.asr!.toFormattedDateTimeFromInt();
|
|
|
|
|
nextPrayerToShowWithTime = "${globalConfigurationsModel.asarText} at $namazTime";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prayersWidgetModel.maghrib!.toDateTimeFromInt().isAfter(current)) {
|
|
|
|
|
final namazTime = prayersWidgetModel.maghrib!.toFormattedDateTimeFromInt();
|
|
|
|
|
nextPrayerToShowWithTime = "${globalConfigurationsModel.maghribText} at $namazTime";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prayersWidgetModel.isha!.toDateTimeFromInt().isAfter(current)) {
|
|
|
|
|
final namazTime = prayersWidgetModel.isha!.toFormattedDateTimeFromInt();
|
|
|
|
|
nextPrayerToShowWithTime = "${globalConfigurationsModel.ishaText} at $namazTime";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
|
|
Future<void> getTheWidgetsConfigurationsEveryMidnight() async {
|
|
|
|
|
if (!(globalConfigurationsModel.isWeatherReq) && !(globalConfigurationsModel.isPrayerTimeReq) && !(globalConfigurationsModel.isRssFeedReq)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!globalConfigurationsModel.isWeatherReq && !globalConfigurationsModel.isPrayerTimeReq && !globalConfigurationsModel.isRssFeedReq) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime current = DateTime.now();
|
|
|
|
|
Stream timer = Stream.periodic(const Duration(minutes: 1), (i) {
|
|
|
|
|
current = current.add(const Duration(minutes: 1));
|
|
|
|
|
return current;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
timer.listen((data) async {
|
|
|
|
|
DateTime dateTime = DateTime.parse(data.toString());
|
|
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
|
|
log("counterValue: $counter");
|
|
|
|
|
|
|
|
|
|
if (counter == 60 && globalConfigurationsModel.isRssFeedReq) {
|
|
|
|
|
await getRssFeedDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (globalConfigurationsModel.isWeatherReq) {
|
|
|
|
|
if (dateTime.day > currentLastTimeUpdated.day) {
|
|
|
|
|
await getWeatherDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (globalConfigurationsModel.isPrayerTimeReq) {
|
|
|
|
|
if (dateTime.day > currentLastTimeUpdated.day) {
|
|
|
|
|
await getPrayerDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (globalConfigurationsModel.isRssFeedReq) {
|
|
|
|
|
if (dateTime.day > currentLastTimeUpdated.day) {
|
|
|
|
|
await getRssFeedDetailsFromServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
getNextPrayerToShow();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime currentLastTimeUpdated = DateTime.now();
|
|
|
|
|
|
|
|
|
|
Future<void> getLastTimeUpdatedFromCache() async {
|
|
|
|
|
DateTime? response = await cacheService.getLastTimeUpdatedFromCache();
|
|
|
|
|
if (response != null) {
|
|
|
|
|
currentLastTimeUpdated = response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> createAutoTickets() async {
|
|
|
|
|
int numOfTicketsToCreate = 20;
|
|
|
|
|
int startTicket = 123456920;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numOfTicketsToCreate; i++) {
|
|
|
|
|
GenericRespModel? response = await screenDetailsRepo.createNextTickets(ticketNumber: startTicket);
|
|
|
|
|
startTicket = startTicket + 1;
|
|
|
|
|
if (response == null) {
|
|
|
|
|
log("response null from createNextTickets");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|