-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom.cs
186 lines (163 loc) · 5.49 KB
/
Custom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Media;
using System.Windows.Media;
using ImaginaryNarwhal;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace EZCounter
{
public class CommandHandler : ICommand
{
private Action _action;
private Func<bool> _canExecute;
/// <summary>
/// Creates instance of the command handler
/// </summary>
/// <param name="action">Action to be executed by the command</param>
/// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>
public CommandHandler(Action action, Func<bool> canExecute)
{
_action = action;
_canExecute = canExecute;
}
/// <summary>
/// Wires CanExecuteChanged event
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Forcess checking if execute is allowed
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return _canExecute.Invoke();
}
public void Execute(object parameter)
{
_action();
}
}
public static class Helpers
{
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
}
public class BoolInverterConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
#endregion
}
public class TimerSounds : ICustomPropertyNotify
{
private MediaPlayer player = new MediaPlayer();
private List<Uri> _soundFiles;
private List<string> _allSounds;
private string _selectedSound;
public List<Uri> SoundFiles
{
get => _soundFiles;
set => SetProperty(ref _soundFiles, value);
}
public List<string> AllSounds
{
get => _allSounds;
set => SetProperty(ref _allSounds, value);
}
public string SelectedSound
{
get => _selectedSound;
set
{
SetProperty(ref _selectedSound, value);
if(!SelectedSound.Contains("System:"))
{
player.Close();
player.Open(GetSoundFileFromString(value));
}
}
}
public TimerSounds()
{
SoundFiles = new List<Uri>();
GetSoundFiles();
AllSounds = new List<string>();
AllSounds.Add("None");
AllSounds.Add("System: Asterisk");
AllSounds.Add("System: Beep");
AllSounds.Add("System: Exclamation");
AllSounds.Add("System: Hand");
AllSounds.Add("System: Question");
foreach(var file in SoundFiles)
{
AllSounds.Add(Path.GetFileName(file.LocalPath));
}
SelectedSound = Properties.Settings.Default.timerSound;
}
public void GetSoundFiles()
{
SoundFiles.Clear();
var ext = new List<string> { "wav", "mp3", "wma" };
var files = Directory.EnumerateFiles(Storage.Sounds, "*.*").Where(f => ext.Contains(Path.GetExtension(f).TrimStart('.').ToLowerInvariant())).ToList();
foreach(var file in files)
{
SoundFiles.Add(new Uri(Path.Combine(Storage.Sounds, file)));
}
}
public Uri GetSoundFileFromString(string sound)
{
return SoundFiles.Where(x => Path.GetFileName(x.LocalPath) == sound).FirstOrDefault();
}
public void PlaySound()
{
switch(SelectedSound)
{
case "None": break;
case "System: Asterisk": SystemSounds.Asterisk.Play(); break;
case "System: Beep": SystemSounds.Beep.Play(); break;
case "System: Exclamation": SystemSounds.Exclamation.Play(); break;
case "System: Hand": SystemSounds.Hand.Play(); break;
case "System: Question": SystemSounds.Question.Play(); break;
default:
player.Play();
break;
}
}
}
public static class Storage
{
public static string Dir => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EZCounter");
public static string Sounds => Path.Combine(Dir, "Sounds");
}
}