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 pathBranchesViewController.cs
More file actions
90 lines (76 loc) · 2.96 KB
/
BranchesViewController.cs
File metadata and controls
90 lines (76 loc) · 2.96 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
using System;
using CodeHub.Core.Services;
using UIKit;
using Splat;
using ReactiveUI;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using CodeHub.iOS.DialogElements;
using System.Collections.Generic;
using CodeHub.Core;
using System.Linq;
using System.Reactive;
using CodeHub.iOS.Views;
using System.Threading.Tasks;
namespace CodeHub.iOS.ViewControllers.Source
{
public class BranchesViewController : DialogViewController
{
private static string LoadErrorMessage = "Unable to load branches.";
private readonly ISubject<Octokit.Branch> _branchSubject = new Subject<Octokit.Branch>();
public IObservable<Octokit.Branch> BranchSelected => _branchSubject.AsObservable();
private ReactiveCommand<Unit, IReadOnlyList<Octokit.Branch>> LoadBranches { get; }
public BranchesViewController(
string username,
string repository,
IReadOnlyList<Octokit.Branch> branches = null,
IApplicationService applicationService = null)
: base(UITableViewStyle.Plain)
{
applicationService = applicationService ?? Locator.Current.GetService<IApplicationService>();
Title = "Branches";
LoadBranches = ReactiveCommand.CreateFromTask(() =>
{
if (branches != null)
return Task.FromResult(branches);
return applicationService.GitHubClient.Repository.Branch.GetAll(username, repository);
});
LoadBranches
.ThrownExceptions
.Do(_ => SetErrorView())
.Select(error => new UserError(LoadErrorMessage, error))
.SelectMany(Interactions.Errors.Handle)
.Subscribe();
LoadBranches
.Do(_ => TableView.TableFooterView = null)
.Subscribe(ItemsLoaded);
Appearing
.Take(1)
.Select(_ => Unit.Default)
.Do(_ => TableView.TableFooterView = new LoadingIndicatorView())
.InvokeReactiveCommand(LoadBranches);
}
private void SetErrorView()
{
var emptyListView = new EmptyListView(Octicon.GitBranch.ToEmptyListImage(), LoadErrorMessage)
{
Alpha = 0
};
TableView.TableFooterView = new UIView();
TableView.BackgroundView = emptyListView;
UIView.Animate(0.8, 0, UIViewAnimationOptions.CurveEaseIn,
() => emptyListView.Alpha = 1, null);
}
private void ItemsLoaded(IEnumerable<Octokit.Branch> branches)
{
var items = branches.Select(CreateElement);
Root.Reset(new Section { items });
}
private Element CreateElement(Octokit.Branch branch)
{
var e = new StringElement(branch.Name);
e.Clicked.Subscribe(_ => _branchSubject.OnNext(branch));
return e;
}
}
}