Skip to content

Commit

Permalink
Refactor: append Async to async methods
Browse files Browse the repository at this point in the history
Consistent naming conventions are important
  • Loading branch information
NickLargen committed Apr 28, 2017
1 parent bf3ee24 commit a990dc3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
32 changes: 15 additions & 17 deletions Junctionizer/Model/FolderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Junctionizer.Properties;

using Microsoft.VisualBasic.FileIO;
using Microsoft.WindowsAPICodePack.Dialogs;

using Prism.Commands;
using Prism.Mvvm;
Expand Down Expand Up @@ -112,7 +111,6 @@ public ObservableCollection<object> SelectedItems
[NotNull]
public IEnumerable<GameFolder> AllSelectedGameFolders =>
SelectedItems.Reverse().Cast<GameFolder>().Where(folder => !folder.IsBeingDeleted);

[NotNull]
public IEnumerable<GameFolder> SelectedFolders => AllSelectedGameFolders.Where(folder => !folder.IsJunction);
[NotNull]
Expand Down Expand Up @@ -194,12 +192,12 @@ private void SetNewLocationImpl(string loc)
[AutoLazy.Lazy]
public IDelegateListCommand ArchiveSelectedCommand => new DelegateListCommand<GameFolder>(
() => BothCollectionsInitialized ? SelectedFolders : Enumerable.Empty<GameFolder>(),
folder => Archive(folder).Forget());
folder => ArchiveAsync(folder).Forget());

[AutoLazy.Lazy]
public IDelegateListCommand CopySelectedCommand => new DelegateListCommand<GameFolder>(
() => BothCollectionsInitialized ? SelectedFolders : Enumerable.Empty<GameFolder>(),
folder => CorrespondingCollection.CopyFolder(folder));
folder => CorrespondingCollection.CopyFolderAsync(folder));

[AutoLazy.Lazy]
public IDelegateListCommand CreateSelectedJunctionCommand => new DelegateListCommand<GameFolder>(
Expand All @@ -209,7 +207,7 @@ private void SetNewLocationImpl(string loc)
[AutoLazy.Lazy]
public IDelegateListCommand DeleteSelectedFoldersCommand => new DelegateListCommand<GameFolder>(
() => SelectedFolders,
selectedFolder => DeleteFolderOrJunction(selectedFolder));
selectedFolder => DeleteFolderOrJunctionAsync(selectedFolder));

[AutoLazy.Lazy]
public IDelegateListCommand DeleteSelectedJunctionsCommand => new DelegateListCommand<GameFolder>(
Expand Down Expand Up @@ -239,9 +237,9 @@ public void SelectUniqueFolders()
}

[AutoLazy.Lazy]
public DelegateCommand SelectLocationCommand => new DelegateCommand(() => SelectLocation().Forget());
public DelegateCommand SelectLocationCommand => new DelegateCommand(() => SelectLocationAsync().Forget());

public async Task SelectLocation()
public async Task SelectLocationAsync()
{
var directoryInfo = await Dialogs.PromptForDirectory(Resources.SelectLocationCommand_Select_directory_containing_folders, FolderBrowserInitialLocation);
if (directoryInfo != null) FolderBrowserInitialLocation = Location = directoryInfo.FullName;
Expand All @@ -258,7 +256,7 @@ public void RefreshSizes()

foreach (var folder in Folders)
{
folder.RecalculateSize();
folder.RecalculateSizeAsync();
}
}

Expand All @@ -268,7 +266,7 @@ private void InitDirectoryWatcher()
_directoryWatcher.InternalBufferSize = 40960;
_directoryWatcher.Created += (sender, args) => {
var newFolder = new GameFolder(args.FullPath);
newFolder.ContinuoslyRecalculateSize().Forget();
newFolder.ContinuoslyRecalculateSizeAsync().Forget();
Folders.AddAsync(newFolder).Forget();
};
_directoryWatcher.Deleted += (sender, args) => {
Expand All @@ -295,12 +293,12 @@ public IEnumerable<GameFolder> GetFoldersByJunctionTarget([NotNull] DirectoryInf
return JunctionTargetsDictionary.TryGetValue(info.FullName, out var enumerable) ? enumerable : Enumerable.Empty<GameFolder>();
}

public async Task Archive([NotNull] GameFolder folder)
public async Task ArchiveAsync([NotNull] GameFolder folder)
{
var createdFolder = await CorrespondingCollection.CopyFolder(folder);
var createdFolder = await CorrespondingCollection.CopyFolderAsync(folder);
if (createdFolder != null)
{
var isFolderDeleted = await DeleteFolderOrJunction(folder);
var isFolderDeleted = await DeleteFolderOrJunctionAsync(folder);
if (isFolderDeleted) CreateJunctionTo(createdFolder);
}
}
Expand All @@ -324,7 +322,7 @@ public void CreateJunctionTo([NotNull] GameFolder junctionTarget)
}

/// <summary>Copies the provided folder to the current directory. Returns the created/overwritten folder only if the copy successfully ran to completion, null otherwise.</summary>
public Task<GameFolder> CopyFolder([NotNull] GameFolder folderToCopy)
public Task<GameFolder> CopyFolderAsync([NotNull] GameFolder folderToCopy)
{
return Task.Run(() => {
string targetDirectory = $@"{Location}\{folderToCopy.Name}";
Expand All @@ -347,22 +345,22 @@ public Task<GameFolder> CopyFolder([NotNull] GameFolder folderToCopy)
else
{
// Since a new folder isn't being created the file system watcher will not trigger size recalculation, so we do it here
overwrittenFolder.ContinuoslyRecalculateSize().Forget();
overwrittenFolder.ContinuoslyRecalculateSizeAsync().Forget();
}
}
FileSystem.CopyDirectory(folderToCopy.DirectoryInfo.FullName, targetDirectory, UIOption.AllDialogs);
var createdFolder = GetFolderByName(targetDirectoryInfo.Name);
// Send a final recalculation request in case the user had previously paused the operation, or if there was a pause while answering the prompt for replacing vs skipping duplicate files.
createdFolder.RecalculateSize();
createdFolder.RecalculateSizeAsync();
return createdFolder;
}
catch (OperationCanceledException e)
{
Debug.WriteLine(e);
// If the user cancels the folder will still be partially copied
var createdFolder = GetFolderByName(targetDirectoryInfo.Name);
createdFolder.RecalculateSize();
createdFolder.RecalculateSizeAsync();
return null;
}
catch (IOException e)
Expand All @@ -374,7 +372,7 @@ public Task<GameFolder> CopyFolder([NotNull] GameFolder folderToCopy)
}

/// <summary>Returns true on successful delete, false if user cancels operation or there is an error.</summary>
public Task<bool> DeleteFolderOrJunction([NotNull] GameFolder folderToDelete)
public Task<bool> DeleteFolderOrJunctionAsync([NotNull] GameFolder folderToDelete)
{
folderToDelete.IsBeingDeleted = true;
return Task.Run(() => {
Expand Down
12 changes: 6 additions & 6 deletions Junctionizer/Model/GameFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public GameFolder([NotNull] DirectoryInfo directory)
JunctionTarget = JunctionPoint.GetTarget(directory);
}

UpdatePropertiesFromSubdirectories().Forget();
UpdatePropertiesFromSubdirectoriesAsync().Forget();
}

[NotNull]
Expand All @@ -61,7 +61,7 @@ public GameFolder([NotNull] DirectoryInfo directory)

public void CancelSubdirectorySearch() => SafeCancelTokenSource(_propertyUpdateTokenSource);

private async Task UpdatePropertiesFromSubdirectories()
private async Task UpdatePropertiesFromSubdirectoriesAsync()
{
using (var tokenSource = new CancellationTokenSource())
{
Expand Down Expand Up @@ -123,11 +123,11 @@ private void SearchSubdirectories(CancellationToken cancellationToken)
}
}

public Task RecalculateSize() => UpdatePropertiesFromSubdirectories();
public Task RecalculateSizeAsync() => UpdatePropertiesFromSubdirectoriesAsync();

/// <summary>Periodically calls <see cref="RecalculateSize"/> until it is determined that the size is not longer changing.</summary>
/// <summary>Periodically calls <see cref="RecalculateSizeAsync"/> until it is determined that the size is not longer changing.</summary>
/// <returns></returns>
public async Task ContinuoslyRecalculateSize()
public async Task ContinuoslyRecalculateSizeAsync()
{
IsContinuoslyRecalculating = true;
long? oldSize;
Expand All @@ -137,7 +137,7 @@ public async Task ContinuoslyRecalculateSize()
Debug.WriteLine($"{DirectoryInfo.FullName} oldSize {oldSize} Size {Size}");
await Task.Delay(1500);

await UpdatePropertiesFromSubdirectories();
await UpdatePropertiesFromSubdirectoriesAsync();
} while (Size != oldSize);

IsContinuoslyRecalculating = false;
Expand Down
14 changes: 7 additions & 7 deletions Junctionizer/Model/GameFolderPairEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ private List<GameFolderPair> RemoveItems(IEnumerable<GameFolder> folders, bool i

private void Delete(GameFolderPair pair)
{
if (pair.SourceEntry != null) SourceCollection.DeleteFolderOrJunction(pair.SourceEntry);
if (pair.DestinationEntry != null) DestinationCollection.DeleteFolderOrJunction(pair.DestinationEntry);
if (pair.SourceEntry != null) SourceCollection.DeleteFolderOrJunctionAsync(pair.SourceEntry);
if (pair.DestinationEntry != null) DestinationCollection.DeleteFolderOrJunctionAsync(pair.DestinationEntry);
}


Expand All @@ -188,7 +188,7 @@ private void Delete(GameFolderPair pair)

private async Task ArchiveAsync(GameFolderPair pair)
{
if (pair.SourceEntry != null) await SourceCollection.Archive(pair.SourceEntry);
if (pair.SourceEntry != null) await SourceCollection.ArchiveAsync(pair.SourceEntry);
else if (pair.DestinationEntry?.IsJunction == false) SourceCollection.CreateJunctionTo(pair.DestinationEntry);
}

Expand All @@ -203,8 +203,8 @@ private async Task RestoreAsync(GameFolderPair gameFolderPair)
{
Debug.Assert(gameFolderPair.DestinationEntry?.IsJunction == false);

var createdFolder = await SourceCollection.CopyFolder(gameFolderPair.DestinationEntry);
if (createdFolder != null) await DestinationCollection.DeleteFolderOrJunction(gameFolderPair.DestinationEntry);
var createdFolder = await SourceCollection.CopyFolderAsync(gameFolderPair.DestinationEntry);
if (createdFolder != null) await DestinationCollection.DeleteFolderOrJunctionAsync(gameFolderPair.DestinationEntry);
}


Expand All @@ -221,12 +221,12 @@ private async Task MirrorAsync(GameFolderPair gameFolderPair)

if (gameFolderPair.DestinationEntry?.IsJunction == false)
{
await SourceCollection.CopyFolder(gameFolderPair.DestinationEntry);
await SourceCollection.CopyFolderAsync(gameFolderPair.DestinationEntry);
}
else
{
Debug.Assert(gameFolderPair.SourceEntry != null);
await DestinationCollection.CopyFolder(gameFolderPair.SourceEntry);
await DestinationCollection.CopyFolderAsync(gameFolderPair.SourceEntry);
}
}
}
Expand Down

0 comments on commit a990dc3

Please sign in to comment.