-
-
Notifications
You must be signed in to change notification settings - Fork 222
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 #451 from TheJoeFin/SettingsService
Settings service and Modern Settings
- Loading branch information
Showing
31 changed files
with
571 additions
and
206 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
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,108 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
using Windows.Storage.Streams; | ||
|
||
namespace Text_Grab.Helpers; | ||
|
||
// Use these extension methods to store and retrieve local and roaming app data | ||
// More details regarding storing and retrieving app data at https://docs.microsoft.com/windows/apps/design/app-settings/store-and-retrieve-app-data | ||
public static class SettingsStorageExtensions | ||
{ | ||
private const string FileExtension = ".json"; | ||
|
||
public static bool IsRoamingStorageAvailable(this ApplicationData appData) | ||
{ | ||
return appData.RoamingStorageQuota == 0; | ||
} | ||
|
||
public static async Task SaveAsync<T>(this StorageFolder folder, string name, T content) | ||
{ | ||
if (content is null) | ||
return; | ||
|
||
StorageFile file = await folder.CreateFileAsync(GetFileName(name), CreationCollisionOption.ReplaceExisting); | ||
string fileContent = await Json.StringifyAsync(content); | ||
|
||
await FileIO.WriteTextAsync(file, fileContent); | ||
} | ||
|
||
public static async Task<T?> ReadAsync<T>(this StorageFolder folder, string name) | ||
{ | ||
if (!File.Exists(Path.Combine(folder.Path, GetFileName(name)))) | ||
{ | ||
return default; | ||
} | ||
|
||
StorageFile file = await folder.GetFileAsync($"{name}.json"); | ||
string fileContent = await FileIO.ReadTextAsync(file); | ||
|
||
return await Json.ToObjectAsync<T>(fileContent); | ||
} | ||
|
||
public static async Task SaveAsync<T>(this ApplicationDataContainer settings, string key, T? value) | ||
{ | ||
if (value is null) | ||
return; | ||
|
||
settings.SaveString(key, await Json.StringifyAsync(value)); | ||
} | ||
|
||
public static void SaveString(this ApplicationDataContainer settings, string key, string value) | ||
{ | ||
settings.Values[key] = value; | ||
} | ||
|
||
public static async Task<T?> ReadAsync<T>(this ApplicationDataContainer settings, string key) | ||
{ | ||
if (settings.Values.TryGetValue(key, out object? obj)) | ||
return await Json.ToObjectAsync<T>((string)obj); | ||
|
||
return default; | ||
} | ||
|
||
public static async Task<StorageFile> SaveFileAsync(this StorageFolder folder, byte[] content, string fileName, CreationCollisionOption options = CreationCollisionOption.ReplaceExisting) | ||
{ | ||
ArgumentNullException.ThrowIfNull(content); | ||
|
||
if (string.IsNullOrEmpty(fileName)) | ||
throw new ArgumentException("File name is null or empty. Specify a valid file name", nameof(fileName)); | ||
|
||
StorageFile storageFile = await folder.CreateFileAsync(fileName, options); | ||
await FileIO.WriteBytesAsync(storageFile, content); | ||
return storageFile; | ||
} | ||
|
||
public static async Task<byte[]?> ReadFileAsync(this StorageFolder folder, string fileName) | ||
{ | ||
IStorageItem item = await folder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false); | ||
|
||
if ((item != null) && item.IsOfType(StorageItemTypes.File)) | ||
{ | ||
StorageFile storageFile = await folder.GetFileAsync(fileName); | ||
byte[]? content = await storageFile.ReadBytesAsync(); | ||
return content; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static async Task<byte[]?> ReadBytesAsync(this StorageFile file) | ||
{ | ||
if (file == null) | ||
return null; | ||
|
||
using IRandomAccessStream stream = await file.OpenReadAsync(); | ||
using DataReader reader = new(stream.GetInputStreamAt(0)); | ||
await reader.LoadAsync((uint)stream.Size); | ||
byte[] bytes = new byte[stream.Size]; | ||
reader.ReadBytes(bytes); | ||
return bytes; | ||
} | ||
|
||
private static string GetFileName(string name) | ||
{ | ||
return string.Concat(name, FileExtension); | ||
} | ||
} |
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.