RootElement GenerateRoot()
        {
            // The full list
            var allSessions = new RootElement ("All Sessions") {
                from s in MonkeySpace.Core.ConferenceManager.Sessions.Values.ToList () //AppDelegate.ConferenceData.Sessions
                orderby s.Start ascending
                group s by s.Start.Ticks into g
                select new Section (HomeViewController.MakeCaption ("", new DateTime (g.Key), true)) {
                    from hs in g
                       select (Element) new SessionElement (hs)
            }};

            var root = new CustomRootElement ("Sessions") {
                    allSessions
            };

            return root;
        }
        RootElement GenerateRoot()
        {
            var favs = AppDelegate.UserData.GetFavoriteCodes();
            var root = 	new CustomRootElement ("Favorites") {
                from s in MonkeySpace.Core.ConferenceManager.Sessions.Values.ToList () //AppDelegate.ConferenceData.Sessions
                            where favs.Contains(s.Code )
                            group s by s.Start.Ticks into g
                            orderby g.Key
                            select new Section (HomeViewController.MakeCaption ("", new DateTime (g.Key))) {
                            from hs in g
                               select (Element) new SessionElement (hs)
            }};

            if(favs.Count == 0)
            {
                var section = new Section("Whoops, Star a few sessions first!");
                root.Add(section);
            }
            return root;
        }
        //
        // This method is invoked when the application has loaded and is ready to run. In this
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);

            root = new CustomRootElement("Penny Far Elements Sample");
            Section section = new Section("Counter Element", "Has a ValueChanged event") {
                new ResponsiveCounterElement("Responsive Counter", "10.0")
            };

            root.Add(section);

            Section customentry = new Section("Loop through the entries");
            CustomKeyboardEntryElement firstElement = new CustomKeyboardEntryElement("entry 1", "", "");
            CustomKeyboardEntryElement secondElement = new CustomKeyboardEntryElement("entry 2", "", "");
            CustomKeyboardEntryElement thirdElement = new CustomKeyboardEntryElement("entry 3", "", "");

            // Now set up the element order so it can go through them correctly
            firstElement.NextElement = secondElement;
            secondElement.PrevElement = firstElement;
            secondElement.NextElement = thirdElement;
            thirdElement.PrevElement = secondElement;

            customentry.Add(firstElement);
            customentry.Add(secondElement);
            customentry.Add(thirdElement);

            root.Add(customentry);

            Section subSection = new Section("Section with sub roots");

            CustomRootElement subRoot = new CustomRootElement("Multi Options");
            Section stuffSect = new Section("Showing Stuff");
            StringElement stuff1 = new StringElement("Stuff");
            StringElement stuff2 = new StringElement("More stuff");

            TimeWithSecondsPickerElement tepe = new TimeWithSecondsPickerElement("new pick", new TimeSpan(4,3,1));

            stuffSect.Add(stuff1);
            stuffSect.Add(stuff2);

            stuffSect.Add(tepe);

            subRoot.BackgroundImage= "images/norwegian_rose";

            subRoot.Add(stuffSect);

            subSection.Add(subRoot);

            root.Add(subSection);

            cDVC = new CustomDialogViewController(root);

            // Add custom background
            cDVC.BackgroundImage = "images/norwegian_rose";

            nav = new UINavigationController(cDVC);

            window.RootViewController = nav;
            window.MakeKeyAndVisible ();

            return true;
        }
        private UINavigationController NewRMEntry(double prevBest)
        {
            CustomRootElement root = new CustomRootElement(" ");

            CustomDialogViewController dvc = new CustomDialogViewController(root, false);
            UINavigationController nav = new UINavigationController(dvc);

            dvc.BackgroundImage = "images/white_carbon";
            dvc.NavigationBarColor = UIColor.FromRGB(39, 113, 205);
            dvc.NavigationBarImage = UIImage.FromBundle("images/navBar");

            UIColor buttonColor = UIColor.FromRGB(39, 113, 205);
            UIBarButtonItem cancelButton = new UIBarButtonItem(UIBarButtonSystemItem.Cancel);
            cancelButton.TintColor = buttonColor;
            cancelButton.Clicked += delegate {
                dvc.NavigationController.DismissViewController(true, null);
            };
            UIBarButtonItem saveButton = new UIBarButtonItem(UIBarButtonSystemItem.Save);
            saveButton.TintColor = buttonColor;
            dvc.NavigationItem.LeftBarButtonItem = cancelButton;
            dvc.NavigationItem.RightBarButtonItem = saveButton;

            Section newRMSection = new Section("Record Details") {};

            string strPrevBest = String.Format("{0:000.0}", prevBest);

            if(prevBest == 0)
                strPrevBest = "100.0";

            ResponsiveCounterElement newRMCounter = new ResponsiveCounterElement("New RM", strPrevBest); // TODO: grab the previous best and use that
            newRMCounter.SetCustomBackButton(UIImage.FromBundle("images/backbutton"), RectangleF.FromLTRB (0, 20, 50, 20));

            DateElement newRMDate = new DateElement("Date", DateTime.Today);

            newRMSection.Add(newRMCounter);
            newRMSection.Add(newRMDate);

            root.Add(newRMSection);

            saveButton.Clicked += delegate {
                RmLog newEntry = new RmLog {
                    DateLogged = newRMDate.DateValue,
                    Weight = Convert.ToDouble(newRMCounter.Value),
                    ExerciseID = this._exercise.ID
                };

                dvc.NavigationController.DismissViewController(true, null);

                db.Insert(newEntry);
                this._rms.Add(newEntry);

                // refresh view
                this.RefreshRecords();

            };

            return nav;
        }
        private UINavigationController EditExerciseName()
        {
            CustomRootElement root = new CustomRootElement(" ");

            CustomDialogViewController dvc = new CustomDialogViewController(root, false);
            UINavigationController nav = new UINavigationController(dvc);

            dvc.BackgroundImage = "images/white_carbon";
            dvc.NavigationBarColor = UIColor.FromRGB(39, 113, 205);
            dvc.NavigationBarImage = UIImage.FromBundle("images/navBar");

            UIColor buttonColor = UIColor.FromRGB(39, 113, 205);
            UIBarButtonItem cancelButton = new UIBarButtonItem(UIBarButtonSystemItem.Cancel);
            cancelButton.TintColor = buttonColor;
            cancelButton.Clicked += delegate {
                dvc.NavigationController.DismissViewController(true, null);
            };
            UIBarButtonItem saveButton = new UIBarButtonItem(UIBarButtonSystemItem.Save);
            saveButton.TintColor = buttonColor;
            dvc.NavigationItem.LeftBarButtonItem = cancelButton;
            dvc.NavigationItem.RightBarButtonItem = saveButton;

            Section nameSection = new Section("Edit Name") {};

            EntryElement nameEdit = new EntryElement("Name", "", this._exercise.Name, false);

            nameSection.Add(nameEdit);

            root.Add(nameSection);

            // TODO: Add all the logs here and set them to delete mode

            saveButton.Clicked += delegate {
                this._exercise.Name = nameEdit.Value;

                dvc.NavigationController.DismissViewController(true, null);

                db.Update(this._exercise);

                if(this._exercise.Name.Length > 16)
                    this.NavigationController.TabBarItem.Title = this._exercise.Name.Substring(0, 16);
                else
                    this.NavigationController.TabBarItem.Title = this._exercise.Name;

                this.lblExName.Text = this._exercise.Name;
            };

            return nav;
        }
        // Fills out the schedule for a given day
        public Element MakeSchedule(DateTime d)
        {
            // Added .ToString/Convert.ToDateTime
            // to avoid System.ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<System.DateTime>:.ctor ()' while running with --aot-only.

            var sections = from s in MonkeySpace.Core.ConferenceManager.Sessions.Values.ToList () //AppDelegate.ConferenceData.Sessions
                where s.Start.Day == d.Day
                orderby s.Start ascending
                group s by s.Start.ToString() into g
                select new Section (MakeCaption ("", Convert.ToDateTime(g.Key))) {
                    from hs in g
                       select (Element) new SessionElement (hs)
            };

            var root = new CustomRootElement (FormatDate (d, DateTime.Now));
            foreach (var s in sections)
                root.Add (s);
            return root;
        }
        // Appends a section to the list.
        public bool AppendSection(RootElement root, IEnumerable<MonkeySpace.Core.Session> list, string title)
        {
            if (list == null || list.Count () == 0)
                return false;

            var sschedule = new Section ();
            var schedule = new CustomRootElement ("Sessions") { sschedule };

            foreach (var s in list)
                sschedule.Add (new SessionElement (s));

            var section = new Section (title);
            section.Add (schedule);

            AppendFavorites (section, list);

            root.Add (section);

            return true;
        }