RhinoReminds/RhinoReminds/Reminder.cs

105 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using S22.Xmpp;
namespace RhinoReminds
{
public class Reminder : IEquatable<Reminder>
{
public int Id { get; }
public string Jid => JidObj.ToString();
public Jid JidObj { get; }
public DateTime Time { get; private set; }
public string Message { get; }
public Reminder(int inId, Jid inJid, DateTime inTime, string inMessage)
{
Id = inId;
JidObj = inJid.GetBareJid();
Time = inTime;
Message = inMessage;
}
public void TweakTime() {
Time = Time.AddMilliseconds(1);
}
#region Overrides
public override bool Equals(object obj) {
if (!(obj is Reminder)) return false;
Reminder otherReminder = obj as Reminder;
return otherReminder.Id == Id &&
otherReminder.JidObj == JidObj && // Will *always* be a bare Jid
otherReminder.Time == Time &&
otherReminder.Message == Message;
}
// For IEquatable<Reminder> implementation
public bool Equals(Reminder otherReminder) => Equals((object)otherReminder);
public override int GetHashCode() {
int hashCode = -81903051;
hashCode = (hashCode * -1521134295) + Id.GetHashCode();
hashCode = (hashCode * -1521134295) + EqualityComparer<Jid>.Default.GetHashCode(JidObj);
hashCode = (hashCode * -1521134295) + Time.GetHashCode();
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Message);
return hashCode;
}
public static bool operator ==(Reminder reminder1, Reminder reminder2) => EqualityComparer<Reminder>.Default.Equals(reminder1, reminder2);
public static bool operator !=(Reminder reminder1, Reminder reminder2) => !(reminder1 == reminder2);
public override string ToString() {
return $"[Reminder Id={Id}, Jid={Jid}, Time={Time}, Message=\"{Message}\"]";
}
#endregion
#region XML
public void WriteToXml(XmlWriter xml)
{
xml.WriteStartElement("Reminder");
xml.WriteElementString("Id", Id.ToString());
xml.WriteElementString("Jid", Jid);
xml.WriteElementString("Time", Time.ToString());
xml.WriteElementString("Message", WebUtility.HtmlEncode(Message));
xml.WriteEndElement();
}
public static Reminder FromXml(XmlNode xml)
{
int id = -1; string jid = string.Empty, message = string.Empty; DateTime dateTime = DateTime.Now;
foreach (XmlNode subNode in xml.ChildNodes) {
switch (subNode.Name) {
case "Id":
id = int.Parse(subNode.InnerText);
break;
case "Jid":
jid = subNode.InnerText;
break;
case "Time":
dateTime = DateTime.Parse(subNode.InnerText);
break;
case "Message":
message = WebUtility.HtmlDecode(subNode.InnerText);
break;
}
}
return new Reminder(id, jid, dateTime, message);
}
#endregion
}
}