-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make beep on success. Added audio-path to audio file that will be pla…
…yed on success Tick. Also added bool value that means play audio on success or not. Also added (SettingsExtensions 2.0) with abstraction. Extract MyTimer to ITimer interface, but it not make sense. Make tests for Settings.
- Loading branch information
Showing
15 changed files
with
347 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
namespace HabiticaHourUpVSIX.AppSettings.Models; | ||
public record struct UserSettingsModel(TimeSpan Divisor, string TaskIDToScoreUp, bool IsAutoScoreUp, bool ShowErrorOnFailure); | ||
public record struct UserSettingsModel(TimeSpan Divisor, string TaskIDToScoreUp, bool IsAutoScoreUp, bool ShowErrorOnFailure, | ||
bool BeepOnSuccess, string BeepAudioPath); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace HabiticaHourUpVSIX; | ||
|
||
public static class DictionaryExtensions | ||
{ | ||
public static TValue GetOrSet<TKey, TValue>(this Dictionary<TKey, TValue> obj1, TKey key, Func<TValue> resultProvider) | ||
=> obj1.TryGetValue(key, out var result) ? result : obj1[key] = resultProvider(); | ||
} |
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,26 @@ | ||
namespace HabiticaHourUpVSIX; | ||
#nullable enable | ||
|
||
internal interface IAudioPlayer | ||
{ | ||
string? AudioPath { get; set; } | ||
void Play(); | ||
} | ||
internal class SoundPlayer : IAudioPlayer | ||
{ | ||
private readonly System.Media.SoundPlayer _soundPlayer = new(); | ||
|
||
public string? AudioPath { get => _soundPlayer.SoundLocation; set => _soundPlayer.SoundLocation = value; } | ||
|
||
public void Play() | ||
=> _soundPlayer.PlaySync(); | ||
} | ||
internal class AudioPlayer : IAudioPlayer | ||
{ | ||
private readonly WMPLib.WindowsMediaPlayer _player = new(); | ||
|
||
public string? AudioPath { get => _player.URL; set => _player.URL = value; } | ||
|
||
public void Play() | ||
=> _player.controls.play(); | ||
} |
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,38 @@ | ||
using HabiticaHourUpVSIX; | ||
using HabiticaHourUpVSIX.AppSettings.Abstractions; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
public static partial class SettingsExtensions | ||
{ | ||
// Object as key because unknown T of SettingsWithSaving<T> | ||
private static readonly Dictionary<(object, string propertyName), PropertyInfo> _cache = []; | ||
private static readonly Dictionary<object, Type> _cachedTypes = []; | ||
|
||
public static void SetWithSave<T, TProperty>(this SettingsWithSaving<T> obj1, | ||
Expression<Func<T, TProperty>> expression, | ||
TProperty value) where T : struct | ||
{ | ||
if (expression.Body is not MemberExpression memberExpression) | ||
throw new InvalidOperationException("Invalid expression. Should have property that returns."); | ||
string memberName = memberExpression.Member.Name; | ||
|
||
var settingsReadBoxed = (object)obj1.Read(); | ||
var property = GetProperty(obj1, memberName); | ||
|
||
property.SetValue(settingsReadBoxed, value); | ||
|
||
var settingsToWrite = (T)settingsReadBoxed; | ||
obj1.Write(settingsToWrite); | ||
obj1.Save(); | ||
|
||
PropertyInfo GetProperty(object obj1, string memberName) | ||
{ | ||
Type type = _cachedTypes.GetOrSet(obj1, () => typeof(T)); | ||
PropertyInfo property = _cache.GetOrSet((obj1, memberName), () => type.GetProperty(memberName)); | ||
|
||
return property; | ||
} | ||
} | ||
} |
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.