Skip to content

Commit

Permalink
Fix settings save bugs
Browse files Browse the repository at this point in the history
- File.OpenWrite was not truncating
- Async operation sometimes wrote 0 bytes
- Fix DeferRefresh exception when switching config tabs
- Committed grid edits instead of cancelling them
- Pretty-printed JSON, just because
  • Loading branch information
luke-gumbley authored and uxsoft committed May 25, 2021
1 parent 0d786ba commit a62b46b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions AppleWirelessKeyboardCore/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChang
}
}

private async void Application_Exit(object sender, ExitEventArgs e)
private void Application_Exit(object sender, ExitEventArgs e)
{
TrayIconService.Close();
await SettingsService.Default.SaveAsync();
SettingsService.Default.Save();
}
}
}
16 changes: 10 additions & 6 deletions AppleWirelessKeyboardCore/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ private static string GetStorageFolderLocation() =>
private static string GetStorageFileLocation() =>
Path.Combine(GetStorageFolderLocation(), "settings.json");

public async Task SaveAsync()
public void Save()
{
var text = "";
try
{
Directory.CreateDirectory(GetStorageFolderLocation());
using var stream = File.OpenWrite(GetStorageFileLocation());
await JsonSerializer.SerializeAsync(stream, this);
var opts = new JsonSerializerOptions();
opts.WriteIndented = true;
text = JsonSerializer.Serialize<SettingsService>(this, opts);
File.WriteAllText(GetStorageFileLocation(), text);
}
catch (Exception ex)
{
Expand All @@ -61,11 +64,12 @@ public async Task SaveAsync()

public static SettingsService Load()
{
var text = "";
try
{
var path = GetStorageFileLocation();
var text = File.ReadAllText(path);
return JsonSerializer.Deserialize<SettingsService>(text) ?? new SettingsService();
text = File.ReadAllText(GetStorageFileLocation());
var settings = JsonSerializer.Deserialize<SettingsService>(text);
return settings ?? new SettingsService();
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion AppleWirelessKeyboardCore/Views/Configuration.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<TabItem Header="{Binding Path=Bindings, Source={x:Static srv:TranslationService.Default}}">
<DataGrid Name="grdBindings" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True"
CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserResizeRows="False"
CanUserSortColumns="True" RowStyle="{StaticResource DefaultRowStyle}">
CanUserSortColumns="True" RowStyle="{StaticResource DefaultRowStyle}" Unloaded="grdBindings_Unloaded">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="clmnKey" Header="{Binding Path=Key, Source={x:Static srv:TranslationService.Default}}"
SelectedItemBinding="{Binding Key}" />
Expand Down
16 changes: 11 additions & 5 deletions AppleWirelessKeyboardCore/Views/Configuration.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ private void cmbLanguage_Loaded(object sender, RoutedEventArgs e)
cmbLanguage.SelectedItem = SettingsService.Default.ActiveLanguage;
}

private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
grdBindings.CancelEdit();
await SettingsService.Default.SaveAsync();
grdBindings.CommitEdit(DataGridEditingUnit.Row, true);
SettingsService.Default.Save();
StartupShortcutService.Check();
}

Expand All @@ -65,7 +65,7 @@ private void mnuDelete_Click(object sender, RoutedEventArgs e)
}
}

private async void btnFactoryReset_Click(object sender, RoutedEventArgs e)
private void btnFactoryReset_Click(object sender, RoutedEventArgs e)
{
if (MessageBoxResult.Yes == MessageBox.Show("Are you sure you want to replace your key bindings to the defaults?", "Reset Bindings", MessageBoxButton.YesNo))
{
Expand All @@ -91,8 +91,14 @@ private async void btnFactoryReset_Click(object sender, RoutedEventArgs e)
Module = nameof(VolumeControl.VolumeMute)
});

await SettingsService.Default.SaveAsync();
SettingsService.Default.Save();
}
}

private void grdBindings_Unloaded(object sender, RoutedEventArgs e)
{
var grid = (DataGrid)sender;
grid.CommitEdit(DataGridEditingUnit.Row, true);
}
}
}

0 comments on commit a62b46b

Please sign in to comment.