Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
Improve the application stability.
Browse files Browse the repository at this point in the history
  • Loading branch information
minidfx committed May 17, 2016
1 parent 172fd6e commit c9c03b1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 53 deletions.
6 changes: 6 additions & 0 deletions Solution/App/ViewModels/Contracts/IListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ public interface IListViewModel
StorageFile StorageFile { get; }

#endregion

#region All other members

void NotifyUi();

#endregion
}
}
34 changes: 10 additions & 24 deletions Solution/App/ViewModels/ListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ namespace BulkRenaming.ViewModels
{
public sealed class ListViewModel : ViewAware, IListViewModel
{
#region Fields

private string _futurResult;

private IEnumerable<string> _parts;

#endregion

#region Constructors

public ListViewModel(string fileName, StorageFile storageFile)
Expand All @@ -36,26 +28,20 @@ public ListViewModel(string fileName, StorageFile storageFile)

public StorageFile StorageFile { get; }

public string FuturResult
{
get { return this._futurResult; }
set
{
this._futurResult = value;
this.NotifyOfPropertyChange(() => this.FuturResult);
}
}
public string FuturResult { get; set; }

public Match RegexMatch { get; set; }

public IEnumerable<string> Parts
public IEnumerable<string> Parts { get; set; }

#endregion

#region Interface Implementations

public void NotifyUi()
{
get { return this._parts; }
set
{
this._parts = value;
this.NotifyOfPropertyChange(() => this.Parts);
}
this.NotifyOfPropertyChange(() => this.Parts);
this.NotifyOfPropertyChange(() => this.FuturResult);
}

#endregion
Expand Down
78 changes: 49 additions & 29 deletions Solution/App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace BulkRenaming.ViewModels
{
public sealed class ShellViewModel : Screen, IShellViewModel
{
#region Fields and Constants

private static readonly ILog Logger = LogManager.GetLog(typeof (ShellViewModel));

#endregion

#region Fields

private readonly CompositeDisposable _disposables = new CompositeDisposable();
Expand Down Expand Up @@ -76,17 +82,28 @@ private set

public async Task ApplyAsync()
{
this.IsLoading = true;

using (Disposable.Create(() => this.IsLoading = false))
try
{
foreach (var file in this.Files.Where(x => x.FuturResult != null))
this.IsLoading = true;

using (Disposable.Create(() => this.IsLoading = false))
{
await file.StorageFile.RenameAsync(file.FuturResult);
}
var actualFiles = await this._folderSelected.GetFilesAsync();

await this.FetchFolderAsync();
await this.CalculateRegexAsync();
foreach (var file in this.Files.AsParallel()
.Where(x => x.FuturResult != null &&
actualFiles.Any(f => f.Path == x.StorageFile.Path)))
{
await file.StorageFile.RenameAsync(file.FuturResult);
}

await this.FetchFolderAsync();
this.CalculateRegex();
}
}
catch (Exception e)
{
Logger.Error(e);
}
}

Expand All @@ -113,7 +130,7 @@ public async Task BrowseAsync()
this.FolderSelected = this._folderSelected.Path;
this.NotifyOfPropertyChange(() => this.FolderSelected);

await this.CalculateRegexAsync();
this.CalculateRegex();
}
}

Expand Down Expand Up @@ -148,46 +165,50 @@ protected override void OnActivate()
this._disposables.Add(this.Pattern
.Throttle(TimeSpan.FromMilliseconds(200))
.Select(x => string.IsNullOrWhiteSpace(x) ? null : x)
.Do(async pattern => await this.CalculateRegexAsync(pattern))
.Do(this.CalculateRegex)
.Merge(this.ReplacePattern
.Throttle(TimeSpan.FromMilliseconds(200))
.Select(x => string.IsNullOrWhiteSpace(x) ? null : x)
.Do(async replacePattern => await this.CalculateReplacePatternAsync(replacePattern)))
.Do(this.CalculateReplacePattern))
.SubscribeOn(TaskPoolScheduler.Default)
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe());
.ObserveOn(UIDispatcherScheduler.Default)
.Subscribe(x =>
{
foreach (var file in this.Files)
{
file.NotifyUi();
}

this.NotifyOfPropertyChange(() => this.CanApplyAsync);
},
e => { Logger.Error(e); }));
}

private Task CalculateReplacePatternAsync()
private void CalculateReplacePattern()
{
return this.CalculateReplacePatternAsync(this.ReplacePattern.Value);
this.CalculateReplacePattern(this.ReplacePattern.Value);
}

private async Task CalculateReplacePatternAsync(string replacePattern)
private void CalculateReplacePattern(string replacePattern)
{
// When the replace pattern is specified
if (!string.IsNullOrWhiteSpace(replacePattern))
{
var increment = 1;
var files = await this._folderSelected.GetFilesAsync();

foreach (var item in this.Files.Where(x => x.RegexMatch.Success))
{
try
{
var match = item.RegexMatch;
var futurResult = match.Result(replacePattern).Replace("%i", increment++.ToString());
var fileExists = files.Any(x => x.Name.Equals(futurResult));

item.FuturResult = fileExists ? null : futurResult;
item.FuturResult = match.Result(replacePattern).Replace("%i", increment++.ToString());
}
catch (ArgumentException)
{
}
}

this.CanApplyAsync = this.Files.Any(x => x.RegexMatch.Success) && this.ReplacePattern.Value != null;
this.NotifyOfPropertyChange(() => this.CanApplyAsync);
}
else
{
Expand All @@ -197,16 +218,15 @@ private async Task CalculateReplacePatternAsync(string replacePattern)
}

this.CanApplyAsync = false;
this.NotifyOfPropertyChange(() => this.CanApplyAsync);
}
}

private Task CalculateRegexAsync()
private void CalculateRegex()
{
return this.CalculateRegexAsync(this.Pattern.Value);
this.CalculateRegex(this.Pattern.Value);
}

private Task CalculateRegexAsync(string pattern)
private void CalculateRegex(string pattern)
{
if (pattern == null)
{
Expand All @@ -216,7 +236,7 @@ private Task CalculateRegexAsync(string pattern)
file.FuturResult = null;
}

return Task.FromResult(0);
return;
}

Regex regex;
Expand All @@ -232,7 +252,7 @@ private Task CalculateRegexAsync(string pattern)
item.Parts = new[] {item.FileName};
}

return Task.FromResult(0);
return;
}

// Items with a match
Expand All @@ -253,7 +273,7 @@ private Task CalculateRegexAsync(string pattern)
: new[] {item.FileName};
}

return this.CalculateReplacePatternAsync();
this.CalculateReplacePattern();
}

/// <summary>
Expand Down

0 comments on commit c9c03b1

Please sign in to comment.