/// <summary> /// Creates an instance of <see cref="WorkShiftVm"/> with the given model and work shift prototype viewModel /// </summary> /// <param name="model"></param> /// <param name="prototype"></param> public WorkShiftVm(Model.WorkShift model, WorkShiftPrototypeVm prototype) { Model = model; Prototype = prototype; StartSeconds = model.StartSeconds; EndSeconds = model.EndSeconds; IsOpen = model.IsOpen; //add workbreak models foreach (var workBreak in model.WorkBreaks) { var wbreak = new WorkBreakVm(workBreak); wbreak.DeleteCommand = new Commands.Command(o => Breaks.Remove(wbreak)); Breaks.Add(wbreak); } //auto add future workbreak models Breaks.CollectionChanged += (s, e) => { if (e.NewItems != null) foreach (WorkBreakVm wbreak in e.NewItems) { Model.WorkBreaks.Add(wbreak.Model); } if(e.OldItems!=null) foreach (WorkBreakVm wbreak in e.OldItems) { Model.WorkBreaks.Remove(wbreak.Model); } }; ToggleIsOpenCommand = new Commands.Command(o => IsOpen = !IsOpen); }
/// <summary> /// Corrects Shifts and Prototypes info to match the desired count /// </summary> /// <param name="desiredCount">Target number of shifts in this profile</param> private void correctShifts(int desiredCount) { //changing ShiftPrototypes makes ShiftPrototype.Index akin to errors //-> Correct Indices var arr = ShiftPrototypes.OrderBy(x => x.Index).ToArray(); for (byte i = 0; i < arr.Length; i++) arr[i].Index = i; //add or remove shift prototypes to/from model and viewModel //-> correct shiftPrototypes while (desiredCount != _model.WorkShiftPrototypes.Count) { if (desiredCount > _model.WorkShiftPrototypes.Count) { //add a new proto until the desired number of protos are reached //each added item have its index and other props properly propped! //-> add ShiftPrototype var protoModel = WorkShiftPrototype.CreateDefault(_model); var protoVm = new WorkShiftPrototypeVm(protoModel, ShiftColors); _model.WorkShiftPrototypes.Add(protoModel); ShiftPrototypes.Add(protoVm); //After adding to shiftPrototypes, WorkShifts of each WorkDay have to be corrected too //-> add WorkShifts foreach (var day in _model.WorkDays) { //find workDayVm from day model var workDayVm = WorkDays.First(x => x.Id == day.Id); //add new shifts to workDay vm and workDay model if (day.WorkShifts.Any(x => x.WorkShiftPrototype.Index == protoModel.Index)) continue; var shift = WorkShift.CreateDefault(day, protoModel); day.WorkShifts.Add(shift); workDayVm.Shifts.Add(new WorkShiftVm(shift, protoVm)); } } else { //remove extra shift prototypes from model and viewModel //remove a new proto until the desired number of protos are reached //-> remove ShiftPrototype var protoModel = _model.WorkShiftPrototypes.OrderBy(x => x.Index).Last(); _model.WorkShiftPrototypes.Remove(protoModel); ShiftPrototypes.RemoveWhere(x => x.Index == protoModel.Index); //After correcting shiftPrototypes, WorkShifts of each WorkDay have to be corrected too //-> remove WorkShifts foreach (var day in _model.WorkDays) { //find workDayVm from day model var workDayVm = WorkDays.First(x => x.Id == day.Id); //remove extra shifts from workDay vm and workDay model var shift = day.WorkShifts.First(x => x.WorkShiftPrototype.Index == protoModel.Index); day.WorkShifts.Remove(shift); workDayVm.Shifts.Remove(workDayVm.Shifts.OrderBy(x => x.Prototype.Index).Last()); } } } }