Skip to content

Commit 4dbeadf

Browse files
committed
V2025.11.4
1 parent 3910d2e commit 4dbeadf

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

Nickvision.Desktop.Tests/ServiceCollectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void Case003_Add()
6666
{
6767
Assert.IsNotNull(_collection);
6868
var service = new TestService();
69-
Assert.IsTrue(_collection.Add<ITestService>(service));
70-
Assert.IsFalse(_collection.Add<ITestService>(service));
69+
Assert.IsNotNull(_collection.Add<ITestService>(service));
70+
Assert.IsNull(_collection.Add<ITestService>(service));
7171
Assert.IsTrue(_collection.Has<ITestService>());
7272
Assert.IsFalse(service.Disposed);
7373
}
@@ -95,7 +95,7 @@ public void Case006_Dispose()
9595
{
9696
Assert.IsNotNull(_collection);
9797
var service = new TestService();
98-
Assert.IsTrue(_collection.Add<ITestService>(service));
98+
Assert.IsNotNull(_collection.Add<ITestService>(service));
9999
Assert.IsFalse(service.Disposed);
100100
_collection.Dispose();
101101
_collection = null;

Nickvision.Desktop/Application/AppVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public AppVersion(Version version)
2424

2525
public Version BaseVersion { get; init; }
2626
public string PreviewLabel { get; init; }
27-
27+
2828
public bool IsPreview => !string.IsNullOrEmpty(PreviewLabel);
2929

3030
public static bool TryParse(string version, out AppVersion? appVersion)

Nickvision.Desktop/Application/ServiceCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void Dispose()
4242
/// </summary>
4343
/// <param name="implementation">The object of the service interface</param>
4444
/// <typeparam name="T">The service interface</typeparam>
45-
/// <returns></returns>
46-
public bool Add<T>(T implementation) where T : IService => _services.TryAdd(typeof(T), implementation);
45+
/// <returns>The object to the service if successfully added, else null</returns>
46+
public T? Add<T>(T implementation) where T : IService => _services.TryAdd(typeof(T), implementation) ? implementation : default(T?);
4747

4848
/// <summary>
4949
/// Gets a service from the collection.

Nickvision.Desktop/Filesystem/IJsonFileService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
namespace Nickvision.Desktop.Filesystem;
45

@@ -7,6 +8,11 @@ namespace Nickvision.Desktop.Filesystem;
78
/// </summary>
89
public interface IJsonFileService : IService
910
{
11+
/// <summary>
12+
/// The event for when json files are saved.
13+
/// </summary>
14+
event EventHandler<JsonFileSavedEventArgs>? Saved;
15+
1016
/// <summary>
1117
/// Loads a json file and deserializes it into an object.
1218
/// </summary>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace Nickvision.Desktop.Filesystem;
4+
5+
/// <summary>
6+
/// A class of event arguments for when a json file is saved.
7+
/// </summary>
8+
public class JsonFileSavedEventArgs : EventArgs
9+
{
10+
public JsonFileSavedEventArgs(object data, Type dataType, string name)
11+
{
12+
Data = data;
13+
DataType = dataType;
14+
Name = name;
15+
}
16+
17+
/// <summary>
18+
/// The object that was saved to a json file.
19+
/// </summary>
20+
public object Data { get; init; }
21+
22+
/// <summary>
23+
/// The type of the saved object.
24+
/// </summary>
25+
public Type DataType { get; init; }
26+
27+
/// <summary>
28+
/// The name of the json file (without the .json extension).
29+
/// </summary>
30+
public string Name { get; init; }
31+
}

Nickvision.Desktop/Filesystem/JsonFileService.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ public JsonFileService(string directory)
4343
/// Constructs a JsonFileService.
4444
/// </summary>
4545
/// <param name="appInfo">The AppInfo object for the app</param>
46-
/// <remarks>If the environment variable "{AppInfo.Id}.portable" is set, the current working directory will be used. Else, a {AppInfo.Name} folder will be created and used inside the user's configuration directory.</remarks>
47-
public JsonFileService(AppInfo appInfo)
48-
: this(Environment.GetEnvironmentVariable($"{appInfo.Id}.portable") is not null ? Directory.GetCurrentDirectory() : Path.Combine(UserDirectories.Config, appInfo.Name))
46+
/// <remarks>
47+
/// If the environment variable "{AppInfo.Id}.portable" is set, the current working directory will be used. Else,
48+
/// a {AppInfo.Name} folder will be created and used inside the user's configuration directory.
49+
/// </remarks>
50+
public JsonFileService(AppInfo appInfo) : this(Environment.GetEnvironmentVariable($"{appInfo.Id}.portable") is not null ? Directory.GetCurrentDirectory() : Path.Combine(UserDirectories.Config, appInfo.Name))
4951
{
50-
5152
}
5253

54+
/// <summary>
55+
/// The event for when json files are saved.
56+
/// </summary>
57+
public event EventHandler<JsonFileSavedEventArgs>? Saved;
58+
5359
/// <summary>
5460
/// Loads a json file and deserializes it into an object.
5561
/// </summary>
@@ -95,10 +101,16 @@ public async Task<T> LoadAsync<T>(string? name = null)
95101
/// <returns>True if the file was saved successfully, else false</returns>
96102
public bool Save<T>(T obj, string? name = null)
97103
{
104+
if (obj is null)
105+
{
106+
return false;
107+
}
108+
name = string.IsNullOrEmpty(name) ? typeof(T).Name.ToLower() : name;
98109
try
99110
{
100111
var text = JsonSerializer.Serialize(obj, JsonOptions);
101-
File.WriteAllText(Path.Combine(_directory, $"{(string.IsNullOrEmpty(name) ? typeof(T).Name.ToLower() : name)}.json"), text);
112+
File.WriteAllText(Path.Combine(_directory, $"{name}.json"), text);
113+
Saved?.Invoke(this, new JsonFileSavedEventArgs(obj, typeof(T), name));
102114
return true;
103115
}
104116
catch
@@ -116,10 +128,16 @@ public bool Save<T>(T obj, string? name = null)
116128
/// <returns>True if the file was saved successfully, else false</returns>
117129
public async Task<bool> SaveAsync<T>(T obj, string? name = null)
118130
{
131+
if (obj is null)
132+
{
133+
return false;
134+
}
135+
name = string.IsNullOrEmpty(name) ? typeof(T).Name.ToLower() : name;
119136
try
120137
{
121138
var text = JsonSerializer.Serialize(obj, JsonOptions);
122-
await File.WriteAllTextAsync(Path.Combine(_directory, $"{(string.IsNullOrEmpty(name) ? typeof(T).Name.ToLower() : name)}.json"), text);
139+
await File.WriteAllTextAsync(Path.Combine(_directory, $"{name}.json"), text);
140+
Saved?.Invoke(this, new JsonFileSavedEventArgs(obj, typeof(T), name));
123141
return true;
124142
}
125143
catch

Nickvision.Desktop/Nickvision.Desktop.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<EnableWindowsTargeting>true</EnableWindowsTargeting>
1010
<PackageId>Nickvision.Desktop</PackageId>
11-
<Version>2025.11.3</Version>
11+
<Version>2025.11.4</Version>
1212
<Company>Nickvision</Company>
1313
<Authors>Nickvision</Authors>
1414
<Description>A cross-platform base for Nickvision desktop applications.</Description>

0 commit comments

Comments
 (0)