示例#1
0
        public void readStoredEventsFile()
        {
            var filePath = pathToStoredEventsFile();

            //values for an event
            string name, source;
            int    month, day, hour, minute, second;
            bool   visible;

            //read in stored events from text file
            string text, entryType, entryValue;

            if (!System.IO.File.Exists(filePath))
            {
                resetStoredEventsFile();
            }
            string[] reader = System.IO.File.ReadAllLines(filePath);
            int      i      = 0;

            while (i < reader.Length)
            {
                //values for an event
                name    = "";
                source  = "";
                month   = 1;
                day     = 1;
                hour    = 0;
                minute  = 0;
                second  = 0;
                visible = false;

                text = convertEntry(reader[i]);
                switch (text)
                {
                case "Event":
                    i++;
                    text = convertEntry(reader[i]);
                    while (!text.Equals("/Event"))
                    {
                        entryType  = getEntryType(text);
                        entryValue = getEntryValue(text);
                        switch (entryType)
                        {
                        case "Name":
                            name = entryValue;
                            break;

                        case "Source":
                            source = entryValue;
                            break;

                        case "Month":
                            month = Int32.Parse(entryValue);
                            break;

                        case "Day":
                            day = Int32.Parse(entryValue);
                            break;

                        case "Hour":
                            hour = Int32.Parse(entryValue);
                            break;

                        case "Visible":
                            visible = bool.Parse(entryValue);
                            break;
                        }
                        i++;
                        text = convertEntry(reader[i]);
                    }

                    //add the new event to list(s)
                    //2016 used to prevent impossible date due to leap day, the year value will be reset anyhow
                    Event newEvent = new Event(name, new DateTime(2016, month, day, hour, minute, second), visible);
                    GlobalVariables.resetEvent(newEvent);
                    GlobalVariables.storedEvents.Add(newEvent);

                    break;
                }
                i++;
            }
        }
示例#2
0
        public async void newDayRefresh()
        {
            bool setToGrid = false;

            if (lastDay == -1)
            {
                await Task.Run(() => {
                    Device.BeginInvokeOnMainThread(() => {
                        populateGrid();
                    });
                });

                setToGrid = true;
            }
            currentDay = DateTime.Now.Day;
            if (lastDay != currentDay)
            {
                lastDay = currentDay;
                FileHandler fileHandler = new FileHandler();

                //set current date text
                Device.BeginInvokeOnMainThread(() => {
                    currentDate.Text = DateTime.Now.ToString("D");
                });

                //new day, change any events that were today to next year
                //*************************??
                int  myEventsIndex     = 0;
                int  storedEventsIndex = 0;
                int  index             = 0;
                bool myEventType;

                while (myEventsIndex < GlobalVariables.myEvents.Count || storedEventsIndex < GlobalVariables.storedEvents.Count)
                {
                    myEventType = true;
                    if (myEventsIndex < GlobalVariables.myEvents.Count)
                    {
                        if (storedEventsIndex < GlobalVariables.storedEvents.Count)
                        {
                            if (GlobalVariables.myEvents[myEventsIndex].getDate() < GlobalVariables.storedEvents[storedEventsIndex].getDate())
                            {
                                index = myEventsIndex++;
                            }
                            else
                            {
                                index       = storedEventsIndex++;
                                myEventType = false;
                            }
                        }
                        else
                        {
                            index = myEventsIndex++;
                        }
                    }
                    else
                    {
                        index       = storedEventsIndex++;
                        myEventType = false;
                    }

                    if (myEventType)
                    {
                        if (GlobalVariables.myEvents[index].getDaysUntil() < 0)
                        {
                            if (GlobalVariables.myEvents[index].isOneTimeEvent())
                            {
                                if (!setToGrid && GlobalVariables.myEvents[index].isVisible())
                                {
                                    GlobalVariables.myEvents[index].removeFromGrid();

                                    //move remaining real child locations to new indices
                                    for (int j = 1; j < GlobalVariables.grid2_realChildLocations.Count; j++)
                                    {
                                        if (GlobalVariables.grid2_realChildLocations[j] > GlobalVariables.grid2_realChildLocations[0])
                                        {
                                            GlobalVariables.grid2_realChildLocations[j] -= 5;
                                        }
                                    }

                                    GlobalVariables.grid2_realChildLocations.RemoveAt(0);
                                }
                                GlobalVariables.myEvents.RemoveAt(index--);
                                myEventsIndex--;
                                Thread.Sleep(200);
                                continue;
                            }
                            GlobalVariables.resetEvent(GlobalVariables.myEvents[index]);
                            if (!setToGrid && GlobalVariables.myEvents[index].isVisible())
                            {
                                Thread.Sleep(100);
                                GlobalVariables.myEvents[index].refreshRow();
                            }
                        }
                        if (!setToGrid && GlobalVariables.myEvents[index].isVisible())
                        {
                            GlobalVariables.myEvents[index].refreshBackground();
                        }
                    }
                    else   //stored event type
                    {
                        if (GlobalVariables.storedEvents[index].getDaysUntil() < 0)
                        {
                            GlobalVariables.resetEvent(GlobalVariables.storedEvents[index]);
                            if (!setToGrid && GlobalVariables.storedEvents[index].isVisible())
                            {
                                Thread.Sleep(100);
                                GlobalVariables.storedEvents[index].refreshRow();
                            }
                        }
                        if (!setToGrid && GlobalVariables.storedEvents[index].isVisible())
                        {
                            GlobalVariables.storedEvents[index].refreshBackground();
                        }
                    }
                }

                //check if a one time event has lapsed since last app open
                for (int i = 0; i < GlobalVariables.myEvents.Count; i++)
                {
                    if (GlobalVariables.myEvents[i].isOneTimeEvent() && eventHasLapsed(GlobalVariables.myEvents[i]))
                    {
                        GlobalVariables.myEvents.RemoveAt(i--);
                        continue;
                    }
                }

                await Task.Run(() => {
                    try {
                        fileHandler.updateMyEventsFile();
                    } catch (Exception ex) {
                        Debug.WriteLine("Exception trying to update my events file:\n" + ex.Message);
                    }
                });

                //set page's content back to original stack
                if (setToGrid)
                {
                    while (animating)
                    {
                        ;
                    }
                    Thread.Sleep(150);
                    while (populatingGrid)
                    {
                        ;
                    }
                    Device.BeginInvokeOnMainThread(() => {
                        Content = animationStack;
                        animationStack.Children.Insert(0, mainStack);
                        Device.BeginInvokeOnMainThread(async() => {
                            animationStack.Children.RemoveAt(1);
                            uint fadeTime = 800;
                            Device.BeginInvokeOnMainThread(async() => {
                                await utilitiesPadding.FadeTo(1.0, fadeTime);
                            });
                            Device.BeginInvokeOnMainThread(async() => {
                                await Buttons.FadeTo(1.0, fadeTime);
                            });
                            await gridStack.FadeTo(1.0, fadeTime);
                        });
                    });
                }
            }
        }
        public async void addEvent(object sender, EventArgs e)
        {
            if (entryName.Text == null)
            {
                await this.DisplayAlert("Error", "Event name cannot be empty.", "Ok");

                return;
            }

            //remove white space on ends
            while (entryName.Text.Length > 0 && entryName.Text[0] == ' ')
            {
                entryName.Text = entryName.Text.Substring(1);
            }
            while (entryName.Text.Length > 0 && entryName.Text[entryName.Text.Length - 1] == ' ')
            {
                entryName.Text = entryName.Text.Substring(0, entryName.Text.Length - 1);
            }

            if (entryName.Text.Length == 0)
            {
                await this.DisplayAlert("Error", "Event name cannot be empty.", "Ok");

                return;
            }
            for (int i = 0; i < GlobalVariables.myEvents.Count; i++)
            {
                if (string.Compare(GlobalVariables.myEvents[i].getName(), entryName.Text, true) == 0)
                {
                    await this.DisplayAlert("Error", "An event with that name already exists.", "Ok");

                    return;
                }
            }
            for (int i = 0; i < GlobalVariables.storedEvents.Count; i++)
            {
                if (string.Compare(GlobalVariables.storedEvents[i].getName(), entryName.Text, true) == 0)
                {
                    await this.DisplayAlert("Error", "An event with that name already exists.", "Ok");

                    return;
                }
            }

            displayActivityIndicator();

            await Task.Run(() => {
                DateTime eventDate = new DateTime(datePicked.Date.Year, datePicked.Date.Month, datePicked.Date.Day, timePicked.Time.Hours, timePicked.Time.Minutes, 0);
                Event newEvent     = new Event(entryName.Text, eventDate);
                newEvent.setAllDayEvent(allDayEvent);
                newEvent.setOneTimeEvent(oneTimeEvent);
                GlobalVariables.resetEvent(newEvent);
                GlobalVariables.myEvents.Add(newEvent);

                //save event to file
                FileHandler fileHandler = new FileHandler();
                fileHandler.appendEventToFile(newEvent);

                Device.BeginInvokeOnMainThread(() => {
                    mainPage.refreshGrid();
                    mainPage.populateGrid();
                    Navigation.PopModalAsync();
                });
            });
        }