Skip to content

Instantly share code, notes, and snippets.

@pornganizer
Last active September 30, 2016 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pornganizer/036378dc775515941953b83baa4deaca to your computer and use it in GitHub Desktop.
Save pornganizer/036378dc775515941953b83baa4deaca to your computer and use it in GitHub Desktop.
public List<FolderVm> GetFolderVms<TItemVm>(ObservableCollection<FolderVm> folders, ObservableCollection<TItemVm> potentialMatches) where TItemVm : IItemVm
{
var matchingFolders = new List<FolderVm>();
foreach (var folder in folders)
{
var directoryName = new DirectoryInfo(folder.Header).Name;
var matchingWebsite = potentialMatches.FirstOrDefault(x => string.Equals(x.Header, directoryName, StringComparison.CurrentCultureIgnoreCase));
if (matchingWebsite != null)
matchingFolders.Add(folder);
matchingFolders.AddRange(GetFolderVms(folder.Folders, potentialMatches));
}
return matchingFolders;
}
private void Organize(object obj)
{
var moveActions = new Stack<KeyValuePair<string, Func<List<FolderVm>>>>();
moveActions.Push(new KeyValuePair<string, Func<List<FolderVm>>>("MoveByCategory", () =>
{
return GetFolderVms(FoldersVm.Instance.Items, Categories);
}));
moveActions.Push(new KeyValuePair<string, Func<List<FolderVm>>>("MoveByStar", () =>
{
return GetFolderVms(FoldersVm.Instance.Items, Stars);
}));
moveActions.Push(new KeyValuePair<string, Func<List<FolderVm>>>("MoveByWebsite", () =>
{
return GetFolderVms(FoldersVm.Instance.Items, Websites);
}));
while (moveActions.Count > 0)
{
var moveAction = moveActions.Pop();
var folders = moveAction.Value.Invoke();
var folder = folders.FirstOrDefault();
if (folder != null)
{
var newPath = System.IO.Path.Combine(folder.Header, Header) + System.IO.Path.GetExtension(Path);
try
{
if (Path != newPath)
{
FileSystem.MoveFile(Path, newPath, UIOption.AllDialogs);
Path = newPath;
}
}
catch (Exception)
{
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment