editEvent method

Future editEvent()

edits the event passed to init() function

Implementation

Future<dynamic> editEvent() async {
  try {
    var key = eventNotificationModel!.key!;
    var keyKeyword = key.split('-')[0];
    var atkeyMicrosecondId = key.split('-')[1].split('@')[0];
    var response = await atClientManager.atClient.getKeys(
      regex: '$keyKeyword-$atkeyMicrosecondId',
    );
    if (response.isEmpty) {
      return 'notification key not found';
    }

    var atKey = getAtKey(response[0]);
    var allAtsignList = <String?>[];
    for (var element
        in EventService().eventNotificationModel!.group!.members!) {
      allAtsignList.add(element.atSign);
    }

    var eventData = EventNotificationModel.convertEventNotificationToJson(
        EventService().eventNotificationModel!);
    var result = await atClientManager.atClient.put(
      atKey,
      eventData,
    );

    /// throwing an error (doesnt start with @),
    /// might lead to functional bug (so, leaving it here)
    // atKey.sharedWith = jsonEncode(allAtsignList);

    await notifyAll(
      atKey,
      eventData,
      allAtsignList,
    );

    EventKeyStreamService()
        .mapUpdatedEventDataToWidget(eventNotificationModel!);

    /// Dont need to sync here as notifyAll is called
    if (onEventSaved != null) {
      onEventSaved!(eventNotificationModel);
    }
    return result;
  } catch (e) {
    return e;
  }
}