private static Action <object> MakeCallback(GistEditViewController ctrl, string key) { var weakCtrl = new WeakReference <GistEditViewController>(ctrl); return(new Action <object>(_ => { var model = weakCtrl.Get()?._model; if (model == null || !model.Files.ContainsKey(key)) { return; } var originalGist = weakCtrl.Get()?._originalGist; var createController = new GistFileEditViewController { Filename = key, Content = model.Files[key].Content }; createController.SaveCommand.Subscribe(__ => weakCtrl.Get()?.NavigationController.PopToViewController(weakCtrl.Get(), true)); 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)) { if (weakCtrl.Get()?.IsDuplicateName(name) == true) { throw new InvalidOperationException("A filename by that type already exists"); } } if (originalGist?.Files.ContainsKey(key) == true) { model.Files[key] = new GistFileUpdate { Content = content, NewFileName = name } } ; else { model.Files.Remove(key); model.Files[name] = new GistFileUpdate { Content = content }; } }; weakCtrl.Get()?.NavigationController.PushViewController(createController, true); })); }
protected void UpdateView() { ICollection <Section> sections = new LinkedList <Section>(); var section = new Section(); sections.Add(section); var desc = new MultilinedElement("Description", ViewModel.Description); desc.Clicked.Subscribe(_ => ChangeDescription()); section.Add(desc); var pub = new BooleanElement("Public", ViewModel.Public); pub.Changed.Subscribe(x => ViewModel.Public = x); section.Add(pub); var fileSection = new Section(); sections.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 StringElement(file, size + " bytes", UITableViewCellStyle.Subtitle) { Accessory = UITableViewCellAccessory.DisclosureIndicator }; el.Clicked.Subscribe(_ => { if (!ViewModel.Files.ContainsKey(key)) { return; } var createController = new GistFileEditViewController { Filename = key, Content = ViewModel.Files[key] }; createController.SaveCommand.Subscribe(__ => NavigationController.PopToViewController(this, true)); 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); } var add = new StringElement("Add New File"); add.Clicked.Subscribe(_ => AddFile()); fileSection.Add(add); Root.Reset(sections); }
protected void UpdateView() { ICollection<Section> sections = new LinkedList<Section>(); var section = new Section(); sections.Add(section); var desc = new MultilinedElement("Description", ViewModel.Description); desc.Clicked.Subscribe(_ => ChangeDescription()); section.Add(desc); var pub = new BooleanElement("Public", ViewModel.Public); pub.Changed.Subscribe(x => ViewModel.Public = x); section.Add(pub); var fileSection = new Section(); sections.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 StringElement(file, size + " bytes", UITableViewCellStyle.Subtitle) { Accessory = UITableViewCellAccessory.DisclosureIndicator }; el.Clicked.Subscribe(_ => { if (!ViewModel.Files.ContainsKey(key)) return; var createController = new GistFileEditViewController { Filename = key, Content = ViewModel.Files[key] }; createController.SaveCommand.Subscribe(__ => NavigationController.PopToViewController(this, true)); 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); } var add = new StringElement("Add New File"); add.Clicked.Subscribe(_ => AddFile()); fileSection.Add(add); Root.Reset(sections); }