updateConfiguration method

void updateConfiguration({
  1. required int? cycleLength,
  2. required int? periodDuration,
  3. String? customerId = "0",
  4. DateTime? lastPeriodDate,
  5. bool isClearData = false,
  6. String fontFamily = "",
  7. DateFormats dateFormat = DateFormats.dmy,
  8. Languages defaultLanguage = Languages.english,
})

Update configuration of MenstrualCycleWidget

Implementation

void updateConfiguration(
    {required int? cycleLength,
    required int? periodDuration,
    String? customerId = "0",
    DateTime? lastPeriodDate,
    bool isClearData = false,
    String fontFamily = "",
    DateFormats dateFormat = DateFormats.dmy,
    Languages defaultLanguage = Languages.english}) async {
  assert(cycleLength! > 0, WidgetBaseLanguage.totalCycleDaysLabel);
  assert(periodDuration! > 0, WidgetBaseLanguage.totalPeriodDaysLabel);
  // printLogs("userId $userId");
  if (customerId!.isNotEmpty) {
    _customerId = customerId;
  }

  initializeDateFormatting();

  if (fontFamily.isNotEmpty) {
    defaultFontFamily = fontFamily;
  }

  currentLanguage = defaultLanguage;
  _currentDateFormat = dateFormat;
  _cycleLength = cycleLength!;
  _periodDuration = periodDuration!;
  final dbHelper = MenstrualCycleDbHelper.instance;

  // Clear Past log data
  if (isClearData) {
    String customerId = getCustomerId();
    await dbHelper.clearPeriodLog(customerId);
  }
  // insert current user details
  await dbHelper.insertCurrentUserDetails(
      customerId: Encryption.instance.encrypt(_customerId),
      cycleLength: _cycleLength,
      periodDuration: _periodDuration);

  // Generate periods days based on last selected period date
  if (lastPeriodDate != null) {
    bool isFoundDate = await dbHelper.isPeriodDateFound(lastPeriodDate);
    if (!isFoundDate) {
      List<DateTime> selectedPeriodsDate = [];
      DateTime lastPeriodDateTime = lastPeriodDate;
      selectedPeriodsDate.add(lastPeriodDateTime);
      for (int i = 1; i < periodDuration; i++) {
        lastPeriodDateTime = lastPeriodDateTime.add(const Duration(days: 1));
        selectedPeriodsDate.add(lastPeriodDateTime);
      }
      await dbHelper.insertPeriodLog(selectedPeriodsDate);
    }
  }
  calculateLastPeriodDate();
}