Exemple #1
0
 public void ToggleFlags1_ReturnsExpected_WhenUsingInvalidOtherFlags()
 {
     Assert.AreEqual((((ColorFlagEnum)16) | ColorFlagEnum.Blue), FlagEnums.ToggleFlags(ColorFlagEnum.Blue, (ColorFlagEnum)16));
 }
Exemple #2
0
 public void ToggleFlags_ReturnsExpected_WhenUsingInvalidValue()
 {
     Assert.AreEqual(ColorFlagEnum.All | ((ColorFlagEnum)16), FlagEnums.ToggleFlags((ColorFlagEnum)16));
     Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue | ((ColorFlagEnum)16), FlagEnums.ToggleFlags(((ColorFlagEnum)16) | ColorFlagEnum.Red | ColorFlagEnum.UltraViolet));
 }
Exemple #3
0
 public void ToggleFlags1()
 {
     Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, FlagEnums.ToggleFlags(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, ColorFlagEnum.Green));
     Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, FlagEnums.ToggleFlags(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, ColorFlagEnum.Red | ColorFlagEnum.UltraViolet));
 }
        public async Task <(string, bool, string)> Handle(CallbackQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            var callback = data.Data.Split(':');

            if (callback[0] != "set")
            {
                return(null, false, null);
            }

            if (!await myChatInfo.CandEditPoll(data.Message.Chat, data.From?.Id, cancellationToken))
            {
                return("You can't edit the poll.", true, null);
            }

            var chatId      = data.Message.Chat.Id;
            var settingsSet = myContext.Set <Settings>();

            var allSettings = await settingsSet.GetSettings(chatId).AsTracking().ToListAsync(cancellationToken);

            Settings settings = null;

            switch (callback.ElementAtOrDefault(1))
            {
            case "list":
                return(await Return(await SettingsList(chatId, cancellationToken)));

            case var identifier when int.TryParse(identifier, out var id):
                settings = allSettings.FirstOrDefault(setting => setting.Id == id);

                break;
            }

            if (settings == null)
            {
                // create a new
                settings = new Settings {
                    Chat = chatId, Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(-1).Max() + 1, Format = VoteEnum.Standard
                };
                await settingsSet.AddAsync(settings, cancellationToken);
            }

            string message = null;

            switch (callback.ElementAtOrDefault(2) ?? "")
            {
            case "default":
                settings.Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(1).Min() - 1;
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Default poll format is changed."));

            case "delete":
                settingsSet.Remove(settings);
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Poll format is deleted."));

            case var format when FlagEnums.TryParseFlags(format, out VoteEnum toggleVotes, EnumFormat.DecimalValue):
                settings.Format = FlagEnums.ToggleFlags(settings.Format, toggleVotes);

                // adjust ⁺¹
                if (settings.Format.HasAnyFlags(VoteEnum.Plus) && !settings.Format.HasAnyFlags(VoteEnum.Countable))
                {
                    settings.Format = settings.Format.RemoveFlags(VoteEnum.Plus);
                }

                if (await myContext.SaveChangesAsync(cancellationToken) > 0)
                {
                    message = "Poll format is changed.";
                }
                goto default;

            default:
                var buttons = new []
                {
                    VoteEnum.Team,
                    VoteEnum.TeamHarmony,
                    VoteEnum.Plus1,
                    VoteEnum.Remotely,
                    VoteEnum.Invitation,
                    VoteEnum.MayBe,
                    VoteEnum.Yes,
                    VoteEnum.Thumbs,
                    VoteEnum.HarryPotter,
                    VoteEnum.Cancel,
                    VoteEnum.Share
                }
                .Select(format => new []
                {
                    InlineKeyboardButton.WithCallbackData($"{(settings.Format.HasAllFlags(format) ? '☑' : '☐')} {format.Format(new StringBuilder())}", $"{ID}:{settings.Id}:{format:D}")
                });

                if (allSettings.FirstOrDefault() is Settings existingDefault && existingDefault != settings)
                {
                    buttons = buttons.Append(new[]
                    {
                        InlineKeyboardButton.WithCallbackData("Make as a default", $"{ID}:{settings.Id}:default")
                    });
                }

                buttons = buttons
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Delete", $"{ID}:{settings.Id}:delete") })
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Back", $"{ID}:list") });

                return(await Return((
                                        settings.Format.Format(new StringBuilder("Selected poll format:").AppendLine()).ToTextMessageContent(),
                                        new InlineKeyboardMarkup(buttons)), message));
            }

            async Task <(string, bool, string)> Return((InputTextMessageContent content, InlineKeyboardMarkup replyMarkup) pass, string notification = "")
            {
                await myTelegramBotClient.EditMessageTextAsync(data.Message.Chat, data.Message.MessageId,
                                                               pass.content.MessageText, pass.content.ParseMode, pass.content.DisableWebPagePreview, pass.replyMarkup, cancellationToken);

                return(notification, false, null);
            }
        }