Files
GSFV2/gsf/lib/views/pages/MenstrualCycleTracker/viewModel/StoringDates.dart
2024-04-25 14:57:00 +05:30

154 lines
5.5 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'package:get/get.dart';
import 'package:gsp_app/api/network_api.dart';
import 'package:gsp_app/modals/QuizModel.dart';
import 'package:gsp_app/repository/endpoints.dart';
import 'package:gsp_app/views/pages/MenstrualCycleTracker/Helper/HelperMethods.dart';
import 'package:gsp_app/views/pages/quiz/quiz_home.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';
import '../../../../api/base_manager.dart';
QuizeModel? quizData;
RxList<PickerDateRange> pickerdateRange = <PickerDateRange>[].obs;
final HelperMethods helperController = Get.put(HelperMethods());
class StoringDates {
Future<ResponseData<dynamic>> storeAllList(updata) async {
final response =
await NetworkApi().postApi(url: Endpoints.storeDates, data: updata);
if (response.status == ResponseStatus.SUCCESS) {}
print("$response");
return response;
}
Future<ResponseData<dynamic>> getAllList() async {
final response = await NetworkApi().getApi(Endpoints.getPeriodDates);
String pickerdateRangeString = response.data['result']['pickerdateRange'];
// Use a regular expression to remove [ and ] from the start and end of the string
// RegExp exp1 = RegExp(r"^\[(.*)\]$");
// Match? match1 = exp1.firstMatch(pickerdateRangeString);
// String resultString = match1!.group(1)!;
// RegExp exp = RegExp(
// r"PickerDateRange#\w+\(startDate: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}), endDate: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\)");
// Iterable<Match> matches = exp.allMatches(resultString);
// pickerdateRange.assignAll(matches.map((match) {
// DateTime? startDate = DateTime.tryParse(match.group(1)!);
// DateTime? endDate = DateTime.tryParse(match.group(2)!);
// return PickerDateRange(startDate, endDate);
// }));
// Custom function -----+++++----
List<Map<String, DateTime>> customParse(String dataString) {
List<Map<String, DateTime>> result = [];
// Remove square brackets and curly braces
String contentString = dataString
.replaceAll('[', '')
.replaceAll(']', '')
.replaceAll('{', '')
.replaceAll('}', '');
// Split the content into individual key-value pairs
List<String> keyValuePairs = contentString.split(', ');
// Initialize variables to store the current key and value
String currentKey = '';
DateTime currentValue =
DateTime(2000); // Replace with a default date if needed
for (String pair in keyValuePairs) {
// Split each key-value pair into key and value
List<String> parts = pair.split(':');
// Extract key and value (removing any leading or trailing whitespaces)
String key = parts[0].trim();
DateTime value = DateTime.parse(parts[1].trim());
// Check if the current key is empty
// If empty, set the current key and value
// If not empty, create a map with the current key and value, and add it to the result list
if (currentKey.isEmpty) {
currentKey = key;
currentValue = value;
} else {
result.add({currentKey: currentValue, key: value});
currentKey = '';
}
}
return result;
}
List<PickerDateRange> convertToPickerDateRanges(
List<Map<String, DateTime>> dateMapList) {
List<PickerDateRange> pickerDateRanges = [];
for (Map<String, DateTime> dateMap in dateMapList) {
DateTime start = dateMap['start']!;
DateTime end = dateMap['end']!;
pickerDateRanges.add(PickerDateRange(start, end));
}
return pickerDateRanges;
}
//-----+++++------
//New logic --------+++++++------
List<Map<String, DateTime>> myList = customParse(pickerdateRangeString);
List<PickerDateRange> pickerDateRanges = convertToPickerDateRanges(myList);
print("converted range is $pickerDateRanges");
pickerdateRange.value = pickerDateRanges;
helperController.pickerdateRange.value = pickerDateRanges;
//--------+++++++++++++----------
// Parse predictedDates
String predictedDatesString = response.data['result']['predictedDates'];
predictedDatesString =
predictedDatesString.replaceAll(RegExp(r'[\[\]"]'), '');
List<String> dateStrings = predictedDatesString.split(', ');
await helperController.storePredictedDateFromApi(dateStrings);
await helperController
.storePeriodCycles(response.data['result']['period_cycle_length']);
DateTime todayMidnight =
DateTime.now().toLocal(); // Get the current date and time
todayMidnight = DateTime(todayMidnight.year, todayMidnight.month,
todayMidnight.day); // Set time to midnight
await helperController.calculateCurrentPositionInCycle(todayMidnight);
await helperController.calculateOutOfDays();
//--------+++++++++++++----------
// Parse ovulation dates
// Get the string representation of predictedDates from the response
String ovulatingDatesString = response.data['result']['ovulatingDates'];
// Remove square brackets and quotes
ovulatingDatesString =
ovulatingDatesString.replaceAll(RegExp(r'[\[\]"]'), '');
// Split the date string into a List of strings
List<String> dateStringsO = ovulatingDatesString.split(', ');
await helperController.storeOvulatingDateFromApi(dateStringsO);
//--------+++++++++++++----------
await helperController.storeFirstPeriodDate();
helperController.getLast5PeriodDates();
return response;
}
}