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 pathAddSourceViewController.cs
More file actions
142 lines (118 loc) · 4.82 KB
/
AddSourceViewController.cs
File metadata and controls
142 lines (118 loc) · 4.82 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using UIKit;
using CodeHub.iOS.Utilities;
using System.Threading.Tasks;
using CodeHub.iOS.Services;
using CodeHub.iOS.DialogElements;
using CodeHub.iOS.TableViewSources;
using CodeHub.Core.Services;
using Splat;
using System.Reactive.Subjects;
using System.Reactive.Linq;
namespace CodeHub.iOS.ViewControllers.Source
{
public class AddSourceViewController : TableViewController
{
private readonly IApplicationService _applicationService;
private readonly string _username;
private readonly string _repository;
private readonly string _path;
private readonly string _branch;
private readonly DummyInputElement _titleElement;
private readonly ExpandingInputElement _descriptionElement;
private readonly ISubject<Octokit.RepositoryContentChangeSet> _successSubject
= new Subject<Octokit.RepositoryContentChangeSet>();
public IObservable<Octokit.RepositoryContentChangeSet> Success => _successSubject.AsObservable();
public AddSourceViewController(
string username,
string repository,
string path,
string branch,
IApplicationService applicationService = null)
: base(UITableViewStyle.Plain)
{
_username = username;
_repository = repository;
_path = path;
_branch = branch;
_applicationService = applicationService ?? Locator.Current.GetService<IApplicationService>();
_titleElement = new DummyInputElement("Name") { SpellChecking = false };
_descriptionElement = new ExpandingInputElement("Content")
{
SpellChecking = false,
Font = UIFont.FromName("Courier", UIFont.PreferredBody.PointSize)
};
EdgesForExtendedLayout = UIRectEdge.None;
Title = "Add File";
var commitButton = new UIBarButtonItem { Title = "Commit" };
NavigationItem.RightBarButtonItem = commitButton;
this.OnActivation(d =>
{
d(commitButton
.GetClickedObservable()
.Subscribe(_ => Commit()));
d(_titleElement
.Changed
.Select(x => x.Length != 0)
.Subscribe(x => commitButton.Enabled = x));
});
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new DialogTableViewSource(TableView);
source.Root.Add(new Section { _titleElement, _descriptionElement });
TableView.Source = source;
TableView.TableFooterView = new UIView();
}
private void Commit()
{
var content = _descriptionElement.Value;
var composer = new Composer
{
Title = "Commit Message",
Text = "Create " + _titleElement.Value
};
composer
.SendItem
.GetClickedObservable()
.Subscribe(_ => CommitThis(composer.Text).ToBackground());
this.PushViewController(composer);
}
private async Task CommitThis(string message)
{
var content = _descriptionElement.Value;
var name = _titleElement.Value;
var path = string.IsNullOrEmpty(_path) ? name : $"{_path.TrimEnd('/')}/{name}";
var hud = this.CreateHud();
try
{
hud.Show("Committing...");
UIApplication.SharedApplication.BeginIgnoringInteractionEvents();
NetworkActivity.PushNetworkActive();
var encodedPath = path == null ? null : System.Net.WebUtility.UrlEncode(path);
var result = await _applicationService.GitHubClient.Repository.Content.CreateFile(
_username, _repository, encodedPath, new Octokit.CreateFileRequest(message, content, _branch));
this.PresentingViewController?.DismissViewController(true, null);
_successSubject.OnNext(result);
}
catch (Octokit.ApiException ex)
{
var errorMessage = ex.Message;
if (ex.ApiError?.DocumentationUrl == "https://developer.github.com/v3/repos/contents/#update-a-file")
errorMessage = "A file with that name already exists!";
AlertDialogService.ShowAlert("Error", errorMessage);
}
catch (Exception ex)
{
AlertDialogService.ShowAlert("Error", ex.Message);
}
finally
{
UIApplication.SharedApplication.EndIgnoringInteractionEvents();
NetworkActivity.PopNetworkActive();
hud.Hide();
}
}
}
}