fetchICSFile static method

Future<Map<String, Map<String, dynamic>>> fetchICSFile(
  1. String? url,
  2. String timeZone,
  3. dynamic onError(
    1. CometChatException
    )
)

Implementation

static Future<Map<String, Map<String, dynamic>>> fetchICSFile(
  String? url,
  String timeZone,
  Function(CometChatException) onError,
) async {
  Map<String, Map<String, dynamic>> blockedDates = {};

  if (url != null && url.isNotEmpty) {
    try {
      final response = await http.get(Uri.parse(url));

      if (response.statusCode == 200) {
        final icsContent = response.body;
        final events = parseICS(icsContent, timeZone);
        blockedDates = events;
      } else {
        onError(
          CometChatException(
            "ERR",
            "FAILED_TO_LOAD_ICS_FILE",
            "Failed to load ICS file: ${response.statusCode}",
          ),
        );
        if (kDebugMode) {
          print('Failed to load ICS file: ${response.statusCode}');
        }
      }
    } catch (e) {
      onError(
        CometChatException(
          "ERR",
          "UNABLE_TO_PARSE_ICS_FILE",
          "Unable to parse ICS file: $e",
        ),
      );
      if (kDebugMode) {
        print('Error: caught while parsing icsFile $e');
      }
    }
  } else {
    onError(
      CometChatException(
        "ERR",
        "INVALID_API_ENDPOINT",
        "the api endpoint is either null of empty",
      ),
    );
  }
  return blockedDates;
}