You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
RhinoReminds/RhinoReminds/ReminderList.cs

57 lines
1.5 KiB

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);
[JsonObject(MemberSerialization.OptIn)]
public class ReminderList
{
[JsonProperty]
public int NextId { get; private set; } = 0;
private object saveLock = new object();
[JsonProperty]
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);
}
}
}