onSave method

Future<void> onSave(
  1. dynamic context
)

Implementation

Future<void> onSave(BuildContext context) async {
  EasyLoading.show(status: 'Saving Pin');
  try {
    final updateBuilder = pin.updateBuilder();
    bool hasChanges = false;

    if (pin.title() != state.title) {
      updateBuilder.title(state.title);
      hasChanges = true;
    }
    if (pin.isLink()) {
      if (pin.url() != state.link) {
        updateBuilder.url(state.link);
        hasChanges = true;
      }
    }
    final content = pin.content();
    if (content != null) {
      if (content.body() != state.markdown) {
        updateBuilder.contentMarkdown(state.markdown);
        hasChanges = true;
      }

      if (state.html != null) {
        if (content.formattedBody() != state.html) {
          updateBuilder.contentHtml(state.markdown, state.html!);
          hasChanges = true;
        }
      }
    }

    if (hasChanges) {
      await updateBuilder.send();
    }
    await pin.refresh();
    if (!mounted) {
      EasyLoading.dismiss();
      return;
    }
    EasyLoading.showToast('Pin Updated Successfully');
  } catch (e, s) {
    _log.severe('Failed to change pin', e, s);
    if (!context.mounted) {
      EasyLoading.dismiss();
      return;
    }
    EasyLoading.showError(
      L10n.of(context).failedToChangePin(e),
      duration: const Duration(seconds: 3),
    );
  }
}