updateConfiguration method

void updateConfiguration({
  1. required int? cycleLength,
  2. required int? periodDuration,
  3. String? userId = "0",
  4. DateTime? lastPeriodDate,
})

Update configuration of MenstrualCycleWidget

Implementation

void updateConfiguration(
    {required int? cycleLength,
    required int? periodDuration,
    String? userId = "0",
    DateTime? lastPeriodDate}) async {
  assert(_cycleLength > 0, Strings.totalCycleDaysLabel);
  assert(_periodDuration > 0, Strings.totalPeriodDaysLabel);
  // printLogs("userId $userId");
  if (userId!.isNotEmpty) {
    _customerId = userId;
  }
  _cycleLength = cycleLength!;
  _periodDuration = periodDuration!;
  final dbHelper = MenstrualCycleDbHelper.instance;

  // insert current user details
  await dbHelper.insertCurrentUserDetails(
      customerId: _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();
}