-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suspend and restore the settings app state using JSON
- Loading branch information
1 parent
bd5f367
commit 577616d
Showing
13 changed files
with
62 additions
and
42 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
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
18 changes: 0 additions & 18 deletions
18
src/KeyboardSwitch.Settings/State/AkavacheSuspensionDriver.cs
This file was deleted.
Oops, something went wrong.
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,25 +1,11 @@ | ||
namespace KeyboardSwitch.Settings.State; | ||
|
||
using System.Runtime.Serialization; | ||
|
||
[DataContract] | ||
public class AppState | ||
{ | ||
[DataMember] | ||
public double WindowWidth { get; set; } | ||
|
||
[DataMember] | ||
public double WindowHeight { get; set; } | ||
|
||
[DataMember] | ||
public int WindowX { get; set; } | ||
|
||
[DataMember] | ||
public int WindowY { get; set; } | ||
|
||
[DataMember] | ||
public bool IsWindowMaximized { get; set; } | ||
|
||
[DataMember] | ||
public bool IsInitialized { get; set; } | ||
} |
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,43 @@ | ||
namespace KeyboardSwitch.Settings.State; | ||
|
||
using System.Text.Json; | ||
|
||
using Microsoft.Extensions.Options; | ||
|
||
internal sealed class JsonSuspensionDriver(IOptions<GlobalSettings> settings) : ISuspensionDriver | ||
{ | ||
private readonly string file = Environment.ExpandEnvironmentVariables(settings.Value.StateFilePath); | ||
|
||
public IObservable<Unit> InvalidateState() | ||
{ | ||
if (File.Exists(this.file)) | ||
{ | ||
File.Delete(this.file); | ||
} | ||
|
||
return Observable.Return(Unit.Default); | ||
} | ||
|
||
public IObservable<object> LoadState() | ||
{ | ||
if (!File.Exists(this.file)) | ||
{ | ||
return Observable.Return(new AppState()); | ||
} | ||
|
||
return Observable.FromAsync(() => File.ReadAllTextAsync(this.file)) | ||
.Select(content => JsonSerializer.Deserialize(content, SourceGenerationContext.Default.AppState)) | ||
.Select(state => state ?? new AppState()); | ||
} | ||
|
||
public IObservable<Unit> SaveState(object state) | ||
{ | ||
if (state is AppState appState) | ||
{ | ||
var content = JsonSerializer.Serialize(appState, SourceGenerationContext.Default.AppState); | ||
return Observable.FromAsync(() => File.WriteAllTextAsync(this.file, content)); | ||
} | ||
|
||
return Observable.Throw<Unit>(new InvalidCastException("AppState must be provided to SaveState")); | ||
} | ||
} |
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,9 @@ | ||
namespace KeyboardSwitch.Settings.State; | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
[JsonSerializable(typeof(AppState))] | ||
[JsonSourceGenerationOptions(WriteIndented = true)] | ||
internal partial class SourceGenerationContext : JsonSerializerContext | ||
{ | ||
} |
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