|
| 1 | +using System; |
| 2 | +using UIKit; |
| 3 | +using CodeHub.iOS.Utilities; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using CodeHub.iOS.Services; |
| 6 | +using CodeHub.iOS.DialogElements; |
| 7 | +using CodeHub.iOS.TableViewSources; |
| 8 | +using CodeHub.Core.Services; |
| 9 | +using Splat; |
| 10 | +using System.Reactive.Subjects; |
| 11 | +using System.Reactive.Linq; |
| 12 | + |
| 13 | +namespace CodeHub.iOS.ViewControllers.Source |
| 14 | +{ |
| 15 | + public class AddSourceViewController : TableViewController |
| 16 | + { |
| 17 | + private readonly IApplicationService _applicationService; |
| 18 | + private readonly string _username; |
| 19 | + private readonly string _repository; |
| 20 | + private readonly string _path; |
| 21 | + private readonly string _branch; |
| 22 | + |
| 23 | + private readonly DummyInputElement _titleElement; |
| 24 | + private readonly ExpandingInputElement _descriptionElement; |
| 25 | + |
| 26 | + private readonly ISubject<Octokit.RepositoryContentChangeSet> _successSubject |
| 27 | + = new Subject<Octokit.RepositoryContentChangeSet>(); |
| 28 | + |
| 29 | + public IObservable<Octokit.RepositoryContentChangeSet> Success => _successSubject.AsObservable(); |
| 30 | + |
| 31 | + public AddSourceViewController( |
| 32 | + string username, |
| 33 | + string repository, |
| 34 | + string path, |
| 35 | + string branch, |
| 36 | + IApplicationService applicationService = null) |
| 37 | + : base(UITableViewStyle.Plain) |
| 38 | + { |
| 39 | + _username = username; |
| 40 | + _repository = repository; |
| 41 | + _path = path; |
| 42 | + _branch = branch; |
| 43 | + _applicationService = applicationService ?? Locator.Current.GetService<IApplicationService>(); |
| 44 | + |
| 45 | + _titleElement = new DummyInputElement("Name") { SpellChecking = false }; |
| 46 | + _descriptionElement = new ExpandingInputElement("Content") |
| 47 | + { |
| 48 | + SpellChecking = false, |
| 49 | + Font = UIFont.FromName("Courier", UIFont.PreferredBody.PointSize) |
| 50 | + }; |
| 51 | + |
| 52 | + EdgesForExtendedLayout = UIRectEdge.None; |
| 53 | + Title = "Add File"; |
| 54 | + |
| 55 | + var commitButton = new UIBarButtonItem { Title = "Commit" }; |
| 56 | + NavigationItem.RightBarButtonItem = commitButton; |
| 57 | + |
| 58 | + this.OnActivation(d => |
| 59 | + { |
| 60 | + d(commitButton |
| 61 | + .GetClickedObservable() |
| 62 | + .Subscribe(_ => Commit())); |
| 63 | + |
| 64 | + d(_titleElement |
| 65 | + .Changed |
| 66 | + .Select(x => x.Length != 0) |
| 67 | + .Subscribe(x => commitButton.Enabled = x)); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public override void ViewDidLoad() |
| 72 | + { |
| 73 | + base.ViewDidLoad(); |
| 74 | + |
| 75 | + var source = new DialogTableViewSource(TableView); |
| 76 | + source.Root.Add(new Section { _titleElement, _descriptionElement }); |
| 77 | + TableView.Source = source; |
| 78 | + TableView.TableFooterView = new UIView(); |
| 79 | + } |
| 80 | + |
| 81 | + private void Commit() |
| 82 | + { |
| 83 | + var content = _descriptionElement.Value; |
| 84 | + |
| 85 | + var composer = new Composer |
| 86 | + { |
| 87 | + Title = "Commit Message", |
| 88 | + Text = "Create " + _titleElement.Value |
| 89 | + }; |
| 90 | + |
| 91 | + composer |
| 92 | + .SendItem |
| 93 | + .GetClickedObservable() |
| 94 | + .Subscribe(_ => CommitThis(composer.Text).ToBackground()); |
| 95 | + |
| 96 | + this.PushViewController(composer); |
| 97 | + } |
| 98 | + |
| 99 | + private async Task CommitThis(string message) |
| 100 | + { |
| 101 | + var content = _descriptionElement.Value; |
| 102 | + var name = _titleElement.Value; |
| 103 | + var path = string.IsNullOrEmpty(_path) ? name : $"{_path.TrimEnd('/')}/{name}"; |
| 104 | + var hud = this.CreateHud(); |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + hud.Show("Committing..."); |
| 109 | + UIApplication.SharedApplication.BeginIgnoringInteractionEvents(); |
| 110 | + NetworkActivity.PushNetworkActive(); |
| 111 | + |
| 112 | + var result = await _applicationService.GitHubClient.Repository.Content.CreateFile( |
| 113 | + _username, _repository, path, new Octokit.CreateFileRequest(message, content, _branch)); |
| 114 | + |
| 115 | + this.PresentingViewController?.DismissViewController(true, null); |
| 116 | + |
| 117 | + _successSubject.OnNext(result); |
| 118 | + } |
| 119 | + catch (Octokit.ApiException ex) |
| 120 | + { |
| 121 | + var errorMessage = ex.Message; |
| 122 | + if (ex.ApiError?.DocumentationUrl == "https://developer.github.com/v3/repos/contents/#update-a-file") |
| 123 | + errorMessage = "A file with that name already exists!"; |
| 124 | + |
| 125 | + AlertDialogService.ShowAlert("Error", errorMessage); |
| 126 | + } |
| 127 | + catch (Exception ex) |
| 128 | + { |
| 129 | + AlertDialogService.ShowAlert("Error", ex.Message); |
| 130 | + } |
| 131 | + finally |
| 132 | + { |
| 133 | + UIApplication.SharedApplication.EndIgnoringInteractionEvents(); |
| 134 | + NetworkActivity.PopNetworkActive(); |
| 135 | + hud.Hide(); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
0 commit comments