|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.Composition; |
| 4 | +using System.Linq; |
| 5 | +using System.Reactive.Linq; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using GitHub.App; |
| 8 | +using GitHub.Factories; |
| 9 | +using GitHub.Logging; |
| 10 | +using GitHub.Models; |
| 11 | +using ReactiveUI; |
| 12 | +using Serilog; |
| 13 | + |
| 14 | +namespace GitHub.ViewModels.Dialog |
| 15 | +{ |
| 16 | + [Export(typeof(IForkRepositorySelectViewModel))] |
| 17 | + [PartCreationPolicy(CreationPolicy.NonShared)] |
| 18 | + public class ForkRepositorySelectViewModel : ViewModelBase, IForkRepositorySelectViewModel |
| 19 | + { |
| 20 | + static readonly ILogger log = LogManager.ForContext<ForkRepositoryViewModel>(); |
| 21 | + |
| 22 | + readonly IModelServiceFactory modelServiceFactory; |
| 23 | + IReadOnlyList<IAccount> accounts; |
| 24 | + IReadOnlyList<IRemoteRepositoryModel> existingForks; |
| 25 | + bool isLoading; |
| 26 | + |
| 27 | + [ImportingConstructor] |
| 28 | + public ForkRepositorySelectViewModel(IModelServiceFactory modelServiceFactory) |
| 29 | + { |
| 30 | + this.modelServiceFactory = modelServiceFactory; |
| 31 | + Selected = ReactiveCommand.Create(); |
| 32 | + } |
| 33 | + |
| 34 | + public string Title => Resources.ForkRepositoryTitle; |
| 35 | + |
| 36 | + public IReadOnlyList<IAccount> Accounts |
| 37 | + { |
| 38 | + get { return accounts; } |
| 39 | + private set { this.RaiseAndSetIfChanged(ref accounts, value); } |
| 40 | + } |
| 41 | + |
| 42 | + public IReadOnlyList<IRemoteRepositoryModel> ExistingForks |
| 43 | + { |
| 44 | + get { return existingForks; } |
| 45 | + private set { this.RaiseAndSetIfChanged(ref existingForks, value); } |
| 46 | + } |
| 47 | + |
| 48 | + public bool IsLoading |
| 49 | + { |
| 50 | + get { return isLoading; } |
| 51 | + private set { this.RaiseAndSetIfChanged(ref isLoading, value); } |
| 52 | + } |
| 53 | + |
| 54 | + public ReactiveCommand<object> Selected { get; } |
| 55 | + |
| 56 | + public IObservable<object> Done => Selected; |
| 57 | + |
| 58 | + public async Task InitializeAsync(ILocalRepositoryModel repository, IConnection connection) |
| 59 | + { |
| 60 | + IsLoading = true; |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + var modelService = await modelServiceFactory.CreateAsync(connection); |
| 65 | + |
| 66 | + Observable.CombineLatest( |
| 67 | + modelService.GetAccounts(), |
| 68 | + modelService.GetForks(repository).ToList(), |
| 69 | + (a, f) => new { Accounts = a, Forks = f }) |
| 70 | + .Finally(() => IsLoading = false) |
| 71 | + .Subscribe(x => |
| 72 | + { |
| 73 | + Accounts = BuildAccounts(x.Accounts, x.Forks); |
| 74 | + ExistingForks = BuildExistingForks(x.Accounts, x.Forks); |
| 75 | + }); |
| 76 | + |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + log.Error(ex, "Error initializing ForkRepositoryViewModel"); |
| 81 | + IsLoading = false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + IReadOnlyList<IAccount> BuildAccounts(IReadOnlyList<IAccount> accounts, IList<IRemoteRepositoryModel> forks) |
| 86 | + { |
| 87 | + var forksByOwner = forks.ToDictionary(x => x.Owner, x => x); |
| 88 | + return accounts.Where(x => !forksByOwner.ContainsKey(x.Login)).ToList(); |
| 89 | + } |
| 90 | + |
| 91 | + IReadOnlyList<IRemoteRepositoryModel> BuildExistingForks(IReadOnlyList<IAccount> accounts, IList<IRemoteRepositoryModel> forks) |
| 92 | + { |
| 93 | + var accountsByLogin = accounts.ToDictionary(x => x.Login, x => x); |
| 94 | + return forks.Where(x => accountsByLogin.ContainsKey(x.Owner)).ToList(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments