This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathGistFileModifyViewController.cs
More file actions
92 lines (72 loc) · 2.84 KB
/
GistFileModifyViewController.cs
File metadata and controls
92 lines (72 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using CodeHub.iOS.DialogElements;
using ReactiveUI;
using UIKit;
using System.Reactive;
using System.Reactive.Linq;
using CodeHub.iOS.Services;
using System.Threading.Tasks;
namespace CodeHub.iOS.ViewControllers.Gists
{
public class GistFileEditViewController : GistFileModifyViewController
{
}
public class GistFileAddViewController : GistFileModifyViewController
{
}
public abstract class GistFileModifyViewController : DialogViewController
{
public Action<string, string> Save;
private string _filename;
public string Filename
{
get { return _filename; }
set { this.RaiseAndSetIfChanged(ref _filename, value); }
}
private string _content;
public string Content
{
get { return _content; }
set { this.RaiseAndSetIfChanged(ref _content, value); }
}
public ReactiveCommand<Unit, Unit> SaveCommand { get; }
protected GistFileModifyViewController()
: base(UITableViewStyle.Plain)
{
SaveCommand = ReactiveCommand.CreateFromTask(t => {
if (String.IsNullOrEmpty(Content))
throw new Exception("You cannot save a file without content!");
Save?.Invoke(Filename, Content);
return Task.FromResult(Unit.Default);
});
SaveCommand.ThrownExceptions.Subscribe(x => AlertDialogService.ShowAlert("Error", x.Message));
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var titleElement = new DummyInputElement("Title") { SpellChecking = false };
var contentElement = new ExpandingInputElement("Content");
Root.Add(new Section { titleElement, contentElement });
TableView.TableFooterView = new UIView();
var saveButton = new UIBarButtonItem(UIBarButtonSystemItem.Save);
NavigationItem.RightBarButtonItem = saveButton;
OnActivation(d =>
{
d(this.Bind(x => x.Filename, true).Subscribe(x =>
{
Title = string.IsNullOrEmpty(x) ? "Gist File" : x;
titleElement.Value = x;
}));
d(titleElement.Changed.Subscribe(x => Filename = x));
d(this.Bind(x => x.Content, true).Subscribe(x => contentElement.Value = x));
d(contentElement.Changed.Subscribe(x => Content = x));
d(SaveCommand.Subscribe(_ => ResignFirstResponder()));
d(saveButton.GetClickedObservable()
.Select(_ => Unit.Default)
.InvokeReactiveCommand(SaveCommand));
d(SaveCommand.IsExecuting
.Subscribe(x => saveButton.Enabled = !x));
});
}
}
}