-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
90 lines (77 loc) · 2.9 KB
/
MainWindow.xaml.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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Media.Core;
using Windows.Media.Playback;
namespace PlantNanny
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
public void SetReminder_Click(object sender, RoutedEventArgs e)
{
string time = TimeInput.Text;
SetReminder(time);
}
public DispatcherTimer timer;
public void SetReminder(string time)
{
string[] timeParts = time.Split(':');
int hour = int.Parse(timeParts[0]);
int minute = int.Parse(timeParts[1]);
DateTime now = DateTime.Now;
DateTime nextReminder = new DateTime(now.Year, now.Month, now.Day, hour, minute, 0);
if (now > nextReminder)
{
nextReminder = nextReminder.AddDays(1);
}
TimeSpan timeUntilNextReminder = nextReminder - now;
timer = new DispatcherTimer();
timer.Interval = timeUntilNextReminder;
timer.Tick += Timer_Tick;
timer.Start();
}
public void Timer_Tick(object sender, object e)
{
timer.Stop();
ShowToastNotification("Plant care reminder", "It's time to take care of your plants.");
timer.Interval = TimeSpan.FromDays(1);
timer.Start();
}
public void ShowToastNotification(string title, string content)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode(title));
stringElements[1].AppendChild(toastXml.CreateTextNode(content));
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
PlayNotificationSound();
}
public async void PlayNotificationSound()
{
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/WAV_Polite.wav"));
MediaPlayer player = new MediaPlayer();
player.Source = MediaSource.CreateFromStorageFile(file);
player.Play();
}
}
}