saveDraft function

Future<void> saveDraft(
  1. String text,
  2. String roomId,
  3. dynamic ref
)

Implementation

Future<void> saveDraft(
  String text,
  String roomId,
  WidgetRef ref,
) async {
  // get the convo object to initiate draft
  final chat = await ref.read(chatProvider(roomId).future);
  final messageId = ref.read(chatInputProvider).selectedMessage?.id;
  final mentions = ref.read(chatInputProvider).mentions;
  final userMentions = [];
  String? htmlText = text;
  if (mentions.isNotEmpty) {
    mentions.forEach((key, value) {
      userMentions.add(value);
      htmlText = htmlText?.replaceAll(
        '@$key',
        '[@$key](https://matrix.to/#/$value)',
      );
    });
  }

  if (chat != null) {
    if (messageId != null) {
      final selectedMessageState =
          ref.read(chatInputProvider).selectedMessageState;
      if (selectedMessageState == SelectedMessageState.edit) {
        await chat.saveMsgDraft(text, htmlText, 'edit', messageId);
      } else if (selectedMessageState == SelectedMessageState.replyTo) {
        await chat.saveMsgDraft(text, htmlText, 'reply', messageId);
      }
    } else {
      await chat.saveMsgDraft(text, htmlText, 'new', null);
    }
  }
}