-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b35e2a0
commit 17802ae
Showing
11 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/SharpDockerizer.AppLayer/Contracts/IRecentlyOpenedSolutionsService.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 SharpDockerizer.AppLayer.Models; | ||
|
||
namespace SharpDockerizer.AppLayer.Contracts; | ||
public interface IRecentlyOpenedSolutionsService | ||
{ | ||
/// <summary> | ||
/// Returns a list of recently opened solutions. | ||
/// </summary> | ||
List<RecentlyOpenedSolution> GetSolutions(); | ||
/// <summary> | ||
/// Adds a solution to recently opened solutions, or moves it to top of the list. | ||
/// </summary> | ||
Task Add(RecentlyOpenedSolution solution); | ||
/// <summary> | ||
/// Removes a solution from recently opened solutions. | ||
/// </summary> | ||
Task RemoveWithPath(string path); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/SharpDockerizer.AppLayer/Models/RecentlyOpenedSolution.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,12 @@ | ||
namespace SharpDockerizer.AppLayer.Models; | ||
public class RecentlyOpenedSolution | ||
{ | ||
/// <summary> | ||
/// Solution name | ||
/// </summary> | ||
public required string Name { get; set; } | ||
/// <summary> | ||
/// Path to solution .sln file | ||
/// </summary> | ||
public required string AbsolutePath { get; set; } | ||
} |
83 changes: 83 additions & 0 deletions
83
src/SharpDockerizer.AppLayer/Services/Solution/RecentlyOpenedSolutionsService.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,83 @@ | ||
using SharpDockerizer.AppLayer.Contracts; | ||
using SharpDockerizer.AppLayer.Models; | ||
using System.Text; | ||
|
||
namespace SharpDockerizer.AppLayer.Services.Solution; | ||
public class RecentlyOpenedSolutionsService : IRecentlyOpenedSolutionsService | ||
{ | ||
|
||
private const string saveFileName = "recentsolutions"; | ||
private string SaveFilePath { get => Path.Combine(AppContext.BaseDirectory, saveFileName); } | ||
public List<RecentlyOpenedSolution> GetSolutions() | ||
{ | ||
List<RecentlyOpenedSolution> result = new List<RecentlyOpenedSolution>(); | ||
// If doesn't exist - then return empty list. | ||
if (!File.Exists(SaveFilePath)) | ||
{ | ||
return result; | ||
} | ||
|
||
// Read file and parse it | ||
try | ||
{ | ||
var solutions = File.ReadAllLines(SaveFilePath); | ||
foreach (var solution in solutions) | ||
{ | ||
var parts = solution.Split(';'); | ||
result.Add(new RecentlyOpenedSolution() | ||
{ | ||
Name = $"{parts[0]} ({parts[1]})", | ||
AbsolutePath = parts[1], | ||
}); | ||
} | ||
|
||
return result; | ||
} | ||
catch | ||
{ | ||
// Cache file could be damaged. Remove it as we don't know what is really broken in it. | ||
File.Delete(SaveFilePath); | ||
return new List<RecentlyOpenedSolution>(); | ||
} | ||
} | ||
|
||
|
||
public async Task Add(RecentlyOpenedSolution solution) | ||
{ | ||
// First we get what is already saved on disk | ||
var recentSolutions = GetSolutions(); | ||
|
||
// If this solution was already opened - remove it from list | ||
var maybeAddedIndex = recentSolutions.FindIndex(x => x.AbsolutePath == solution.AbsolutePath); | ||
if (maybeAddedIndex != -1) | ||
recentSolutions.RemoveAt(maybeAddedIndex); | ||
|
||
// Insert new solution at start of the list and save | ||
recentSolutions.Insert(0, solution); | ||
await Save(recentSolutions); | ||
} | ||
|
||
public async Task RemoveWithPath(string path) | ||
{ | ||
var recentSolutions = GetSolutions(); | ||
var maybeAddedIndex = recentSolutions.FindIndex(x => x.AbsolutePath == path); | ||
if (maybeAddedIndex != -1) | ||
recentSolutions.RemoveAt(maybeAddedIndex); | ||
|
||
await Save(recentSolutions); | ||
} | ||
|
||
/// <summary> | ||
/// Saves recent solutions to disk. Implemented as csv file. | ||
/// </summary> | ||
private async Task Save(List<RecentlyOpenedSolution> recentlyOpenedSolutions) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
foreach (var solution in recentlyOpenedSolutions) | ||
{ | ||
sb.AppendLine($"{solution.Name};{solution.AbsolutePath}"); | ||
} | ||
|
||
await File.WriteAllTextAsync(SaveFilePath, sb.ToString()); | ||
} | ||
} |
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
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
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
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
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
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
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
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