/src/MarkPad/DocumentSources/MetaWeblog/OpenFromWebViewModel.cs
C# | 133 lines | 110 code | 23 blank | 0 comment | 7 complexity | 0ddadb38eb65d4c37eb6fddd5c2a7a7d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- using Caliburn.Micro;
- using MarkPad.DocumentSources.GitHub;
- using MarkPad.DocumentSources.MetaWeblog.Service;
- using MarkPad.Helpers;
- using MarkPad.Infrastructure.Abstractions;
- using MarkPad.Infrastructure.DialogService;
- using MarkPad.Settings.Models;
- namespace MarkPad.DocumentSources.MetaWeblog
- {
- public class OpenFromWebViewModel : Screen
- {
- private readonly IDialogService dialogService;
- private readonly Func<string, IMetaWeblogService> getMetaWeblog;
- private readonly ITaskSchedulerFactory taskScheduler;
- readonly IGithubApi github;
- public OpenFromWebViewModel(
- IDialogService dialogService,
- Func<string, IMetaWeblogService> getMetaWeblog,
- ITaskSchedulerFactory taskScheduler, IGithubApi github)
- {
- this.dialogService = dialogService;
- this.getMetaWeblog = getMetaWeblog;
- this.taskScheduler = taskScheduler;
- this.github = github;
- }
- public void InitializeBlogs(List<BlogSetting> blogs)
- {
- Blogs = blogs;
- SelectedBlog = blogs.FirstOrDefault();
- }
- public List<BlogSetting> Blogs { get; private set; }
- public BlogSetting SelectedBlog { get; set; }
- public Post SelectedPost { get; set; }
- public Entry CurrentPost
- {
- get
- {
- return new Entry { Key = SelectedPost.title, Value = SelectedPost };
- }
- set
- {
- SelectedPost = value.Value;
- }
- }
- public ObservableCollection<Entry> Posts { get; private set; }
- public bool CanFetch { get { return SelectedBlog != null; } }
- protected override void OnActivate()
- {
- base.OnActivate();
- if (CanFetch)
- {
- Fetch();
- }
- }
- public bool CanContinue
- {
- get { return !string.IsNullOrWhiteSpace(CurrentPost.Key); }
- }
- public bool IsFetching { get; private set; }
- public void Continue()
- {
- TryClose(true);
- }
- public void Cancel()
- {
- TryClose(false);
- }
- public Task Fetch()
- {
- Posts = new ObservableCollection<Entry>();
- IsFetching = true;
- Task<Post[]> fetchingTask;
- if (SelectedBlog.WebSourceType == WebSourceType.MetaWebLog)
- {
- var proxy = getMetaWeblog(SelectedBlog.WebAPI);
- fetchingTask = proxy.GetRecentPostsAsync(SelectedBlog, 100);
- }
- else
- {
- fetchingTask = github.FetchFiles(SelectedBlog.Username, SelectedBlog.WebAPI, SelectedBlog.BlogInfo.blogid, SelectedBlog.Token);
- }
- return fetchingTask
- .ContinueWith(UpdateBlogPosts, taskScheduler.FromCurrentSynchronisationContext())
- .ContinueWith(HandleFetchError)
- .ContinueWith(t => IsFetching = false);
- }
- private void UpdateBlogPosts(Task<Post[]> t)
- {
- t.PropagateExceptions();
- foreach (var p in t.Result)
- {
- Posts.Add(new Entry { Key = p.title, Value = p });
- }
- var topPost = Posts.FirstOrDefault();
- if (topPost != null)
- CurrentPost = topPost;
- }
- private void HandleFetchError(Task t)
- {
- if (!t.IsFaulted)
- return;
- dialogService.ShowError("Markpad", "There was a problem contacting the website. Check the settings and try again.", t.Exception.GetErrorMessage());
- }
- }
- }