예제 #1
0
        private void DeleteEvents()
        {
            if (logListView.SelectedItems.Count < 1)
                return;

            if (MessageBox.Show(string.Concat("Sigurno želiš obrisati odabrane unose? (", logListView.SelectedItems.Count, " komad(a))"), "Upozorenje", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;

            using (var context = new LovroContext(connectionString))
            {
                var toBeDeletedIDs = logListView.SelectedItems.Cast<ListViewItem>().Select(item => (int)item.Tag);
                var toBeDeletedItems = context.BaseEvents.Where(item => toBeDeletedIDs.Contains(item.ID));

                foreach (LovroBaseEvent lovroEvent in toBeDeletedItems)
                    context.BaseEvents.Remove(lovroEvent);

                context.SaveChanges();
            }
        }
예제 #2
0
        private void EditEvent(int eventID)
        {
            using (var context = new LovroContext(connectionString))
            {
                LovroBaseEvent eventInEditing = context.BaseEvents.FirstOrDefault(item => item.ID == eventID);

                if (eventInEditing == null)
                    throw new InvalidOperationException("Nepostojeći unos!");

                using (var editForm = new LovroEventEditForm())
                {
                    editForm.EventInEditing = eventInEditing;
                    if (editForm.ShowDialog() == DialogResult.OK) // eventInEditing properties will be changed in the dialog
                        context.SaveChanges();
                }
            }
        }
예제 #3
0
        private void CheckSoundWarning(object state)
        {
            using (var context = new LovroContext(connectionString))
            {
                LovroDiaperChangedEvent lastDiaperChangedEvent = context.DiaperChangedEvents.OrderByDescending(item => item.Time).FirstOrDefault();

                //if (!ignoreWarnings && lastDiaperChangedEvent != null && DateTime.Now > lastDiaperChangedEvent.Time.AddMinutes(GetDiaperChangeWarningLimitInMinutes()))
                if (lastDiaperChangedEvent != null && DateTime.Now > lastDiaperChangedEvent.Time.AddMinutes(GetDiaperChangeWarningLimitInMinutes()))
                {
                    if (!ignoreWarnings || warningLastSet > warningLastIgnored) // ??? WTF? TODO
                    {
                        player = new System.Media.SoundPlayer(@"c:\windows\media\Windows Battery Critical.wav");
                        player.PlayLooping();

                        if (!warningDisplayed)
                        {
                            warningLastSet = DateTime.Now;
                            using (var form = new ShowWarningForm())
                            {
                                warningDisplayed = true;
                                var result = form.ShowDialog();
                                if (result == DialogResult.OK && player != null)
                                {
                                    player.Stop();
                                    player.Dispose();
                                    ignoreWarnings = true;
                                    warningLastIgnored = DateTime.Now;
                                    warningLastSet = DateTime.MinValue;
                                }
                            }
                        }
                    }
                }
                else if (player != null)
                    player.Stop();
            }
        }
예제 #4
0
        private void RefreshStopwatch(object state)
        {
            if (logListView.InvokeRequired)
            {
                RefreshControlCallback callback = new RefreshControlCallback(RefreshStopwatch);
                if (!this.Disposing)
                    this.Invoke(callback, new object[] { null });
            }
            else
            {
                using (var context = new LovroContext(connectionString))
                {
                    TimeSpan timeExpired;

                    LovroDiaperChangedEvent diaperChangedEvent = context.DiaperChangedEvents.OrderByDescending(item => item.Time).FirstOrDefault();
                    if (diaperChangedEvent != null)
                    {
                        timeExpired = DateTime.Now - context.DiaperChangedEvents.OrderByDescending(item => item.Time).FirstOrDefault().Time;
                        stopwatchDiaperLabel.Text = timeExpired.ToString(@"hh\:mm\:ss");
                        if (timeExpired.TotalMinutes > GetDiaperChangeWarningLimitInMinutes())
                            stopwatchDiaperLabel.ForeColor = Color.Red;
                        else
                            stopwatchDiaperLabel.ForeColor = Color.Black;
                    }

                    DateTime lastWokeUp = context.BaseEvents.Where(item => item.Type == LovroEventType.WokeUp).OrderByDescending(item => item.Time).FirstOrDefault().Time;
                    DateTime lastFellAsleep = context.BaseEvents.Where(item => item.Type == LovroEventType.FellAsleep).OrderByDescending(item => item.Time).FirstOrDefault().Time;

                    if (lastWokeUp > lastFellAsleep) //if not sleeping atm
                    {
                        timeExpired = DateTime.Now - lastWokeUp;
                        stopwatchSleepLabel.Text = timeExpired.ToString(@"hh\:mm\:ss");                        
                    }
                    else
                        stopwatchSleepLabel.Text = "00:00:00";

                    timeExpired = DateTime.Now - context.BaseEvents.Where(item => item.Type == LovroEventType.AteFood).OrderByDescending(item => item.Time).FirstOrDefault().Time;
                    stopwatchFoodLabel.Text = timeExpired.ToString(@"hh\:mm\:ss");
                }
            }
        }
예제 #5
0
        private void RefreshLines(object state)
        {
            using (var context = new LovroContext(connectionString))
            {
                DatabaseSummary summary = context.Summaries.FirstOrDefault();

                if (summary != null)
                    lastDataUpdate = summary.LastModified;
                else
                    lastDataUpdate = DateTime.MinValue.AddSeconds(1);
            }

            if (lastDataUpdate > lastRedraw)
            {
                if (logListView.InvokeRequired)
                {
                    RefreshControlCallback callback = new RefreshControlCallback(RefreshLines);
                    this.Invoke(callback, new object[] { null });
                }
                else
                {
                    logListView.Items.Clear();

                    using (var context = new LovroContext(connectionString))
                    {
                        LovroEventType typeFilter = LovroEventType.Default;
                        if (!Enum.TryParse(((KeyValuePair<string, string>)filterByTypeComboBox.SelectedItem).Key, out typeFilter))
                            typeFilter = LovroEventType.Default;

                        DateTime dateFilter = displayedDatePicker.Value.Date;

                        #region Displaying individual items
                        
                        foreach (LovroBaseEvent lovroEvent in context.BaseEvents.OrderBy(item => item.Time))
                        {
                            if (filterByTypeCheckBox.Checked && typeFilter != LovroEventType.Default && typeFilter != lovroEvent.Type)
                                continue;

                            if (filterByDateCheckBox.Checked && dateFilter != lovroEvent.Time.Date)
                                continue;

                            ListViewItem viewItem = CreateViewItem(lovroEvent, context);
                            logListView.Items.Add(viewItem);

                            if (viewItem.Index % 2 == 0)
                                viewItem.BackColor = Color.Beige;

                            logListView.TopItem = viewItem;
                            logListView.EnsureVisible(logListView.Items.Count - 1);
                        }
                        
                        logListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        Refresh();
                        
                        #endregion

                        #region Displaying summary info

                        int diapersChangedCount = 0;
                        int feedingTimesCount = 0;
                        int wakeUpCount = 0;

                        DateTime today = filterByDateCheckBox.Checked ? displayedDatePicker.Value.Date : DateTime.Now.Date;
                        DateTime tomorrow = today.AddDays(1);

                        #region Calculating total nap time

                        TimeSpan totalNapTime = TimeSpan.Zero;
                        IEnumerable<LovroBaseEvent> wakeUpEvents = context.BaseEvents.Where(item => item.Time >= today && item.Time < tomorrow && item.Type == LovroEventType.WokeUp);
                        IEnumerable<LovroBaseEvent> correspondingFellAsleepEvents = wakeUpEvents.Select(wakeUpEvent => context.BaseEvents.OrderByDescending(item => item.Time).FirstOrDefault(item => item.Type == LovroEventType.FellAsleep && wakeUpEvent.Time > item.Time));

                        wakeUpCount = wakeUpEvents.Count();
                        if (wakeUpCount != correspondingFellAsleepEvents.Count())
                            throw new InvalidOperationException("Nešto ne štima u bazi. Najebo si, useru moj!");

                        for (int i = 0; i < wakeUpCount; i++)
                        {
                            DateTime sleepStartTime = correspondingFellAsleepEvents.ElementAt(i).Time;
                            if (sleepStartTime.Date < today)
                                sleepStartTime = today; // at midnight

                            totalNapTime += (wakeUpEvents.ElementAt(i).Time - sleepStartTime);
                        }

                        // TODO--nema smisla ako se ovo polje ne updatea svake sekunde/redovito
                        //DateTime lastWokeUp = context.BaseEvents.Where(item => item.Type == LovroEventType.WokeUp).OrderByDescending(item => item.Time).FirstOrDefault().Time;
                        //DateTime lastFellAsleep = context.BaseEvents.Where(item => item.Type == LovroEventType.FellAsleep).OrderByDescending(item => item.Time).FirstOrDefault().Time;

                        //if (lastWokeUp < lastFellAsleep) //if sleeping atm
                        //    totalNapTime += (DateTime.Now - lastFellAsleep);

                        #endregion

                        diapersChangedCount = context.DiaperChangedEvents.Count(item => item.Time >= today && item.Time < tomorrow);
                        feedingTimesCount = context.BaseEvents.Count(item => item.Type == LovroEventType.AteFood && item.Time >= today && item.Time < tomorrow);

                        summaryLabel.Text = string.Concat("Tokom dana ukupno spavao: ", totalNapTime.ToString(@"hh\:mm"), " sati, jeo ", feedingTimesCount, " puta, promijenjeno ", diapersChangedCount, " pelena");

                        #endregion

                        lastRedraw = DateTime.Now;
                    }
                }
            }
        }
예제 #6
0
        private ListViewItem CreateViewItem(LovroBaseEvent lovroEvent, LovroContext context)
        {
            ListViewItem viewItem = new ListViewItem(lovroEvent.Time.ToString());
            viewItem.Tag = lovroEvent.ID;
            //viewItem.SubItems.Add(GetEnumDescription(typeof(LovroDiaperStatus), lovroEvent.Type.ToString()));
            viewItem.SubItems.Add(GetEventTypeDescription(lovroEvent.Type));

            SetImageIndex(lovroEvent, viewItem);

            if (lovroEvent.Type == LovroEventType.DiaperChanged)
            {
                //viewItem.SubItems.Add(GetEnumDescription(typeof(LovroDiaperChangedEvent), (lovroEvent as LovroDiaperChangedEvent).Status.ToString()));
                viewItem.SubItems.Add(GetStatusDescription((lovroEvent as LovroDiaperChangedEvent).Status));
            }
            else
                viewItem.SubItems.Add("");

            if (lovroEvent.Type == LovroEventType.DiaperChanged)
            {
                LovroBaseEvent diaperLastChanged = context.BaseEvents.Where(item => item.Type == LovroEventType.DiaperChanged && item.ID != lovroEvent.ID && item.Time < lovroEvent.Time).OrderByDescending(item => item.Time).FirstOrDefault();
                viewItem.SubItems.Add(diaperLastChanged == null ? "" : (lovroEvent.Time - diaperLastChanged.Time).ToString(@"hh\:mm"));
            }
            else if (lovroEvent.Type == LovroEventType.WokeUp)
            {
                LovroBaseEvent lastFellAsleepEvent = context.BaseEvents.Where(item => item.Type == LovroEventType.FellAsleep && item.Time < lovroEvent.Time).OrderByDescending(item => item.Time).FirstOrDefault();
                if (lastFellAsleepEvent != null)
                    viewItem.SubItems.Add((lovroEvent.Time - lastFellAsleepEvent.Time).ToString(@"hh\:mm"));
            }
            else
                viewItem.SubItems.Add("");

            viewItem.SubItems.Add(lovroEvent.Note);
            
            return viewItem;
        }
예제 #7
0
        private void AddEvent(LovroBaseEvent lovroEvent)
        {
            if (!AskForDetails(lovroEvent))
            {
                return;
            }

            using (var context = new LovroContext(connectionString))
            {
                //context.AddBaseEvent(lovroEvent);

                context.BaseEvents.Add(lovroEvent);
                context.SaveChanges();
            }
        }