54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Newtonsoft.Json;
|
|
using S22.Xmpp;
|
|
|
|
namespace RhinoReminds
|
|
{
|
|
public delegate void OnReminderListUpdateHandler(object sender, Reminder newReminder);
|
|
|
|
public class ReminderList
|
|
{
|
|
private int nextId = 0;
|
|
private object saveLock = new object();
|
|
public SortedList<DateTime, Reminder> Reminders = new SortedList<DateTime, Reminder>();
|
|
|
|
public event OnReminderListUpdateHandler OnReminderListUpdate;
|
|
|
|
|
|
public ReminderList() {
|
|
|
|
}
|
|
|
|
public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
|
|
Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
|
|
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
|
|
Reminders.Add(time, result);
|
|
OnReminderListUpdate(this, result);
|
|
return result;
|
|
}
|
|
|
|
public void Save(string filename)
|
|
{
|
|
// Make sure that the reminder thread doesn't try to save the reminders at the exact same time
|
|
// we receive a request for a new reminder
|
|
lock (saveLock) { // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead
|
|
File.WriteAllText(filename, JsonConvert.SerializeObject(this));
|
|
}
|
|
}
|
|
|
|
public Reminder GetNextReminder()
|
|
{
|
|
if (Reminders.Count == 0)
|
|
return null;
|
|
|
|
return Reminders.Values[0];
|
|
}
|
|
|
|
public void DeleteReminder(Reminder nextReminder) {
|
|
Reminders.Remove(nextReminder.Time);
|
|
}
|
|
}
|
|
}
|