Skip to content

Commit

Permalink
Merge pull request #41 from jeremyknight-me/di
Browse files Browse the repository at this point in the history
add dependency injection
  • Loading branch information
jeremyknight-me authored Oct 29, 2023
2 parents 18aec3e + d50e22a commit 5ac94d0
Show file tree
Hide file tree
Showing 58 changed files with 689 additions and 625 deletions.
76 changes: 0 additions & 76 deletions PhotoCollage.Common/Data/FileSystemPhotoRepositoryBase.cs

This file was deleted.

24 changes: 0 additions & 24 deletions PhotoCollage.Common/Data/OrderedFileSystemPhotoRepository.cs

This file was deleted.

15 changes: 0 additions & 15 deletions PhotoCollage.Common/Data/PhotoRepositoryFactory.cs

This file was deleted.

50 changes: 0 additions & 50 deletions PhotoCollage.Common/Data/RandomFileSystemPhotoRepository.cs

This file was deleted.

9 changes: 0 additions & 9 deletions PhotoCollage.Common/Enums/BorderType.cs

This file was deleted.

7 changes: 0 additions & 7 deletions PhotoCollage.Common/Enums/FullScreenMode.cs

This file was deleted.

10 changes: 0 additions & 10 deletions PhotoCollage.Common/Enums/ScreensaverSpeed.cs

This file was deleted.

10 changes: 0 additions & 10 deletions PhotoCollage.Common/IPhotoRepository.cs

This file was deleted.

7 changes: 0 additions & 7 deletions PhotoCollage.Common/ISettingsRepository.cs

This file was deleted.

56 changes: 56 additions & 0 deletions PhotoCollage.Common/Photos/FileSystem/FileSystemPhotoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using PhotoCollage.Common.Settings;

namespace PhotoCollage.Common.Photos.FileSystem;

public sealed class FileSystemPhotoRepository : IPhotoRepository
{
private readonly IPhotoPathRepository photoPathRepo;
private readonly ISettingsRepository settingsRepo;

public FileSystemPhotoRepository(
IPhotoPathRepository photoPathRepository,
ISettingsRepository settingsRepository)
{
this.photoPathRepo = photoPathRepository;
this.settingsRepo = settingsRepository;
}

public void LoadPhotoPaths()
{
var files = Directory.EnumerateFiles(this.settingsRepo.Current.Directory, "*", SearchOption.AllDirectories);
var paths = this.GetPathsWithExtension(files);
this.photoPathRepo.LoadPaths(paths);
}

private IEnumerable<string> GetPathsWithExtension(IEnumerable<string> files)
{
var extensions = new HashSet<string> { ".jpg", ".jpeg", ".png" };
var length = this.settingsRepo.Current.Directory.Length;
var paths = new ConcurrentQueue<string>();
var exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(files, file =>
{
try
{
var fileExtension = Path.GetExtension(file);
if (extensions.Contains(fileExtension, StringComparer.OrdinalIgnoreCase))
{
var path = file.Remove(0, length).TrimStart(new[] { '\\' });
paths.Enqueue(path);
}
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}
});

return exceptions.IsEmpty
? paths
: throw new AggregateException(exceptions);
}
}
8 changes: 8 additions & 0 deletions PhotoCollage.Common/Photos/IPhotoPathRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PhotoCollage.Common.Photos;

public interface IPhotoPathRepository
{
bool HasPhotos { get; }
string GetNextPath();
void LoadPaths(IEnumerable<string> paths);
}
6 changes: 6 additions & 0 deletions PhotoCollage.Common/Photos/IPhotoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PhotoCollage.Common.Photos;

public interface IPhotoRepository
{
void LoadPhotoPaths();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using PhotoCollage.Common.Settings;

namespace PhotoCollage.Common.Photos.InMemory;

public sealed class InMemoryOrderedPhotoPathRepository : InMemoryPhotoPathRepositoryBase
{
public InMemoryOrderedPhotoPathRepository(ISettingsRepository settingsRepository)
: base(settingsRepository)
{
}

public override void LoadPaths(IEnumerable<string> paths)
=> this.LoadPathsIntoQueue(paths);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Concurrent;
using System.IO;
using PhotoCollage.Common.Settings;

namespace PhotoCollage.Common.Photos.InMemory;

public abstract class InMemoryPhotoPathRepositoryBase : IPhotoPathRepository
{
private readonly ISettingsRepository settingsRepo;

protected InMemoryPhotoPathRepositoryBase(ISettingsRepository settingsRepository)
{
this.settingsRepo = settingsRepository;
}

public bool HasPhotos => !this.Paths.IsEmpty;

protected ConcurrentBag<string> DisplayedPaths { get; } = new();
protected ConcurrentQueue<string> Paths { get; } = new();

public string GetNextPath()
{
if (!this.Paths.TryDequeue(out var path))
{
this.LoadPaths(this.DisplayedPaths);
this.DisplayedPaths.Clear();
this.Paths.TryDequeue(out path);
}

this.DisplayedPaths.Add(path);
return Path.Combine(this.settingsRepo.Current.Directory, path);
}

public abstract void LoadPaths(IEnumerable<string> paths);

protected void LoadPathsIntoQueue(IEnumerable<string> paths)
{
this.Paths.Clear();
foreach (var path in paths)
{
this.Paths.Enqueue(path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Linq;
using PhotoCollage.Common.Settings;

namespace PhotoCollage.Common.Photos.InMemory;

public sealed class InMemoryRandomPhotoPathRepository : InMemoryPhotoPathRepositoryBase
{
public InMemoryRandomPhotoPathRepository(ISettingsRepository settingsRepository)
: base(settingsRepository)
{
}

public override void LoadPaths(IEnumerable<string> paths)
{
var randomizedPaths = paths.OrderBy(item => Random.Shared.Next());
this.LoadPathsIntoQueue(randomizedPaths);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
namespace PhotoCollage.Common.Enums;
namespace PhotoCollage.Common.Settings;

public enum BorderType
{
None,
Border,
BorderHeader,
BorderFooter
}

public static class BorderTypeHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using PhotoCollage.Common.Enums;
namespace PhotoCollage.Common.Settings;

namespace PhotoCollage.Common;

public class CollageSettings
public sealed class CollageSettings
{
public string Directory { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
public bool IsGrayscale { get; set; } = false;
Expand Down
Loading

0 comments on commit 5ac94d0

Please sign in to comment.