private async Task SelectIthTranslation(int messageId, int index) { _areSelected[index] = !_areSelected[index]; if (_areSelected[index]) //if become selected { await _addWordService.AddTranslationToUser(Chat.User, _translations[index].GetEnRu()); } else { await _addWordService.RemoveTranslationFromUser(Chat.User, _translations[index].GetEnRu()); } var buttons = CreateButtons(); await Chat.EditMessageButtons(messageId, buttons.ToArray()); }
public async Task Handle(Update update) { var buttonData = AddWordHelper.ParseQueryDataOrNull(update.CallbackQuery.Data); if (buttonData == null) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } // last translation is cached. var cached = _cachedHandlerTranslationOrNull; if (cached != null && cached.OriginWordText.Equals(buttonData.Origin)) { // if translation is cached - fall into handler for fast handling await cached.Handle(buttonData.Translation, update); return; } // word is not cached // so we need to find already translated items var allTranslations = await _addWordService.FindInLocalDictionaryWithExamples(buttonData.Origin); var originMessageButtons = update.CallbackQuery .Message ?.ReplyMarkup ?.InlineKeyboard ?.SelectMany(i => i) .ToArray(); if (originMessageButtons == null) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } if (originMessageButtons.Length < allTranslations.Count) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } var selectionMarks = GetSelectionMarks(allTranslations, originMessageButtons); var index = AddWordHelper.FindIndexOf(allTranslations, buttonData.Translation); if (index == -1) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } var selectedBefore = selectionMarks[index]; if (!selectedBefore) { selectionMarks[index] = true; await _addWordService.AddTranslationToUser(Chat.User, allTranslations[index].GetEnRu()); await Chat.AnswerCallbackQueryWithTooltip(update.CallbackQuery.Id, Chat.Texts.MessageAfterTranslationIsSelected(allTranslations[index])); } else { selectionMarks[index] = false; await _addWordService.RemoveTranslationFromUser(Chat.User, allTranslations[index].GetEnRu()); await Chat.AnswerCallbackQueryWithTooltip(update.CallbackQuery.Id, Chat.Texts.MessageAfterTranslationIsDeselected(allTranslations[index])); } await Chat.EditMessageButtons( update.CallbackQuery.Message.MessageId, allTranslations .Select((t, i) => AddWordHelper.CreateButtonFor(t, selectionMarks[i])) .ToArray()); }