/// <summary> /// Calendar item and sub item creator; months ans days /// </summary> public async Task CreateCalendarItems() { var months = new ObservableCollection <CalendarMonth>(); for (int i = 1; i < 13; i++) { var days = new List <CalendarDay>(); var firstDayOfMonth = new DateTime(TargetYear.YearNumber, i, 1); var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1); var lastDayLastMonth = firstDayOfMonth.AddDays(-1); var previousCoundown = PreviousNextMonthDays.GetPreviousCountdown(firstDayOfMonth.DayOfWeek); var nextCoundown = PreviousNextMonthDays.GetNextCountdown(lastDayOfMonth.DayOfWeek); // previous month days for (int ii = previousCoundown - 1; ii > -1; ii--) { days.Add(new CalendarDay { Date = new DateTime(TargetYear.YearNumber, i - 1 < 2 ? 1 : i - 1, lastDayLastMonth.Day - ii), DayStatus = DayStatus.DayOfPreviousMonth }); } // this month days var thisMonthDayCount = DateTime.DaysInMonth(TargetYear.YearNumber, i); for (int ii = 1; ii < thisMonthDayCount + 1; ii++) { var date = new DateTime(TargetYear.YearNumber, i, ii); days.Add(new CalendarDay { IsEnabled = true, IsDefault = date != DateTime.Now ? false : true, Date = date, DayStatus = date != DateTime.Now ? DayStatus.EmptyDay : DayStatus.EmptyToday }); } // next month days var daysBlockCount = previousCoundown + nextCoundown + thisMonthDayCount; if (daysBlockCount < 42) { nextCoundown += 42 - daysBlockCount; } for (int ii = 1; ii < nextCoundown + 1; ii++) { days.Add(new CalendarDay { Date = new DateTime(TargetYear.YearNumber, i + 1 > 12 ? 12 : i + 1, ii), DayStatus = DayStatus.DayOfNextMonth }); } months.Add(new CalendarMonth { MonthNumber = i, MonthName = Settings.CultureInfo.DateTimeFormat.GetMonthName(i), Year = TargetYear.YearNumber, CalendarDays = days }); } Months = months; }
public void LoadMonths() { var date = DateTime.Now; DietLogMonth = new DietLogMonth { MonthNumber = date.Month, MonthName = Settings.CultureInfo.DateTimeFormat.GetMonthName(date.Month), Year = date.Year }; var DietDays = new List <DietDay>(); var firstDayOfMonth = new DateTime(date.Year, date.Month, 1); var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1); var lastDayLastMonth = firstDayOfMonth.AddDays(-1); var previousCoundown = PreviousNextMonthDays.GetPreviousCountdown(firstDayOfMonth.DayOfWeek); var nextCoundown = PreviousNextMonthDays.GetNextCountdown(lastDayOfMonth.DayOfWeek); // previous month days for (int ii = previousCoundown - 1; ii > -1; ii--) { DietDays.Add(new DietDay { Date = new DateTime(date.Year, date.Month - 1, lastDayLastMonth.Day - ii), DayStatus = DayStatus.DayOfPreviousMonth }); } // this month days var thisMonthDayCount = DateTime.DaysInMonth(date.Year, date.Month); for (int ii = 1; ii < thisMonthDayCount + 1; ii++) { var dietDay = new DietDay { Date = new DateTime(date.Year, date.Month, ii), DayStatus = DayStatus.EmptyToday, IsEnabled = true }; using var db = new AppDbContext(); if (db.DietLogs.Any(x => x.AddedDate.ToShortDateString() == dietDay.Date.ToShortDateString())) { dietDay.DietLogItems = db.DietLogs.Where(x => x.AddedDate.ToShortDateString() == dietDay.Date.ToShortDateString()).Select(x => new DietLogItem { DietLog = x, DietFood = db.DietFoods.FirstOrDefault(c => c.Id == x.DietFoodId) }).ToObservableCollection(); } if (date.Day == ii) { dietDay.DayStatus = dietDay.DietLogItems.Count > 0 ? DayStatus.LoggedToday : DayStatus.EmptyToday; dietDay.IsDefault = true; } else { dietDay.DayStatus = dietDay.DietLogItems.Count > 0 ? DayStatus.LoggedDay : DayStatus.EmptyDay; } DietDays.Add(dietDay); } // next month days var daysBlockCount = previousCoundown + nextCoundown + thisMonthDayCount; if (daysBlockCount < 42) { nextCoundown += 42 - daysBlockCount; } for (int ii = 1; ii < nextCoundown + 1; ii++) { DietDays.Add(new DietDay { Date = new DateTime(date.Year, date.Month + 1, ii), DayStatus = DayStatus.DayOfNextMonth }); } DietLogMonth.DietDays = DietDays.ToObservableCollection(); }