-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow saving with default folder & naming
- Loading branch information
1 parent
57d68a5
commit dd7930a
Showing
17 changed files
with
288 additions
and
102 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace FlightRecorder.Client.Logics; | ||
|
||
public interface IDialogLogic | ||
{ | ||
bool Confirm(string message); | ||
void Error(string error); | ||
Task<string?> SaveAsync(SavedData data); | ||
Task<(string? fileName, SavedData? data)> LoadAsync(); | ||
Task<string?> PickSaveFileAsync(); | ||
Task<(string filePath, Stream fileStream)?> PickOpenFileAsync(); | ||
Task<string?> PickSaveFolderAsync(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace FlightRecorder.Client.Logics; | ||
|
||
public interface IStorageLogic | ||
{ | ||
Task<SavedData?> LoadAsync(Stream file); | ||
Task SaveAsync(string filePath, SavedData data); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using ICSharpCode.SharpZipLib.Zip; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace FlightRecorder.Client.Logics; | ||
|
||
public class StorageLogic : IStorageLogic | ||
{ | ||
private readonly ILogger<StorageLogic> logger; | ||
|
||
public StorageLogic(ILogger<StorageLogic> logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
/// <param name="filePath">This must not be null or empty</param> | ||
public async Task SaveAsync(string filePath, SavedData data) | ||
{ | ||
using (var fileStream = new FileStream(filePath, FileMode.Create)) | ||
{ | ||
using var outStream = new ZipOutputStream(fileStream); | ||
|
||
outStream.SetLevel(9); | ||
|
||
var entry = new ZipEntry("data.json") | ||
{ | ||
DateTime = DateTime.Now | ||
}; | ||
outStream.PutNextEntry(entry); | ||
|
||
await JsonSerializer.SerializeAsync(outStream, data); | ||
outStream.Finish(); | ||
|
||
logger.LogDebug("Saved file into {fileName}", filePath); | ||
} | ||
} | ||
|
||
public async Task<SavedData?> LoadAsync(Stream file) | ||
{ | ||
using (file) | ||
{ | ||
using var zipFile = new ZipFile(file); | ||
|
||
foreach (ZipEntry entry in zipFile) | ||
{ | ||
if (entry.IsFile && entry.Name == "data.json") | ||
{ | ||
using var stream = zipFile.GetInputStream(entry); | ||
|
||
var result = await JsonSerializer.DeserializeAsync<SavedData>(stream); | ||
|
||
logger.LogDebug("Loaded file data from file stream"); | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
Oops, something went wrong.