private void AddFile() { var createController = new ModifyGistFileController(); createController.Save = (name, content) => { if (string.IsNullOrEmpty(name)) { //Keep trying until we get a valid filename while (true) { name = "gistfile" + (++_gistFileCounter) + ".txt"; if (ViewModel.Files.ContainsKey(name)) continue; break; } } if (ViewModel.Files.ContainsKey(name)) throw new InvalidOperationException("A filename by that type already exists"); ViewModel.Files.Add(name, content); ViewModel.Files = ViewModel.Files; }; NavigationController.PushViewController(createController, true); }
protected void UpdateView() { var root = new RootElement(Title) { UnevenRows = true }; var section = new Section(); root.Add(section); var desc = new MultilinedElement("Description") { Value = ViewModel.Description }; desc.Tapped += ChangeDescription; section.Add(desc); var pub = new TrueFalseElement("Public", ViewModel.Public, (e) => ViewModel.Public = e.Value); section.Add(pub); var fileSection = new Section(); root.Add(fileSection); foreach (var file in ViewModel.Files.Keys) { var key = file; if (string.IsNullOrEmpty(ViewModel.Files[file])) continue; var size = System.Text.Encoding.UTF8.GetByteCount(ViewModel.Files[file]); var el = new StyledStringElement(file, size + " bytes", UITableViewCellStyle.Subtitle) { Accessory = UITableViewCellAccessory.DisclosureIndicator }; el.Tapped += () => { if (!ViewModel.Files.ContainsKey(key)) return; var createController = new ModifyGistFileController(key, ViewModel.Files[key]); createController.Save = (name, content) => { if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Please enter a name for the file"); //If different name & exists somewhere else if (!name.Equals(key) && ViewModel.Files.ContainsKey(name)) throw new InvalidOperationException("A filename by that type already exists"); ViewModel.Files.Remove(key); ViewModel.Files[name] = content; ViewModel.Files = ViewModel.Files; // Trigger refresh }; NavigationController.PushViewController(createController, true); }; fileSection.Add(el); } fileSection.Add(new StyledStringElement("Add New File", AddFile)); Root = root; }