void HandleTimeClicked(BoxPreviewVM pvm, FrameworkElement eventSource)
 {
     LastFocusedBoxId             = pvm.Persistent.Box.RowId;
     UIGlobals.Deferred.OnOpenBox = new DeferredBehaviors.OpenBoxBehavior {
         MakeUndone = true
     };
     if (VM.DoneMode)
     {
         UIGlobals.Do.ShowTimedMessge("Completed task was opened, so it is no longer marked as done!");
     }
     HandleCommand(Globals.Commands.OPEN);
 }
        void DragStartRequested(BoxPreviewVM pvm, FrameworkElement eventSource)
        {
            if (DraggableBoxVM == null)
            {
                return;
            }
            var dragData = new DataObject(nameof(BoxDragInfo), new BoxDragInfo {
                Box = DraggableBoxVM.Persistent.Box
            });

            DragDrop.DoDragDrop(eventSource, dragData, DragDropEffects.Move);
        }
 static void SetDropTarget(BoxPreviewVM vm)
 {
     if (LastDropTarget != null)
     {
         LastDropTarget.DragTargetColor = UIGlobals.TRANSPARENT_BRUSH;
         LastDropTarget = null;
     }
     if (vm != null)
     {
         vm.DragTargetColor = UIGlobals.DRAG_TARGET_BRUSH;
         LastDropTarget     = vm;
     }
 }
 void ItemGotFocus(BoxPreviewVM itemVM)
 {
     LastFocusedBoxId = itemVM.Persistent.Box.RowId;
 }
        public override void Refresh(BoxEditingPool.Item changes)
        {
            //no refresh needed if we know the reason for calling is a non-scheduled box change
            //(but we still might need to refresh if there was no change)
            if (changes != null && !changes.IsAgendaChanged)
            {
                return;
            }

            SaveChunks();

            string fourHours           = DateUtil.ToYMDHM(DateTime.Now.AddHours(4));
            bool   anyChunkAssignments = false;

            //collect items from cache
            var agenda      = Globals.BoxCache.GetAgenda();
            var agendaItems = agenda.Where(r => r.Time.StartsWith(Date)).ToList();

            if (IsToday)
            {
                string midnight = Date + "0000";
                var    oldStuff = agenda.Where(r => DateUtil.IsBefore(r.Time, midnight));
                agendaItems.AddRange(oldStuff);
            }

            //disregard low-clutter items if its time is more than 4 hrs in the future
            agendaItems = agendaItems.Where(a =>
            {
                bool shouldHide = a.Box.Visibility == Constants.VISIBILITY_LOWCLUTTER &&
                                  DateUtil.IsBefore(fourHours, a.Box.BoxTime);
                return(!shouldHide);
            })
                          .OrderBy(a => a.Box.BoxTime)
                          .ToList();

            //get list of chunks established for the day
            var chunkList = Globals.DayChunks.Days.FirstOrDefault(c => c.Date == Date);

            //exit now if no changes
            int  hash       = HashContent(agendaItems, chunkList);
            bool wasChanged = hash != priorContentHash || chunkList == null;

            priorContentHash = hash;
            if (!wasChanged)
            {
                return;
            }

            VM.Chunks.Clear();

            if (chunkList != null && chunkList.Chunks.Count > 0)
            {
                //init VM's chunks
                foreach (var c in chunkList.Chunks)
                {
                    VM.Chunks.Add(new TodayVM.ChunkVM(VM, UserRemovedChunk)
                    {
                        Title = c.Title
                    });
                }

                //init VM's boxes within chunks
                foreach (var a in agendaItems)
                {
                    //chunk assignment can come from persistence, from DeferredBehavior, from changes argument, or default to first chunk
                    var assignedChunk = chunkList.Chunks.FirstOrDefault(c => c.BoxIds != null && c.BoxIds.Contains(a.Box.RowId));
                    if (assignedChunk == null)
                    {
                        var ta = UIGlobals.Deferred.GetAndRemoveTaskAssign(a.Box.RowId);
                        if (ta != null)
                        {
                            assignedChunk       = chunkList.Chunks.FirstOrDefault(c => c.Title == ta.ChunkTitle);
                            anyChunkAssignments = true;
                        }
                        else if (changes != null && changes.NewBoxId == a.Box.RowId && changes.OldBoxTime == null)
                        {
                            //newly added task should go to the 2nd or 3rd chunk based on time of day
                            int cidx = MultiDayChunkSet.GetDefaultChunkIndex(a.Time);
                            if (cidx == 2 && chunkList.Chunks.Count > 2)
                            {
                                assignedChunk = chunkList.Chunks[2];
                            }
                            if (cidx == 1 && chunkList.Chunks.Count > 1)
                            {
                                assignedChunk = chunkList.Chunks[1];
                            }
                            anyChunkAssignments = true;
                        }
                    }
                    if (assignedChunk == null)
                    {
                        assignedChunk = chunkList.Chunks[0];
                    }
                    int chindex = chunkList.Chunks.IndexOf(assignedChunk);

                    var preview = new BoxPreviewVM(a, Date, ItemGotFocus)
                    {
                        TimeClicked           = HandleTimeClicked,
                        DragStartRequested    = DragStartRequested,
                        MouseOpenRequested    = MouseOpenRequested,
                        DropUnderBoxRequested = DropUnderBox
                    };
                    VM.Chunks[chindex].Items.Add(preview);
                }
            }
            if (anyChunkAssignments)
            {
                SaveChunks(force: true);
            }
        }
 void ItemGotFocus(BoxPreviewVM itemVM)
 {
     LastFocusedChunk = null;
     LastFocusedBoxId = itemVM.Persistent.Box.RowId;
     DraggableBoxVM   = itemVM;
 }
 void MouseOpenRequested(BoxPreviewVM pvm)
 {
     HandleCommand(Globals.Commands.OPEN);
 }
        //handler for result of drag onto another boxpreview
        void DropUnderBox(BoxDragInfo di, BoxPreviewVM target)
        {
            //allowed?
            if (di.Box.TimeType >= Constants.TIMETYPE_MINUTE)
            {
                UIGlobals.Do.ShowTimedMessge("Cannot drag tasks scheduled for an exact time; instead, open the task and change the time");
                return;
            }
            if (!di.Box.BoxTime.StartsWith(Date))
            {
                if (!VM.ContainsBoxId(di.Box.RowId))
                {
                    UIGlobals.Do.ShowTimedMessge("Cannot drag task to a different day");
                    return;
                }
            }

            //get target chunk and position of this target previewbox in that chunk
            TodayVM.ChunkVM targetChunk   = null;
            int             indexInTarget = -1;

            foreach (var ch in VM.Chunks)
            {
                for (int i = 0; i < ch.Items.Count; ++i)
                {
                    if (ReferenceEquals(ch.Items[i].Persistent.Box, target.Persistent.Box))
                    {
                        targetChunk   = ch;
                        indexInTarget = i;
                        goto escape1;
                    }
                }
            }
escape1:
            if (targetChunk == null) //should not happen
            {
                UIGlobals.Do.ShowTimedMessge("Cannot drag task: unexpected error");
                return;
            }

            //what time is represented by the drop location?
            var    timesInChunk = targetChunk.Items.Select(bp => bp.Persistent.Time).OrderBy(s => s).ToArray();
            string time1        = target.Persistent.Box.BoxTime;
            string time2        = Date + "2359";

            if (indexInTarget < targetChunk.Items.Count - 1)
            {
                time2 = targetChunk.Items[indexInTarget + 1].Persistent.Box.BoxTime;
            }
            string halfTime = DateUtil.HalfWayBetween(time1, time2);

            //change time and save box
            try
            {
                var ebox = Globals.UI.LoadBoxForEditing(di.Box.RowId);
                ebox.Box.BoxTime = halfTime;
                Globals.UI.SaveBox(ebox, false);
            }
            catch (Exception ex)
            {
                UIGlobals.Do.ShowTimedMessge("Could not move: " + ex.Message);
                return;
            }

            //inherit chunk from target and rebuild views
            MoveBoxToChunk(di.Box, targetChunk, true);
        }
 void HandleTimeClicked(BoxPreviewVM pvm, FrameworkElement eventSource)
 {
     LastFocusedChunk = null;
     LastFocusedBoxId = pvm.Persistent.Box.RowId;
     DraggableBoxVM   = pvm;
 }