Skip to content

Commit

Permalink
Added saving of a single option
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoCoderMatrix86 committed Jul 4, 2024
1 parent a7d8e09 commit 19e153f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
45 changes: 45 additions & 0 deletions AudioCuesheetEditor/Data/Options/LocalStorageOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Model.Entity;
using AudioCuesheetEditor.Model.Options;
using Microsoft.JSInterop;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;

namespace AudioCuesheetEditor.Data.Options
Expand Down Expand Up @@ -64,5 +67,47 @@ public async Task SaveOptions(IOptions options)
await _jsRuntime.InvokeVoidAsync(String.Format("{0}.set", options.GetType().Name), optionsJson);
OptionSaved?.Invoke(this, options);
}

public async Task SaveOptionsValue<T>(Expression<Func<T, object>> propertyExpression, object value) where T : class, IOptions, new()
{
var options = await GetOptions<T>();
if (propertyExpression.Body is MemberExpression memberExpression)
{
var propertyInfo = memberExpression.Member as PropertyInfo;
if (propertyInfo != null)
{
propertyInfo.SetValue(options, Convert.ChangeType(value, propertyInfo.PropertyType));
}
else
{
throw new ArgumentException("The provided expression does not reference a valid property.");
}
}
else if (propertyExpression.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression unaryMemberExpression)
{
var propertyInfo = unaryMemberExpression.Member as PropertyInfo;
if (propertyInfo != null)
{
propertyInfo.SetValue(options, Convert.ChangeType(value, propertyInfo.PropertyType));
}
else
{
throw new ArgumentException("The provided expression does not reference a valid property.");
}
}
else
{
throw new ArgumentException("The provided expression does not reference a valid property.");
}
Boolean saveOptions = true;
if (options is IValidateable<T> validateable)
{
saveOptions = validateable.Validate(propertyExpression).Status != ValidationStatus.Error;
}
if (saveOptions)
{
await SaveOptions(options);
}
}
}
}
47 changes: 29 additions & 18 deletions AudioCuesheetEditor/Shared/RecordOptions.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,28 @@ along with Foobar. If not, see
<Field Horizontal="true">
<FieldLabel ColumnSize="ColumnSize.Is5.OnWidescreen.Is12.OnDesktop">@_localizer["Filename for recorded audio"]</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is7.OnWidescreen.Is12.OnDesktop">
@if (applicationOptions != null)
{
<TextEdit @bind-Text="applicationOptions.RecordedAudiofilename" Immediate="false">
<Feedback>
<ValidationError Tooltip />
</Feedback>
</TextEdit>
}
<TextEdit Text="@applicationOptions?.RecordedAudiofilename" TextChanged="RecordedAudiofilenameChanged" Immediate="false">
<Feedback>
<ValidationError Tooltip />
</Feedback>
</TextEdit>
</FieldBody>
</Field>
</Validation>
<Field Horizontal="true">
<FieldLabel ColumnSize="ColumnSize.Is5.OnWidescreen.Is12.OnDesktop">@_localizer["Record time sensitivity"]</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is7.OnWidescreen.Is12.OnDesktop">
@if (applicationOptions != null)
{
<Select TValue="String" @bind-SelectedValue="@applicationOptions.RecordTimeSensitivityname">
@foreach (var name in Enum.GetNames(typeof(TimeSensitivityMode)))
{
<SelectItem Value="@name">@_localizer["TimeSensitivityMode." + name]</SelectItem>
}
</Select>
}
<Select TValue="String" SelectedValue="@applicationOptions?.RecordTimeSensitivityname" SelectedValueChanged="RecordTimeSensitivityChanged">
@foreach (var name in Enum.GetNames(typeof(TimeSensitivityMode)))
{
<SelectItem Value="@name">@_localizer["TimeSensitivityMode." + name]</SelectItem>
}
</Select>
</FieldBody>
</Field>
</Validations>

@code {
//TODO: Save each property when changed
//TODO: Save, reset, reload, etc. like import options
ApplicationOptions? applicationOptions;

Expand Down Expand Up @@ -86,4 +79,22 @@ along with Foobar. If not, see
applicationOptions = applicationOption;
}
}

async Task RecordedAudiofilenameChanged(string newValue)
{
if (applicationOptions != null)
{
applicationOptions.RecordedAudiofilename = newValue;
}
await _localStorageOptionsProvider.SaveOptionsValue<ApplicationOptions>(x => x.RecordedAudiofilename, newValue);
}

async Task RecordTimeSensitivityChanged(string newValue)
{
if (applicationOptions != null)
{
applicationOptions.RecordTimeSensitivityname = newValue;
}
await _localStorageOptionsProvider.SaveOptionsValue<ApplicationOptions>(x => x.RecordTimeSensitivityname!, newValue);
}
}

0 comments on commit 19e153f

Please sign in to comment.