예제 #1
0
        /// <summary>
        /// Casts a vote
        /// </summary>
        /// <param name="args"></param>
        /// <param name="api">API to use to vote</param>
        /// <param name="db">Optional DB to mark as Read</param>
        /// <returns>Throws exception on errors</returns>
        public static async Task Vote(ButtonClickedEventArgs args, IDevRantClient api, IPersistentDataStore db = null)
        {
            Vote vote = null;

            Votable votable = args.SelectedItem as Votable;

            if (votable.Voted == VoteState.Disabled)
            {
                return;
            }

            if (votable != null)
            {
                switch (args.Type)
                {
                case ButtonType.Down:
                    if (votable.Voted == VoteState.Down)
                    {
                        vote = Dtos.Vote.ClearVote();
                    }
                    else
                    {
                        var dlg = new DownvoteReasonWindow();
                        dlg.Topmost = true;
                        dlg.ShowDialog();

                        if (dlg.Reason != null)
                        {
                            vote = Dtos.Vote.DownVote(dlg.Reason.Value);
                        }
                        else
                        {
                            return;
                        }
                    }
                    break;

                case ButtonType.Up:
                    if (votable.Voted == VoteState.Up)
                    {
                        vote = Dtos.Vote.ClearVote();
                    }
                    else
                    {
                        vote = Dtos.Vote.UpVote();
                    }
                    break;
                }

                FeedItem item = args.SelectedItem as FeedItem;

                switch (args.SelectedItem.Type)
                {
                case FeedItem.FeedItemType.Post:
                    var rant = item.AsRant();

                    var r1 = await api.User.VoteRant(rant.ID, vote);

                    rant.Update(r1);
                    rant.Read = true;

                    if (db != null)
                    {
                        db.MarkRead(rant.ID);
                    }

                    break;

                case FeedItem.FeedItemType.Collab:
                    var collab = item.AsCollab();

                    var r2 = await api.User.VoteCollab(collab.ID, vote);

                    collab.Update(r2);
                    break;

                case FeedItem.FeedItemType.Comment:
                    var comment = item.AsComment();
                    var r3      = await api.User.VoteComment(comment.ID, vote);

                    comment.Update(r3);
                    break;
                }

                args.InvokeCallback();
            }
        }