sync method

Future<void> sync({
  1. bool force = false,
})

Implementation

Future<void> sync({bool force = false}) async {
  final countryId = _locationService.countryId;
  final regionId = _locationService.regionId;
  final coords = _locationService.coordinates;

  if (coords == null && countryId == null) {
    return;
  }

  final now = DateTime.now();
  final currentMonth = now.month;
  final nextMonthIs = currentMonth == 12 ? 1 : currentMonth + 1;

  final daysInCurrent = DateTime(now.year, currentMonth + 1, 0).day;
  final daysInNext = DateTime(now.year, nextMonthIs + 1, 0).day;

  final hasCurrentMonth = _storage.prayerTimes.containsKey(
    "${daysInCurrent}-$currentMonth",
  );
  final hasNextMonth = _storage.prayerTimes.containsKey(
    "${daysInNext}-$nextMonthIs",
  );

  if (!force && hasCurrentMonth && hasNextMonth) {
    return;
  }

  if (force || !hasCurrentMonth) {
    await _fetchOrCalculate(
      now.year,
      currentMonth,
      daysInCurrent,
      countryId,
      regionId,
      coords,
    );
  }

  if (force || !hasNextMonth) {
    await _fetchOrCalculate(
      now.year,
      nextMonthIs,
      daysInNext,
      countryId,
      regionId,
      coords,
    );
  }
}