protected BaseUserCollectionViewModel()
        {
            Users = UsersCollection.CreateDerivedCollection(
                x => x, x => x.Login.StartsWith(SearchKeyword ?? string.Empty, StringComparison.OrdinalIgnoreCase),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            GoToUserCommand = ReactiveCommand.Create();
            GoToUserCommand.OfType <BasicUserModel>().Subscribe(x =>
            {
                var vm      = CreateViewModel <ProfileViewModel>();
                vm.Username = x.Login;
                ShowViewModel(vm);
            });
        }
Пример #2
0
        public GistViewModel(IApplicationService applicationService, IShareService shareService)
        {
            _applicationService = applicationService;
            Comments            = new ReactiveList <GistCommentModel>();

            ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null));
            ShareCommand.Subscribe(_ => shareService.ShareUrl(Gist.HtmlUrl));

            ToggleStarCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue),
                async t =>
            {
                try
                {
                    if (!IsStarred.HasValue)
                    {
                        return;
                    }
                    var request = IsStarred.Value ? _applicationService.Client.Gists[Id].Unstar() : _applicationService.Client.Gists[Id].Star();
                    await _applicationService.Client.ExecuteAsync(request);
                    IsStarred = !IsStarred.Value;
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to start gist. Please try again.", e);
                }
            });

            ForkCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var data       = await _applicationService.Client.ExecuteAsync(_applicationService.Client.Gists[Id].ForkGist());
                var forkedGist = data.Data;
                var vm         = CreateViewModel <GistViewModel>();
                vm.Id          = forkedGist.Id;
                vm.Gist        = forkedGist;
                ShowViewModel(vm);
            });

            GoToViewableFileCommand = ReactiveCommand.Create();
            GoToViewableFileCommand.OfType <GistFileModel>().Subscribe(x =>
            {
                var vm      = CreateViewModel <GistViewableFileViewModel>();
                vm.GistFile = x;
                ShowViewModel(vm);
            });

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)));
            GoToHtmlUrlCommand.Subscribe(_ => GoToUrlCommand.ExecuteIfCan(Gist.HtmlUrl));

            GoToFileSourceCommand = ReactiveCommand.Create();
            GoToFileSourceCommand.OfType <GistFileModel>().Subscribe(x =>
            {
                var vm      = CreateViewModel <GistFileViewModel>();
                vm.Id       = Id;
                vm.GistFile = x;
                vm.Filename = x.Filename;
                ShowViewModel(vm);
            });

            GoToUserCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null));
            GoToUserCommand.Subscribe(x =>
            {
                var vm      = CreateViewModel <UserViewModel>();
                vm.Username = Gist.Owner.Login;
                ShowViewModel(vm);
            });

            GoToForksCommand = ReactiveCommand.Create();

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var forceCacheInvalidation = t as bool?;
                var t1 = this.RequestModel(_applicationService.Client.Gists[Id].Get(), forceCacheInvalidation, response => Gist = response.Data);
                this.RequestModel(_applicationService.Client.Gists[Id].IsGistStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget();
                Comments.SimpleCollectionLoad(_applicationService.Client.Gists[Id].GetComments(), forceCacheInvalidation).FireAndForget();
                return(t1);
            });
        }
Пример #3
0
        public GistViewModel(IApplicationService applicationService, IShareService shareService,
                             IActionMenuService actionMenuService, IStatusIndicatorService statusIndicatorService)
        {
            Comments = new ReactiveList <GistCommentModel>();

            Title = "Gist";

            GoToUrlCommand = this.CreateUrlCommand();

            this.WhenAnyValue(x => x.Gist).Where(x => x != null && x.Files != null && x.Files.Count > 0)
            .Select(x => x.Files.First().Key).Subscribe(x =>
                                                        Title = x);

            ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null));
            ShareCommand.Subscribe(_ => shareService.ShareUrl(Gist.HtmlUrl));

            ToggleStarCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue),
                async t =>
            {
                try
                {
                    if (!IsStarred.HasValue)
                    {
                        return;
                    }
                    var request = IsStarred.Value ? applicationService.Client.Gists[Id].Unstar() : applicationService.Client.Gists[Id].Star();
                    await applicationService.Client.ExecuteAsync(request);
                    IsStarred = !IsStarred.Value;
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to start gist. Please try again.", e);
                }
            });

            ForkCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var data       = await applicationService.Client.ExecuteAsync(applicationService.Client.Gists[Id].ForkGist());
                var forkedGist = data.Data;
                var vm         = CreateViewModel <GistViewModel>();
                vm.Id          = forkedGist.Id;
                vm.Gist        = forkedGist;
                ShowViewModel(vm);
            });

            ForkCommand.IsExecuting.Subscribe(x =>
            {
                if (x)
                {
                    statusIndicatorService.Show("Forking...");
                }
                else
                {
                    statusIndicatorService.Hide();
                }
            });

            GoToEditCommand = ReactiveCommand.Create();

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)));
            GoToHtmlUrlCommand.Select(_ => Gist.HtmlUrl).Subscribe(this.ShowWebBrowser);

            GoToFileSourceCommand = ReactiveCommand.Create();
            GoToFileSourceCommand.OfType <GistFileModel>().Subscribe(x =>
            {
                var vm      = CreateViewModel <GistFileViewModel>();
                vm.Id       = Id;
                vm.GistFile = x;
                vm.Filename = x.Filename;
                ShowViewModel(vm);
            });

            GoToUserCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && x.Owner != null));
            GoToUserCommand.Subscribe(x =>
            {
                var vm      = CreateViewModel <UserViewModel>();
                vm.Username = Gist.Owner.Login;
                ShowViewModel(vm);
            });

            AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm = CreateViewModel <GistCommentViewModel>();
                vm.Id  = Id;
                vm.CommentAdded.Subscribe(Comments.Add);
                ShowViewModel(vm);
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.Gist).Select(x => x != null),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                if (Gist.Owner != null && string.Equals(applicationService.Account.Username, Gist.Owner.Login, StringComparison.OrdinalIgnoreCase))
                {
                    menu.AddButton("Edit", GoToEditCommand);
                }
                else
                {
                    menu.AddButton("Fork", ForkCommand);
                }
                menu.AddButton("Share", ShareCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                return(menu.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var forceCacheInvalidation = t as bool?;
                var t1 = this.RequestModel(applicationService.Client.Gists[Id].Get(), forceCacheInvalidation, response => Gist = response.Data);
                this.RequestModel(applicationService.Client.Gists[Id].IsGistStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget();
                Comments.SimpleCollectionLoad(applicationService.Client.Gists[Id].GetComments(), forceCacheInvalidation).FireAndForget();
                return(t1);
            });
        }