PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MarkPad/DocumentSources/MetaWeblog/OpenFromWebViewModel.cs

https://github.com/bcott/DownmarkerWPF
C# | 133 lines | 110 code | 23 blank | 0 comment | 7 complexity | 0ddadb38eb65d4c37eb6fddd5c2a7a7d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Caliburn.Micro;
  7. using MarkPad.DocumentSources.GitHub;
  8. using MarkPad.DocumentSources.MetaWeblog.Service;
  9. using MarkPad.Helpers;
  10. using MarkPad.Infrastructure.Abstractions;
  11. using MarkPad.Infrastructure.DialogService;
  12. using MarkPad.Settings.Models;
  13. namespace MarkPad.DocumentSources.MetaWeblog
  14. {
  15. public class OpenFromWebViewModel : Screen
  16. {
  17. private readonly IDialogService dialogService;
  18. private readonly Func<string, IMetaWeblogService> getMetaWeblog;
  19. private readonly ITaskSchedulerFactory taskScheduler;
  20. readonly IGithubApi github;
  21. public OpenFromWebViewModel(
  22. IDialogService dialogService,
  23. Func<string, IMetaWeblogService> getMetaWeblog,
  24. ITaskSchedulerFactory taskScheduler, IGithubApi github)
  25. {
  26. this.dialogService = dialogService;
  27. this.getMetaWeblog = getMetaWeblog;
  28. this.taskScheduler = taskScheduler;
  29. this.github = github;
  30. }
  31. public void InitializeBlogs(List<BlogSetting> blogs)
  32. {
  33. Blogs = blogs;
  34. SelectedBlog = blogs.FirstOrDefault();
  35. }
  36. public List<BlogSetting> Blogs { get; private set; }
  37. public BlogSetting SelectedBlog { get; set; }
  38. public Post SelectedPost { get; set; }
  39. public Entry CurrentPost
  40. {
  41. get
  42. {
  43. return new Entry { Key = SelectedPost.title, Value = SelectedPost };
  44. }
  45. set
  46. {
  47. SelectedPost = value.Value;
  48. }
  49. }
  50. public ObservableCollection<Entry> Posts { get; private set; }
  51. public bool CanFetch { get { return SelectedBlog != null; } }
  52. protected override void OnActivate()
  53. {
  54. base.OnActivate();
  55. if (CanFetch)
  56. {
  57. Fetch();
  58. }
  59. }
  60. public bool CanContinue
  61. {
  62. get { return !string.IsNullOrWhiteSpace(CurrentPost.Key); }
  63. }
  64. public bool IsFetching { get; private set; }
  65. public void Continue()
  66. {
  67. TryClose(true);
  68. }
  69. public void Cancel()
  70. {
  71. TryClose(false);
  72. }
  73. public Task Fetch()
  74. {
  75. Posts = new ObservableCollection<Entry>();
  76. IsFetching = true;
  77. Task<Post[]> fetchingTask;
  78. if (SelectedBlog.WebSourceType == WebSourceType.MetaWebLog)
  79. {
  80. var proxy = getMetaWeblog(SelectedBlog.WebAPI);
  81. fetchingTask = proxy.GetRecentPostsAsync(SelectedBlog, 100);
  82. }
  83. else
  84. {
  85. fetchingTask = github.FetchFiles(SelectedBlog.Username, SelectedBlog.WebAPI, SelectedBlog.BlogInfo.blogid, SelectedBlog.Token);
  86. }
  87. return fetchingTask
  88. .ContinueWith(UpdateBlogPosts, taskScheduler.FromCurrentSynchronisationContext())
  89. .ContinueWith(HandleFetchError)
  90. .ContinueWith(t => IsFetching = false);
  91. }
  92. private void UpdateBlogPosts(Task<Post[]> t)
  93. {
  94. t.PropagateExceptions();
  95. foreach (var p in t.Result)
  96. {
  97. Posts.Add(new Entry { Key = p.title, Value = p });
  98. }
  99. var topPost = Posts.FirstOrDefault();
  100. if (topPost != null)
  101. CurrentPost = topPost;
  102. }
  103. private void HandleFetchError(Task t)
  104. {
  105. if (!t.IsFaulted)
  106. return;
  107. dialogService.ShowError("Markpad", "There was a problem contacting the website. Check the settings and try again.", t.Exception.GetErrorMessage());
  108. }
  109. }
  110. }