-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from jeremyknight-me/di
add dependency injection
- Loading branch information
Showing
58 changed files
with
689 additions
and
625 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
PhotoCollage.Common/Data/OrderedFileSystemPhotoRepository.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
50 changes: 0 additions & 50 deletions
50
PhotoCollage.Common/Data/RandomFileSystemPhotoRepository.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
PhotoCollage.Common/Photos/FileSystem/FileSystemPhotoRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PhotoCollage.Common.Photos; | ||
|
||
public interface IPhotoRepository | ||
{ | ||
void LoadPhotoPaths(); | ||
} |
14 changes: 14 additions & 0 deletions
14
PhotoCollage.Common/Photos/InMemory/InMemoryOrderedPhotoPathRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
44 changes: 44 additions & 0 deletions
44
PhotoCollage.Common/Photos/InMemory/InMemoryPhotoPathRepositoryBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
PhotoCollage.Common/Photos/InMemory/InMemoryRandomPhotoPathRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 9 additions & 1 deletion
10
...oCollage.Common/Enums/BorderTypeHelper.cs → PhotoCollage.Common/Settings/BorderType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
PhotoCollage.Common/CollageSettings.cs → ...ollage.Common/Settings/CollageSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.