Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
- Adds a blog factory and uses polymorphism to control between the
different kind of blogs in the ManagerController class.
- Improves downloader detection.
- Runs the blog addition on one task of the threadpool (Task.Run) to
mitigate ui lag during the blog addition using the Clipboard Manager.
- Defaults to .NET Framework version 4.6 as it should be available for
all supported windows versions (Windows Vista and above).
  • Loading branch information
johanneszab committed Aug 13, 2017
1 parent f5abb5b commit c0ef034
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 110 deletions.
2 changes: 1 addition & 1 deletion lib/RateLimiter/RateLimiter/RateLimiter.csproj
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Guava.RateLimiter</RootNamespace>
<AssemblyName>Guava.RateLimiter</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,12 +10,13 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>System.Waf</RootNamespace>
<AssemblyName>WpfApplicationFramework</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
11 changes: 5 additions & 6 deletions src/TumblThree/TumblThree.Applications/App.config
@@ -1,17 +1,16 @@
<?xml
version="1.0"?>
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>

<runtime>
<loadFromRemoteSources enabled="true" />
<ThrowUnobservedTaskExceptions enabled="true" />
<loadFromRemoteSources enabled="true"/>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
<!--<system.net>
<connectionManagement>
<add address="*" maxconnection="400"/>
</connectionManagement>
</system.net>-->
</configuration>
</configuration>
Expand Up @@ -51,14 +51,15 @@ internal class ManagerController

[ImportingConstructor]
public ManagerController(IShellService shellService, ISelectionService selectionService, ICrawlerService crawlerService,
IManagerService managerService, IDownloaderFactory downloaderFactory, Lazy<ManagerViewModel> managerViewModel)
IManagerService managerService, IDownloaderFactory downloaderFactory, IBlogFactory blogFactory, Lazy<ManagerViewModel> managerViewModel)
{
this.shellService = shellService;
this.selectionService = selectionService;
this.crawlerService = crawlerService;
this.managerService = managerService;
this.managerViewModel = managerViewModel;
DownloaderFactory = downloaderFactory;
BlogFactory = blogFactory;
addBlogCommand = new AsyncDelegateCommand(AddBlog, CanAddBlog);
removeBlogCommand = new DelegateCommand(RemoveBlog, CanRemoveBlog);
showFilesCommand = new DelegateCommand(ShowFiles, CanShowFiles);
Expand All @@ -80,6 +81,9 @@ private ManagerViewModel ManagerViewModel
public QueueManager QueueManager { get; set; }

public IDownloaderFactory DownloaderFactory { get; set; }

public IBlogFactory BlogFactory { get; set; }

public event BlogManagerFinishedLoadingHandler BlogManagerFinishedLoading;

public async Task Initialize()
Expand Down Expand Up @@ -266,7 +270,11 @@ private bool CanRemoveBlog()
private void RemoveBlog()
{
IBlog[] blogs = selectionService.SelectedBlogFiles.ToArray();
RemoveBlog(blogs);
}

private void RemoveBlog(IEnumerable<IBlog> blogs)
{
foreach (IBlog blog in blogs)
{
if (!shellService.Settings.DeleteOnlyIndex)
Expand Down Expand Up @@ -341,17 +349,17 @@ private async Task AddBlogAsync(string blogUrl)
{
blogUrl = crawlerService.NewBlogUrl;
}
IBlog blog;

// TODO: Dependency, SOLID!
if (Validator.IsValidTumblrUrl(blogUrl))
blog = new Blog(blogUrl, Path.Combine(shellService.Settings.DownloadLocation, "Index"), BlogTypes.tumblr);
else if (Validator.IsValidTumblrLikedByUrl(blogUrl))
blog = new TumblrLikeByBlog(blogUrl, Path.Combine(shellService.Settings.DownloadLocation, "Index"), BlogTypes.tlb);
//else if (Validator.IsValidTumblrSearchUrl(blogUrl))
// blog = new TumblrSearchBlog(blogUrl, Path.Combine(shellService.Settings.DownloadLocation, "Index"), BlogTypes.ts);
else
// TODO: Dependency, not SOLID!
IBlog blog;
try
{
blog = BlogFactory.GetBlog(blogUrl, Path.Combine(shellService.Settings.DownloadLocation, "Index"));
}
catch (ArgumentException)
{
return;
}

TransferGlobalSettingsToBlog(blog);

Expand Down Expand Up @@ -419,20 +427,20 @@ private void OnClipboardContentChanged(object sender, EventArgs e)
// Count each whitespace as new url
string[] urls = Clipboard.GetText().Split();

Task addBlogBatchedTask = AddBlogBatchedAsync(urls);
Task.Run(() => { Task addBlogBatchedTask = AddBlogBatchedAsync(urls); });
}
}

private async Task AddBlogBatchedAsync(IEnumerable<string> urls)
{
var semaphoreSlim = new SemaphoreSlim(15);
//foreach (string url in urls.Where(url => Validator.IsValidTumblrUrl(url) || Validator.IsValidTumblrLikedByUrl(url) || Validator.IsValidTumblrSearchUrl(url)))
foreach (string url in urls.Where(url => Validator.IsValidTumblrUrl(url) || Validator.IsValidTumblrLikedByUrl(url)))
var semaphoreSlim = new SemaphoreSlim(25);
IEnumerable<Task> tasks = urls.Select(async url =>
{
await semaphoreSlim.WaitAsync();
await AddBlogAsync(url);
semaphoreSlim.Release();
}
});
await Task.WhenAll(tasks);
}

private void ListenClipboard()
Expand Down
Expand Up @@ -31,7 +31,7 @@ public TumblrDownloader(IShellService shellService, CancellationToken ct, PauseT
{
}

public new async Task IsBlogOnlineAsync()
public override async Task IsBlogOnlineAsync()
{
try
{
Expand Down Expand Up @@ -327,10 +327,6 @@ protected virtual async Task<string> RequestDataAsync(string limit, string offse
}
}
}
catch
{
return string.Empty;
}
finally
{
requestRegistration.Dispose();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,10 +10,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TumblThree.Applications</RootNamespace>
<AssemblyName>TumblThree.Applications</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
11 changes: 5 additions & 6 deletions src/TumblThree/TumblThree.Domain/App.config
@@ -1,12 +1,11 @@
<?xml
version="1.0"?>
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>

<runtime>
<loadFromRemoteSources enabled="true" />
<ThrowUnobservedTaskExceptions enabled="true" />
<loadFromRemoteSources enabled="true"/>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
</configuration>
25 changes: 25 additions & 0 deletions src/TumblThree/TumblThree.Domain/Models/BlogFactory.cs
@@ -0,0 +1,25 @@
using System;
using System.ComponentModel.Composition;

namespace TumblThree.Domain.Models
{
[Export(typeof(IBlogFactory))]
public class BlogFactory : IBlogFactory
{
[ImportingConstructor]
internal BlogFactory()
{
}

public IBlog GetBlog(string blogUrl, string path)
{
if (Validator.IsValidTumblrUrl(blogUrl))
return new Blog(blogUrl, path, BlogTypes.tumblr);
if (Validator.IsValidTumblrLikedByUrl(blogUrl))
return new Blog(blogUrl, path, BlogTypes.tlb);
//if (Validator.IsValidTumblrSearchUrl(blogUrl))
// return new Blog(blogUrl, path, BlogTypes.ts);
throw new ArgumentException("Website is not supported!", nameof(blogUrl));
}
}
}
7 changes: 7 additions & 0 deletions src/TumblThree/TumblThree.Domain/Models/IBlogFactory.cs
@@ -0,0 +1,7 @@
namespace TumblThree.Domain.Models
{
public interface IBlogFactory
{
IBlog GetBlog(string blogUrl, string path);
}
}
45 changes: 18 additions & 27 deletions src/TumblThree/TumblThree.Domain/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0ef034

Please sign in to comment.