void UserRemovedChunk(TodayVM.ChunkVM cvm) { //can't remove last one if (VM.Chunks.Count <= 1) { return; } VM.Chunks.Remove(cvm); SaveChunks(force: true); Refresh(null); }
static void SetDropTarget(TodayVM.ChunkVM vm) { if (LastDropTarget != null) { LastDropTarget.DragTargetColor = UIGlobals.TRANSPARENT_BRUSH; LastDropTarget = null; } if (vm != null) { vm.DragTargetColor = UIGlobals.DRAG_TARGET_BRUSH; LastDropTarget = vm; } }
public TodayController(bool isToday, Action <BlockController> blockGotFocusHandler, Action <BlockController, bool> collapseRequested) : base(blockGotFocusHandler, collapseRequested) { IsToday = isToday; DateTime dt = DateTime.Now; if (!isToday) { dt = dt.AddDays(1); } string dateS = DateUtil.ToYMD(dt); Date = dateS; VM = new TodayVM(isToday, dateS, VMGotFocus); //hook up VM events VM.ChunkGotFocus = idx => { LastFocusedChunk = VM.Chunks[idx]; }; VM.RequestAddChunk = () => { VM.Chunks.Add(new TodayVM.ChunkVM(VM, UserRemovedChunk) { Title = VM.NewChunkTitle, IsDirty = true }); VM.NewChunkTitle = ""; }; VM.DropOnChunkRequested = (BoxDragInfo di, TodayVM.ChunkVM chunkVM) => { MoveBoxToChunk(di.Box, chunkVM, true); }; VM.FocusBarClicked = () => { VisualUtils.DelayThen(30, () => { VM.GetMainControl?.Invoke()?.Focus(); }); }; Refresh(null); }
/// <summary> /// handler for result of a drag onto a chunk title, or onto another boxpreview (indirectly). /// </summary> void MoveBoxToChunk(CachedBox box, TodayVM.ChunkVM chunk, bool rebulidViews) { if (!VM.ContainsBoxId(box.RowId)) { UIGlobals.Do.ShowTimedMessge("Cannot drag task to a different day"); return; } //remove box from all chunks foreach (var c in VM.Chunks) { for (int i = c.Items.Count - 1; i >= 0; --i) { if (c.Items[i].Persistent.Box.RowId == box.RowId) { c.Items.RemoveAt(i); c.IsDirty = true; } } } //add to target chunk var ae = new AgendaEntry { Box = box }; //this is short lived since we will rebuild today anyway chunk.Items.Add(new BoxPreviewVM(ae, Date, ItemGotFocus)); chunk.IsDirty = true; SaveChunks(force: true); //this handler can only affect today so we only refresh this one view if (rebulidViews) { Refresh(null); } }
void ItemGotFocus(BoxPreviewVM itemVM) { LastFocusedChunk = null; LastFocusedBoxId = itemVM.Persistent.Box.RowId; DraggableBoxVM = itemVM; }
//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; }