Beispiel #1
0
 public WebViewController(StyledHtmlElement container)
     : base()
 {
     this.container = container;
 }
Beispiel #2
0
        /*
        private static StackPanel RenderEditItemImageButtonPanel(TextBox tb)
        {
            tb.MinWidth -= 64;
            StackPanel innerPanel = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
            innerPanel.Children.Add(tb);
            ImageButton imageButton = new ImageButton()
            {
                Image = new BitmapImage(new Uri("/Images/button.search.png", UriKind.Relative)),
                PressedImage = new BitmapImage(new Uri("/Images/button.search.pressed.png", UriKind.Relative)),
                Width = 48,
                Height = 48,
                Template = (ControlTemplate)App.Current.Resources["ImageButtonControlTemplate"]
            };
            innerPanel.Children.Add(imageButton);
            return innerPanel;
        }

        private void RenderEditItemTagList(TextBox taglist, Item item, PropertyInfo pi)
        {
            taglist.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Text } } };

            // build the comma delimited tag folder for this item
            bool addDelimiter = false;
            StringBuilder sb = new StringBuilder();
            var itemtags = (IEnumerable<ItemTag>)pi.GetValue(item, null);
            if (itemtags != null)
            {
                foreach (ItemTag tt in itemtags)
                {
                    if (addDelimiter)
                        sb.Append(",");
                    Tag tag = App.ViewModel.Tags.Single(t => t.ID == tt.TagID);
                    sb.Append(tag.Name);
                    addDelimiter = true;
                }
                taglist.Text = sb.ToString();
            }

            // retrieve the itemtags for the item, creating new tags along the way
            taglist.LostFocus += new RoutedEventHandler(delegate
            {
                //ObservableCollection<ItemTag> existingTags = (ObservableCollection<ItemTag>)pi.GetValue(item, null);
                ObservableCollection<ItemTag> newTags = new ObservableCollection<ItemTag>();
                string[] tags = taglist.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var tt in tags)
                {
                    string str = tt.Trim();
                    Tag tag;
                    try
                    {
                        tag = App.ViewModel.Tags.Single(t => t.Name == str);
                        newTags.Add(new ItemTag() { Name = str, TagID = tag.ID, ItemID = item.ID });
                    }
                    catch (Exception)
                    {
                        // this is a new tag that we need to create
                        tag = new Tag() { Name = str };
                        newTags.Add(new ItemTag() { Name = str, TagID = tag.ID, ItemID = item.ID });

                        // enqueue the Web Request Record
                        RequestQueue.EnqueueRequestRecord(
                            new RequestQueue.RequestRecord()
                            {
                                ReqType = RequestQueue.RequestRecord.RequestType.Insert,
                                Body = new Tag(tag)
                            });

                        // add the tag to the tag folder
                        App.ViewModel.Tags.Add(tag);

                        // save the changes to local storage
                        StorageHelper.WriteTags(App.ViewModel.Tags);
                    }
                }

                // store the new ItemTag collection in the item
                pi.SetValue(item, newTags, null);

                // create the mirror Tags collection in the item
                item.CreateTags(App.ViewModel.Tags);
            });
        }
         */
        private RootElement RenderViewItem(Item item)
        {
            // get the item type
            ItemType itemType = null;
            if (ItemType.ItemTypes.TryGetValue(item.ItemTypeID, out itemType) == false)
                return null;

            Section name = new Section(item.Name);
            Section section = new Section(item.Description);

            var picFV = item.GetFieldValue(FieldNames.Picture);
            if (picFV != null && !String.IsNullOrWhiteSpace(picFV.Value))
            {
                var image = ImageLoader.DefaultRequestImage(new Uri(picFV.Value), this);//new ImageLoaderCallback(actionsViewController, name));
                if (image != null)
                    name.FooterView = RenderViewItemPicture(image);
            }

            // create a list of all the first subitems in each of the sublists of the item (e.g. Contacts, Places, etc)
            var subLists = App.ViewModel.Items.Where(i => i.ParentID == item.ID && i.IsList == true).ToList();
            var subItems = new List<Item>();
            foreach (var subList in subLists)
            {
                var itemRef = App.ViewModel.Items.FirstOrDefault(i => i.ParentID == subList.ID && i.ItemTypeID == SystemItemTypes.Reference);
                if (itemRef != null)
                {
                    var target = App.ViewModel.Items.FirstOrDefault(i => i.ID == itemRef.ItemRef);
                    if (target != null)
                        subItems.Add(target);
                }
            }

            // render fields
            foreach (ActionType action in App.ViewModel.Constants.ActionTypes.OrderBy(a => a.SortOrder))
            {
                FieldValue fieldValue = item.FieldValues.FirstOrDefault(fv => fv.FieldName == action.FieldName);
                if (fieldValue == null)
                {
                    bool found = false;
                    foreach (var i in subItems)
                    {
                        fieldValue = i.FieldValues.FirstOrDefault(fv => fv.FieldName == action.FieldName);
                        if (fieldValue != null)
                        {
                            found = true;
                            break;
                        }
                    }

                    // if fieldvalue isn't found on this item or other subitems, don't process the action
                    if (found == false)
                        continue;
                }

                // get the value of the property
                string currentValue = fieldValue.Value;

                // for our purposes, an empty value is the same as null
                if (currentValue == "")
                    currentValue = null;

                // render this property if it's not null/empty
                if (currentValue != null)
                {
                    // first make sure that we do want to render (type-specific logic goes here)
                    switch (action.ActionName)
                    {
                        case "Postpone":
                            // if the date is already further in the future than today, omit adding this action
                            if (Convert.ToDateTime(currentValue).Date > DateTime.Today.Date)
                                continue;
                            break;
                    }

                    // Create a StyledStringElement to hold the action.  The Caption is the verb (action),
                    // while the Value is the noun that the verb will act upon
                    // (usually extracted from the item field's contents)
                    StyledStringElement stringElement = new StyledStringElement(action.DisplayName, currentValue, UITableViewCellStyle.Value1);
                    Element element = stringElement;

                    // render the action based on the action type
                    switch (action.ActionName)
                    {
                        case ActionNames.Navigate:
                            try
                            {
                                Item newItem = App.ViewModel.Items.Single(it => it.ID == Guid.Parse(currentValue));
                                //stringElement.Value = String.Format("to {0}", newItem.Name);
                                stringElement.Value = "";
                                stringElement.Tapped += delegate
                                {
                                    // Navigate to the new page
                                    if (newItem != null)
                                    {
                                        if (newItem.IsList == true)
                                        {
                                            // Navigate to the list page
                                            UIViewController nextController = new ListViewController(this.controller, folder, newItem.ID);
                                            TraceHelper.StartMessage("Item: Navigate to ListPage");
                                            this.controller.PushViewController(nextController, true);
                                        }
                                        else
                                        {
                                            // if the item is a reference, traverse to the target
                                            while (newItem.ItemTypeID == SystemItemTypes.Reference && newItem.ItemRef != null)
                                            {
                                                try
                                                {
                                                    newItem = App.ViewModel.Items.Single(it => it.ID == newItem.ItemRef);
                                                }
                                                catch
                                                {
                                                    TraceHelper.AddMessage(String.Format("Couldn't find item reference for name {0}, id {1}, ref {2}",
                                                                                         newItem.Name, newItem.ID, newItem.ItemRef));
                                                    break;
                                                }
                                            }
                                            ItemPage itemPage = new ItemPage(controller.NavigationController, newItem);
                                            TraceHelper.StartMessage("Item: Navigate to ItemPage");
                                            itemPage.PushViewController();
                                        }
                                    }
                                };
                            }
                            catch (Exception)
                            {
                                stringElement.Value = "(item not found)";
                            }
                            break;
                        case ActionNames.Postpone:
                            stringElement.Value = "to tomorrow";
                            stringElement.Tapped += delegate
                            {
                                TimeSpan time = Convert.ToDateTime(currentValue).TimeOfDay;
                                fieldValue.Value = (DateTime.Today.Date.AddDays(1.0) + time).ToString();
                                // save the item and trigger a sync with the service
                                SaveButton_Click(null, null);
                                // reload the Actions page
                                var oldroot = root;
                                root = RenderViewItem(ThisItem);
                                actionsViewController.Root = root;
                                actionsViewController.ReloadData();
                                oldroot.Dispose();

                                folder.NotifyPropertyChanged("FirstDue");
                                folder.NotifyPropertyChanged("FirstDueColor");
                            };
                            break;
                        case ActionNames.AddToCalendar:
                            DateTime dt = Convert.ToDateTime(currentValue);
                            stringElement.Value = dt.TimeOfDay == TimeSpan.FromSeconds(0d) ? dt.Date.ToString() : dt.ToString();
                            stringElement.Tapped += delegate
                            {
                                folder.NotifyPropertyChanged("FirstDue");
                                folder.NotifyPropertyChanged("FirstDueColor");
                            };
                            break;
                        case ActionNames.Map:
                            stringElement.Tapped += delegate
                            {
                                // try to use the maps: URL scheme
                                string url = "maps://" + currentValue.Replace(" ", "%20");
                                if (UIApplication.SharedApplication.CanOpenUrl(new NSUrl(url)))
                                    UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
                                else
                                {
                                    // open the google maps website
                                    url = url.Replace("maps://", "http://maps.google.com/maps?q=");
                                    UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
                                }
                            };
                            break;
                        case ActionNames.Call:
                           stringElement.Tapped += delegate
                            {
                                // construct the correct URL
                                List<string> urlComponents = new List<string>() { "tel://" };
                                urlComponents.AddRange(currentValue.Split('(', ')', '-', ' '));
                                string url = String.Concat(urlComponents);
                                if (UIApplication.SharedApplication.OpenUrl(new NSUrl(url)) == false)
                                    MessageBox.Show("Can't make a phone call");
                            };
                            break;
                        case ActionNames.TextMessage:
                            stringElement.Tapped += delegate
                            {
                                // construct the correct URL
                                List<string> urlComponents = new List<string>() { "sms:" };
                                urlComponents.AddRange(currentValue.Split('(', ')', '-', ' '));
                                string url = String.Concat(urlComponents);
                                if (UIApplication.SharedApplication.OpenUrl(new NSUrl(url)) == false)
                                    MessageBox.Show("Can't send a text message");
                            };
                            break;
                        case ActionNames.Browse:
                            // construct the correct URL
                            var linkList = JsonConvert.DeserializeObject<List<Link>>(currentValue);
                            if (linkList.Count == 0)
                                continue;
                            string linkUrl = linkList[0].Url.Replace(" ", "%20");
                            if (linkUrl.Substring(0, 4) != "http")
                                linkUrl = String.Format("http://{0}", linkUrl);
                            StyledHtmlElement browserElement = new StyledHtmlElement(action.DisplayName, linkUrl, UITableViewCellStyle.Value1, linkUrl);
                            element = browserElement;
                            break;
                        case ActionNames.SendEmail:
                            stringElement.Tapped += delegate
                            {
                                // construct the correct URL
                                string emailUrl = currentValue.Trim();
                                if (UIApplication.SharedApplication.OpenUrl(new NSUrl(emailUrl)) == false)
                                    MessageBox.Show("Can't launch the email application");
                            };
                            break;
                    }

                    // add the element to the section (note that the reference may have been
                    // reset in the switch statement)
                    section.Add (element);
                }
            }

            return new RootElement("Actions") { name, section };
        }
Beispiel #3
0
 public WebViewController(StyledHtmlElement container) : base()
 {
     this.container = container;
 }