/// <summary>
        /// Called by the consumer when a post vote should be changed
        /// </summary>
        /// <param name="post">The post to be actioned</param>
        /// <param name="postPosition">A hint to the position of the post</param>
        public void ChangePostVote(Post post, PostVoteAction action, int postPosition = 0)
        {
            // Ensure we are signed in.
            if (!m_baconMan.UserMan.IsUserSignedIn)
            {
                m_baconMan.MessageMan.ShowSigninMessage("vote");
                return;
            }

            // Using the post and suggested index, find the real post and index
            Post collectionPost = post;

            FindPostInCurrentCollection(ref collectionPost, ref postPosition);

            if (collectionPost == null || postPosition == -1)
            {
                // We didn't find it.
                return;
            }

            // Update the like status
            bool likesAction    = action == PostVoteAction.UpVote;
            int  voteMultiplier = action == PostVoteAction.UpVote ? 1 : -1;

            if (collectionPost.Likes.HasValue)
            {
                if (collectionPost.Likes.Value == likesAction)
                {
                    // duplicate vote would undo the action
                    collectionPost.Likes  = null;
                    collectionPost.Score -= voteMultiplier;
                }
                else
                {
                    // opposite vote, takes into account previous vote
                    collectionPost.Likes  = likesAction;
                    collectionPost.Score += 2 * voteMultiplier;
                }
            }
            else
            {
                // first vote
                collectionPost.Likes  = likesAction;
                collectionPost.Score += voteMultiplier;
            }

            // Fire off that a update happened.
            FireCollectionUpdated(postPosition, new List <Post>()
            {
                collectionPost
            }, false, false);

            // Start a task to make the vote
            Task.Run(async() =>
            {
                try
                {
                    // Build the data
                    string voteDir = collectionPost.Likes.HasValue ? collectionPost.Likes.Value ? "1" : "-1" : "0";
                    List <KeyValuePair <string, string> > postData = new List <KeyValuePair <string, string> >();
                    postData.Add(new KeyValuePair <string, string>("id", "t3_" + collectionPost.Id));
                    postData.Add(new KeyValuePair <string, string>("dir", voteDir));

                    // Make the call
                    string str = await m_baconMan.NetworkMan.MakeRedditPostRequestAsString("api/vote", postData);

                    // Do some super simple validation
                    if (str != "{}")
                    {
                        throw new Exception("Failed to set vote! The response indicated a failure");
                    }
                }
                catch (Exception ex)
                {
                    m_baconMan.MessageMan.DebugDia("failed to vote!", ex);
                    m_baconMan.MessageMan.ShowMessageSimple("That's Not Right", "Something went wrong while trying to cast your vote, try again later.");
                }
            });
        }
Exemple #2
0
        /// <summary>
        /// Called by the consumer when a comment vote should be changed
        /// </summary>
        /// <param name="comment">The post to be actioned</param>
        /// <param name="postPosition">A hint to the position of the post</param>
        public void ChangeCommentVote(Comment comment, PostVoteAction action, int postPosition = 0)
        {
            // Ensure we are signed in.
            if (!m_baconMan.UserMan.IsUserSignedIn)
            {
                m_baconMan.MessageMan.ShowSigninMessage("vote");
                return;
            }

            // Using the post and suggested index, find the real post and index
            Comment collectionComment = comment;

            FindCommentInCurrentCollection(ref collectionComment, ref postPosition);

            if (collectionComment == null || postPosition == -1)
            {
                // We didn't find it.
                return;
            }

            // Update the like status
            if (action == PostVoteAction.UpVote)
            {
                if (collectionComment.Likes.HasValue && collectionComment.Likes.Value)
                {
                    collectionComment.Likes = null;
                    collectionComment.Score--;
                }
                else
                {
                    collectionComment.Likes = true;
                    collectionComment.Score++;
                }
            }
            else
            {
                if (collectionComment.Likes.HasValue && !collectionComment.Likes.Value)
                {
                    collectionComment.Likes = null;
                    collectionComment.Score++;
                }
                else
                {
                    collectionComment.Likes = false;
                    collectionComment.Score--;
                }
            }

            // Fire off that a update happened.
            FireCollectionUpdated(postPosition, new List <Comment>()
            {
                collectionComment
            }, false, false);

            // Start a task to make the vote
            new Task(async() =>
            {
                try
                {
                    // Build the data
                    string voteDir = collectionComment.Likes.HasValue ? collectionComment.Likes.Value ? "1" : "-1" : "0";
                    List <KeyValuePair <string, string> > postData = new List <KeyValuePair <string, string> >();
                    postData.Add(new KeyValuePair <string, string>("id", "t1_" + collectionComment.Id));
                    postData.Add(new KeyValuePair <string, string>("dir", voteDir));

                    // Make the call
                    string str = await m_baconMan.NetworkMan.MakeRedditPostRequest("api/vote", postData);

                    // Do some super simple validation
                    if (str != "{}")
                    {
                        throw new Exception("Failed to set vote! The response indicated a failure");
                    }
                }
                catch (Exception ex)
                {
                    m_baconMan.MessageMan.DebugDia("failed to vote!", ex);
                    m_baconMan.MessageMan.ShowMessageSimple("That's Not Right", "Something went wrong while trying to cast your vote, try again later.");
                }
            }).Start();
        }