Ejemplo n.º 1
0
        public EditPostWindowViewModel(Window window, EditPostWindow.Type type, Draft existing = null, Commentable parent = null, FeedItem edit = null)
        {
            this.window = window;
            this.api    = AppManager.Instance.API;
            this.db     = AppManager.Instance.DB;

            this.type     = type;
            this.existing = existing;
            this.parent   = parent;
            this.editing  = edit;

            Cancelled = true;

            if (parent != null)
            {
                mode = Mode.NewComment;

                ViewModels.Comment comment = parent as ViewModels.Comment;
                if (comment != null)
                {
                    if (AppManager.Instance.API.User.LoggedInUser != comment.Username)
                    {
                        Text = "@" + comment.Username + " ";
                    }
                }
            }
            else if (existing != null)
            {
                mode = Mode.EditDraft;

                Text       = existing.Text;
                TagsString = existing.Tags;
                ImagePath  = existing.ImagePath;
            }
            else if (edit != null)
            {
                mode = Mode.EditExisting;

                var r       = edit.AsRant();
                var comment = edit.AsComment();
                if (r != null)
                {
                    Text       = r.Text;
                    TagsString = r.TagsString;
                }
                else if (comment != null)
                {
                    Text = comment.Text;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
        private async void Post()
        {
            try
            {
                string tmp = Utilities.ReplaceNewLines(Text);
                if (string.IsNullOrEmpty(tmp) || tmp.Length < 5)
                {
                    throw new Exception("Rant or comment must be more than 5 characters long.");
                }

                PostContent data = new PostContent(tmp);

                if (type == EditPostWindow.Type.Rant && !string.IsNullOrEmpty(TagsString))
                {
                    data.SetTag(TagsString);
                }

                if (!string.IsNullOrEmpty(ImagePath))
                {
                    byte[] bytes = File.ReadAllBytes(ImagePath);
                    data.AddImage(bytes, ImagePath);
                }

                window.IsEnabled = false;

                if (type == EditPostWindow.Type.Rant)
                {
                    if (editing != null)
                    {
                        api.User.EditRant(editing.AsRant().ID, data);
                    }
                    else
                    {
                        await api.User.PostRant(data);
                    }

                    if (existing != null)
                    {
                        db.RemoveDraft(existing.ID.Value);
                    }
                }
                else if (type == EditPostWindow.Type.Comment)
                {
                    if (editing != null)
                    {
                        api.User.EditComment(editing.AsComment().ID, data);
                    }
                    else
                    {
                        await api.User.PostComment(parent.RantId, data);
                    }
                }

                Cancelled = false;
                window.Close();
            }
            catch (Exception e)
            {
                MessageBoxFactory.ShowError(e, owner: window);
            }
            finally
            {
                window.IsEnabled = true;
            }
        }