getSymptomsPattern method

Future<List<SymptomsCount>> getSymptomsPattern({
  1. bool isForTomorrow = false,
})

Get symptoms patter for today or tomorrow

Implementation

Future<List<SymptomsCount>> getSymptomsPattern(
    {bool isForTomorrow = false}) async {
  int currentCycleDay = 0;
  List<SymptomsCount> symptomAnalysisData = [];
  // Find current cycle day
  final dbHelper = MenstrualCycleDbHelper.instance;
  String lastPeriodDate = await dbHelper.getLastPeriodDate();

  if (lastPeriodDate.isNotEmpty) {
    final difference =
        DateTime.now().difference(DateTime.parse(lastPeriodDate)).inDays;
    currentCycleDay = difference + 1;
    // Check past cycle length
    // if not same as current then calculate current cycle day based on past cycle
    int pastCycleLength = await getPreviousCycleLength();

    if (pastCycleLength > 0) {
      if (pastCycleLength != getCycleLength()) {
        currentCycleDay =
            ((currentCycleDay / getCycleLength()) * pastCycleLength).round();
      }
    }

    if (isForTomorrow) {
      // Check if period start from tomorrow then set currentCycleDay is 1
      if (currentCycleDay >= getCycleLength()) {
        currentCycleDay = 1;
      } else {
        currentCycleDay = currentCycleDay + 1;
      }
    }
  }

  if (currentCycleDay > 0) {
    final dbHelper = MenstrualCycleDbHelper.instance;
    String customerId = getCustomerId();
    Database? db = await dbHelper.database;
    final List<Map<String, dynamic>> queryResponse;
    queryResponse = await db!.rawQuery(
        "Select * from ${MenstrualCycleDbHelper.tableDailyUserSymptomsLogsData} WHERE ${MenstrualCycleDbHelper.columnCustomerId}='$customerId' AND ${MenstrualCycleDbHelper.columnCycleDay}=$currentCycleDay AND ${MenstrualCycleDbHelper.columnLogDate} < '$lastPeriodDate'");

    List<SymptomsData> pastSymptomsData = [];
    final encryption = Encryption.instance;

    // printMenstrualCycleLogs("Count: ${queryResponse.length}");
    List.generate(queryResponse.length, (i) {
      String symptomsData = encryption.decrypt(
          queryResponse[i][MenstrualCycleDbHelper.columnUserEncryptData]);
      List<dynamic> jsonData = json.decode(symptomsData.trim());
      pastSymptomsData.addAll(
          jsonData.map((symptom) => SymptomsData.fromMap(symptom)).toList());
    });

    for (var symptomsData in pastSymptomsData) {
      String symptomName = symptomsData.symptomName!;
      bool containsSymptom = symptomAnalysisData.any((symptom) =>
          symptom.name!.toLowerCase() == symptomName.toLowerCase());
      if (containsSymptom) {
        int index = symptomAnalysisData.indexWhere((symptom) =>
            symptom.name!.toLowerCase() == symptomName.toLowerCase());
        SymptomsCount oldSymptomsData = symptomAnalysisData[index];
        oldSymptomsData.occurrences = oldSymptomsData.occurrences! + 1;
        oldSymptomsData.accuracy =
            ((oldSymptomsData.occurrences! * 100) / queryResponse.length)
                .toStringAsFixed(2);
        symptomAnalysisData.removeAt(index);
        symptomAnalysisData.add(oldSymptomsData);
      } else {
        symptomAnalysisData
            .add(SymptomsCount(name: symptomName, occurrences: 1));
      }
    }
  }
  // only those data which come more then 2  times
  List<SymptomsCount> newSymptomAnalysisData = [];
  for (var symptomsData in symptomAnalysisData) {
    if (symptomsData.occurrences! > 2) {
      newSymptomAnalysisData.add(symptomsData);
    }
  }
  symptomAnalysisData.clear();
  symptomAnalysisData.addAll(newSymptomAnalysisData);
  return symptomAnalysisData;
}