Пример #1
0
        public override InteractionResult Handle(InteractionContext context)
        {
            Identity id;
            var      source = context.Request.Data;

            if (string.IsNullOrEmpty(source))
            {
                id = context.Request.CurrentStoryId;
                if (id.IsEmpty)
                {
                    return(Error("Id was not specified and there is no focused story"));
                }
            }
            else if (!context.Request.TryGetId(source, out id))
            {
                return(Error("Could not find story ID '{0}'", source));
            }
            var store = context.Storage;

            if (id is StoryId)
            {
                var result = store.GetEntity <StoryView>(id);
                if (!result.HasValue)
                {
                    return(Error("Story id not found '{0}'", id));
                }
                var story      = result.Value;
                var activities = store.GetEntity <ActivityList>(id).GetValue(new ActivityList());
                var tasks      = store.GetEntity <TaskList>(id).GetValue(new TaskList());
                var notes      = store.GetEntity <NoteList>(id).GetValue(new NoteList());
                var composite  = new FocusComposite(story.StoryId, story.Name, activities.List, tasks.List, notes.Notes);
                context.Response.RenderView(composite);
                context.Response.FocusStory(story.StoryId, story.Name);
                return(Handled());
            }
            if (id is TagId)
            {
                var result = store.GetEntity <TagView>(id);
                if (!result.HasValue)
                {
                    return(Error("Tag not found '{0}'", id));
                }
                var view    = result.Value;
                var stories = view.Stories
                              .Select(s => store.GetEntity <StoryView>(s.Story))
                              .Where(o => o.HasValue)
                              .Select(o => o.Value)
                              .ToList();
                var activities = stories.SelectMany(s => store.GetEntity <ActivityList>(s.StoryId).Convert(al => al.List, new List <ActivityList.Item>())).ToList();
                var tasks      = stories.SelectMany(s => store.GetEntity <TaskList>(s.StoryId).Convert(al => al.List, new List <TaskList.Item>())).ToList();
                var notes      = stories.SelectMany(s => store.GetEntity <NoteList>(s.StoryId).Convert(al => al.Notes, new List <NoteList.Item>())).ToList();
                var composite  = new FocusComposite(id, view.Name, activities, tasks, notes);
                context.Response.RenderView(composite);
                return(Handled());
            }
            return(Error("Can't focus"));
        }
Пример #2
0
        public void When(FocusComposite composite)
        {
            //var view = composite.View;

            string txt;

            if (composite.Focus is StoryId)
            {
                txt = string.Format("Story: {0} .{1}", composite.Name, AddReference(composite.Focus, composite.Name));
            }
            else if (composite.Focus is TagId)
            {
                txt = string.Format("Tag: {0} .{1}", composite.Name, AddReference(composite.Focus, composite.Name));
            }
            else
            {
                throw new InvalidOperationException("Unexpected thing to focus on");
            }



            using (_rich.Styled(Solarized.Yellow))
            {
                _rich.AppendLine(txt);
            }
            _rich.AppendLine(new string('=', txt.Length));

            var tasks = composite.Tasks.Where(c => !c.Completed).ToList();

            if (tasks.Count > 0)
            {
                using (_rich.Styled(Solarized.Green))
                {
                    _rich.AppendLine("## Tasks");
                }

                foreach (var task in tasks)
                {
                    _rich.AppendLine(string.Format("  {1} {2} .{0}", AddReference(task.TaskId), task.Completed ? "x" : "□",
                                                   task.Text));
                }
                _rich.AppendLine();
            }

            var activities = composite.Activities;

            if (activities.Count > 0)
            {
                foreach (var activity in activities)
                {
                    var text         = activity.Text;
                    var explicitRefs = activity.References.Where(r => !string.IsNullOrEmpty(r.Source));

                    foreach (var r in explicitRefs)
                    {
                        var rid    = AddReference(r.Item);
                        var newRef = string.Format("[{0}].{1}", r.Title, rid);
                        text = text.Replace(r.Source, newRef);
                    }
                    _rich.AppendLine(text);
                    var implicitRefs = activity.References.Where(r => string.IsNullOrEmpty(r.Source)).ToList();
                    using (_rich.Styled(Solarized.Base1))
                    {
                        _rich.AppendText("  -- " + FormatEvil.OffsetUtc(activity.Created));
                        if (implicitRefs.Any())
                        {
                            var values = implicitRefs
                                         .Select(r => string.Format("[{0}].{1}", r.Title, AddReference(r.Item))).ToArray();
                            _rich.AppendText(" from " + string.Join(", ", values));
                        }
                        _rich.AppendLine();
                    }
                }
                _rich.AppendLine();
            }

            var notes = composite.Notes;

            if (notes.Count > 0)
            {
                _rich.AppendLine();
                using (_rich.Styled(Solarized.Green))
                {
                    _rich.AppendLine("## Notes");
                }

                foreach (var note in notes.OrderBy(s => s.Title))
                {
                    _rich.AppendLine("{0} .{1}", note.Title, AddReference(note.NoteId));
                }
            }
            if (notes.Count == 0 && tasks.Count == 0 && activities.Count == 0)
            {
                using (_rich.Styled(Solarized.Red))
                {
                    _rich.AppendLine("  Story is empty");
                }
            }
        }